LCOV - code coverage report
Current view: top level - activity - door.go Coverage Total Hit
Test: coverage.lcov Lines: 85.2 % 135 115
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package activity
       2              : 
       3              : import (
       4              :         "fmt"
       5              :         "slices"
       6              : 
       7              :         gc "github.com/kijimaD/ruins/internal/components"
       8              :         "github.com/kijimaD/ruins/internal/consts"
       9              :         "github.com/kijimaD/ruins/internal/gamelog"
      10              :         w "github.com/kijimaD/ruins/internal/world"
      11              : 
      12              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      13              :         "github.com/kijimaD/ruins/internal/world/query"
      14              :         "github.com/mlange-42/ark/ecs"
      15              : )
      16              : 
      17              : // OpenDoorBehavior はBehaviorの実装
      18              : type OpenDoorBehavior struct{}
      19              : 
      20              : // Info はBehaviorの実装
      21              : func (odb *OpenDoorBehavior) Info() Info {
      22            1 :         return Info{
      23            1 :                 Name:            "Open/Close Door",
      24            1 :                 Description:     "Open a door",
      25            1 :                 Interruptible:   false,
      26            1 :                 Resumable:       false,
      27            1 :                 ActionPointCost: consts.StandardActionCost,
      28            1 :                 TotalRequiredAP: 0,
      29            1 :         }
      30              : }
      31              : 
      32              : // Name はBehaviorの実装
      33              : func (odb *OpenDoorBehavior) Name() gc.BehaviorName {
      34            1 :         return gc.BehaviorOpenDoor
      35            1 : }
      36              : 
      37              : // NewOpenDoorActivity は対象扉を指定して開扉アクティビティを組む。
      38              : func NewOpenDoorActivity(target ecs.Entity) *gc.Activity {
      39            1 :         comp := NewActivity(gc.BehaviorOpenDoor, 0)
      40            1 :         comp.Params = &gc.OpenDoorParams{Target: target}
      41            1 :         return comp
      42            1 : }
      43              : 
      44              : // Validate は扉開閉アクティビティの検証を行う
      45              : func (odb *OpenDoorBehavior) Validate(comp *gc.Activity, _ ecs.Entity, world w.World) error {
      46            1 :         p, ok := comp.Params.(*gc.OpenDoorParams)
      47            1 :         if !ok {
      48            0 :                 return ErrParamsTypeMismatch
      49            0 :         }
      50              : 
      51            1 :         targetEntity := p.Target
      52            1 : 
      53              :         // ゼロ値・死亡エンティティはArkのHasでパニックするため先に弾く
      54            1 :         if !world.ECS.Alive(targetEntity) {
      55            1 :                 return fmt.Errorf("target is not alive")
      56            1 :         }
      57              : 
      58              :         // Doorコンポーネントを持っているか確認
      59            1 :         if !world.Components.Door.Has(targetEntity) {
      60            1 :                 return fmt.Errorf("target is not a door")
      61            1 :         }
      62              : 
      63            1 :         return nil
      64              : }
      65              : 
      66              : // Start は扉開閉開始時の処理を実行する
      67              : func (odb *OpenDoorBehavior) Start(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
      68            1 :         log.Debug("open door started", "actor", actor)
      69            1 :         return nil
      70            1 : }
      71              : 
      72              : // DoTurn は扉開閉アクティビティの1ターン分の処理を実行する
      73              : func (odb *OpenDoorBehavior) DoTurn(comp *gc.Activity, _ ecs.Entity, world w.World) error {
      74            1 :         p, ok := comp.Params.(*gc.OpenDoorParams)
      75            1 :         if !ok {
      76            0 :                 return ErrParamsTypeMismatch
      77            0 :         }
      78            1 :         targetEntity := p.Target
      79            1 : 
      80            1 :         if !world.Components.Door.Has(targetEntity) {
      81            0 :                 return fmt.Errorf("cannot get door component")
      82            0 :         }
      83            1 :         raw := world.Components.Door.Get(targetEntity)
      84            1 :         doorComp := raw
      85            1 : 
      86              :         // 扉を開く
      87            1 :         if !doorComp.IsOpen {
      88            1 :                 if err := lifecycle.OpenDoor(world, targetEntity); err != nil {
      89            0 :                         return err
      90            0 :                 }
      91              : 
      92            1 :                 log.Debug("door opened", "door", targetEntity)
      93            1 : 
      94              :                 // 視界の更新が必要
      95            1 :                 query.GetVisionState(world).RequestUpdate()
      96              :         }
      97              : 
      98            1 :         Complete(comp)
      99            1 :         return nil
     100              : }
     101              : 
     102              : // Finish は扉開閉完了時の処理を実行する
     103              : func (odb *OpenDoorBehavior) Finish(_ *gc.Activity, actor ecs.Entity, world w.World) error {
     104            1 :         log.Debug("open door activity finished", "actor", actor)
     105            1 : 
     106              :         // プレイヤーの場合のみメッセージを表示
     107            1 :         if world.Components.Player.Has(actor) {
     108            1 :                 gamelog.New(query.GetGameLog(world)).
     109            1 :                         Markup(query.T(world, "Opened the door.")).
     110            1 :                         Log()
     111            1 :         }
     112              : 
     113            1 :         return nil
     114              : }
     115              : 
     116              : // Canceled は扉開閉キャンセル時の処理を実行する
     117              : func (odb *OpenDoorBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
     118            1 :         log.Debug("open door canceled", "actor", actor, "reason", comp.CancelReason)
     119            1 :         return nil
     120            1 : }
     121              : 
     122              : // CloseDoorBehavior はBehaviorの実装
     123              : type CloseDoorBehavior struct{}
     124              : 
     125              : // Info はBehaviorの実装
     126              : func (cdb *CloseDoorBehavior) Info() Info {
     127            1 :         return Info{
     128            1 :                 Name:            "Close Door",
     129            1 :                 Description:     "Close a door",
     130            1 :                 Interruptible:   false,
     131            1 :                 Resumable:       false,
     132            1 :                 ActionPointCost: consts.StandardActionCost,
     133            1 :                 TotalRequiredAP: 0,
     134            1 :         }
     135              : }
     136              : 
     137              : // Name はBehaviorの実装
     138              : func (cdb *CloseDoorBehavior) Name() gc.BehaviorName {
     139            1 :         return gc.BehaviorCloseDoor
     140            1 : }
     141              : 
     142              : // NewCloseDoorActivity は対象扉を指定して閉扉アクティビティを組む。
     143              : func NewCloseDoorActivity(target ecs.Entity) *gc.Activity {
     144            1 :         comp := NewActivity(gc.BehaviorCloseDoor, 0)
     145            1 :         comp.Params = &gc.CloseDoorParams{Target: target}
     146            1 :         return comp
     147            1 : }
     148              : 
     149              : // Validate は扉閉鎖アクティビティの検証を行う
     150              : func (cdb *CloseDoorBehavior) Validate(comp *gc.Activity, _ ecs.Entity, world w.World) error {
     151            1 :         p, ok := comp.Params.(*gc.CloseDoorParams)
     152            1 :         if !ok {
     153            0 :                 return ErrParamsTypeMismatch
     154            0 :         }
     155              : 
     156            1 :         targetEntity := p.Target
     157            1 : 
     158              :         // ゼロ値・死亡エンティティはArkのHasでパニックするため先に弾く
     159            1 :         if !world.ECS.Alive(targetEntity) {
     160            0 :                 return fmt.Errorf("target is not alive")
     161            0 :         }
     162              : 
     163              :         // Doorコンポーネントを持っているか確認
     164            1 :         if !world.Components.Door.Has(targetEntity) {
     165            0 :                 return fmt.Errorf("target is not a door")
     166            0 :         }
     167              : 
     168              :         // 扉のマスにキャラクターか拾えるアイテムがいれば閉じられない。人や物の上に扉を閉じない。
     169              :         // 扉相互作用は隣接マスから発動するため、閉じる本人はこの判定に当たらない
     170            1 :         coord := world.Components.GridElement.Get(targetEntity).Coord
     171            1 :         if doorTileOccupied(world, coord) {
     172            1 :                 return &UserError{Msg: query.T(world, "Something is in the doorway.")}
     173            1 :         }
     174              : 
     175            1 :         return nil
     176              : }
     177              : 
     178              : // doorTileOccupied は扉の座標にキャラクターか拾えるアイテムがいるかを返す
     179              : func doorTileOccupied(world w.World, coord consts.Coord[consts.Tile]) bool {
     180              :         // si は現ステージのフィールドが無いときだけ nil。扉閉鎖では実際には nil にならない
     181            1 :         if si := query.GetSpatialIndex(world); si != nil {
     182            1 :                 if _, ok := si.CharacterAt(coord); ok {
     183            1 :                         return true
     184            1 :                 }
     185              :         }
     186              :         // 扉自身も LocationOnField を持つため、Item を持つ拾える物だけ IsPickable で見る
     187            1 :         return slices.ContainsFunc(query.GetEntitiesAt(world, coord.X, coord.Y), func(e ecs.Entity) bool {
     188            1 :                 return query.IsPickable(e, world)
     189            1 :         })
     190              : }
     191              : 
     192              : // Start は扉閉鎖開始時の処理を実行する
     193              : func (cdb *CloseDoorBehavior) Start(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
     194            1 :         log.Debug("close door started", "actor", actor)
     195            1 :         return nil
     196            1 : }
     197              : 
     198              : // DoTurn は扉閉鎖アクティビティの1ターン分の処理を実行する
     199              : func (cdb *CloseDoorBehavior) DoTurn(comp *gc.Activity, _ ecs.Entity, world w.World) error {
     200            1 :         p, ok := comp.Params.(*gc.CloseDoorParams)
     201            1 :         if !ok {
     202            0 :                 return ErrParamsTypeMismatch
     203            0 :         }
     204            1 :         targetEntity := p.Target
     205            1 : 
     206            1 :         if !world.Components.Door.Has(targetEntity) {
     207            0 :                 return fmt.Errorf("cannot get door component")
     208            0 :         }
     209            1 :         raw := world.Components.Door.Get(targetEntity)
     210            1 :         doorComp := raw
     211            1 : 
     212              :         // 扉を閉じる
     213            1 :         if doorComp.IsOpen {
     214            1 :                 if err := lifecycle.CloseDoor(world, targetEntity); err != nil {
     215            0 :                         return err
     216            0 :                 }
     217              : 
     218            1 :                 log.Debug("door closed", "door", targetEntity)
     219            1 : 
     220              :                 // 視界の更新が必要であることをマーク(BlockViewが変更されたため)
     221            1 :                 query.GetVisionState(world).RequestUpdate()
     222              :         }
     223              : 
     224            1 :         Complete(comp)
     225            1 :         return nil
     226              : }
     227              : 
     228              : // Finish は扉閉鎖完了時の処理を実行する
     229              : func (cdb *CloseDoorBehavior) Finish(_ *gc.Activity, actor ecs.Entity, world w.World) error {
     230            1 :         log.Debug("close door activity finished", "actor", actor)
     231            1 : 
     232              :         // プレイヤーの場合のみメッセージを表示
     233            1 :         if world.Components.Player.Has(actor) {
     234            1 :                 gamelog.New(query.GetGameLog(world)).
     235            1 :                         Markup(query.T(world, "Closed the door.")).
     236            1 :                         Log()
     237            1 :         }
     238              : 
     239            1 :         return nil
     240              : }
     241              : 
     242              : // Canceled は扉閉鎖キャンセル時の処理を実行する
     243              : func (cdb *CloseDoorBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
     244            1 :         log.Debug("close door canceled", "actor", actor, "reason", comp.CancelReason)
     245            1 :         return nil
     246            1 : }
        

Generated by: LCOV version 2.0-1