LCOV - code coverage report
Current view: top level - states - character_job.go Coverage Total Hit
Test: coverage.lcov Lines: 59.6 % 104 62
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              : 
       8              :         "github.com/hajimehoshi/ebiten/v2"
       9              :         text "github.com/hajimehoshi/ebiten/v2/text/v2"
      10              :         gc "github.com/kijimaD/ruins/internal/components"
      11              :         "github.com/kijimaD/ruins/internal/consts"
      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              :         "github.com/kijimaD/ruins/internal/menuloop"
      17              :         "github.com/kijimaD/ruins/internal/raw"
      18              :         "github.com/kijimaD/ruins/internal/rawschema"
      19              :         "github.com/kijimaD/ruins/internal/resources"
      20              :         "github.com/kijimaD/ruins/internal/widgets/menuframe"
      21              :         "github.com/kijimaD/ruins/internal/widgets/styled"
      22              :         "github.com/kijimaD/ruins/internal/widgets/theme"
      23              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      24              :         w "github.com/kijimaD/ruins/internal/world"
      25              : 
      26              :         "github.com/kijimaD/ruins/internal/world/gameaction"
      27              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      28              :         "github.com/kijimaD/ruins/internal/world/query"
      29              : )
      30              : 
      31              : // CharacterJobState はキャラクター職業選択画面のステート
      32              : type CharacterJobState struct {
      33              :         es.BaseState[w.World]
      34              :         screen     *menuloop.Screen[JobMenuProps]
      35              :         playerName string // TODO: どうにかする。キャラメイクは複数のstateで構成され、前の決定事項を保持する必要がある...
      36              : }
      37              : 
      38              : // NewCharacterJobState は職業選択ステートのファクトリを返す
      39              : func NewCharacterJobState(playerName string) es.StateFactory[w.World] {
      40            1 :         return func() (es.State[w.World], error) {
      41            1 :                 return &CharacterJobState{
      42            1 :                         playerName: playerName,
      43            1 :                 }, nil
      44            1 :         }
      45              : }
      46              : 
      47              : // State interface ================
      48              : 
      49              : var _ es.State[w.World] = &CharacterJobState{}
      50              : 
      51              : // OnStart はステート開始時の処理を行う
      52              : func (st *CharacterJobState) OnStart(_ w.World) error {
      53            1 :         st.screen = menuloop.NewScreen[JobMenuProps](st)
      54            1 :         return nil
      55            1 : }
      56              : 
      57              : // Update はゲームステートの更新処理を行う
      58              : func (st *CharacterJobState) Update(world w.World) (es.Transition[w.World], error) {
      59            1 :         return st.screen.Update(world)
      60            1 : }
      61              : 
      62              : // Draw はスクリーンに描画する
      63              : func (st *CharacterJobState) Draw(_ w.World, screen *ebiten.Image) error {
      64            1 :         screen.Fill(theme.ScreenBackground)
      65            1 :         st.screen.Draw(screen)
      66            1 :         return nil
      67            1 : }
      68              : 
      69              : // DoAction はActionを実行する
      70              : func (st *CharacterJobState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
      71            1 :         switch action {
      72              :         case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
      73            1 :                 return es.Transition[w.World]{Type: es.TransPop}, nil
      74              :         case inputmapper.ActionMenuSelect:
      75            0 :                 return st.handleSelection(world)
      76              :         default:
      77            0 :                 return es.Transition[w.World]{}, fmt.Errorf("characterJob: unsupported action: %s", action)
      78              :         }
      79              : }
      80              : 
      81              : // ================
      82              : // Props
      83              : // ================
      84              : 
      85              : // JobMenuProps は職業選択メニューのProps
      86              : type JobMenuProps struct {
      87              :         Items []jobMenuItem
      88              : }
      89              : 
      90              : // jobMenuItem は職業メニューの項目
      91              : type jobMenuItem struct {
      92              :         Profession rawschema.Profession
      93              : }
      94              : 
      95              : // Fetch は世界から表示 props を構築する。menuloop.Model の Model 部にあたる
      96              : func (st *CharacterJobState) Fetch(world w.World) (JobMenuProps, error) {
      97            1 :         professions := raw.PtrSlice(world.Resources.RawMaster.Professions)
      98            1 :         items := make([]jobMenuItem, len(professions))
      99            1 :         for i := range professions {
     100            1 :                 items[i] = jobMenuItem{Profession: professions[i]}
     101            1 :         }
     102            1 :         return JobMenuProps{Items: items}, nil
     103              : }
     104              : 
     105              : // Menu は一覧の構成を返す。menuloop.Model の Menu 部にあたる
     106              : func (st *CharacterJobState) Menu(props JobMenuProps) menuloop.MenuConfig {
     107            1 :         return menuloop.MenuConfig{Key: "job", TabCount: 1, ItemCounts: []int{len(props.Items)}}
     108            1 : }
     109              : 
     110              : func (st *CharacterJobState) handleSelection(world w.World) (es.Transition[w.World], error) {
     111            0 :         props := st.screen.Props()
     112            0 :         itemIndex := st.screen.Selection().ItemIndex
     113            0 :         if itemIndex >= len(props.Items) {
     114            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     115            0 :         }
     116              : 
     117            0 :         prof := props.Items[itemIndex].Profession
     118            0 : 
     119              :         // 既存プレイヤーがいれば削除する
     120            0 :         if existing, err := query.GetPlayerEntity(world); err == nil {
     121            0 :                 world.ECS.RemoveEntity(existing)
     122            0 :         }
     123              : 
     124            0 :         memberID, err := raw.PlayerMemberID(world.Resources.RawMaster)
     125            0 :         if err != nil {
     126            0 :                 return es.Transition[w.World]{}, err
     127            0 :         }
     128            0 :         player, err := lifecycle.SpawnPlayer(world, consts.Coord[consts.Tile]{X: 5, Y: 5}, memberID)
     129            0 :         if err != nil {
     130            0 :                 return es.Transition[w.World]{}, fmt.Errorf("failed to spawn player: %w", err)
     131            0 :         }
     132            0 :         if err := gameaction.ApplyProfession(world, player, prof); err != nil {
     133            0 :                 return es.Transition[w.World]{}, fmt.Errorf("failed to apply profession: %w", err)
     134            0 :         }
     135              : 
     136              :         // プレイヤー名を上書き
     137            0 :         name := world.Components.Name.Get(player)
     138            0 :         name.Custom = st.playerName
     139            0 : 
     140              :         // 操作ガイドを表示する
     141            0 :         gamelog.New(query.GetGameLog(world)).Markup(gamelog.Tag("system", query.T(world, "Arrows: Move."))).Log()
     142            0 :         gamelog.New(query.GetGameLog(world)).Markup(gamelog.Tag("system", query.T(world, "M key: Open base menu."))).Log()
     143            0 :         gamelog.New(query.GetGameLog(world)).Markup(gamelog.Tag("system", query.T(world, "Space key: Open action menu."))).Log()
     144            0 : 
     145            0 :         st.SetTransition(es.Transition[w.World]{
     146            0 :                 Type:          es.TransReplace,
     147            0 :                 NewStateFuncs: []es.StateFactory[w.World]{newGameOverworldState(world)},
     148            0 :         })
     149            0 : 
     150            0 :         return st.ConsumeTransition(), nil
     151              : }
     152              : 
     153              : // ================
     154              : // View
     155              : // ================
     156              : 
     157              : // ViewUI は見出し・左の職業一覧・右の詳細パネル・下の説明を左右2枠の画面へ載せる。
     158              : func (st *CharacterJobState) ViewUI(world w.World, props JobMenuProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
     159            1 :         itemIndex := cursor.ItemIndex
     160            1 : 
     161            1 :         rows := make([]menuframe.Row, len(props.Items))
     162            1 :         for i := range props.Items {
     163            1 :                 rows[i] = menuframe.Row{Cells: styled.TextCells(query.T(world, i18n.Msgid(props.Items[i].Profession.Name)))}
     164            1 :         }
     165            1 :         list := menuframe.SplitList(itemIndex, rows, res)
     166            1 :         detail := menuframe.PanelBox(res, buildJobDetailRowsUI(world, props, itemIndex, res.Text.BodyFace)...)
     167            1 : 
     168            1 :         description := ""
     169            1 :         if itemIndex < len(props.Items) {
     170            1 :                 description = query.T(world, i18n.Msgid(props.Items[itemIndex].Profession.Description))
     171            1 :         }
     172            1 :         return menuframe.SplitScreen(world, res, query.T(world, "Profession"), list, detail, description, keybind.HelpHint(world))
     173              : }
     174              : 
     175              : // buildJobDetailRowsUI は装備・所持品・スキルの見出しと行を返す。
     176              : func buildJobDetailRowsUI(world w.World, props JobMenuProps, itemIndex int, face text.Face) []uicore.Drawable {
     177            1 :         if itemIndex >= len(props.Items) {
     178            0 :                 return nil
     179            0 :         }
     180            1 :         prof := props.Items[itemIndex].Profession
     181            1 :         var items []uicore.Drawable
     182            1 :         section := func(title string) {
     183            1 :                 items = append(items, uicore.NewText(title, face, theme.TextSecondary))
     184            1 :         }
     185            1 :         line := func(s string) {
     186            1 :                 items = append(items, uicore.NewText(s, face, theme.TextPrimary))
     187            1 :         }
     188            1 :         if len(prof.Equips) > 0 {
     189            1 :                 section(query.T(world, "Equipment"))
     190            1 :                 for _, equip := range prof.Equips {
     191            1 :                         slotLabel := string(equip.Slot)
     192            1 :                         if slot, ok := gc.ParseEquipmentSlot(equip.Slot); ok {
     193            1 :                                 slotLabel = query.T(world, slot.Name())
     194            1 :                         }
     195            1 :                         line(fmt.Sprintf(" %s %s", slotLabel, query.T(world, raw.ItemName(world.Resources.RawMaster, equip.Name))))
     196              :                 }
     197              :         }
     198            1 :         if len(prof.Items) > 0 {
     199            1 :                 section(query.T(world, "Items"))
     200            1 :                 for _, item := range prof.Items {
     201            1 :                         line(fmt.Sprintf(" %s x%d", query.T(world, raw.ItemName(world.Resources.RawMaster, item.Name)), item.Count))
     202            1 :                 }
     203              :         }
     204            1 :         if profSkills := raw.PtrSlice(prof.Skills); len(profSkills) > 0 {
     205            0 :                 section(query.T(world, "Skills"))
     206            0 :                 for _, skill := range profSkills {
     207            0 :                         line(fmt.Sprintf(" %s Lv.%d", query.SkillName(world, skill.Id), skill.Value))
     208            0 :                 }
     209              :         }
     210            1 :         return items
     211              : }
        

Generated by: LCOV version 2.0-1