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/activity"
9 : gc "github.com/kijimaD/ruins/internal/components"
10 : "github.com/kijimaD/ruins/internal/consts"
11 : es "github.com/kijimaD/ruins/internal/engine/states"
12 : "github.com/kijimaD/ruins/internal/gamelog"
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 : "github.com/kijimaD/ruins/internal/world/query"
21 : "github.com/mlange-42/ark/ecs"
22 : )
23 :
24 : // directionAction は矢印で選んだ隣接タイルへ行う行動
25 : type directionAction struct {
26 : prompt string
27 : emptyMsg string
28 : accepts func(world w.World, c consts.Coord[consts.Tile]) bool
29 : run func(world w.World, c consts.Coord[consts.Tile]) error
30 : }
31 :
32 : // directionOption は選択キーと、そのキーが指す候補タイルの組
33 : type directionOption struct {
34 : action inputmapper.ActionID
35 : tile consts.Coord[consts.Tile]
36 : }
37 :
38 : // startDirectionAction は候補が無ければログを出し、1つならそのまま実行し、複数なら DirectionSelectState を開く
39 : func startDirectionAction(world w.World, a directionAction) (es.Transition[w.World], error) {
40 1 : options, err := directionOptions(world, a)
41 1 : if err != nil {
42 0 : return es.Transition[w.World]{Type: es.TransNone}, err
43 0 : }
44 1 : switch len(options) {
45 : case 0:
46 1 : gamelog.New(query.GetGameLog(world)).Markup(a.emptyMsg).Log()
47 1 : return es.Transition[w.World]{Type: es.TransNone}, nil
48 : case 1:
49 1 : return es.Transition[w.World]{Type: es.TransNone}, a.run(world, options[0].tile)
50 : }
51 1 : return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{
52 1 : func() (es.State[w.World], error) { return &DirectionSelectState{action: a, options: options}, nil },
53 : }}, nil
54 : }
55 :
56 : // directionOptions は選択キーごとの候補タイルを北・南・西・東の順に返す
57 : func directionOptions(world w.World, a directionAction) ([]directionOption, error) {
58 1 : player, err := query.GetPlayerEntity(world)
59 1 : if err != nil {
60 0 : return nil, err
61 0 : }
62 1 : var yaw float64
63 1 : if camera := query.GetPlayerCamera(world); camera != nil {
64 1 : yaw = camera.Yaw()
65 1 : }
66 1 : origin := world.Components.GridElement.Get(player).Coord
67 1 : var options []directionOption
68 1 : for _, o := range []directionOption{
69 1 : {action: inputmapper.ActionMoveNorth, tile: origin.Add(gc.RotateScreenDir(gc.DirectionUp, yaw).GetDelta())},
70 1 : {action: inputmapper.ActionMoveSouth, tile: origin.Add(gc.RotateScreenDir(gc.DirectionDown, yaw).GetDelta())},
71 1 : {action: inputmapper.ActionMoveWest, tile: origin.Add(gc.RotateScreenDir(gc.DirectionLeft, yaw).GetDelta())},
72 1 : {action: inputmapper.ActionMoveEast, tile: origin.Add(gc.RotateScreenDir(gc.DirectionRight, yaw).GetDelta())},
73 1 : } {
74 1 : if a.accepts(world, o.tile) {
75 1 : options = append(options, o)
76 1 : }
77 : }
78 1 : return options, nil
79 : }
80 :
81 : // DirectionSelectState は directionAction の対象タイルを矢印で選ぶステート
82 : type DirectionSelectState struct {
83 : es.BaseState[w.World]
84 : action directionAction
85 : options []directionOption
86 : }
87 :
88 : var _ es.State[w.World] = &DirectionSelectState{}
89 :
90 : // OnPause はステートが一時停止される際に呼ばれる
91 0 : func (st *DirectionSelectState) OnPause(_ w.World) error { return nil }
92 :
93 : // OnResume はステートが再開される際に呼ばれる
94 0 : func (st *DirectionSelectState) OnResume(_ w.World) error { return nil }
95 :
96 : // OnStart はステートが開始される際に呼ばれる
97 0 : func (st *DirectionSelectState) OnStart(_ w.World) error { return nil }
98 :
99 : // OnStop はステートが終了する際に呼ばれる
100 0 : func (st *DirectionSelectState) OnStop(_ w.World) error { return nil }
101 :
102 : // directionSelectBindings は方向選択の束縛表
103 : var directionSelectBindings = []keybind.Binding{
104 : {Key: ebiten.KeyEscape, Action: inputmapper.ActionCloseMenu},
105 : {Key: ebiten.KeyUp, Action: inputmapper.ActionMoveNorth},
106 : {Key: ebiten.KeyDown, Action: inputmapper.ActionMoveSouth},
107 : {Key: ebiten.KeyLeft, Action: inputmapper.ActionMoveWest},
108 : {Key: ebiten.KeyRight, Action: inputmapper.ActionMoveEast},
109 : }
110 :
111 : // Update はステートの更新処理
112 : func (st *DirectionSelectState) Update(world w.World) (es.Transition[w.World], error) {
113 0 : if action, ok := keybind.ReadInput(world, directionSelectBindings); ok {
114 0 : return st.doAction(world, action)
115 0 : }
116 0 : return st.ConsumeTransition(), nil
117 : }
118 :
119 : // doAction はActionIDを実行する
120 : func (st *DirectionSelectState) doAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
121 1 : if action == inputmapper.ActionCloseMenu {
122 1 : return es.Transition[w.World]{Type: es.TransPop}, nil
123 1 : }
124 2 : i := slices.IndexFunc(st.options, func(o directionOption) bool { return o.action == action })
125 1 : if i < 0 {
126 1 : return es.Transition[w.World]{Type: es.TransNone}, nil
127 1 : }
128 1 : if err := st.action.run(world, st.options[i].tile); err != nil {
129 0 : return es.Transition[w.World]{Type: es.TransNone}, err
130 0 : }
131 1 : return es.Transition[w.World]{Type: es.TransPop}, nil
132 : }
133 :
134 : // Draw は候補タイルの枠と操作の案内を描画する
135 : func (st *DirectionSelectState) Draw(world w.World, screen *ebiten.Image) error {
136 0 : projector, err := render3d.WorldProjector(world)
137 0 : if err != nil {
138 0 : return err
139 0 : }
140 0 : for _, o := range st.options {
141 0 : if corners, ok := projector.TileCorners(o.tile, render3d.TileTopHeight(world, o.tile)); ok {
142 0 : hud.TileFrame(screen, corners, cursorFrameWidth, theme.CursorLook)
143 0 : }
144 : }
145 :
146 0 : const panelHeight = 60
147 0 : panel := hud.NewInfoPanel(uicore.NewEbitenCanvas(screen), hud.Chrome{}, world.Resources.UIResources.Text.BodyFace, screen.Bounds().Dx(), panelHeight)
148 0 : panel.Line(st.action.prompt)
149 0 : panel.Line(query.T(world, "Arrows: Select Esc: Cancel"))
150 0 : return nil
151 : }
152 :
153 : // attackAction は隣接タイルの攻撃対象を近接攻撃する方向行動
154 : func attackAction(world w.World) directionAction {
155 1 : return directionAction{
156 1 : prompt: query.T(world, "Attack which direction?"),
157 1 : emptyMsg: query.T(world, "There is nothing to attack nearby."),
158 1 : accepts: func(world w.World, c consts.Coord[consts.Tile]) bool {
159 1 : _, ok := meleeTargetAt(world, c)
160 1 : return ok
161 1 : },
162 : run: func(world w.World, c consts.Coord[consts.Tile]) error {
163 1 : player, err := query.GetPlayerEntity(world)
164 1 : if err != nil {
165 0 : return err
166 0 : }
167 1 : target, ok := meleeTargetAt(world, c)
168 1 : if !ok {
169 0 : return fmt.Errorf("melee target disappeared at %v", c)
170 0 : }
171 1 : _, err = activity.ExecuteInteraction(player, target, gc.InteractionMelee, world)
172 1 : return err
173 : },
174 : }
175 : }
176 :
177 : // meleeTargetAt はタイル上の近接攻撃の対象を返す。歩ける岩などの prop と敵が重なるので、prop でない相手を優先する
178 : func meleeTargetAt(world w.World, c consts.Coord[consts.Tile]) (ecs.Entity, bool) {
179 1 : var prop ecs.Entity
180 1 : found := false
181 1 : for _, e := range query.GetEntitiesAt(world, c.X, c.Y) {
182 1 : if world.Components.Dead.Has(e) || !world.Components.Interactable.Has(e) ||
183 1 : !slices.Contains(world.Components.Interactable.Get(e).Interactions, gc.InteractionMelee) {
184 0 : continue
185 : }
186 1 : if !world.Components.Prop.Has(e) {
187 1 : return e, true
188 1 : }
189 1 : if !found {
190 1 : prop, found = e, true
191 1 : }
192 : }
193 1 : return prop, found
194 : }
195 :
196 : // interactableAt はタイル上で相互作用 kind を受け付ける生存中のエンティティを返す
197 : func interactableAt(world w.World, c consts.Coord[consts.Tile], kind gc.InteractionKind) (ecs.Entity, bool) {
198 1 : for _, e := range query.GetEntitiesAt(world, c.X, c.Y) {
199 1 : if world.Components.Dead.Has(e) || !world.Components.Interactable.Has(e) {
200 1 : continue
201 : }
202 1 : if slices.Contains(world.Components.Interactable.Get(e).Interactions, kind) {
203 1 : return e, true
204 1 : }
205 : }
206 1 : return ecs.Entity{}, false
207 : }
|