Line data Source code
1 : package states
2 :
3 : import (
4 : "fmt"
5 : "slices"
6 :
7 : "github.com/hajimehoshi/ebiten/v2"
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/raw"
14 : "github.com/kijimaD/ruins/internal/resources"
15 : "github.com/kijimaD/ruins/internal/widgets/entityspec"
16 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
17 : "github.com/kijimaD/ruins/internal/widgets/overlay"
18 : "github.com/kijimaD/ruins/internal/widgets/styled"
19 : "github.com/kijimaD/ruins/internal/widgets/theme"
20 : "github.com/kijimaD/ruins/internal/widgets/uicore"
21 : w "github.com/kijimaD/ruins/internal/world"
22 :
23 : "github.com/kijimaD/ruins/internal/world/gameaction"
24 : "github.com/kijimaD/ruins/internal/world/query"
25 : "github.com/mlange-42/ark/ecs"
26 : )
27 :
28 : // CraftMenuState はクラフトメニューのゲームステート
29 : type CraftMenuState struct {
30 : es.BaseState[w.World]
31 : detail overlay.Detail // レシピの性能・材料・説明を出す詳細モーダル。overlay として Screen に登録する
32 : result overlay.Detail // クラフト結果の詳細モーダル。overlay として Screen に登録する
33 : resultEntity ecs.Entity // 直近でクラフトしたアイテム
34 : screen *menuloop.Screen[CraftProps]
35 : }
36 :
37 : // State interface ================
38 :
39 : var _ es.State[w.World] = &CraftMenuState{}
40 : var _ menuloop.KeyBindings = &CraftMenuState{}
41 :
42 : // OnStart はステートが開始される際に呼ばれる
43 : func (st *CraftMenuState) OnStart(_ w.World) error {
44 1 : st.detail = overlay.NewDetail(st.detailContent)
45 2 : st.result = overlay.NewEntityDetail(func() (ecs.Entity, bool) { return st.resultEntity, true })
46 : // result を先に登録する。クラフト結果が開いている間はそちらが入力を専有する
47 1 : st.screen = menuloop.NewScreen[CraftProps](st, &st.result, &st.detail)
48 1 : return nil
49 : }
50 :
51 : // Update はゲームステートの更新処理を行う
52 : func (st *CraftMenuState) Update(world w.World) (es.Transition[w.World], error) {
53 1 : return st.screen.Update(world)
54 1 : }
55 :
56 : // Draw はゲームステートの描画処理を行う
57 : func (st *CraftMenuState) Draw(_ w.World, screen *ebiten.Image) error {
58 1 : st.screen.Draw(screen)
59 1 : return nil
60 1 : }
61 :
62 : // KeyBindings は x の詳細表示を共通入力に足す
63 : func (st *CraftMenuState) KeyBindings() []keybind.Binding {
64 1 : return detailOpenBindings
65 1 : }
66 :
67 : // DoAction はActionを実行する
68 : func (st *CraftMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
69 1 : switch action {
70 : case inputmapper.ActionOpenDebugMenu:
71 0 : return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewDebugMenuState}}, nil
72 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
73 1 : return es.Transition[w.World]{Type: es.TransPop}, nil
74 : case inputmapper.ActionOpenItemDetail:
75 0 : st.detail.Open(world)
76 : case inputmapper.ActionMenuSelect:
77 1 : if err := st.craftSelected(world); err != nil {
78 0 : return es.Transition[w.World]{}, err
79 0 : }
80 : default:
81 0 : return es.Transition[w.World]{}, fmt.Errorf("craftMenu: unsupported action: %s", action)
82 : }
83 1 : return es.Transition[w.World]{Type: es.TransNone}, nil
84 : }
85 :
86 : // ================
87 : // Props
88 : // ================
89 :
90 : // CraftProps は画面の表示 props。menuloop.Screen の型引数として渡す
91 : type CraftProps struct {
92 : Tabs []craftTabData
93 : }
94 :
95 : type craftTabData struct {
96 : ID string
97 : Label string
98 : Items []craftItemData
99 : }
100 :
101 : type craftItemData struct {
102 : RecipeID string // クラフトの同定キー。NewRecipeSpec/CanCraft/Craft はこれで引く
103 : RecipeName string // 表示名
104 : CanCraft bool
105 : }
106 :
107 : // Fetch は世界から表示 props を構築する。menuloop.Model の Model 部にあたる
108 : func (st *CraftMenuState) Fetch(world w.World) (CraftProps, error) {
109 1 : return CraftProps{
110 1 : Tabs: []craftTabData{
111 1 : {ID: "consumables", Label: query.T(world, "Consumables"), Items: st.createMenuItems(world, st.queryMenuConsumable(world))},
112 1 : {ID: "weapons", Label: query.T(world, "Weapons"), Items: st.createMenuItems(world, st.queryMenuWeapon(world))},
113 1 : {ID: "wearables", Label: query.T(world, "Armor"), Items: st.createMenuItems(world, st.queryMenuWearable(world))},
114 1 : },
115 1 : }, nil
116 1 : }
117 :
118 : // Menu は一覧の構成を返す。menuloop.Model の Menu 部にあたる
119 : func (st *CraftMenuState) Menu(props CraftProps) menuloop.MenuConfig {
120 1 : itemCounts := make([]int, len(props.Tabs))
121 1 : for i, tab := range props.Tabs {
122 1 : itemCounts[i] = len(tab.Items)
123 1 : }
124 1 : return menuloop.MenuConfig{Key: "craft", TabCount: len(props.Tabs), ItemCounts: itemCounts, ItemsPerPage: menuloop.ItemsPerPageAuto}
125 : }
126 :
127 : func (st *CraftMenuState) createMenuItems(world w.World, recipeIDs []string) []craftItemData {
128 1 : items := make([]craftItemData, len(recipeIDs))
129 1 :
130 1 : for i, recipeID := range recipeIDs {
131 1 : canCraft, _ := gameaction.CanCraft(world, recipeID)
132 1 : // レシピ id は生成アイテム id と一致する。表示名はアイテムの英語名を翻訳して出し、材料表示と経路を揃える
133 1 : items[i] = craftItemData{
134 1 : RecipeID: recipeID,
135 1 : RecipeName: query.T(world, raw.ItemName(world.Resources.RawMaster, recipeID)),
136 1 : CanCraft: canCraft,
137 1 : }
138 : }
139 :
140 1 : return items
141 : }
142 :
143 : func (st *CraftMenuState) queryMenuConsumable(world w.World) []string {
144 1 : var items []string
145 1 :
146 1 : for _, recipe := range raw.PtrSlice(world.Resources.RawMaster.Recipes) {
147 1 : spec, err := raw.NewRecipeSpec(world.Resources.RawMaster, recipe.Id)
148 1 : if err != nil {
149 0 : continue
150 : }
151 1 : if spec.Consumable != nil {
152 1 : items = append(items, recipe.Id)
153 1 : }
154 : }
155 :
156 1 : slices.Sort(items)
157 1 : return items
158 : }
159 :
160 : func (st *CraftMenuState) queryMenuWeapon(world w.World) []string {
161 1 : var items []string
162 1 :
163 1 : for _, recipe := range raw.PtrSlice(world.Resources.RawMaster.Recipes) {
164 1 : spec, err := raw.NewRecipeSpec(world.Resources.RawMaster, recipe.Id)
165 1 : if err != nil {
166 0 : continue
167 : }
168 : // TODO: カテゴリ定義で判定したい
169 1 : if spec.Melee != nil || spec.Fire != nil {
170 1 : items = append(items, recipe.Id)
171 1 : }
172 : }
173 :
174 1 : slices.Sort(items)
175 1 : return items
176 : }
177 :
178 : func (st *CraftMenuState) queryMenuWearable(world w.World) []string {
179 1 : var items []string
180 1 :
181 1 : for _, recipe := range raw.PtrSlice(world.Resources.RawMaster.Recipes) {
182 1 : spec, err := raw.NewRecipeSpec(world.Resources.RawMaster, recipe.Id)
183 1 : if err != nil {
184 0 : continue
185 : }
186 1 : if spec.Wearable != nil {
187 1 : items = append(items, recipe.Id)
188 1 : }
189 : }
190 :
191 1 : slices.Sort(items)
192 1 : return items
193 : }
194 :
195 : // ================
196 : // クラフト
197 : // ================
198 :
199 : // craftSelected は現在カーソルが当たっているレシピをクラフトし、結果モーダルを開く。
200 : // クラフト不可のレシピは何もしない。決定で即実行し、途中のアクション選択は挟まない
201 : func (st *CraftMenuState) craftSelected(world w.World) error {
202 1 : item, ok := st.selectedRecipe()
203 1 : if !ok || !item.CanCraft {
204 1 : return nil
205 1 : }
206 0 : resultEntity, err := gameaction.Craft(world, item.RecipeID)
207 0 : if err != nil {
208 0 : return fmt.Errorf("failed to craft: %w", err)
209 0 : }
210 0 : st.resultEntity = resultEntity
211 0 : st.result.Open(world)
212 0 : return nil
213 : }
214 :
215 : // selectedRecipe は現在カーソルが当たっているレシピを返す
216 : func (st *CraftMenuState) selectedRecipe() (craftItemData, bool) {
217 1 : props := st.screen.Props()
218 1 : cursor := st.screen.Selection()
219 1 : if cursor.TabIndex >= len(props.Tabs) {
220 1 : return craftItemData{}, false
221 1 : }
222 0 : items := props.Tabs[cursor.TabIndex].Items
223 0 : if cursor.ItemIndex >= len(items) {
224 0 : return craftItemData{}, false
225 0 : }
226 0 : return items[cursor.ItemIndex], true
227 : }
228 :
229 : // ================
230 : // View
231 : // ================
232 :
233 : // ViewUI はカテゴリタブとクラフト可否印つきレシピ一覧を組む。
234 : func (st *CraftMenuState) ViewUI(world w.World, props CraftProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
235 1 : labels := make([]string, len(props.Tabs))
236 1 : for i, tab := range props.Tabs {
237 1 : labels[i] = tab.Label
238 1 : }
239 1 : content, pager := st.buildItemListUI(world, props.Tabs, cursor.TabIndex, cursor.ItemIndex, cursor.PageSize, res)
240 1 : return menuframe.TabScreen(world, res, "", labels, cursor.TabIndex, content, keybind.HelpHint(world), pager)
241 : }
242 :
243 : // buildItemListUI は行列とフッタ右端のページ表示を返す。
244 : func (st *CraftMenuState) buildItemListUI(world w.World, tabs []craftTabData, tabIndex, itemIndex, perPage int, res resources.UIResources) ([]uicore.Drawable, string) {
245 1 : if tabIndex >= len(tabs) {
246 0 : return nil, ""
247 0 : }
248 1 : currentTab := tabs[tabIndex]
249 1 : cols := styled.Cols(styled.Fit(), styled.Name())
250 1 : rows := make([]menuframe.Row, len(currentTab.Items))
251 1 : for i, it := range currentTab.Items {
252 1 : mark := consts.IconClose
253 1 : if it.CanCraft {
254 1 : mark = consts.IconCheck
255 1 : }
256 1 : rows[i] = menuframe.Row{Cells: styled.TextCells(mark, it.RecipeName)}
257 : }
258 1 : return menuframe.RenderList(itemIndex, rows, cols, menuframe.ListOpts{EmptyText: query.T(world, "No recipes"), ItemsPerPage: perPage}, res)
259 : }
260 :
261 : // detailContent は現在カーソルが当たっているレシピの性能・材料・説明を返す。詳細モーダルの唯一の定義点
262 : func (st *CraftMenuState) detailContent(world w.World) (overlay.DetailContent, bool) {
263 1 : item, ok := st.selectedRecipe()
264 1 : if !ok || item.RecipeID == "" {
265 1 : return overlay.DetailContent{}, false
266 1 : }
267 0 : spec, err := raw.NewRecipeSpec(world.Resources.RawMaster, item.RecipeID)
268 0 : if err != nil {
269 0 : return overlay.DetailContent{}, false
270 0 : }
271 :
272 : // 必要材料を先頭に置き、所持数が足りていれば成功色、足りなければ警告色で示す。
273 : // その後ろに生成物の性能行を続ける
274 0 : var rows []entityspec.SpecRow
275 0 : if spec.Recipe != nil {
276 0 : rows = append(rows, entityspec.SpecRow{Label: query.T(world, "Materials"), Header: true})
277 0 : for _, in := range spec.Recipe.Inputs {
278 0 : owned := 0
279 0 : if entity, found := query.FindStackInInventory(world, in.ID); found {
280 0 : owned = query.GetEntityCount(world, entity)
281 0 : }
282 0 : rowColor := theme.StatusDanger
283 0 : if owned >= in.Amount {
284 0 : rowColor = theme.StatusSuccess
285 0 : }
286 0 : label := query.T(world, raw.ItemName(world.Resources.RawMaster, in.ID))
287 0 : rows = append(rows, entityspec.SpecRow{Label: label, Value: fmt.Sprintf("%d / %d", in.Amount, owned), Color: &rowColor})
288 : }
289 : }
290 0 : rows = append(rows, entityspec.SpecRowsFromSpec(world, spec)...)
291 0 :
292 0 : desc := ""
293 0 : if spec.Description != nil {
294 0 : desc = query.T(world, spec.Description.Description)
295 0 : }
296 0 : return overlay.DetailContent{Name: item.RecipeName, Desc: desc, Rows: rows}, true
297 : }
|