Line data Source code
1 : package states
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/hajimehoshi/ebiten/v2"
7 : gc "github.com/kijimaD/ruins/internal/components"
8 : es "github.com/kijimaD/ruins/internal/engine/states"
9 : "github.com/kijimaD/ruins/internal/inputmapper"
10 : "github.com/kijimaD/ruins/internal/keybind"
11 : "github.com/kijimaD/ruins/internal/menuloop"
12 : "github.com/kijimaD/ruins/internal/resources"
13 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
14 : "github.com/kijimaD/ruins/internal/widgets/overlay"
15 : "github.com/kijimaD/ruins/internal/widgets/styled"
16 : "github.com/kijimaD/ruins/internal/widgets/uicore"
17 : w "github.com/kijimaD/ruins/internal/world"
18 : "github.com/kijimaD/ruins/internal/world/lifecycle"
19 : "github.com/kijimaD/ruins/internal/world/query"
20 : "github.com/mlange-42/ark/ecs"
21 : )
22 :
23 : // EquipSelectState はスロット1つの装備を選ぶメニュー。装備タブでスロットを選ぶと push される。
24 : // 装備済みなら一覧の先頭に「外す」が並ぶ。x の詳細は現装備と候補を2枚並べて比較する。
25 : type EquipSelectState struct {
26 : es.BaseState[w.World]
27 : slotNumber gc.EquipmentSlotNumber
28 : targetCharacter ecs.Entity
29 : previousEquipment *ecs.Entity // 装備中エンティティ。空きなら nil
30 : detail overlay.Detail
31 : screen *menuloop.Screen[EquipSelectProps]
32 : }
33 :
34 : var _ es.State[w.World] = &EquipSelectState{}
35 : var _ menuloop.KeyBindings = &EquipSelectState{}
36 :
37 : // EquipSelectProps は装備選択の表示 props
38 : type EquipSelectProps struct {
39 : Items []ecs.Entity
40 : SlotNumber gc.EquipmentSlotNumber
41 : PreviousEquipment *ecs.Entity
42 : TargetCharacter ecs.Entity
43 : }
44 :
45 : // newEquipSelectState はスロットに対する装備選択を開くファクトリを返す
46 : func newEquipSelectState(slot equipItemData) es.StateFactory[w.World] {
47 1 : return func() (es.State[w.World], error) {
48 1 : return &EquipSelectState{
49 1 : slotNumber: slot.SlotNumber,
50 1 : targetCharacter: slot.Character,
51 1 : previousEquipment: slot.Entity,
52 1 : }, nil
53 1 : }
54 : }
55 :
56 : // OnStart はステートが開始される際に呼ばれる
57 : func (st *EquipSelectState) OnStart(_ w.World) error {
58 1 : st.detail = overlay.NewComparison(st.compareContents)
59 1 : st.screen = menuloop.NewScreen[EquipSelectProps](st, &st.detail)
60 1 : return nil
61 1 : }
62 :
63 : // Update はゲームステートの更新処理を行う
64 : func (st *EquipSelectState) Update(world w.World) (es.Transition[w.World], error) {
65 1 : return st.screen.Update(world)
66 1 : }
67 :
68 : // Draw はゲームステートの描画処理を行う
69 : func (st *EquipSelectState) Draw(_ w.World, screen *ebiten.Image) error {
70 1 : st.screen.Draw(screen)
71 1 : return nil
72 1 : }
73 :
74 : // KeyBindings は x の詳細表示を共通入力に足す
75 : func (st *EquipSelectState) KeyBindings() []keybind.Binding {
76 1 : return detailOpenBindings
77 1 : }
78 :
79 : // DoAction はActionを実行する。選択で装着または外して閉じる
80 : func (st *EquipSelectState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
81 1 : switch action {
82 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
83 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
84 : case inputmapper.ActionOpenItemDetail:
85 1 : st.detail.Open(world)
86 1 : return es.Transition[w.World]{Type: es.TransNone}, nil
87 : case inputmapper.ActionMenuSelect:
88 0 : if choice, ok := st.selection(); ok {
89 0 : if err := applyEquipChoice(world, choice, st.slotNumber, st.targetCharacter, st.previousEquipment); err != nil {
90 0 : return es.Transition[w.World]{}, err
91 0 : }
92 : }
93 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
94 : default:
95 0 : return es.Transition[w.World]{}, fmt.Errorf("equipSelect: unsupported action: %s", action)
96 : }
97 : }
98 :
99 : // Fetch は世界から表示 props を構築する
100 : func (st *EquipSelectState) Fetch(world w.World) (EquipSelectProps, error) {
101 1 : return EquipSelectProps{
102 1 : Items: equipableForSlot(world, st.slotNumber),
103 1 : SlotNumber: st.slotNumber,
104 1 : PreviousEquipment: st.previousEquipment,
105 1 : TargetCharacter: st.targetCharacter,
106 1 : }, nil
107 1 : }
108 :
109 : // Menu は単一リストの構成を返す。装備済みなら先頭の「外す」ぶんを1つ足す
110 : func (st *EquipSelectState) Menu(props EquipSelectProps) menuloop.MenuConfig {
111 1 : return menuloop.MenuConfig{Key: "equip_select", TabCount: 1, ItemCounts: []int{equipChoiceCount(props)}, ItemsPerPage: menuloop.ItemsPerPageAuto}
112 1 : }
113 :
114 : // ViewUI は候補一覧を中央パネルへ組む
115 : func (st *EquipSelectState) ViewUI(world w.World, props EquipSelectProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
116 1 : return buildEquipSelectUI(world, props, cursor.ItemIndex, res)
117 1 : }
118 :
119 : // selection は現在カーソルが指す選択を返す
120 : func (st *EquipSelectState) selection() (equipChoice, bool) {
121 1 : return equipChoiceAt(st.screen.Props(), st.screen.Selection().ItemIndex)
122 1 : }
123 :
124 : // compareContents は詳細に出す内容を返す。候補にカーソルがあり装備済みなら現装備と候補の
125 : // 2枚、空スロットの候補なら候補1枚、「外す」なら外す対象1枚を返す。並びは左が現装備、右が候補
126 : func (st *EquipSelectState) compareContents(world w.World) ([]overlay.DetailContent, bool) {
127 1 : choice, ok := st.selection()
128 1 : if !ok {
129 0 : return nil, false
130 0 : }
131 : // 「外す」は装備済みのときだけ選べるので previousEquipment は非nil
132 1 : if choice.unequip {
133 0 : return []overlay.DetailContent{overlay.EntityDetailContent(world, *st.previousEquipment)}, true
134 0 : }
135 1 : candidate := overlay.EntityDetailContent(world, choice.entity)
136 1 : if st.previousEquipment == nil {
137 0 : return []overlay.DetailContent{candidate}, true
138 0 : }
139 1 : return []overlay.DetailContent{overlay.EntityDetailContent(world, *st.previousEquipment), candidate}, true
140 : }
141 :
142 : // equipChoice はカーソルが指す装備選択。unequip なら「外す」、そうでなければ entity が候補
143 : type equipChoice struct {
144 : unequip bool
145 : entity ecs.Entity
146 : }
147 :
148 : // equipChoiceAt は候補一覧の index を選択へ写す。装備済みなら先頭が「外す」で、候補は1つ後ろへ詰まる
149 : func equipChoiceAt(props EquipSelectProps, index int) (equipChoice, bool) {
150 1 : if props.PreviousEquipment != nil {
151 1 : if index == 0 {
152 1 : return equipChoice{unequip: true}, true
153 1 : }
154 1 : index--
155 : }
156 1 : if index < 0 || index >= len(props.Items) {
157 1 : return equipChoice{}, false
158 1 : }
159 1 : return equipChoice{entity: props.Items[index]}, true
160 : }
161 :
162 : // equipChoiceCount は選択できる項目数を返す。装備済みなら先頭の「外す」を1つ足す
163 : func equipChoiceCount(props EquipSelectProps) int {
164 1 : if props.PreviousEquipment != nil {
165 1 : return len(props.Items) + 1
166 1 : }
167 1 : return len(props.Items)
168 : }
169 :
170 : // applyEquipChoice は選択を実行する。「外す」なら現装備を外し、候補なら装着する。
171 : // 既存の装備があれば持ち物へ戻す
172 : func applyEquipChoice(world w.World, choice equipChoice, slotNumber gc.EquipmentSlotNumber, target ecs.Entity, previous *ecs.Entity) error {
173 1 : if choice.unequip {
174 1 : if previous == nil {
175 1 : return nil
176 1 : }
177 1 : itemName := query.GetEntityName(*previous, world)
178 1 : if err := lifecycle.MoveToBackpack(world, *previous, target); err != nil {
179 0 : return err
180 0 : }
181 1 : logEquipChange(world, target, itemName, query.T(world, "%s unequipped %s."))
182 1 : return nil
183 : }
184 1 : itemName := query.GetEntityName(choice.entity, world)
185 1 : if previous != nil {
186 1 : if err := lifecycle.MoveToBackpack(world, *previous, target); err != nil {
187 0 : return err
188 0 : }
189 : }
190 1 : lifecycle.MoveToEquip(world, choice.entity, target, slotNumber)
191 1 : logEquipChange(world, target, itemName, query.T(world, "%s equipped %s."))
192 1 : return nil
193 : }
194 :
195 : // buildEquipSelectUI はアイコン付きの候補一覧を中央パネルへ組む。装備済みなら先頭に「外す」が並ぶ
196 : func buildEquipSelectUI(world w.World, props EquipSelectProps, selectedIndex int, res resources.UIResources) uicore.Drawable {
197 1 : var rows []menuframe.Row
198 1 : if props.PreviousEquipment != nil {
199 1 : rows = append(rows, menuframe.Row{Cells: []styled.Cell{styled.IconCell(nil), styled.TextCell(query.T(world, "Unequip"))}})
200 1 : }
201 1 : for _, entity := range props.Items {
202 1 : rows = append(rows, menuframe.Row{Cells: []styled.Cell{styled.IconCell(menuIcon(world, entity)), styled.TextCell(query.GetEntityName(entity, world))}})
203 1 : }
204 1 : list, pager := menuframe.RenderList(selectedIndex, rows, styled.Cols(styled.Icon(), styled.Name()),
205 1 : menuframe.ListOpts{EmptyText: query.T(world, "Nothing to equip")}, res)
206 1 : return menuframe.PanelScreen(world, res, query.T(world, "Choose equipment"), list, keybind.HelpHint(world), pager)
207 : }
|