Line data Source code
1 : package query
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : "github.com/kijimaD/ruins/internal/consts"
6 : w "github.com/kijimaD/ruins/internal/world"
7 : "github.com/mlange-42/ark/ecs"
8 : )
9 :
10 : // HasStorageItems は収納に中身が1つでもあるかを返す。1つ見つけ次第打ち切るので、
11 : // スライスを組む GetStorageItems と違い確保が無く、有無判定だけの用途に向く。
12 : func HasStorageItems(world w.World, storage ecs.Entity) bool {
13 1 : q := ecs.NewFilter1[gc.LocationInStorage](world.ECS).Query()
14 1 : for q.Next() {
15 1 : if world.Components.LocationInStorage.Get(q.Entity()).Owner == storage {
16 1 : q.Close()
17 1 : return true
18 1 : }
19 : }
20 1 : return false
21 : }
22 :
23 : // GetStorageItems は収納内のアイテムを取得する
24 : func GetStorageItems(world w.World, storage ecs.Entity) []ecs.Entity {
25 1 : var items []ecs.Entity
26 1 : itemsQuery := ecs.NewFilter1[gc.LocationInStorage](world.ECS).Query()
27 1 : for itemsQuery.Next() {
28 1 : entity := itemsQuery.Entity()
29 1 : loc := world.Components.LocationInStorage.Get(entity)
30 1 : if loc.Owner == storage {
31 1 : items = append(items, entity)
32 1 : }
33 : }
34 1 : return items
35 : }
36 :
37 : // GetEntityWeight はエンティティ1個の重量を返す。1個1エンティティなので個数は掛けない。
38 : // 総重量は所有エンティティを1個ずつ合算して求める。
39 : func GetEntityWeight(world w.World, entity ecs.Entity) consts.Milligram {
40 1 : if !world.Components.Weight.Has(entity) {
41 1 : return 0
42 1 : }
43 1 : return world.Components.Weight.Get(entity).Milligram
44 : }
45 :
46 : // GetStorageCurrentWeight は収納の現在重量を返す
47 : func GetStorageCurrentWeight(world w.World, storage ecs.Entity) consts.Milligram {
48 1 : if !world.Components.WeightCapacity.Has(storage) {
49 1 : return 0
50 1 : }
51 1 : return world.Components.WeightCapacity.Get(storage).Current
52 : }
53 :
54 : // CanAddToStorage は収納にアイテムを追加できるか判定する
55 : func CanAddToStorage(world w.World, storage ecs.Entity, item ecs.Entity) bool {
56 1 : if !world.Components.WeightCapacity.Has(storage) {
57 1 : return false
58 1 : }
59 1 : wc := world.Components.WeightCapacity.Get(storage)
60 1 : return wc.Current+GetEntityWeight(world, item) <= wc.Max
61 : }
62 :
63 : // CanAddStackToStorage は items をまとめて収納に追加できるか、合計重量で判定する。
64 : // スタック丸ごとの移動可否を1回で判定するために使う。
65 : func CanAddStackToStorage(world w.World, storage ecs.Entity, items []ecs.Entity) bool {
66 1 : if !world.Components.WeightCapacity.Has(storage) {
67 1 : return false
68 1 : }
69 1 : var total consts.Milligram
70 1 : for _, it := range items {
71 1 : total += GetEntityWeight(world, it)
72 1 : }
73 1 : wc := world.Components.WeightCapacity.Get(storage)
74 1 : return wc.Current+total <= wc.Max
75 : }
|