LCOV - code coverage report
Current view: top level - activity - player_actions.go Coverage Total Hit
Test: coverage.lcov Lines: 85.8 % 127 109
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package activity
       2              : 
       3              : import (
       4              :         "fmt"
       5              : 
       6              :         "github.com/kijimaD/ruins/internal/i18n"
       7              : 
       8              :         gc "github.com/kijimaD/ruins/internal/components"
       9              :         "github.com/kijimaD/ruins/internal/gamelog"
      10              :         w "github.com/kijimaD/ruins/internal/world"
      11              : 
      12              :         "github.com/kijimaD/ruins/internal/world/query"
      13              :         "github.com/mlange-42/ark/ecs"
      14              : )
      15              : 
      16              : // ExecuteMoveAction は移動アクションを実行する
      17              : func ExecuteMoveAction(world w.World, direction gc.Direction) error {
      18            1 :         entity, err := query.GetPlayerEntity(world)
      19            1 :         if err != nil {
      20            1 :                 return err
      21            1 :         }
      22              : 
      23            1 :         if !world.Components.GridElement.Has(entity) {
      24            1 :                 return fmt.Errorf("player has no GridElement component")
      25            1 :         }
      26              : 
      27            1 :         gridElement := world.Components.GridElement.Get(entity)
      28            1 :         current := gridElement.Coord
      29            1 : 
      30            1 :         next := current.Add(direction.GetDelta())
      31            1 : 
      32              :         // 移動先の同居 Interactable を全て見て、OnCollision 方式のものを自動実行する。ポータルのような
      33              :         // Manual 単独の Interactable が同じタイルに同居しても、NPC の会話などの OnCollision を取りこぼさない。
      34            1 :         targetGrid := &gc.GridElement{Coord: next}
      35            1 :         for _, interactableEntity := range interactablesAtSameTile(world, targetGrid) {
      36            1 :                 interactable := world.Components.Interactable.Get(interactableEntity)
      37            1 :                 for _, interaction := range interactable.Interactions {
      38            1 :                         if interaction.Config().ActivationWay != gc.ActivationWayOnCollision {
      39            1 :                                 continue
      40              :                         }
      41            1 :                         switch interaction {
      42              :                         case gc.InteractionDoor:
      43              :                                 // 扉は閉じている場合のみ実行(開いている場合は通過)
      44            0 :                                 if world.Components.Door.Has(interactableEntity) {
      45            0 :                                         door := world.Components.Door.Get(interactableEntity)
      46            0 :                                         if !door.IsOpen {
      47            0 :                                                 _, err := ExecuteInteraction(entity, interactableEntity, interaction, world)
      48            0 :                                                 return err
      49            0 :                                         }
      50              :                                 }
      51              :                         case gc.InteractionMelee:
      52            1 :                                 if query.FactionRelation(world, entity, interactableEntity) == query.RelationHostile {
      53            1 :                                         _, err := ExecuteInteraction(entity, interactableEntity, interaction, world)
      54            1 :                                         return err
      55            1 :                                 }
      56              :                         case gc.InteractionTalk, gc.InteractionSwap:
      57              :                                 // 会話と入れ替えは歩き込むだけで発動する
      58            1 :                                 _, err := ExecuteInteraction(entity, interactableEntity, interaction, world)
      59            1 :                                 return err
      60            0 :                         default:
      61              :                                 // 衝突時に自動発動しない種類はここでは扱わない
      62              :                         }
      63              :                 }
      64              :         }
      65              : 
      66            1 :         canMove := CanMoveTo(world, next, current, entity)
      67            1 :         if canMove {
      68            1 :                 destination := gc.GridElement{Coord: next}
      69            1 :                 // 重量超過はプレイヤーの通常状態。Execute はユーザ起因の失敗を gamelog へ出したうえで
      70              :                 // err=nil を返すため、壁への歩き込みと同じく no-op になる
      71            1 :                 _, err := Execute(NewMoveActivity(destination), entity, world)
      72            1 :                 return err
      73            1 :         }
      74              : 
      75            0 :         return nil
      76              : }
      77              : 
      78              : // ExecuteWaitAction は待機アクションを実行する
      79              : func ExecuteWaitAction(world w.World) error {
      80            1 :         entity, err := query.GetPlayerEntity(world)
      81            1 :         if err != nil {
      82            1 :                 return err
      83            1 :         }
      84              : 
      85            1 :         _, err = Execute(NewWaitActivity(1), entity, world)
      86            1 :         return err
      87              : }
      88              : 
      89              : // interactablesAtSameTile は指定タイルにある生存 Interactable を全て返す。同一タイルにポータルと
      90              : // NPC のように複数の Interactable が同居しうるため、先着1件でなく全件を返して取りこぼしを防ぐ。
      91              : func interactablesAtSameTile(world w.World, targetGrid *gc.GridElement) []ecs.Entity {
      92            1 :         var found []ecs.Entity
      93            1 :         interactableQuery := query.ActiveFilter2[gc.GridElement, gc.Interactable](world).Without(ecs.C[gc.Dead]()).Query()
      94            1 :         for interactableQuery.Next() {
      95            1 :                 entity := interactableQuery.Entity()
      96            1 :                 ge := world.Components.GridElement.Get(entity)
      97            1 :                 // 直上タイルのみ
      98            1 :                 if ge.X == targetGrid.X && ge.Y == targetGrid.Y {
      99            1 :                         found = append(found, entity)
     100            1 :                 }
     101              :         }
     102            1 :         return found
     103              : }
     104              : 
     105              : // GetAllInteractiveInteractablesInRange は範囲内の生存 Interactable を返す。Manual と OnCollision が対象。
     106              : // 死体を含めると再攻撃で不変条件違反になるため除外する
     107              : func GetAllInteractiveInteractablesInRange(world w.World, targetGrid *gc.GridElement) []ecs.Entity {
     108            1 :         var results []ecs.Entity
     109            1 : 
     110            1 :         rangeQuery := query.ActiveFilter2[gc.GridElement, gc.Interactable](world).Without(ecs.C[gc.Dead]()).Query()
     111            1 :         for rangeQuery.Next() {
     112            1 :                 entity := rangeQuery.Entity()
     113            1 :                 interactable := world.Components.Interactable.Get(entity)
     114            1 :                 gridElement := world.Components.GridElement.Get(entity)
     115            1 : 
     116            1 :                 for _, interaction := range interactable.Interactions {
     117            1 :                         way := interaction.Config().ActivationWay
     118            1 :                         if (way == gc.ActivationWayManual || way == gc.ActivationWayOnCollision) &&
     119            1 :                                 query.IsInActivationRange(targetGrid, gridElement, interaction.Config().ActivationRange) {
     120            1 :                                 results = append(results, entity)
     121            1 :                                 break // 同じエンティティを重複追加しない
     122              :                         }
     123              :                 }
     124              :         }
     125              : 
     126            1 :         return results
     127              : }
     128              : 
     129              : // GetDirectionLabel はプレイヤーからターゲットへの方向ラベルを取得する
     130              : func GetDirectionLabel(playerGrid, targetGrid *gc.GridElement) i18n.Msgid {
     131            1 :         d := targetGrid.Sub(playerGrid.Coord)
     132            1 : 
     133            1 :         if d.X == 0 && d.Y == 0 {
     134            1 :                 return "here"
     135            1 :         }
     136              : 
     137              :         // 8方向を判定
     138            1 :         if d.Y < 0 {
     139            1 :                 if d.X < 0 {
     140            1 :                         return "upper left"
     141            2 :                 } else if d.X > 0 {
     142            1 :                         return "upper right"
     143            1 :                 }
     144            1 :                 return "up"
     145            1 :         } else if d.Y > 0 {
     146            1 :                 if d.X < 0 {
     147            1 :                         return "lower left"
     148            2 :                 } else if d.X > 0 {
     149            1 :                         return "lower right"
     150            1 :                 }
     151            1 :                 return "down"
     152              :         }
     153            1 :         if d.X < 0 {
     154            1 :                 return "left"
     155            1 :         }
     156            1 :         return "right"
     157              : }
     158              : 
     159              : // showTileInteractionMessage は範囲内の全Manual相互作用のメッセージを表示する
     160              : func showTileInteractionMessage(world w.World, playerGrid *gc.GridElement) {
     161            1 :         entities := GetAllInteractiveInteractablesInRange(world, playerGrid)
     162            1 :         // 同一スタックは1行にまとめる。床の同種エンティティごとに重複ログを出さない
     163            1 :         loggedItemStacks := map[query.StackKey]bool{}
     164            1 :         for _, entity := range entities {
     165            1 :                 interactable := world.Components.Interactable.Get(entity)
     166            1 :                 entityGrid := world.Components.GridElement.Get(entity)
     167            1 :                 for _, interaction := range interactable.Interactions {
     168            1 :                         config := interaction.Config()
     169            1 :                         if config.ActivationWay != gc.ActivationWayManual {
     170            1 :                                 continue
     171              :                         }
     172              :                         // 実体はいずれかの相互作用で範囲内だが、ログはこの相互作用自身の範囲を満たすときだけ出す。
     173              :                         // 別の相互作用で範囲入りした実体の、直上専用ログを隣接で出さない
     174            1 :                         if !query.IsInActivationRange(playerGrid, entityGrid, config.ActivationRange) {
     175            0 :                                 continue
     176              :                         }
     177            1 :                         switch interaction {
     178              :                         case gc.InteractionItem:
     179            1 :                                 key, ok := query.StackKeyOf(world, entity)
     180            1 :                                 if ok && loggedItemStacks[key] {
     181            1 :                                         continue
     182              :                                 }
     183            1 :                                 if ok {
     184            1 :                                         loggedItemStacks[key] = true
     185            1 :                                 }
     186            1 :                                 formattedName := query.FormatItemName(world, entity)
     187            1 :                                 gamelog.New(query.GetGameLog(world)).
     188            1 :                                         Markup(query.T(world, "%s is here.", gamelog.Tag("item", formattedName))).
     189            1 :                                         Log()
     190              :                         case gc.InteractionPortalNext:
     191            0 :                                 gamelog.New(query.GetGameLog(world)).
     192            0 :                                         Markup(query.T(world, "There is a warp gate. Press Enter to move.")).
     193            0 :                                         Log()
     194              :                         case gc.InteractionPortalPrev:
     195            0 :                                 gamelog.New(query.GetGameLog(world)).
     196            0 :                                         Markup(query.T(world, "There is an up staircase. Press Enter to move.")).
     197            0 :                                         Log()
     198              :                         case gc.InteractionDungeonEnter:
     199            0 :                                 gamelog.New(query.GetGameLog(world)).
     200            0 :                                         Markup(query.T(world, "There is a ruins entrance. Press Enter to enter.")).
     201            0 :                                         Log()
     202              :                         case gc.InteractionDoor, gc.InteractionTalk, gc.InteractionStorage, gc.InteractionMelee, gc.InteractionIgnite, gc.InteractionFeedFuel,
     203            1 :                                 gc.InteractionOpenCubeMenu, gc.InteractionSwap:
     204              :                                 // 足元ログを出さない種類。default を置かず exhaustive に全種別を
     205              :                                 // 明示させ、新しい InteractionKind の対応漏れを lint で検知する
     206              :                         }
     207              :                 }
     208              :         }
     209              : }
        

Generated by: LCOV version 2.0-1