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 : "github.com/kijimaD/ruins/internal/rawschema"
7 : w "github.com/kijimaD/ruins/internal/world"
8 : "github.com/mlange-42/ark/ecs"
9 : )
10 :
11 : const (
12 : // baseCarryingWeight は基本所持可能重量
13 : baseCarryingWeight = consts.Milligram(10 * consts.MilligramPerKg)
14 : // strengthWeightMultiplier は筋力1あたりの追加所持可能重量
15 : strengthWeightMultiplier = consts.Milligram(2 * consts.MilligramPerKg)
16 : )
17 :
18 : // UpdateWeightCapacity はエンティティのWeightCapacityを更新する。
19 : // PlayerはAbilitiesからMaxを計算し、Backpack+Equippedの重量をCurrentに設定する。
20 : // StorageはMaxを変更せず、LocationInStorageの重量をCurrentに設定する
21 : func UpdateWeightCapacity(world w.World, entity ecs.Entity) {
22 1 : if !world.Components.WeightCapacity.Has(entity) {
23 1 : return
24 1 : }
25 :
26 1 : wc := world.Components.WeightCapacity.Get(entity)
27 1 :
28 : // Abilitiesを持つエンティティはMaxを再計算する(Player用)
29 1 : if world.Components.Abilities.Has(entity) {
30 1 : abilities := world.Components.Abilities.Get(entity)
31 1 : maxWeight := calculateMaxCarryingWeight(abilities)
32 1 : maxWeight = consts.Milligram(ProficiencyValue(world, entity, rawschema.ProficiencyIdMaxWeight).ApplyInt(int(maxWeight)))
33 1 : wc.Max = maxWeight
34 1 : }
35 :
36 : // Currentを再計算する
37 1 : wc.Current = calculateOwnedWeight(world, entity)
38 : }
39 :
40 : // calculateMaxCarryingWeight は筋力ステータスから所持可能重量を計算する
41 : func calculateMaxCarryingWeight(abilities *gc.Abilities) consts.Milligram {
42 1 : if abilities == nil {
43 1 : return baseCarryingWeight
44 1 : }
45 : // Total は StatsChangedSystem の集計後にだけ正しい。装備補正込みの現在値を
46 : // Base + Modifier から直接求め、集計の実行順に依存しないようにする
47 1 : strength := abilities.Strength.Base + abilities.Strength.Modifier
48 1 : return baseCarryingWeight + consts.Milligram(strength)*strengthWeightMultiplier
49 : }
50 :
51 : // calculateOwnedWeight はエンティティが所有するアイテムの総重量を計算する。
52 : // Backpack内、装備中、Storage内のアイテムをOwnerで判定して合算する
53 : func calculateOwnedWeight(world w.World, entity ecs.Entity) consts.Milligram {
54 1 : var totalWeight consts.Milligram
55 1 :
56 1 : weightQuery := ecs.NewFilter1[gc.Weight](world.ECS).Query()
57 1 : for weightQuery.Next() {
58 1 : itemEntity := weightQuery.Entity()
59 1 : switch {
60 : case world.Components.LocationInBackpack.Has(itemEntity):
61 1 : if world.Components.LocationInBackpack.Get(itemEntity).Owner == entity {
62 1 : totalWeight += GetEntityWeight(world, itemEntity)
63 1 : }
64 : case world.Components.LocationEquipped.Has(itemEntity):
65 1 : if world.Components.LocationEquipped.Get(itemEntity).Owner == entity {
66 1 : totalWeight += GetEntityWeight(world, itemEntity)
67 1 : }
68 : case world.Components.LocationInStorage.Has(itemEntity):
69 1 : if world.Components.LocationInStorage.Get(itemEntity).Owner == entity {
70 1 : totalWeight += GetEntityWeight(world, itemEntity)
71 1 : }
72 1 : case world.Components.LocationOnField.Has(itemEntity):
73 : // フィールド上に落ちているアイテムは所持していないので所持重量に含めない
74 : }
75 : }
76 :
77 1 : return totalWeight
78 : }
|