Line data Source code
1 : package states
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/hajimehoshi/ebiten/v2"
7 : es "github.com/kijimaD/ruins/internal/engine/states"
8 : "github.com/kijimaD/ruins/internal/gamelog"
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 : // FeedFuelMenuState は給油メニュー。持ち物の燃料をくべ先 FuelSink へくべる。
24 : // くべても閉じず、続けて足せる。
25 : type FeedFuelMenuState struct {
26 : es.BaseState[w.World]
27 : sink FuelSink
28 : detail overlay.Detail
29 : screen *menuloop.Screen[FeedFuelProps]
30 : }
31 :
32 : // FuelSink は給油メニューのくべ先。見出し、1つくべると増える量の表示、くべる処理を持つ。
33 : type FuelSink struct {
34 : title func(world w.World) string
35 : gain func(world w.World, fuel ecs.Entity) string
36 : feed func(world w.World, fuel ecs.Entity)
37 : }
38 :
39 : // FireFuelSink は火をくべ先にする。増える量は燃焼ターン数で表す。
40 : // メニュー表示中に火が燃え尽きても AddFuel が何もしないので、火の状態はここで確かめない
41 : func FireFuelSink(fire ecs.Entity) FuelSink {
42 1 : return FuelSink{
43 1 : title: func(world w.World) string {
44 1 : return query.T(world, "Burning, %s left", query.EstimateBurnTurns(world, fire).String())
45 1 : },
46 : gain: func(world w.World, fuel ecs.Entity) string {
47 1 : return query.FuelBurnTurns(world, fire, fuel).String()
48 1 : },
49 0 : feed: func(world w.World, fuel ecs.Entity) { lifecycle.AddFuel(world, fire, fuel) },
50 : }
51 : }
52 :
53 : // CubeFuelSink はキューブをくべ先にする。増える量は燃料ポイントで表す。
54 : func CubeFuelSink(cube ecs.Entity) FuelSink {
55 0 : return FuelSink{
56 0 : title: func(world w.World) string {
57 0 : return query.T(world, "Fuel %s", fuelTankText(*world.Components.FuelTank.Get(cube)))
58 0 : },
59 : gain: func(world w.World, fuel ecs.Entity) string {
60 0 : return query.HeatContent(world, fuel).String()
61 0 : },
62 : feed: func(world w.World, fuel ecs.Entity) {
63 0 : if !lifecycle.AddCubeFuel(world, cube, fuel) {
64 0 : gamelog.New(query.GetGameLog(world)).
65 0 : Markup(query.T(world, "The fuel tank is full.")).
66 0 : Log()
67 0 : }
68 : },
69 : }
70 : }
71 :
72 : var _ es.State[w.World] = &FeedFuelMenuState{}
73 : var _ menuloop.KeyBindings = &FeedFuelMenuState{}
74 :
75 : // FeedFuelProps は給油メニューの表示 props
76 : type FeedFuelProps struct {
77 : Title string
78 : Rows []feedFuelRow
79 : }
80 :
81 : // feedFuelRow は給油一覧の1行。燃料スタックの代表と個数、1つくべると増える量の表示を持つ
82 : type feedFuelRow struct {
83 : Entity ecs.Entity
84 : Count int
85 : Gain string
86 : }
87 :
88 : // OnStart はステートが開始される際に呼ばれる
89 : func (st *FeedFuelMenuState) OnStart(_ w.World) error {
90 1 : st.detail = overlay.NewEntityDetail(st.selectedEntity)
91 1 : st.screen = menuloop.NewScreen[FeedFuelProps](st, &st.detail)
92 1 : return nil
93 1 : }
94 :
95 : // Update はゲームステートの更新処理を行う
96 : func (st *FeedFuelMenuState) Update(world w.World) (es.Transition[w.World], error) {
97 1 : return st.screen.Update(world)
98 1 : }
99 :
100 : // Draw はゲームステートの描画処理を行う
101 : func (st *FeedFuelMenuState) Draw(_ w.World, screen *ebiten.Image) error {
102 1 : st.screen.Draw(screen)
103 1 : return nil
104 1 : }
105 :
106 : // KeyBindings は x の詳細表示を共通入力に足す
107 : func (st *FeedFuelMenuState) KeyBindings() []keybind.Binding {
108 1 : return detailOpenBindings
109 1 : }
110 :
111 : // DoAction はActionを実行する。選択で1つくべてメニューに留まり、続けて足せる
112 : func (st *FeedFuelMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
113 0 : switch action {
114 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
115 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
116 : case inputmapper.ActionOpenItemDetail:
117 0 : st.detail.Open(world)
118 : case inputmapper.ActionMenuSelect:
119 : // 走査時の代表は消費されるので、次フレームの Fetch が新しい代表で一覧を組み直す
120 0 : if entity, ok := st.selectedEntity(); ok && world.ECS.Alive(entity) && query.IsCombustible(world, entity) {
121 0 : st.sink.feed(world, entity)
122 0 : }
123 : default:
124 0 : return es.Transition[w.World]{}, fmt.Errorf("feedFuelMenu: unsupported action: %s", action)
125 : }
126 0 : return es.Transition[w.World]{Type: es.TransNone}, nil
127 : }
128 :
129 : // Fetch は世界から表示 props を構築する
130 : func (st *FeedFuelMenuState) Fetch(world w.World) (FeedFuelProps, error) {
131 1 : title := st.sink.title(world)
132 1 : player, err := query.GetPlayerEntity(world)
133 1 : if err != nil {
134 0 : return FeedFuelProps{Title: title}, err
135 0 : }
136 1 : var rows []feedFuelRow
137 1 : for _, stack := range query.BackpackStacks(world, player) {
138 1 : if !query.IsCombustible(world, stack.Rep) {
139 1 : continue
140 : }
141 1 : rows = append(rows, feedFuelRow{
142 1 : Entity: stack.Rep,
143 1 : Count: stack.Count,
144 1 : Gain: st.sink.gain(world, stack.Rep),
145 1 : })
146 : }
147 1 : return FeedFuelProps{Title: title, Rows: rows}, nil
148 : }
149 :
150 : // Menu は単一リストの構成を返す
151 : func (st *FeedFuelMenuState) Menu(props FeedFuelProps) menuloop.MenuConfig {
152 1 : return menuloop.MenuConfig{Key: "feedfuel", TabCount: 1, ItemCounts: []int{len(props.Rows)}, ItemsPerPage: menuloop.ItemsPerPageAuto}
153 1 : }
154 :
155 : // ViewUI は燃料一覧を収納やインベントリと同じ item-row 形式で組む。
156 : // アイコン・名前の共通先頭に、右寄せの増える量の列を足す
157 : func (st *FeedFuelMenuState) ViewUI(world w.World, props FeedFuelProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
158 1 : cols := itemMenuColumns(styled.Num())
159 1 : rows := make([]menuframe.Row, len(props.Rows))
160 1 : for i, r := range props.Rows {
161 1 : rows[i] = itemMenuRow(world, r.Entity, r.Count, r.Gain)
162 1 : }
163 1 : perPage := menuframe.ListCapacity(world, false, true)
164 1 : list, pager := menuframe.RenderList(cursor.ItemIndex, rows, cols, menuframe.ListOpts{EmptyText: query.T(world, "No fuel to add"), ItemsPerPage: perPage}, res)
165 1 : return menuframe.PanelScreen(world, res, props.Title, list, keybind.HelpHint(world), pager)
166 : }
167 :
168 : // selectedEntity は現在カーソルが当たっている燃料の代表エンティティを返す
169 : func (st *FeedFuelMenuState) selectedEntity() (ecs.Entity, bool) {
170 0 : props := st.screen.Props()
171 0 : cursor := st.screen.Selection()
172 0 : if cursor.ItemIndex < 0 || cursor.ItemIndex >= len(props.Rows) {
173 0 : return ecs.Entity{}, false
174 0 : }
175 0 : return props.Rows[cursor.ItemIndex].Entity, true
176 : }
|