Line data Source code
1 : package states
2 :
3 : import (
4 : "fmt"
5 : "time"
6 :
7 : "github.com/kijimaD/ruins/internal/i18n"
8 :
9 : "github.com/hajimehoshi/ebiten/v2"
10 : "github.com/kijimaD/ruins/internal/consts"
11 : es "github.com/kijimaD/ruins/internal/engine/states"
12 : "github.com/kijimaD/ruins/internal/inputmapper"
13 : "github.com/kijimaD/ruins/internal/keybind"
14 : "github.com/kijimaD/ruins/internal/menuloop"
15 : "github.com/kijimaD/ruins/internal/resources"
16 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
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/query"
21 : )
22 :
23 : // run 統計の閲覧画面。統計を Label と Value の2列テーブルで並べる読み取り専用画面で、
24 : // 道中の統計閲覧と、死とクリアの結果画面を共用する。値の組み立ては runStatsItems に1本化する。
25 :
26 : const runStatsMenuKey = "run_stats"
27 :
28 : // RunStatsState は run 統計をテーブルで見る読み取り専用画面
29 : type RunStatsState struct {
30 : es.BaseState[w.World]
31 : headerMsgid i18n.Msgid // 見出し
32 : // result が真なら run の結果画面。統計だけをタブなしで出し、任意キーでメインメニューへ戻す。
33 : // 結果画面を開いてもプレイヤーは残りプレイ時間の時計が進むので、開いた時点の playTime を出し続ける。
34 : // 偽なら道中の統計画面で、統計と環境のタブを持ち、閉じると元の画面へ戻る
35 : result bool
36 : playTime time.Duration
37 : screen *menuloop.Screen[RunStatsProps]
38 : }
39 :
40 : // RunStatsProps は統計画面の表示 props
41 : type RunStatsProps struct {
42 : Tabs []statsTab
43 : }
44 :
45 : // statsTab は統計画面の1タブ。見出しと2列テーブルの行を持つ
46 : type statsTab struct {
47 : Label string
48 : Items []statusItemData
49 : }
50 :
51 : var _ es.State[w.World] = &RunStatsState{}
52 :
53 : // NewRunStatsState は道中で run 統計を見る画面を作る。常時メニューから開き、閉じると元へ戻る
54 : func NewRunStatsState() (es.State[w.World], error) {
55 1 : return &RunStatsState{headerMsgid: "Statistics"}, nil
56 1 : }
57 :
58 : // NewRunResultState は run の死の結果画面を作る
59 : func NewRunResultState() (es.State[w.World], error) {
60 1 : return &RunStatsState{headerMsgid: "You died.", result: true}, nil
61 1 : }
62 :
63 : // NewRunClearState は run のクリアの結果画面を作る
64 : func NewRunClearState() (es.State[w.World], error) {
65 1 : return &RunStatsState{headerMsgid: "You escaped the planet.", result: true}, nil
66 1 : }
67 :
68 : // OnStart は Screen を組み立てる。overlay は持たない読み取り専用画面
69 : func (st *RunStatsState) OnStart(world w.World) error {
70 1 : st.playTime = query.GetPlayTime(world).Elapsed()
71 1 : st.screen = menuloop.NewScreen[RunStatsProps](st)
72 1 : return nil
73 1 : }
74 :
75 : // Update はステートの更新処理を Screen へ委譲する
76 : func (st *RunStatsState) Update(world w.World) (es.Transition[w.World], error) {
77 1 : return st.screen.Update(world)
78 1 : }
79 :
80 : // Draw はステートの描画を Screen へ委譲する
81 : func (st *RunStatsState) Draw(_ w.World, screen *ebiten.Image) error {
82 1 : st.screen.Draw(screen)
83 1 : return nil
84 1 : }
85 :
86 : // DoAction は閲覧中の Action を処理する。読み取り専用なので Cancel と Select だけ扱う
87 : func (st *RunStatsState) DoAction(_ w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
88 0 : switch action {
89 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
90 0 : return st.exitTransition(), nil
91 : case inputmapper.ActionMenuSelect:
92 0 : if st.result {
93 0 : return st.exitTransition(), nil
94 0 : }
95 0 : return es.Transition[w.World]{Type: es.TransNone}, nil
96 : default:
97 0 : return es.Transition[w.World]{}, fmt.Errorf("unknown action: %s", action)
98 : }
99 : }
100 :
101 : // exitTransition は画面を抜ける先を返す。結果画面はメインメニューへ、道中の統計画面は元の画面へ戻る
102 : func (st *RunStatsState) exitTransition() es.Transition[w.World] {
103 0 : if st.result {
104 0 : return es.Transition[w.World]{Type: es.TransReplace, NewStateFuncs: []es.StateFactory[w.World]{NewMainMenuState}}
105 0 : }
106 0 : return es.Transition[w.World]{Type: es.TransPop}
107 : }
108 :
109 : // Fetch は表示するタブと統計行を組む。結果画面は統計だけ、道中の統計画面は統計と環境の2タブを持つ
110 : func (st *RunStatsState) Fetch(world w.World) (RunStatsProps, error) {
111 1 : summary := statsTab{Label: query.T(world, "Summary"), Items: runStatsItems(world, st.shownPlayTime(world))}
112 1 : if st.result {
113 1 : return RunStatsProps{Tabs: []statsTab{summary}}, nil
114 1 : }
115 1 : return RunStatsProps{Tabs: []statsTab{summary, {Label: query.T(world, "Environment"), Items: environmentItems(world)}}}, nil
116 : }
117 :
118 : // Menu は読み取り専用のタブ構成を返す。見出し行が無いのでスキップは不要
119 : func (st *RunStatsState) Menu(props RunStatsProps) 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: runStatsMenuKey, TabCount: len(props.Tabs), ItemCounts: itemCounts}
125 : }
126 :
127 : // ViewUI はステータス表を組む。結果画面はタブ帯なしの枠、道中の統計画面はタブ帯つきの枠に載せる
128 : func (st *RunStatsState) ViewUI(world w.World, props RunStatsProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
129 1 : labels := make([]string, len(props.Tabs))
130 1 : for i, tab := range props.Tabs {
131 1 : labels[i] = tab.Label
132 1 : }
133 1 : tabIndex := cursor.TabIndex
134 1 : if tabIndex >= len(props.Tabs) {
135 0 : tabIndex = 0
136 0 : }
137 1 : content, pager := buildStatsTableUI(world, props.Tabs[tabIndex].Items, cursor.ItemIndex, res)
138 1 : if st.result {
139 1 : return menuframe.PanelScreen(world, res, query.T(world, st.headerMsgid), content, keybind.HelpHint(world), pager)
140 1 : }
141 1 : return menuframe.TabScreen(world, res, query.T(world, st.headerMsgid), labels, tabIndex, content, keybind.HelpHint(world), pager)
142 : }
143 :
144 : // buildStatsTableUI はラベル左・値右の2列表とフッタ右端のページ表示を返す。
145 : func buildStatsTableUI(world w.World, items []statusItemData, itemIndex int, res resources.UIResources) ([]uicore.Drawable, string) {
146 1 : cols := styled.Cols(styled.Name(), styled.Num())
147 1 : rows := make([]menuframe.Row, len(items))
148 1 : for i, it := range items {
149 1 : rows[i] = menuframe.Row{Cells: styled.TextCells(it.Label, it.Value)}
150 1 : }
151 1 : return menuframe.RenderList(itemIndex, rows, cols, menuframe.ListOpts{
152 1 : EmptyText: query.T(world, "No entries"),
153 1 : ItemsPerPage: menuframe.ListCapacity(world, true, true),
154 1 : }, res)
155 : }
156 :
157 : // shownPlayTime は画面に出すプレイ時間を返す
158 : func (st *RunStatsState) shownPlayTime(world w.World) time.Duration {
159 1 : if st.result {
160 1 : return st.playTime
161 1 : }
162 1 : return query.GetPlayTime(world).Elapsed()
163 : }
164 :
165 : // runStatsItems は run 統計をテーブル行に組む。結果画面と道中の統計画面で共通。
166 : // Label と Value の2列で、値は右寄せの数値。到達度は経過ターンで示す
167 : func runStatsItems(world w.World, playTime time.Duration) []statusItemData {
168 1 : days, turns, kills, items, sales := runStatsFields(world)
169 1 : rows := make([]statusItemData, 0, 6)
170 1 : // 死因は決着時にだけ記録される。道中の統計画面では空なので出さない
171 1 : if s := query.GetRunStats(world); s != nil && s.Cause != "" {
172 0 : rows = append(rows, statusItemData{Label: query.T(world, "Cause of death"), Value: query.T(world, s.Cause.DisplayName())})
173 0 : }
174 1 : return append(rows,
175 1 : statusItemData{Label: query.T(world, "Play time"), Value: formatPlayTime(playTime)},
176 1 : statusItemData{Label: query.T(world, "Days"), Value: fmt.Sprintf("%d", days)},
177 1 : statusItemData{Label: query.T(world, "Turns"), Value: fmt.Sprintf("%d", turns)},
178 1 : statusItemData{Label: query.T(world, "Enemies killed"), Value: fmt.Sprintf("%d", kills)},
179 1 : statusItemData{Label: query.T(world, "Items scavenged"), Value: fmt.Sprintf("%d", items)},
180 1 : statusItemData{Label: query.T(world, "Sales"), Value: fmt.Sprintf("%d", sales)},
181 1 : )
182 : }
183 :
184 : // runStatsFields は現在の統計を返す。撃破・漁り・売上は RunStats、日数・ターンは GameTime から引く
185 : func runStatsFields(world w.World) (days, turns, kills, items int, sales consts.Currency) {
186 1 : if s := query.GetRunStats(world); s != nil {
187 1 : kills = s.EnemiesKilled
188 1 : items = s.ItemsScavenged
189 1 : sales = s.SalesTotal
190 1 : }
191 1 : if gt := query.GetGameTime(world); gt != nil {
192 1 : days = gt.GetDayNumber()
193 1 : turns = int(gt.TotalTurns)
194 1 : }
195 1 : return
196 : }
197 :
198 : // environmentItems は現在地の環境情報をテーブル行に組む。周囲気温・時間帯・季節。
199 : // 周囲気温はプレイヤーの位置から引く。プレイヤーが居ない、または座標を持たないときは0にする
200 : func environmentItems(world w.World) []statusItemData {
201 1 : gt := query.GetGameTime(world)
202 1 : if gt == nil {
203 0 : return nil
204 0 : }
205 :
206 1 : ambient := 0
207 1 : if player, err := query.GetPlayerEntity(world); err == nil && query.AliveHas(world, world.Components.GridElement, player) {
208 1 : g := world.Components.GridElement.Get(player)
209 1 : if temp, terr := query.AmbientTemperatureAt(world, g.X, g.Y); terr == nil {
210 1 : ambient = temp
211 1 : }
212 : }
213 :
214 1 : return []statusItemData{
215 1 : {Label: query.T(world, "Ambient temperature"), Value: fmt.Sprintf("%d%s", ambient, consts.IconDegree)},
216 1 : {Label: query.T(world, "Time of day"), Value: query.TimeOfDayName(world, gt.GetTimeOfDay())},
217 1 : {Label: query.T(world, "Season"), Value: query.SeasonName(world, gt.GetSeason())},
218 1 : }
219 : }
|