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 : "github.com/kijimaD/ruins/internal/consts"
9 : es "github.com/kijimaD/ruins/internal/engine/states"
10 : "github.com/kijimaD/ruins/internal/inputmapper"
11 : "github.com/kijimaD/ruins/internal/keybind"
12 : "github.com/kijimaD/ruins/internal/menuloop"
13 : "github.com/kijimaD/ruins/internal/resources"
14 : gs "github.com/kijimaD/ruins/internal/systems"
15 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
16 : "github.com/kijimaD/ruins/internal/widgets/overlay"
17 : "github.com/kijimaD/ruins/internal/widgets/styled"
18 : "github.com/kijimaD/ruins/internal/widgets/uicore"
19 : w "github.com/kijimaD/ruins/internal/world"
20 : "github.com/kijimaD/ruins/internal/world/lifecycle"
21 : "github.com/kijimaD/ruins/internal/world/query"
22 : "github.com/mlange-42/ark/ecs"
23 : )
24 :
25 : // CubeModuleMenuState はキューブのモジュールスロット一覧。装備画面のスロット一覧と同型で、
26 : // 装着中のモジュールと空きスロットを consts.CubeModuleSlots 個並べる。スロットを選ぶと候補選択へ進む。
27 : type CubeModuleMenuState struct {
28 : es.BaseState[w.World]
29 : cube ecs.Entity
30 : screen *menuloop.Screen[CubeModuleMenuProps]
31 : }
32 :
33 : var _ es.State[w.World] = &CubeModuleMenuState{}
34 :
35 : // CubeModuleMenuProps はスロット一覧の表示 props
36 : type CubeModuleMenuProps struct {
37 : Slots []ecs.Entity // 長さ consts.CubeModuleSlots。各スロットの装着モジュール。空きは gc.InvalidEntity
38 : }
39 :
40 : // NewCubeModuleMenuState はキューブのモジュールスロット一覧を開くファクトリを返す
41 : func NewCubeModuleMenuState(cube ecs.Entity) (es.State[w.World], error) {
42 0 : return &CubeModuleMenuState{cube: cube}, nil
43 0 : }
44 :
45 : // OnStart はステートが開始される際に呼ばれる
46 : func (st *CubeModuleMenuState) OnStart(_ w.World) error {
47 0 : st.screen = menuloop.NewScreen[CubeModuleMenuProps](st)
48 0 : return nil
49 0 : }
50 :
51 : // Update はゲームステートの更新処理を行う
52 : func (st *CubeModuleMenuState) Update(world w.World) (es.Transition[w.World], error) {
53 : // モジュールの着脱で燃料タンクの上限と重量が変わるので再計算を回す
54 0 : if err := runUpdaters(world, &gs.StatsChangedSystem{}, &gs.WeightDirtySystem{}); err != nil {
55 0 : return es.Transition[w.World]{}, err
56 0 : }
57 0 : return st.screen.Update(world)
58 : }
59 :
60 : // Draw はゲームステートの描画処理を行う
61 : func (st *CubeModuleMenuState) Draw(_ w.World, screen *ebiten.Image) error {
62 0 : st.screen.Draw(screen)
63 0 : return nil
64 0 : }
65 :
66 : // Fetch は世界から表示 props を構築する。装着モジュールを保存スロット番号どおりに並べる
67 : func (st *CubeModuleMenuState) Fetch(world w.World) (CubeModuleMenuProps, error) {
68 1 : slots := make([]ecs.Entity, consts.CubeModuleSlots)
69 1 : for i := range slots {
70 1 : slots[i] = gc.InvalidEntity
71 1 : }
72 1 : for _, m := range query.GetCubeModules(world, st.cube) {
73 1 : s := world.Components.LocationInstalled.Get(m).Slot
74 1 : // 範囲外・スロット重複は握りつぶさず error で返して早期に検知する。片方を黙って捨てると不整合を隠す
75 1 : if s < 0 || s >= consts.CubeModuleSlots {
76 1 : return CubeModuleMenuProps{}, fmt.Errorf("cube module: slot %d out of range [0,%d)", s, consts.CubeModuleSlots)
77 1 : }
78 1 : if slots[s] != gc.InvalidEntity {
79 1 : return CubeModuleMenuProps{}, fmt.Errorf("cube module: duplicate slot %d", s)
80 1 : }
81 1 : slots[s] = m
82 : }
83 0 : return CubeModuleMenuProps{Slots: slots}, nil
84 : }
85 :
86 : // Menu はスロット数ぶんの単一リストを返す。装着中と空きを合わせて常に上限ぶん並べる
87 : func (st *CubeModuleMenuState) Menu(_ CubeModuleMenuProps) menuloop.MenuConfig {
88 0 : return menuloop.MenuConfig{Key: "cube_module", TabCount: 1, ItemCounts: []int{consts.CubeModuleSlots}, ItemsPerPage: menuloop.ItemsPerPageAuto}
89 0 : }
90 :
91 : // ViewUI はスロット一覧を中央パネルへ組む
92 : func (st *CubeModuleMenuState) ViewUI(world w.World, props CubeModuleMenuProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
93 : // スロット名を左、アイコンとアイテム名を右へ寄せる。間の伸縮スペーサで両者を離す
94 0 : cols := styled.Cols(styled.Fit(), styled.Name(), styled.Icon(), styled.Fit())
95 0 : rows := make([]menuframe.Row, consts.CubeModuleSlots)
96 0 : for i := range rows {
97 0 : label := query.T(world, "Slot %d", i+1)
98 0 : var icon *ebiten.Image
99 0 : name := query.T(world, "(empty)")
100 0 : if props.Slots[i] != gc.InvalidEntity {
101 0 : icon = menuIcon(world, props.Slots[i])
102 0 : name = query.GetEntityName(props.Slots[i], world)
103 0 : }
104 0 : rows[i] = menuframe.Row{Cells: []styled.Cell{styled.TextCell(label), styled.TextCell(""), styled.IconCell(icon), styled.TextCell(name)}}
105 : }
106 0 : list, pager := menuframe.RenderList(cursor.ItemIndex, rows, cols, menuframe.ListOpts{}, res)
107 0 : return menuframe.PanelScreen(world, res, query.T(world, "Module"), list, keybind.HelpHint(world), pager)
108 : }
109 :
110 : // DoAction はActionを実行する。スロットを選ぶと候補選択へ進む
111 : func (st *CubeModuleMenuState) DoAction(_ w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
112 0 : switch action {
113 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
114 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
115 : case inputmapper.ActionMenuSelect:
116 : // menuloop が ItemCounts=CubeModuleSlots でカーソルを範囲内に保つので slots[idx] は安全
117 0 : slots := st.screen.Props().Slots
118 0 : idx := st.screen.Selection().ItemIndex
119 0 : var current *ecs.Entity
120 0 : if slots[idx] != gc.InvalidEntity {
121 0 : m := slots[idx]
122 0 : current = &m
123 0 : }
124 0 : return es.Transition[w.World]{
125 0 : Type: es.TransPush,
126 0 : NewStateFuncs: []es.StateFactory[w.World]{newCubeModuleSelectState(st.cube, idx, current)},
127 0 : }, nil
128 : default:
129 0 : return es.Transition[w.World]{}, fmt.Errorf("cubeModuleMenu: unsupported action: %s", action)
130 : }
131 : }
132 :
133 : // CubeModuleSelectState はスロット1つのモジュールを選ぶメニュー。装備の EquipSelectState と同型で、
134 : // 装着済みなら先頭に「外す」が並ぶ。x で装着中と候補の詳細を見比べる。
135 : type CubeModuleSelectState struct {
136 : es.BaseState[w.World]
137 : cube ecs.Entity
138 : slot int // 編集対象のスロット番号
139 : installed *ecs.Entity // このスロットの装着中モジュール。空きなら nil
140 : detail overlay.Detail
141 : screen *menuloop.Screen[CubeModuleSelectProps]
142 : }
143 :
144 : var _ es.State[w.World] = &CubeModuleSelectState{}
145 : var _ menuloop.KeyBindings = &CubeModuleSelectState{}
146 :
147 : // CubeModuleSelectProps はモジュール選択の表示 props
148 : type CubeModuleSelectProps struct {
149 : Candidates []ecs.Entity // 装着できるモジュール。キューブ収納とバックパックから集める
150 : Installed *ecs.Entity
151 : }
152 :
153 : // newCubeModuleSelectState はスロットに対するモジュール選択を開くファクトリを返す
154 : func newCubeModuleSelectState(cube ecs.Entity, slot int, installed *ecs.Entity) es.StateFactory[w.World] {
155 0 : return func() (es.State[w.World], error) {
156 0 : return &CubeModuleSelectState{cube: cube, slot: slot, installed: installed}, nil
157 0 : }
158 : }
159 :
160 : // OnStart はステートが開始される際に呼ばれる
161 : func (st *CubeModuleSelectState) OnStart(_ w.World) error {
162 0 : st.detail = overlay.NewComparison(st.compareContents)
163 0 : st.screen = menuloop.NewScreen[CubeModuleSelectProps](st, &st.detail)
164 0 : return nil
165 0 : }
166 :
167 : // Update はゲームステートの更新処理を行う
168 : func (st *CubeModuleSelectState) Update(world w.World) (es.Transition[w.World], error) {
169 0 : return st.screen.Update(world)
170 0 : }
171 :
172 : // Draw はゲームステートの描画処理を行う
173 : func (st *CubeModuleSelectState) Draw(_ w.World, screen *ebiten.Image) error {
174 0 : st.screen.Draw(screen)
175 0 : return nil
176 0 : }
177 :
178 : // KeyBindings は x の詳細表示を共通入力に足す
179 : func (st *CubeModuleSelectState) KeyBindings() []keybind.Binding {
180 0 : return detailOpenBindings
181 0 : }
182 :
183 : // Fetch は世界から表示 props を構築する。候補はプレイヤーのバックパックから集める。
184 : // プレイヤー不在は握りつぶさず error で返して早期に検知する。
185 : func (st *CubeModuleSelectState) Fetch(world w.World) (CubeModuleSelectProps, error) {
186 0 : player, err := query.GetPlayerEntity(world)
187 0 : if err != nil {
188 0 : return CubeModuleSelectProps{}, fmt.Errorf("cube module select: %w", err)
189 0 : }
190 0 : return CubeModuleSelectProps{Candidates: query.BackpackCubeModules(world, player), Installed: st.installed}, nil
191 : }
192 :
193 : // Menu は単一リストの構成を返す。装着済みなら先頭の「外す」ぶんを1つ足す
194 : func (st *CubeModuleSelectState) Menu(props CubeModuleSelectProps) menuloop.MenuConfig {
195 0 : return menuloop.MenuConfig{Key: "cube_module_select", TabCount: 1, ItemCounts: []int{cubeModuleChoiceCount(props)}, ItemsPerPage: menuloop.ItemsPerPageAuto}
196 0 : }
197 :
198 : // ViewUI は候補一覧を中央パネルへ組む。装着済みなら先頭に「外す」が並ぶ
199 : func (st *CubeModuleSelectState) ViewUI(world w.World, props CubeModuleSelectProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
200 0 : var rows []menuframe.Row
201 0 : if props.Installed != nil {
202 0 : rows = append(rows, menuframe.Row{Cells: []styled.Cell{styled.IconCell(nil), styled.TextCell(query.T(world, "Remove"))}})
203 0 : }
204 0 : for _, entity := range props.Candidates {
205 0 : rows = append(rows, menuframe.Row{Cells: []styled.Cell{styled.IconCell(menuIcon(world, entity)), styled.TextCell(query.GetEntityName(entity, world))}})
206 0 : }
207 0 : list, pager := menuframe.RenderList(cursor.ItemIndex, rows, styled.Cols(styled.Icon(), styled.Name()),
208 0 : menuframe.ListOpts{EmptyText: query.T(world, "No modules to install")}, res)
209 0 : return menuframe.PanelScreen(world, res, query.T(world, "Choose module"), list, keybind.HelpHint(world), pager)
210 : }
211 :
212 : // DoAction はActionを実行する。選択で装着または外して閉じる
213 : func (st *CubeModuleSelectState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
214 0 : switch action {
215 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
216 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
217 : case inputmapper.ActionOpenItemDetail:
218 0 : st.detail.Open(world)
219 0 : return es.Transition[w.World]{Type: es.TransNone}, nil
220 : case inputmapper.ActionMenuSelect:
221 0 : if choice, ok := st.selection(); ok {
222 0 : if err := applyCubeModuleChoice(world, st.cube, st.slot, choice, st.installed); err != nil {
223 0 : return es.Transition[w.World]{}, err
224 0 : }
225 : }
226 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
227 : default:
228 0 : return es.Transition[w.World]{}, fmt.Errorf("cubeModuleSelect: unsupported action: %s", action)
229 : }
230 : }
231 :
232 : // selection は現在カーソルが指す選択を返す
233 : func (st *CubeModuleSelectState) selection() (cubeModuleChoice, bool) {
234 0 : return cubeModuleChoiceAt(st.screen.Props(), st.screen.Selection().ItemIndex)
235 0 : }
236 :
237 : // compareContents は詳細に出す内容を返す。装着中と候補を2枚、空きスロットの候補なら候補1枚、
238 : // 「外す」なら外す対象1枚を返す。並びは左が装着中、右が候補
239 : func (st *CubeModuleSelectState) compareContents(world w.World) ([]overlay.DetailContent, bool) {
240 0 : choice, ok := st.selection()
241 0 : if !ok {
242 0 : return nil, false
243 0 : }
244 0 : if choice.remove {
245 0 : return []overlay.DetailContent{overlay.EntityDetailContent(world, *st.installed)}, true
246 0 : }
247 0 : candidate := overlay.EntityDetailContent(world, choice.entity)
248 0 : if st.installed == nil {
249 0 : return []overlay.DetailContent{candidate}, true
250 0 : }
251 0 : return []overlay.DetailContent{overlay.EntityDetailContent(world, *st.installed), candidate}, true
252 : }
253 :
254 : // cubeModuleChoice はカーソルが指すモジュール選択。remove なら「外す」、そうでなければ entity が候補
255 : type cubeModuleChoice struct {
256 : remove bool
257 : entity ecs.Entity
258 : }
259 :
260 : // cubeModuleChoiceAt は候補一覧の index を選択へ写す。装着済みなら先頭が「外す」で、候補は1つ後ろへ詰まる
261 : func cubeModuleChoiceAt(props CubeModuleSelectProps, index int) (cubeModuleChoice, bool) {
262 1 : if props.Installed != nil {
263 1 : if index == 0 {
264 1 : return cubeModuleChoice{remove: true}, true
265 1 : }
266 1 : index--
267 : }
268 1 : if index < 0 || index >= len(props.Candidates) {
269 1 : return cubeModuleChoice{}, false
270 1 : }
271 1 : return cubeModuleChoice{entity: props.Candidates[index]}, true
272 : }
273 :
274 : // cubeModuleChoiceCount は選択できる項目数を返す。装着済みなら先頭の「外す」を1つ足す
275 : func cubeModuleChoiceCount(props CubeModuleSelectProps) int {
276 0 : if props.Installed != nil {
277 0 : return len(props.Candidates) + 1
278 0 : }
279 0 : return len(props.Candidates)
280 : }
281 :
282 : // applyCubeModuleChoice は選択を指定スロットへ実行する。「外す」なら装着中をプレイヤーのバックパックへ戻し、
283 : // 候補なら装着する。スロットに装着中があれば先にバックパックへ戻してから付け替える。キューブは browse できる
284 : // 収納を持たないので、外したモジュールは取り出せるバックパックへ返す。
285 : func applyCubeModuleChoice(world w.World, cube ecs.Entity, slot int, choice cubeModuleChoice, installed *ecs.Entity) error {
286 1 : player, err := query.GetPlayerEntity(world)
287 1 : if err != nil {
288 1 : return fmt.Errorf("cube module: %w", err)
289 1 : }
290 1 : if choice.remove {
291 1 : if installed == nil {
292 0 : return nil
293 0 : }
294 1 : return lifecycle.MoveToBackpack(world, *installed, player)
295 : }
296 1 : if installed != nil {
297 0 : if err := lifecycle.MoveToBackpack(world, *installed, player); err != nil {
298 0 : return err
299 0 : }
300 : }
301 1 : lifecycle.MoveToInstalled(world, choice.entity, cube, slot)
302 1 : return nil
303 : }
|