LCOV - code coverage report
Current view: top level - world/gameaction - damage.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 61 61
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package gameaction
       2              : 
       3              : import (
       4              :         gc "github.com/kijimaD/ruins/internal/components"
       5              :         "github.com/kijimaD/ruins/internal/gamelog"
       6              :         w "github.com/kijimaD/ruins/internal/world"
       7              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
       8              :         "github.com/kijimaD/ruins/internal/world/query"
       9              :         "github.com/mlange-42/ark/ecs"
      10              : )
      11              : 
      12              : // dealHP は HP を damage ぶん削り、致死なら Dead を付けて died を返す。
      13              : // ログや死因記録は呼び出し側が状況に応じて行う。
      14              : func dealHP(world w.World, target ecs.Entity, damage int) (died bool) {
      15            1 :         hp := world.Components.HP.Get(target)
      16            1 : 
      17            1 :         beforeHP := hp.Current
      18            1 :         hp.Current -= damage
      19            1 :         if hp.Current < 0 {
      20            1 :                 hp.Current = 0
      21            1 :         }
      22              : 
      23            1 :         if hp.Current <= 0 && beforeHP > 0 {
      24            1 :                 world.Components.Dead.Add(target, &gc.Dead{})
      25            1 :                 return true
      26            1 :         }
      27            1 :         return false
      28              : }
      29              : 
      30              : // ApplyDamage は共通のダメージ処理を実行する
      31              : // source から target へダメージを与え、死亡判定とログ出力を行う
      32              : func ApplyDamage(world w.World, target ecs.Entity, damage int, source ecs.Entity) {
      33            1 :         died := dealHP(world, target, damage)
      34            1 : 
      35              :         // 被ダメージによる態度変化
      36            1 :         reactToHostileAction(world, target)
      37            1 : 
      38            1 :         if died {
      39              :                 // 戦闘のダメージで倒れたプレイヤーは戦闘死として死因を記録する
      40            1 :                 if isPlayerEntity(target, world) {
      41            1 :                         if rs := query.GetRunStats(world); rs != nil {
      42            1 :                                 rs.Cause = gc.CauseKilled
      43            1 :                         }
      44              :                 }
      45            1 :                 logDeath(world, target, source)
      46              :         }
      47              : }
      48              : 
      49              : // ApplyConditionDamage は低体温や病気など発生源のない不調から target の HP を削る。
      50              : // 死因ラベルを受け取り、プレイヤーが倒れたら RunStats へ記録する。
      51              : // 攻撃者がいないので AI の敵対反応は起こさない。
      52              : func ApplyConditionDamage(world w.World, target ecs.Entity, damage int, cause gc.DeathCause) {
      53            1 :         died := dealHP(world, target, damage)
      54            1 : 
      55            1 :         if died {
      56            1 :                 if isPlayerEntity(target, world) {
      57            1 :                         if rs := query.GetRunStats(world); rs != nil {
      58            1 :                                 rs.Cause = cause
      59            1 :                         }
      60              :                 }
      61              :                 // 発生源が無いので source は target 自身を渡す。プレイヤー関与のログ判定はこれで足りる
      62            1 :                 logDeath(world, target, target)
      63              :         }
      64              : }
      65              : 
      66              : // reactToHostileAction は被ダメージ時にAIの戦闘方針を変化させる。
      67              : // CombatIgnore は反撃のため CombatAttack に遷移する
      68              : func reactToHostileAction(world w.World, target ecs.Entity) {
      69            1 :         if world.Components.SoloAI.Has(target) {
      70            1 :                 world.Components.SoloAI.Get(target).ReactToHostile()
      71            1 :         }
      72              : }
      73              : 
      74              : // logDeath は死亡・破壊ログを出力する。
      75              : // Prop は「壊れた」、それ以外は「倒れた」と表示する。
      76              : // プレイヤーが関与する場合のみログを出力する
      77              : func logDeath(world w.World, target ecs.Entity, source ecs.Entity) {
      78            1 :         isRelevant := isPlayerEntity(source, world) || isPlayerEntity(target, world)
      79            1 :         if !isRelevant {
      80            1 :                 return
      81            1 :         }
      82              : 
      83            1 :         targetName := query.GetEntityName(target, world)
      84            1 : 
      85            1 :         suffix := query.T(world, " was defeated.")
      86            1 :         if world.Components.Prop.Has(target) {
      87            1 :                 suffix = query.T(world, " was destroyed.")
      88            1 :         }
      89              : 
      90            1 :         gamelog.New(query.GetGameLog(world)).
      91            1 :                 Markup(query.NameMarkup(target, targetName, world) + suffix).
      92            1 :                 Log()
      93              : }
      94              : 
      95              : // isPlayerEntity はエンティティがプレイヤーかを判定する
      96              : func isPlayerEntity(entity ecs.Entity, world w.World) bool {
      97            1 :         return world.Components.Player.Has(entity)
      98            1 : }
      99              : 
     100              : // ApplyHealing は共通の回復処理を実行する
     101              : // target に amount 分のHPを回復させる
     102              : // 実際の回復量を返す
     103              : func ApplyHealing(world w.World, target ecs.Entity, amount int) int {
     104            1 :         hp := world.Components.HP.Get(target)
     105            1 : 
     106            1 :         beforeHP := hp.Current
     107            1 :         hp.Current += amount
     108            1 :         if hp.Current > hp.Max {
     109            1 :                 hp.Current = hp.Max
     110            1 :         }
     111            1 :         actualHealing := hp.Current - beforeHP
     112            1 : 
     113              :         // 回復エフェクトを生成
     114            1 :         if actualHealing > 0 {
     115            1 :                 lifecycle.SpawnVisualEffect(target, gc.NewHealEffect(actualHealing), world)
     116            1 :         }
     117              : 
     118            1 :         return actualHealing
     119              : }
        

Generated by: LCOV version 2.0-1