LCOV - code coverage report
Current view: top level - states - dungeon_input.go Coverage Total Hit
Test: coverage.lcov Lines: 26.1 % 199 52
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package states
       2              : 
       3              : import (
       4              :         "fmt"
       5              : 
       6              :         "github.com/kijimaD/ruins/internal/i18n"
       7              :         gs "github.com/kijimaD/ruins/internal/systems"
       8              : 
       9              :         "github.com/hajimehoshi/ebiten/v2"
      10              :         "github.com/kijimaD/ruins/internal/activity"
      11              :         gc "github.com/kijimaD/ruins/internal/components"
      12              :         es "github.com/kijimaD/ruins/internal/engine/states"
      13              :         "github.com/kijimaD/ruins/internal/gamelog"
      14              :         "github.com/kijimaD/ruins/internal/inputmapper"
      15              :         "github.com/kijimaD/ruins/internal/keybind"
      16              :         mapplanner "github.com/kijimaD/ruins/internal/mapplanner"
      17              :         "github.com/kijimaD/ruins/internal/menuloop"
      18              :         "github.com/kijimaD/ruins/internal/messagedata"
      19              :         "github.com/kijimaD/ruins/internal/raw"
      20              :         w "github.com/kijimaD/ruins/internal/world"
      21              : 
      22              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      23              :         "github.com/kijimaD/ruins/internal/world/query"
      24              :         "github.com/mlange-42/ark/ecs"
      25              : )
      26              : 
      27              : // 入力・アクション・イベント処理を dungeon.go から分離する。DungeonState のメソッドはこのファイルにも置く。
      28              : 
      29              : // dungeonBindings はダンジョン操作の束縛表。モーダル開閉・動詞直達・移動・視点回転・待機・武器切替。
      30              : // 全行の条件は互いに素で行順に意味は無い
      31              : var dungeonBindings = []keybind.Binding{
      32              :         // モーダル開閉
      33              :         {Key: ebiten.KeyM, Action: inputmapper.ActionOpenDungeonMenu, Label: "Menu"},
      34              :         {Key: ebiten.KeySpace, Action: inputmapper.ActionOpenInteractionMenu, Label: "Interact"},
      35              :         // 視界情報表示。調べる X すなわち Shift+x へ X を譲り、フィールド情報は L に置く
      36              :         {Key: ebiten.KeyL, Action: inputmapper.ActionOpenFieldInfo, Label: "Field info"},
      37              :         {Key: ebiten.KeyN, Action: inputmapper.ActionOpenOverworldMap, Label: "Map"},
      38              :         {Key: ebiten.KeyF, Action: inputmapper.ActionShoot, Label: "Shoot"},
      39              :         {Key: ebiten.KeyS, Action: inputmapper.ActionAttack, Label: "Attack"},
      40              :         {Key: ebiten.KeyG, Action: inputmapper.ActionPickup, Label: "Pick up"},
      41              :         // 動詞タブ画面への直達。調べる X は Shift+x で区別する
      42              :         {Key: ebiten.KeyX, Shift: keybind.ShiftRequired, Action: inputmapper.ActionVerbExamine, Label: "Inspect"},
      43              :         {Key: ebiten.KeyD, Shift: keybind.ShiftForbidden, Action: inputmapper.ActionVerbPlace, Label: "Drop"},
      44              :         {Key: ebiten.KeyD, Shift: keybind.ShiftRequired, Action: inputmapper.ActionVerbDisassemble, Label: "Disassemble"},
      45              :         {Key: ebiten.KeyE, Action: inputmapper.ActionVerbConsume, Label: "Eat"},
      46              :         {Key: ebiten.KeyR, Action: inputmapper.ActionVerbRead, Label: "Read"},
      47              :         {Key: ebiten.KeyT, Action: inputmapper.ActionVerbUse, Label: "Use"},
      48              :         {Key: ebiten.KeyB, Action: inputmapper.ActionSleep, Label: "Sleep"},
      49              :         // 移動。WASD は動詞へ空けるため矢印キーのみを使う。斜めへは視点を回してから直進する
      50              :         {Key: ebiten.KeyUp, Press: keybind.PressRepeat, Action: inputmapper.ActionMoveNorth, Label: "Move"},
      51              :         {Key: ebiten.KeyDown, Press: keybind.PressRepeat, Action: inputmapper.ActionMoveSouth, Label: "Move"},
      52              :         {Key: ebiten.KeyLeft, Press: keybind.PressRepeat, Action: inputmapper.ActionMoveWest, Label: "Move"},
      53              :         {Key: ebiten.KeyRight, Press: keybind.PressRepeat, Action: inputmapper.ActionMoveEast, Label: "Move"},
      54              :         // 視点回転。左の Z を反時計回り、右の C を時計回りにしてキーの左右と回る向きをそろえる。
      55              :         // JIS でズレる記号は避ける
      56              :         {Key: ebiten.KeyZ, Action: inputmapper.ActionRotateLeft, Label: "Rotate"},
      57              :         {Key: ebiten.KeyC, Action: inputmapper.ActionRotateRight, Label: "Rotate"},
      58              :         // 待機・足元の相互作用
      59              :         {Key: ebiten.KeyPeriod, Press: keybind.PressRepeat, Action: inputmapper.ActionWait, Label: "Wait"},
      60              :         {Key: ebiten.KeyEnter, Action: inputmapper.ActionInteract, Label: "Use here"},
      61              :         // 武器スロット切り替え。同じラベルの連続行なのでヒントでは 12345 と連結される
      62              :         {Key: ebiten.Key1, Action: inputmapper.ActionSwitchWeaponSlot1, Label: "Weapon slot"},
      63              :         {Key: ebiten.Key2, Action: inputmapper.ActionSwitchWeaponSlot2, Label: "Weapon slot"},
      64              :         {Key: ebiten.Key3, Action: inputmapper.ActionSwitchWeaponSlot3, Label: "Weapon slot"},
      65              :         {Key: ebiten.Key4, Action: inputmapper.ActionSwitchWeaponSlot4, Label: "Weapon slot"},
      66              :         {Key: ebiten.Key5, Action: inputmapper.ActionSwitchWeaponSlot5, Label: "Weapon slot"},
      67              :         // キー一覧ヘルプ
      68              :         {Key: ebiten.KeySlash, Shift: keybind.ShiftRequired, Action: inputmapper.ActionOpenKeyHelp, Label: "Help"},
      69              : }
      70              : 
      71              : // dungeonDebugBindings はデバッグ設定のときだけ有効なキー。本番の表とは分けて合成する。
      72              : // Slash はキー一覧ヘルプの Shift+Slash とキーを共有するので Shift 無しに限定する。
      73              : // 条件が重なれば MustMerge が構築時に拒否する
      74              : var dungeonDebugBindings = []keybind.Binding{
      75              :         {Key: ebiten.KeySlash, Shift: keybind.ShiftForbidden, Action: inputmapper.ActionOpenDebugMenu},
      76              : }
      77              : 
      78              : // dungeonTable と dungeonDebugTable は合成済みの束縛表。デバッグ設定で使う表ごと分け、
      79              : // 実行時に表を重ねる階層を持たない。デバッグ行との重なりも構築時に検証される
      80              : var (
      81              :         dungeonTable      = keybind.MustMerge(dungeonBindings)
      82              :         dungeonDebugTable = keybind.MustMerge(dungeonDebugBindings, dungeonBindings)
      83              : )
      84              : 
      85              : // readAction は1フレームのダンジョン操作を Action として読む。供給源があればそこから読み、
      86              : // 再生ドライバがメニューと同じ注入点でダンジョンも駆動できる
      87              : func (st *DungeonState) readAction(world w.World) (inputmapper.ActionID, bool) {
      88            1 :         if world.Resources.Config.Debug {
      89            1 :                 return keybind.ReadInput(world, dungeonDebugTable)
      90            1 :         }
      91            0 :         return keybind.ReadInput(world, dungeonTable)
      92              : }
      93              : 
      94              : // moveDir は移動方向を3Dカメラの向きへ回して合わせる。
      95              : func (st *DungeonState) moveDir(world w.World, base gc.Direction) gc.Direction {
      96            1 :         return st.three.moveDir(world, base)
      97            1 : }
      98              : 
      99              : // DoAction はActionを実行する
     100              : //
     101              : //nolint:gocyclo // 多くのアクションを処理するためswitch文が大きくなる
     102              : func (st *DungeonState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
     103              :         // UI系アクションは常に実行可能
     104            1 :         switch action {
     105              :         case inputmapper.ActionOpenDungeonMenu, inputmapper.ActionOpenDebugMenu, inputmapper.ActionOpenInventory, inputmapper.ActionOpenInteractionMenu, inputmapper.ActionOpenFieldInfo, inputmapper.ActionOpenOverworldMap, inputmapper.ActionOpenKeyHelp, inputmapper.ActionShoot,
     106              :                 inputmapper.ActionVerbExamine, inputmapper.ActionVerbPlace, inputmapper.ActionVerbConsume, inputmapper.ActionVerbRead, inputmapper.ActionVerbUse, inputmapper.ActionVerbDisassemble, inputmapper.ActionVerbThrow,
     107            1 :                 inputmapper.ActionRotateLeft, inputmapper.ActionRotateRight:
     108              :                 // UI系と視点操作はターンを消費しないのでターンチェック不要
     109              :         default:
     110              :                 // ゲーム内アクション(移動、攻撃など)はターンチェックが必要
     111            1 :                 if !query.CanPlayerAct(world) {
     112            1 :                         return es.Transition[w.World]{Type: es.TransNone}, nil
     113            1 :                 }
     114              :                 // プレイヤーが継続アクション中は新しいアクションを受け付けない
     115            1 :                 if playerEntity, err := query.GetPlayerEntity(world); err == nil && query.HasActivity(world, playerEntity) {
     116            1 :                         return es.Transition[w.World]{Type: es.TransNone}, nil
     117            1 :                 }
     118              :         }
     119              : 
     120            1 :         switch action {
     121              :         // UI系アクション(ステート遷移)
     122              :         case inputmapper.ActionOpenDungeonMenu:
     123            1 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewDungeonMenuState}}, nil
     124              :         case inputmapper.ActionOpenDebugMenu:
     125            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewDebugMenuState}}, nil
     126              :         case inputmapper.ActionOpenInventory:
     127              :                 // 所持品は動詞タブ画面の調べるタブで一覧する
     128            1 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewItemActionState(verbExamine)}}, nil
     129              :         case inputmapper.ActionOpenInteractionMenu:
     130            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     131            0 :                         func() (es.State[w.World], error) { return NewInteractionMenuState(world) },
     132              :                 }}, nil
     133              :         case inputmapper.ActionOpenFieldInfo:
     134            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     135            0 :                         func() (es.State[w.World], error) { return &LookAroundState{}, nil },
     136              :                 }}, nil
     137              :         case inputmapper.ActionOpenKeyHelp:
     138              :                 // ダンジョン文脈のキー一覧を開く。表示は束縛表から導出する
     139            1 :                 return es.Transition[w.World]{Type: es.TransPush,
     140            1 :                         NewStateFuncs: []es.StateFactory[w.World]{menuloop.NewKeyHelpState(dungeonTable)}}, nil
     141              :         case inputmapper.ActionOpenOverworldMap:
     142              :                 // 地図は今まさにオーバーワールドにいるときだけ開く。ダンジョンやキューブ内部では
     143              :                 // 帯が現ステージにないので無視する。State 属性の isSeamless でなく現ステージで判定する
     144            0 :                 if !query.IsOnOverworld(world) {
     145            0 :                         return es.Transition[w.World]{Type: es.TransNone}, nil
     146            0 :                 }
     147            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     148            0 :                         func() (es.State[w.World], error) { return &OverworldMapState{}, nil },
     149              :                 }}, nil
     150              :         case inputmapper.ActionShoot:
     151            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     152            0 :                         func() (es.State[w.World], error) { return &ShootingState{}, nil },
     153              :                 }}, nil
     154              :         case inputmapper.ActionAttack:
     155            0 :                 return startDirectionAction(world, attackAction(world))
     156              :         case inputmapper.ActionPickup:
     157            0 :                 return startPickup(world)
     158              :         case inputmapper.ActionSleep:
     159            0 :                 return st.handleSleep(world)
     160              :         case inputmapper.ActionVerbExamine, inputmapper.ActionVerbPlace, inputmapper.ActionVerbConsume, inputmapper.ActionVerbRead, inputmapper.ActionVerbUse, inputmapper.ActionVerbDisassemble, inputmapper.ActionVerbThrow:
     161            0 :                 verb, ok := verbByAction(action)
     162            0 :                 if !ok {
     163            0 :                         return es.Transition[w.World]{Type: es.TransNone}, nil
     164            0 :                 }
     165            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewItemActionState(verb)}}, nil
     166              : 
     167              :         // 移動系アクション
     168              :         case inputmapper.ActionMoveNorth:
     169            1 :                 if err := activity.ExecuteMoveAction(world, st.moveDir(world, gc.DirectionUp)); err != nil {
     170            0 :                         return es.Transition[w.World]{Type: es.TransNone}, err
     171            0 :                 }
     172            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     173              :         case inputmapper.ActionMoveSouth:
     174            1 :                 if err := activity.ExecuteMoveAction(world, st.moveDir(world, gc.DirectionDown)); err != nil {
     175            0 :                         return es.Transition[w.World]{Type: es.TransNone}, err
     176            0 :                 }
     177            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     178              :         case inputmapper.ActionMoveEast:
     179            1 :                 if err := activity.ExecuteMoveAction(world, st.moveDir(world, gc.DirectionRight)); err != nil {
     180            0 :                         return es.Transition[w.World]{Type: es.TransNone}, err
     181            0 :                 }
     182            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     183              :         case inputmapper.ActionMoveWest:
     184            1 :                 if err := activity.ExecuteMoveAction(world, st.moveDir(world, gc.DirectionLeft)); err != nil {
     185            0 :                         return es.Transition[w.World]{Type: es.TransNone}, err
     186            0 :                 }
     187            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     188              :         // 視点回転。回してから直進すると斜めの world 方向へ動ける
     189              :         case inputmapper.ActionRotateLeft:
     190            1 :                 st.three.rotate(world, 1)
     191            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     192              :         case inputmapper.ActionRotateRight:
     193            0 :                 st.three.rotate(world, -1)
     194            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     195              :         case inputmapper.ActionWait:
     196            0 :                 if err := activity.ExecuteWaitAction(world); err != nil {
     197            0 :                         return es.Transition[w.World]{Type: es.TransNone}, err
     198            0 :                 }
     199            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     200              : 
     201              :         // 相互作用系アクション
     202              :         case inputmapper.ActionInteract:
     203            0 :                 actions := GetSameTileManualActions(world)
     204            0 :                 switch len(actions) {
     205            0 :                 case 0:
     206              :                         // 何もしない
     207              :                 case 1:
     208            0 :                         playerEntity, err := query.GetPlayerEntity(world)
     209            0 :                         if err != nil {
     210            0 :                                 return es.Transition[w.World]{Type: es.TransNone}, err
     211            0 :                         }
     212            0 :                         if _, err := activity.ExecuteInteraction(playerEntity, actions[0].Target, actions[0].Interaction, world); err != nil {
     213            0 :                                 return es.Transition[w.World]{Type: es.TransNone}, err
     214            0 :                         }
     215              :                 default:
     216            0 :                         return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     217            0 :                                 func() (es.State[w.World], error) { return NewChoiceMenu(sameTileActionChoices), nil },
     218              :                         }}, nil
     219              :                 }
     220            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     221              : 
     222              :         // 武器スロット切り替え系アクション
     223              :         case inputmapper.ActionSwitchWeaponSlot1:
     224            0 :                 st.switchWeaponSlot(world, 1)
     225            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     226              :         case inputmapper.ActionSwitchWeaponSlot2:
     227            0 :                 st.switchWeaponSlot(world, 2)
     228            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     229              :         case inputmapper.ActionSwitchWeaponSlot3:
     230            0 :                 st.switchWeaponSlot(world, 3)
     231            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     232              :         case inputmapper.ActionSwitchWeaponSlot4:
     233            0 :                 st.switchWeaponSlot(world, 4)
     234            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     235              :         case inputmapper.ActionSwitchWeaponSlot5:
     236            0 :                 st.switchWeaponSlot(world, 5)
     237            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     238              : 
     239              :         default:
     240            0 :                 return es.Transition[w.World]{}, fmt.Errorf("unknown action: %s", action)
     241              :         }
     242              : }
     243              : 
     244              : // ================
     245              : 
     246              : // handleStateChangeRequest はステート遷移リクエストを消費し、対応する遷移を返す
     247              : func (st *DungeonState) handleStateChangeRequest(world w.World) (es.Transition[w.World], error) {
     248            1 :         req := lifecycle.ConsumeStateChange(world)
     249            1 :         if req == nil {
     250            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     251            1 :         }
     252              : 
     253            1 :         switch p := req.Payload.(type) {
     254              :         case gc.ShowDialog:
     255              :                 // SpeakerEntityからNameを取得
     256            0 :                 if !world.Components.Name.Has(p.SpeakerEntity) {
     257            0 :                         return es.Transition[w.World]{}, fmt.Errorf("speaker entity does not have Name component")
     258            0 :                 }
     259            0 :                 speakerName := query.GetEntityName(p.SpeakerEntity, world)
     260            0 : 
     261            0 :                 if hasStock(world, p.SpeakerEntity) {
     262            0 :                         merchant := p.SpeakerEntity
     263            0 :                         return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     264            0 :                                 func() (es.State[w.World], error) { return NewMerchantDialogState(speakerName, merchant, p.MessageKey) },
     265              :                         }}, nil
     266              :                 }
     267            0 :                 dialogMessage := messagedata.GetDialogue(world, p.MessageKey, speakerName)
     268            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     269            0 :                         func() (es.State[w.World], error) { return NewMessageState(dialogMessage) },
     270              :                 }}, nil
     271              :         case gc.WarpDescend:
     272              :                 // 共存方式の下り。同一 State 内で swapTo する。現階は退避され再訪で復元できる
     273            0 :                 if err := st.descend(world); err != nil {
     274            0 :                         return es.Transition[w.World]{}, err
     275            0 :                 }
     276            0 :                 return st.completeSwap(world)
     277              :         case gc.WarpAscend:
     278              :                 // 上り階段に結線があればそこへ移動する。浅い階でも遺跡→地上でも同一機構。
     279              :                 // 全ダンジョンはオーバーワールド入口から入り、生成時に戻り先が結線される。よって
     280              :                 // handled=false は結線焼き込みの取りこぼし=バグであり、黙って握り潰さず error で落とす
     281            0 :                 handled, err := st.ascend(world)
     282            0 :                 if err != nil {
     283            0 :                         return es.Transition[w.World]{}, err
     284            0 :                 }
     285            0 :                 if !handled {
     286            0 :                         return es.Transition[w.World]{}, fmt.Errorf("top floor up stairs has no return link")
     287            0 :                 }
     288            0 :                 return st.completeSwap(world)
     289              :         case gc.WarpDungeonEnter:
     290              :                 // オーバーワールドから遺跡へ入る。同一 State 内 swapTo で帯を退避し遺跡へ切り替える。
     291              :                 // プランナー名の指定があれば固定して生成する。デバッグのプランナー単位進入で使う
     292            1 :                 if p.PlannerName != "" {
     293            1 :                         builderType, ok := mapplanner.PlannerTypeByName(world.Resources.RawMaster, p.PlannerName)
     294            1 :                         if !ok {
     295            1 :                                 return es.Transition[w.World]{}, fmt.Errorf("unknown planner name: %s", p.PlannerName)
     296            1 :                         }
     297              :                         // デバッグはプランナーを変えて見た目を試す用途なので、選ぶたびに作り直す
     298            1 :                         if err := st.enterDebugPlannerFloor(world, p.DefinitionName, builderType); err != nil {
     299            0 :                                 return es.Transition[w.World]{}, err
     300            0 :                         }
     301            1 :                         return st.completeSwap(world)
     302              :                 }
     303            1 :                 if err := st.enterDungeon(world, p.DefinitionName); err != nil {
     304            0 :                         return es.Transition[w.World]{}, err
     305            0 :                 }
     306            1 :                 return st.completeSwap(world)
     307              :         case gc.OpenStorage:
     308              :                 // 収納を開ける経路は拾うメニューだけなので、その上に重ねて閉じると一覧へ戻す
     309            1 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     310            2 :                         func() (es.State[w.World], error) { return NewChoiceMenu(pickupChoices), nil },
     311            1 :                         func() (es.State[w.World], error) { return NewStorageMenuState(p.StorageEntity) },
     312              :                 }}, nil
     313              :         case gc.OpenFeedFuel:
     314              :                 // 火への給油メニューを開く
     315            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     316            0 :                         func() (es.State[w.World], error) { return NewFeedFuelMenuState(FireFuelSink(p.FireEntity)) },
     317              :                 }}, nil
     318              :         case gc.DebugJumpToDistrict:
     319            1 :                 if st.driver == nil || !query.IsOnOverworld(world) {
     320            0 :                         return es.Transition[w.World]{}, fmt.Errorf("jumping to a district requires the overworld")
     321            0 :                 }
     322            1 :                 if err := st.driver.JumpToDistrict(world, p.DistrictID); err != nil {
     323            0 :                         return es.Transition[w.World]{}, err
     324            0 :                 }
     325              :                 // 帯を進めるとプレイヤーが動くので、カメラを合わせ直す
     326            1 :                 if err := (&gs.CameraSystem{}).Update(world); err != nil {
     327            0 :                         return es.Transition[w.World]{}, err
     328            0 :                 }
     329            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     330              :         case gc.Escape:
     331            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewRunClearState}}, nil
     332              :         case gc.OpenCubeMenu:
     333              :                 // 移動拠点キューブのメニューを開く
     334            0 :                 return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     335            0 :                         func() (es.State[w.World], error) { return NewCubeMenuState(p.Cube) },
     336              :                 }}, nil
     337              :         default:
     338              :                 // この switch で扱わない種別。未実装の scaffold もここに落ちる
     339            0 :                 return es.Transition[w.World]{}, fmt.Errorf("unhandled StateChangeRequest: %T", req.Payload)
     340              :         }
     341              : }
     342              : 
     343              : // switchWeaponSlot は指定されたスロット番号(1-5)に武器を切り替える
     344              : func (st *DungeonState) switchWeaponSlot(world w.World, slotNumber int) {
     345            0 :         query.GetWeaponSelection(world).Slot = slotNumber
     346            0 : 
     347              :         // プレイヤーの武器スロット情報を取得してログメッセージを出力
     348            0 :         query.Player(world, func(playerEntity ecs.Entity) {
     349            0 :                 weapons := query.GetWeapons(world, playerEntity)
     350            0 :                 weaponIndex := slotNumber - 1 // 1-based to 0-based
     351            0 :                 weapon := weapons[weaponIndex]
     352            0 : 
     353            0 :                 if weapon != nil {
     354              :                         // 武器が装備されている場合は武器名を表示
     355            0 :                         if world.Components.Name.Has(*weapon) {
     356            0 :                                 weaponName := query.GetEntityName(*weapon, world)
     357            0 :                                 gamelog.New(query.GetGameLog(world)).
     358            0 :                                         Markup(query.T(world, "Readied %s.", gamelog.Tag("item", weaponName))).
     359            0 :                                         Log()
     360            0 :                         }
     361              :                 }
     362              :         })
     363              : }
     364              : 
     365              : // handleSleep は睡眠の確認プロンプトを開く。入眠可否と各条件は activity.EvaluateSleepConditions が
     366              : // 1箇所で評価し、プロンプトはそれを見せて確認を取る。実際の入眠は選択肢の Sleep で行う。
     367              : func (st *DungeonState) handleSleep(world w.World) (es.Transition[w.World], error) {
     368            0 :         player, err := query.GetPlayerEntity(world)
     369            0 :         if err != nil {
     370            0 :                 return es.Transition[w.World]{Type: es.TransNone}, err
     371            0 :         }
     372              :         // 入眠できないときはプロンプトを開かず、妨げている最初の理由だけをログへ出す。
     373              :         // 可能なときだけ確認プロンプトを開く
     374            0 :         if reason, blocked := sleepBlockReason(activity.EvaluateSleepConditions(world, player)); blocked {
     375            0 :                 gamelog.New(query.GetGameLog(world)).Markup(query.T(world, reason)).Log()
     376            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     377            0 :         }
     378            0 :         return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
     379            0 :                 func() (es.State[w.World], error) { return NewChoiceMenu(st.sleepConfirmChoices), nil },
     380              :         }}, nil
     381              : }
     382              : 
     383              : // sleepBlockReason は入眠を妨げる最初の条件の理由 msgid を返す。妨げが無ければ blocked=false。
     384              : // 検査順は SleepBehavior.Validate と揃え、表示とゲートで同じ優先順位にする。
     385              : // 返す文字列は Validate が query.T へインラインで渡す msgid と同じもの
     386              : func sleepBlockReason(sc activity.SleepConditions) (msgid i18n.Msgid, blocked bool) {
     387            0 :         switch {
     388              :         case sc.TooTired():
     389            0 :                 return "Not tired enough to sleep.", true
     390              :         case !sc.TemperatureOK:
     391            0 :                 return "Too cold or hot to sleep.", true
     392              :         case !sc.AreaSafe:
     393            0 :                 return "Cannot sleep because enemies are nearby.", true
     394              :         }
     395            0 :         return "", false
     396              : }
     397              : 
     398              : // sleepConfirmChoices は入眠可能なときだけ開く yes/no プロンプトを組む。入眠可否は handleSleep が
     399              : // 済ませ、不可の理由はログへ出しているので、ここは Sleep と Cancel の確認だけに徹する。入眠は
     400              : // Validate 済みのチェックポイントなので、入った直後にオートセーブする。
     401              : func (st *DungeonState) sleepConfirmChoices(world w.World) (string, []Choice) {
     402            0 :         title := query.T(world, "Sleep here?")
     403            0 :         return title, []Choice{
     404            0 :                 {
     405            0 :                         Label: query.T(world, "Sleep"),
     406            0 :                         Run: func(world w.World) (es.Transition[w.World], error) {
     407            0 :                                 p, perr := query.GetPlayerEntity(world)
     408            0 :                                 if perr != nil {
     409            0 :                                         return es.Transition[w.World]{}, perr
     410            0 :                                 }
     411            0 :                                 res, eerr := activity.Execute(activity.NewSleepActivity(), p, world)
     412            0 :                                 if eerr != nil {
     413            0 :                                         return es.Transition[w.World]{}, eerr
     414            0 :                                 }
     415              :                                 // 入眠に成功したときだけ保存する。Validate 失敗は Execute が err=nil・Success=false で返すため
     416            0 :                                 if res != nil && res.Success {
     417            0 :                                         if serr := autoSave(world, nil); serr != nil {
     418            0 :                                                 return es.Transition[w.World]{}, serr
     419            0 :                                         }
     420              :                                 }
     421            0 :                                 return es.Transition[w.World]{Type: es.TransPop}, nil
     422              :                         },
     423              :                 },
     424              :                 {
     425              :                         Label: query.T(world, "Cancel"),
     426            0 :                         Run:   func(_ w.World) (es.Transition[w.World], error) { return es.Transition[w.World]{Type: es.TransPop}, nil },
     427              :                 },
     428              :         }
     429              : }
     430              : 
     431              : // hasStock は entity の member の行が店の在庫を持つかを返す
     432              : func hasStock(world w.World, entity ecs.Entity) bool {
     433            1 :         member, err := raw.FindMember(world.Resources.RawMaster, query.GetEntityID(entity, world))
     434            1 :         return err == nil && len(raw.PtrSlice(member.Stock)) > 0
     435            1 : }
        

Generated by: LCOV version 2.0-1