Line data Source code
1 : package states
2 :
3 : import (
4 : "fmt"
5 : "math"
6 :
7 : "github.com/kijimaD/ruins/internal/i18n"
8 :
9 : "github.com/hajimehoshi/ebiten/v2"
10 : gc "github.com/kijimaD/ruins/internal/components"
11 : "github.com/kijimaD/ruins/internal/consts"
12 : es "github.com/kijimaD/ruins/internal/engine/states"
13 : "github.com/kijimaD/ruins/internal/inputmapper"
14 : "github.com/kijimaD/ruins/internal/keybind"
15 : "github.com/kijimaD/ruins/internal/render3d"
16 : "github.com/kijimaD/ruins/internal/widgets/hud"
17 : "github.com/kijimaD/ruins/internal/widgets/theme"
18 : "github.com/kijimaD/ruins/internal/widgets/uicore"
19 : w "github.com/kijimaD/ruins/internal/world"
20 :
21 : "github.com/kijimaD/ruins/internal/world/query"
22 : "github.com/mlange-42/ark/ecs"
23 : )
24 :
25 : // LookAroundState はタイル情報確認モードのステート
26 : // カーソルをマップ上で動かしてタイル・エンティティ情報を確認できる
27 : type LookAroundState struct {
28 : es.BaseState[w.World]
29 : cursor consts.Coord[consts.Tile]
30 : blinkCounter int
31 : }
32 :
33 : var _ es.State[w.World] = &LookAroundState{}
34 :
35 : // OnPause はステートが一時停止される際に呼ばれる
36 0 : func (st *LookAroundState) OnPause(_ w.World) error { return nil }
37 :
38 : // OnResume はステートが再開される際に呼ばれる
39 0 : func (st *LookAroundState) OnResume(_ w.World) error { return nil }
40 :
41 : // OnStart はステートが開始される際に呼ばれる
42 : func (st *LookAroundState) OnStart(world w.World) error {
43 : // プレイヤー位置からカーソルを開始
44 1 : playerEntity, err := query.GetPlayerEntity(world)
45 1 : if err != nil {
46 0 : return err
47 0 : }
48 :
49 1 : if !world.Components.GridElement.Has(playerEntity) {
50 0 : return fmt.Errorf("player does not have GridElement")
51 0 : }
52 :
53 1 : playerGrid := world.Components.GridElement.Get(playerEntity)
54 1 : st.cursor.X = playerGrid.X
55 1 : st.cursor.Y = playerGrid.Y
56 1 :
57 1 : return nil
58 : }
59 :
60 : // OnStop はステートが終了する際に呼ばれる
61 0 : func (st *LookAroundState) OnStop(_ w.World) error { return nil }
62 :
63 : // Update はステートの更新処理
64 : func (st *LookAroundState) Update(world w.World) (es.Transition[w.World], error) {
65 1 : st.blinkCounter++
66 1 :
67 1 : if action, ok := keybind.ReadInput(world, lookAroundBindings); ok {
68 1 : return st.doAction(world, action)
69 1 : }
70 :
71 1 : return st.ConsumeTransition(), nil
72 : }
73 :
74 : // lookAroundBindings は見回しモードの束縛表。矢印でカーソルを動かし、Esc で閉じる
75 : var lookAroundBindings = []keybind.Binding{
76 : {Key: ebiten.KeyEscape, Action: inputmapper.ActionCloseMenu},
77 : {Key: ebiten.KeyUp, Action: inputmapper.ActionMoveNorth},
78 : {Key: ebiten.KeyDown, Action: inputmapper.ActionMoveSouth},
79 : {Key: ebiten.KeyLeft, Action: inputmapper.ActionMoveWest},
80 : {Key: ebiten.KeyRight, Action: inputmapper.ActionMoveEast},
81 : }
82 :
83 : // doAction はActionIDを実行する
84 : func (st *LookAroundState) doAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
85 1 : switch action {
86 : case inputmapper.ActionCloseMenu:
87 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
88 : case inputmapper.ActionMoveNorth:
89 1 : st.moveCursor(world, gc.DirectionUp)
90 : case inputmapper.ActionMoveSouth:
91 0 : st.moveCursor(world, gc.DirectionDown)
92 : case inputmapper.ActionMoveWest:
93 0 : st.moveCursor(world, gc.DirectionLeft)
94 : case inputmapper.ActionMoveEast:
95 1 : st.moveCursor(world, gc.DirectionRight)
96 : default:
97 0 : return es.Transition[w.World]{}, fmt.Errorf("unsupported action: %s", action)
98 : }
99 :
100 1 : return st.ConsumeTransition(), nil
101 : }
102 :
103 : // moveCursor はカーソルを移動する。押した向きはカメラの水平角で回し、画面の上下左右に合わせる。
104 : // カメラが回転していても、右キーで画面の右にあるタイルへ動く。
105 : func (st *LookAroundState) moveCursor(world w.World, base gc.Direction) {
106 1 : var yaw float64
107 1 : if camera := query.GetPlayerCamera(world); camera != nil {
108 1 : yaw = camera.Yaw()
109 1 : }
110 1 : next := st.cursor.Add(gc.RotateScreenDir(base, yaw).GetDelta())
111 1 :
112 1 : field := query.GetCurrentStageField(world)
113 1 : if field == nil {
114 0 : return
115 0 : }
116 1 : level := field.Level
117 1 : if next.X >= 0 && next.X < level.TileWidth && next.Y >= 0 && next.Y < level.TileHeight {
118 1 : st.cursor = next
119 1 : }
120 : }
121 :
122 : // Draw はステートの描画処理
123 : func (st *LookAroundState) Draw(world w.World, screen *ebiten.Image) error {
124 : // カーソルを描画
125 1 : if err := st.drawCursor(world, screen); err != nil {
126 0 : return err
127 0 : }
128 :
129 : // タイル情報パネルを描画
130 1 : return st.drawInfoPanel(world, screen)
131 : }
132 :
133 : // cursorFrameWidth はカーソル枠の線の太さ
134 : const cursorFrameWidth = 3
135 :
136 : // drawCursor はカーソルを描画する。
137 : // タイルは透視投影で台形になるので、投影した四隅を線で結んで実際の輪郭に合わせる。
138 : func (st *LookAroundState) drawCursor(world w.World, screen *ebiten.Image) error {
139 1 : projector, err := render3d.WorldProjector(world)
140 1 : if err != nil {
141 0 : return err
142 0 : }
143 1 : corners, ok := projector.TileCorners(st.cursor, render3d.TileTopHeight(world, st.cursor))
144 1 : if !ok {
145 0 : return nil
146 0 : }
147 :
148 1 : cursorColor := theme.CursorLook
149 1 : // 点滅エフェクト: アルファ値を変化させる。アニメーション無効時は固定値
150 1 : if !world.Resources.Config.DisableAnimation {
151 0 : cursorColor = hud.ScaleAlpha(cursorColor, 0.6+0.4*math.Sin(float64(st.blinkCounter)*0.15))
152 0 : }
153 1 : hud.TileFrame(screen, corners, cursorFrameWidth, cursorColor)
154 1 : return nil
155 : }
156 :
157 : // drawInfoPanel はタイル情報パネルを描画する
158 : func (st *LookAroundState) drawInfoPanel(world w.World, screen *ebiten.Image) error {
159 1 : face := world.Resources.UIResources.Text.BodyFace
160 1 :
161 1 : const panelHeight = 200
162 1 : panel := hud.NewInfoPanel(uicore.NewEbitenCanvas(screen), hud.Chrome{}, face, screen.Bounds().Dx(), panelHeight)
163 1 : drawText := panel.Line
164 1 :
165 : // 座標表示
166 1 : drawText(fmt.Sprintf("%s: %s", query.T(world, "Coord"), st.cursor))
167 1 : panel.Gap(5)
168 1 :
169 : // 視界内かどうかをチェック
170 1 : playerEntity, err := query.GetPlayerEntity(world)
171 1 : if err != nil {
172 0 : return err
173 0 : }
174 1 : playerGrid := world.Components.GridElement.Get(playerEntity)
175 1 : inVision := query.IsInVision(world, playerGrid.Coord, st.cursor)
176 1 :
177 1 : if !inVision {
178 0 : drawText(query.T(world, "Darkness"))
179 0 : return nil
180 0 : }
181 :
182 : // タイル上のエンティティを取得
183 1 : entities := query.GetEntitiesAt(world, st.cursor.X, st.cursor.Y)
184 1 :
185 1 : if len(entities) == 0 {
186 0 : drawText(query.T(world, "Nothing here"))
187 0 : } else {
188 : // 床の同一スタックは1行に束ね、拾得メニューと見え方を揃える
189 1 : for _, stack := range query.GroupStacks(world, entities) {
190 1 : st.drawEntityInfo(world, stack.Rep, stack.Count, drawText)
191 1 : }
192 : }
193 :
194 : // 移動コストを表示
195 1 : st.drawPassCost(world, entities, panel)
196 1 :
197 : // タイルの環境属性を表示する
198 1 : st.drawTileEnvironment(world, entities, panel)
199 1 :
200 1 : panel.SeekBottom(30)
201 1 : drawText(query.T(world, "Arrows: Move Esc: Close"))
202 1 :
203 1 : return nil
204 : }
205 :
206 : // drawEntityInfo はエンティティ情報を描画する。個数は束ねた結果を呼び出し側が渡す
207 : func (st *LookAroundState) drawEntityInfo(world w.World, entity ecs.Entity, count int, drawText func(string)) {
208 1 : name := query.FormatNameCount(query.GetEntityName(entity, world), count)
209 1 :
210 1 : cat, ok := world.Components.CategoryOf(gc.FieldLookCategoryKey, entity)
211 1 : if !ok {
212 : // 壁などは名前だけ表示する
213 0 : if name != "" {
214 0 : drawText(name)
215 0 : }
216 0 : return
217 : }
218 :
219 1 : typeStr := fmt.Sprintf("[%s]", query.T(world, i18n.Msgid(cat)))
220 1 : if name != "" {
221 1 : drawText(fmt.Sprintf("%s %s", typeStr, name))
222 1 : } else {
223 0 : drawText(typeStr)
224 0 : }
225 :
226 : // HPを持つエンティティはHP表示
227 1 : if world.Components.HP.Has(entity) {
228 1 : hp := world.Components.HP.Get(entity)
229 1 : label := query.T(world, "HP")
230 1 : if world.Components.Prop.Has(entity) {
231 1 : label = query.T(world, "Durability")
232 1 : }
233 1 : drawText(fmt.Sprintf(" %s: %d/%d", label, hp.Current, hp.Max))
234 : }
235 : }
236 :
237 : // drawPassCost は移動コストを描画する
238 : func (st *LookAroundState) drawPassCost(world w.World, entities []ecs.Entity, panel *hud.InfoPanel) {
239 1 : blocked := false
240 1 : totalAdd := 0
241 1 : for _, entity := range entities {
242 1 : if world.Components.BlockPass.Has(entity) {
243 0 : blocked = true
244 0 : }
245 1 : if world.Components.PassCost.Has(entity) {
246 0 : mc := world.Components.PassCost.Get(entity)
247 0 : totalAdd += mc.Value
248 0 : }
249 : }
250 1 : panel.Gap(5)
251 1 : if blocked {
252 0 : panel.Line(query.T(world, "Move cost") + ": " + query.T(world, "Impassable"))
253 0 : } else {
254 1 : cost := consts.StandardActionCost + totalAdd
255 1 : panel.Line(fmt.Sprintf("%s: %d", query.T(world, "Move cost"), cost))
256 1 : }
257 : }
258 :
259 : // drawTileEnvironment はタイルの環境属性を描画する
260 : func (st *LookAroundState) drawTileEnvironment(world w.World, entities []ecs.Entity, panel *hud.InfoPanel) {
261 1 : for _, entity := range entities {
262 1 : if world.Components.TileEnvironment.Has(entity) {
263 1 : temp := world.Components.TileEnvironment.Get(entity)
264 1 : panel.Gap(5)
265 1 : defs := world.Resources.TileEnvDefs
266 1 : panel.Line(fmt.Sprintf("%s: %+d", query.T(world, "Temperature modifier"), defs.TempModifier(*temp)))
267 1 : // Shelter は加算℃でなく世界温度の緩和度なので、数値でなく囲われの度合いの名前だけ示す
268 1 : if defs.Shelters[temp.Shelter].Covered {
269 1 : panel.Line(" " + query.ShelterName(world, temp.Shelter))
270 1 : }
271 1 : if v := defs.Water[temp.Water]; v != 0 {
272 0 : panel.Line(fmt.Sprintf(" %s: %+d", query.T(world, "Waterside"), v))
273 0 : }
274 1 : if v := defs.Foliage[temp.Foliage]; v != 0 {
275 0 : panel.Line(fmt.Sprintf(" %s: %+d", query.T(world, "Foliage"), v))
276 0 : }
277 1 : return
278 : }
279 : }
280 : }
|