Line data Source code
1 : package states
2 :
3 : import (
4 : "github.com/hajimehoshi/ebiten/v2"
5 : "github.com/kijimaD/ruins/internal/config"
6 : "github.com/kijimaD/ruins/internal/inputmapper"
7 : )
8 :
9 : // TransType is a transition type
10 : type TransType int
11 :
12 : const (
13 : // TransNone does nothing
14 : TransNone TransType = iota
15 : // TransPop removes the active state and resume the next state
16 : TransPop
17 : // TransPush pauses the active state and add new states to the stack
18 : TransPush
19 : // TransSwitch removes the active state and replace it by a new one
20 : TransSwitch
21 : // TransReplace removes all states and insert a new stack
22 : TransReplace
23 : // TransQuit removes all states and quit
24 : TransQuit
25 : )
26 :
27 : // Transition は状態遷移を表す
28 : type Transition[T any] struct {
29 : Type TransType
30 : NewStateFuncs []StateFactory[T]
31 : }
32 :
33 : // State はゲームステートのジェネリックインターフェース
34 : type State[T any] interface {
35 : // Executed when the state begins
36 : OnStart(world T) error
37 : // Executed when the state exits
38 : OnStop(world T) error
39 : // Executed when a new state is pushed over this one
40 : OnPause(world T) error
41 : // Executed when the state become active again (states pushed over this one have been popped)
42 : OnResume(world T) error
43 : // Executed on every frame when the state is active
44 : Update(world T) (Transition[T], error)
45 : // 描画
46 : Draw(world T, screen *ebiten.Image) error
47 : }
48 :
49 : // ActionHandler はActionベースの入力処理を行うステートのためのオプショナルインターフェース
50 : // - HandleInput でキー入力をActionIDに変換
51 : // - DoAction でActionIDを受け取ってステート遷移を返す
52 : type ActionHandler[T any] interface {
53 : // HandleInput はキー入力をActionIDに変換する
54 : HandleInput(cfg *config.Config) (inputmapper.ActionID, bool)
55 :
56 : // DoAction はActionを実行してステート遷移を返す
57 : DoAction(world T, action inputmapper.ActionID) (Transition[T], error)
58 : }
59 :
60 : // StateFactory はステートを作成するファクトリー関数の型
61 : type StateFactory[T any] func() (State[T], error)
62 :
63 : // StateMachine はジェネリックな状態スタックを管理する
64 : type StateMachine[T any] struct {
65 : states []State[T]
66 : lastTransition Transition[T]
67 : }
68 :
69 : // Init は新しいステートマシンを初期化する
70 : func Init[T any](s State[T], world T) (StateMachine[T], error) {
71 1 : if err := s.OnStart(world); err != nil {
72 1 : return StateMachine[T]{}, err
73 1 : }
74 1 : return StateMachine[T]{
75 1 : states: []State[T]{s},
76 1 : lastTransition: Transition[T]{Type: TransNone},
77 1 : }, nil
78 : }
79 :
80 : // Update はステートマシンを更新する
81 : func (sm *StateMachine[T]) Update(world T) error {
82 : // ファクトリー関数からステートを作成
83 1 : states, err := sm.createStatesFromFunc()
84 1 : if err != nil {
85 1 : return err
86 1 : }
87 :
88 1 : switch sm.lastTransition.Type {
89 1 : case TransNone:
90 : // 何もしない
91 : case TransPop:
92 1 : if err := sm.pop(world); err != nil {
93 1 : return err
94 1 : }
95 : case TransPush:
96 1 : if err := sm.push(world, states); err != nil {
97 1 : return err
98 1 : }
99 : case TransSwitch:
100 1 : if err := sm.switchState(world, states); err != nil {
101 1 : return err
102 1 : }
103 : case TransReplace:
104 1 : if err := sm.replace(world, states); err != nil {
105 1 : return err
106 1 : }
107 : case TransQuit:
108 1 : if err := sm.quit(world); err != nil {
109 1 : return err
110 1 : }
111 : }
112 :
113 1 : if len(sm.states) < 1 {
114 1 : return nil
115 1 : }
116 :
117 : // アクティブなステートを更新
118 1 : trans, err := sm.states[len(sm.states)-1].Update(world)
119 1 : if err != nil {
120 1 : return err
121 1 : }
122 1 : sm.lastTransition = trans
123 1 : return nil
124 : }
125 :
126 : // Draw は画面を描画する
127 : func (sm *StateMachine[T]) Draw(world T, screen *ebiten.Image) error {
128 1 : for _, state := range sm.states {
129 1 : if err := state.Draw(world, screen); err != nil {
130 1 : return err
131 1 : }
132 : }
133 1 : return nil
134 : }
135 :
136 : // createStatesFromFunc はファクトリー関数からステートを作成する
137 : func (sm *StateMachine[T]) createStatesFromFunc() ([]State[T], error) {
138 1 : if len(sm.lastTransition.NewStateFuncs) == 0 {
139 1 : return []State[T]{}, nil
140 1 : }
141 :
142 1 : states := make([]State[T], len(sm.lastTransition.NewStateFuncs))
143 1 : for i, factory := range sm.lastTransition.NewStateFuncs {
144 1 : s, err := factory()
145 1 : if err != nil {
146 1 : return nil, err
147 1 : }
148 1 : states[i] = s
149 : }
150 1 : return states, nil
151 : }
152 :
153 : // pop はアクティブなステートを削除して次のステートを再開する
154 : func (sm *StateMachine[T]) pop(world T) error {
155 1 : if len(sm.states) == 0 {
156 1 : return nil
157 1 : }
158 :
159 1 : currentState := sm.states[len(sm.states)-1]
160 1 : if err := currentState.OnStop(world); err != nil {
161 1 : return err
162 1 : }
163 1 : sm.states = sm.states[:len(sm.states)-1]
164 1 :
165 1 : if len(sm.states) > 0 {
166 1 : resumeState := sm.states[len(sm.states)-1]
167 1 : if err := resumeState.OnResume(world); err != nil {
168 1 : return err
169 1 : }
170 : }
171 1 : return nil
172 : }
173 :
174 : // push はアクティブなステートを一時停止して新しいステートをスタックに追加する
175 : func (sm *StateMachine[T]) push(world T, newStates []State[T]) error {
176 1 : if len(newStates) == 0 {
177 1 : return nil
178 1 : }
179 :
180 1 : if len(sm.states) > 0 {
181 1 : currentState := sm.states[len(sm.states)-1]
182 1 : if err := currentState.OnPause(world); err != nil {
183 1 : return err
184 1 : }
185 : }
186 :
187 1 : for _, state := range newStates[:len(newStates)-1] {
188 1 : if err := state.OnStart(world); err != nil {
189 1 : return err
190 1 : }
191 1 : if err := state.OnPause(world); err != nil {
192 1 : return err
193 1 : }
194 : }
195 :
196 1 : activeState := newStates[len(newStates)-1]
197 1 : if err := activeState.OnStart(world); err != nil {
198 1 : return err
199 1 : }
200 :
201 1 : sm.states = append(sm.states, newStates...)
202 1 : return nil
203 : }
204 :
205 : // switchState はアクティブなステートを新しいものに置き換える
206 : func (sm *StateMachine[T]) switchState(world T, newStates []State[T]) error {
207 1 : if len(newStates) != 1 {
208 1 : return nil
209 1 : }
210 :
211 1 : if len(sm.states) > 0 {
212 1 : currentState := sm.states[len(sm.states)-1]
213 1 : newState := newStates[0]
214 1 :
215 1 : if err := currentState.OnStop(world); err != nil {
216 1 : return err
217 1 : }
218 1 : if err := newState.OnStart(world); err != nil {
219 1 : return err
220 1 : }
221 1 : sm.states[len(sm.states)-1] = newState
222 : }
223 1 : return nil
224 : }
225 :
226 : // replace はすべてのステートを削除して新しいスタックを挿入する
227 : func (sm *StateMachine[T]) replace(world T, newStates []State[T]) error {
228 1 : for len(sm.states) > 0 {
229 1 : currentState := sm.states[len(sm.states)-1]
230 1 : if err := currentState.OnStop(world); err != nil {
231 1 : return err
232 1 : }
233 1 : sm.states = sm.states[:len(sm.states)-1]
234 : }
235 :
236 1 : if len(newStates) > 0 {
237 1 : for _, state := range newStates[:len(newStates)-1] {
238 1 : if err := state.OnStart(world); err != nil {
239 1 : return err
240 1 : }
241 1 : if err := state.OnPause(world); err != nil {
242 1 : return err
243 1 : }
244 : }
245 1 : activeState := newStates[len(newStates)-1]
246 1 : if err := activeState.OnStart(world); err != nil {
247 1 : return err
248 1 : }
249 : }
250 1 : sm.states = newStates
251 1 : return nil
252 : }
253 :
254 : // quit はすべてのステートを削除して終了する
255 : func (sm *StateMachine[T]) quit(world T) error {
256 1 : for len(sm.states) > 0 {
257 1 : currentState := sm.states[len(sm.states)-1]
258 1 : if err := currentState.OnStop(world); err != nil {
259 1 : return err
260 1 : }
261 1 : sm.states = sm.states[:len(sm.states)-1]
262 : }
263 1 : return nil
264 : }
265 :
266 : // GetStates はステートスタックのコピーを返す
267 : func (sm *StateMachine[T]) GetStates() []State[T] {
268 1 : states := make([]State[T], len(sm.states))
269 1 : copy(states, sm.states)
270 1 : return states
271 1 : }
272 :
273 : // GetCurrentState は現在アクティブなステートを返す(テスト用)
274 : func (sm *StateMachine[T]) GetCurrentState() State[T] {
275 1 : if len(sm.states) == 0 {
276 1 : return nil
277 1 : }
278 1 : return sm.states[len(sm.states)-1]
279 : }
280 :
281 : // GetStateCount はステートスタックの数を返す(テスト用)
282 : func (sm *StateMachine[T]) GetStateCount() int {
283 1 : return len(sm.states)
284 1 : }
285 :
286 : // PushState は外部から新しいステートをスタックにpushする
287 : func (sm *StateMachine[T]) PushState(world T, states ...State[T]) error {
288 1 : return sm.push(world, states)
289 1 : }
|