last_light/game/world/generate_items.go

58 lines
1.1 KiB
Go
Raw Normal View History

2024-05-06 21:19:08 +03:00
package world
import (
"math/rand"
"mvvasilev/last_light/engine"
"mvvasilev/last_light/game/model"
2024-05-12 23:22:39 +03:00
"slices"
2024-05-06 21:19:08 +03:00
)
2024-05-12 23:22:39 +03:00
func SpawnItems(spawnableAreas []engine.BoundingBox, maxItemRatio float32, genTable map[float32]*model.ItemType, forbiddenPositions []engine.Position) []Tile {
2024-05-06 21:19:08 +03:00
rooms := spawnableAreas
itemTiles := make([]Tile, 0, 10)
for _, r := range rooms {
maxItems := int(maxItemRatio * float32(r.Size().Area()))
if maxItems < 1 {
continue
}
numItems := rand.Intn(maxItems)
for range numItems {
itemType := GenerateItemType(genTable)
if itemType == nil {
continue
}
pos := engine.PositionAt(
engine.RandInt(r.Position().X()+1, r.Position().X()+r.Size().Width()-1),
engine.RandInt(r.Position().Y()+1, r.Position().Y()+r.Size().Height()-1),
)
2024-05-12 23:22:39 +03:00
if slices.Contains(forbiddenPositions, pos) {
continue
}
2024-05-06 21:19:08 +03:00
itemTiles = append(itemTiles, CreateItemTile(pos, itemType, 1))
}
}
return itemTiles
}
func GenerateItemType(genTable map[float32]*model.ItemType) *model.ItemType {
num := rand.Float32()
for k, v := range genTable {
if num > k {
return v
}
}
return nil
}