LCOV - code coverage report
Current view: top level - systems - stats_changed.go Coverage Total Hit
Test: coverage.lcov Lines: 97.9 % 95 93
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package systems
       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              : 
       8              :         "github.com/kijimaD/ruins/internal/world/query"
       9              :         "github.com/mlange-42/ark/ecs"
      10              : )
      11              : 
      12              : // StatsChangedSystem はステータス再計算のダーティフラグが立ったら、ステータス補正まわりを再計算する
      13              : // TODO: 最大HP/SPの更新はここでやったほうがよさそう
      14              : // TODO: マイナスにならないようにする
      15              : type StatsChangedSystem struct{}
      16              : 
      17              : // String はシステム名を返す
      18              : // w.Updater interfaceを実装
      19              : func (sys StatsChangedSystem) String() string {
      20            1 :         return "StatsChangedSystem"
      21            1 : }
      22              : 
      23              : // Update はステータス再計算フラグをチェックし、必要に応じてステータスを再計算する
      24              : // w.Updater interfaceを実装
      25              : func (sys *StatsChangedSystem) Update(world w.World) error {
      26            1 :         var targets []ecs.Entity
      27            1 :         q := query.ActiveFilter1[gc.StatsChanged](world).Query()
      28            1 :         for q.Next() {
      29            1 :                 targets = append(targets, q.Entity())
      30            1 :         }
      31              : 
      32              :         // 印は実体ごとに1回だけ外し、持つコンポーネントに応じて計算し直す
      33            1 :         for _, entity := range targets {
      34            1 :                 world.Components.StatsChanged.Remove(entity)
      35            1 :                 if world.Components.Abilities.Has(entity) {
      36            1 :                         updateCharacterStats(world, entity)
      37            1 :                 }
      38            1 :                 if world.Components.FuelTank.Has(entity) {
      39            1 :                         updateCubeFuelCapacity(world, entity)
      40            1 :                 }
      41              :         }
      42            1 :         return nil
      43              : }
      44              : 
      45              : // updateCharacterStats は装備から能力値を集計し直し、HP・AP の上限と光源を更新する。
      46              : func updateCharacterStats(world w.World, entity ecs.Entity) {
      47            1 :         abils := world.Components.Abilities.Get(entity)
      48            1 : 
      49              :         // Abilities初期化
      50              :         {
      51            1 :                 abils.Vitality.Modifier = 0
      52            1 :                 abils.Vitality.Total = abils.Vitality.Base
      53            1 :                 abils.Strength.Modifier = 0
      54            1 :                 abils.Strength.Total = abils.Strength.Base
      55            1 :                 abils.Sensation.Modifier = 0
      56            1 :                 abils.Sensation.Total = abils.Sensation.Base
      57            1 :                 abils.Dexterity.Modifier = 0
      58            1 :                 abils.Dexterity.Total = abils.Dexterity.Base
      59            1 :                 abils.Agility.Modifier = 0
      60            1 :                 abils.Agility.Total = abils.Agility.Base
      61            1 :                 abils.Defense.Modifier = 0
      62            1 :                 abils.Defense.Total = abils.Defense.Base
      63            1 :         }
      64              : 
      65              :         // 装備効果を加算
      66            1 :         equipQuery := query.ActiveFilter2[gc.LocationEquipped, gc.Wearable](world).Query()
      67            1 :         for equipQuery.Next() {
      68            1 :                 item := equipQuery.Entity()
      69            1 :                 equipped := world.Components.LocationEquipped.Get(item)
      70            1 : 
      71              :                 // このエンティティの装備のみ処理
      72            1 :                 if equipped.Owner != entity {
      73            0 :                         continue
      74              :                 }
      75              : 
      76            1 :                 wearable := world.Components.Wearable.Get(item)
      77            1 : 
      78            1 :                 abils.Defense.Modifier += wearable.Defense
      79            1 :                 abils.Vitality.Modifier += wearable.EquipBonus.Vitality
      80            1 :                 abils.Strength.Modifier += wearable.EquipBonus.Strength
      81            1 :                 abils.Sensation.Modifier += wearable.EquipBonus.Sensation
      82            1 :                 abils.Dexterity.Modifier += wearable.EquipBonus.Dexterity
      83            1 :                 abils.Agility.Modifier += wearable.EquipBonus.Agility
      84              :         }
      85              : 
      86              :         // 装備した光源をプレイヤー自身の LightSource へ写す。装備品は位置を持たず vision に
      87              :         // 拾われないので、位置を持つプレイヤーが代わりに光源になる。複数あれば最も明るいもの。
      88              :         // プレイヤー限定なのは、発光する member 等の内蔵光源を装備の有無で消さないため。
      89            1 :         if world.Components.Player.Has(entity) && world.Components.LightSource.Has(entity) {
      90            1 :                 ls := world.Components.LightSource.Get(entity)
      91            1 :                 ls.Enabled = false
      92            1 :                 var bestRadius consts.Tile
      93            1 :                 lightQuery := query.ActiveFilter2[gc.LocationEquipped, gc.LightSource](world).Query()
      94            1 :                 for lightQuery.Next() {
      95            1 :                         litem := lightQuery.Entity()
      96            1 :                         if world.Components.LocationEquipped.Get(litem).Owner != entity {
      97            0 :                                 continue
      98              :                         }
      99            1 :                         src := world.Components.LightSource.Get(litem)
     100            1 :                         if src.Enabled && src.Radius > bestRadius {
     101            1 :                                 bestRadius = src.Radius
     102            1 :                                 ls.Radius = src.Radius
     103            1 :                                 ls.Color = src.Color
     104            1 :                                 ls.Enabled = true
     105            1 :                         }
     106              :                 }
     107              :                 // 光源が変わったので視界を再計算させる
     108            1 :                 query.GetVisionState(world).RequestUpdate()
     109              :         }
     110              : 
     111              :         // Total を計算
     112            1 :         abils.Vitality.Total = abils.Vitality.Base + abils.Vitality.Modifier
     113            1 :         abils.Strength.Total = abils.Strength.Base + abils.Strength.Modifier
     114            1 :         abils.Sensation.Total = abils.Sensation.Base + abils.Sensation.Modifier
     115            1 :         abils.Dexterity.Total = abils.Dexterity.Base + abils.Dexterity.Modifier
     116            1 :         abils.Agility.Total = abils.Agility.Base + abils.Agility.Modifier
     117            1 :         abils.Defense.Total = abils.Defense.Base + abils.Defense.Modifier
     118            1 : 
     119              :         // HP/Poolsを更新。abils を使うので、WeightDirty の Add による構造変更で
     120              :         // abils ポインタが無効化される前に行う
     121            1 :         if world.Components.HP.Has(entity) {
     122            1 :                 hp := world.Components.HP.Get(entity)
     123            1 :                 hp.Max = maxHP(abils)
     124            1 :                 hp.Current = min(hp.Max, hp.Current)
     125            1 :         }
     126              : 
     127            1 :         if world.Components.WeightCapacity.Has(entity) {
     128              :                 // 所持重量を再計算する。力が変化した場合に最大重量が変わるので
     129            1 :                 if !world.Components.WeightDirty.Has(entity) {
     130            1 :                         world.Components.WeightDirty.Add(entity, &gc.WeightDirty{})
     131            1 :                 }
     132              :         }
     133              : 
     134              :         // APを再計算
     135            1 :         if world.Components.TurnBased.Has(entity) {
     136            1 :                 maxAP := query.CalculateMaxActionPoints(world, entity)
     137            1 :                 // WeightDirty の Add より後に取得するので、構造変更後の新しい領域を指す。
     138              :                 // 構造変更前に取得したポインタを使い回さない限り安全
     139            1 :                 turnBased := world.Components.TurnBased.Get(entity)
     140            1 : 
     141              :                 // 最大APを更新
     142            1 :                 turnBased.AP.Max = maxAP
     143            1 : 
     144              :                 // 現在APが最大APを超えている場合は切り詰める
     145            1 :                 if turnBased.AP.Current > maxAP {
     146            1 :                         turnBased.AP.Current = maxAP
     147            1 :                 }
     148              :         }
     149              : }
     150              : 
     151              : // updateCubeFuelCapacity はキューブの燃料タンクの上限を計算し直し、現在値を切り詰める。
     152              : func updateCubeFuelCapacity(world w.World, cube ecs.Entity) {
     153            1 :         tank := world.Components.FuelTank.Get(cube)
     154            1 :         tank.Max = query.CubeFuelCapacity(world, cube)
     155            1 :         tank.Current = min(tank.Current, tank.Max)
     156            1 : }
     157              : 
     158              : // 30+(体力*8+力+感覚)
     159              : func maxHP(abils *gc.Abilities) int {
     160            1 :         return 30 + abils.Vitality.Total*8 + abils.Strength.Total + abils.Sensation.Total
     161            1 : }
        

Generated by: LCOV version 2.0-1