add game state
This commit is contained in:
parent
fb287b8264
commit
efb9a4e9d9
5 changed files with 50 additions and 10 deletions
25
game/src/GameState.gd
Normal file
25
game/src/GameState.gd
Normal file
|
@ -0,0 +1,25 @@
|
|||
extends Node
|
||||
class_name GameState
|
||||
|
||||
# https://github.com/GDQuest/godot-demos/blob/master/2018/04-24-finite-state-machine/player_v2/state_machine.gd
|
||||
# http://www.gameprogrammingpatterns.com/state.html
|
||||
|
||||
enum State { MAIN, WINNING, GAME_OVER }
|
||||
var transitions = {
|
||||
State.MAIN : [ State.WINNING ],
|
||||
State.WINNING : [ State.GAME_OVER ],
|
||||
State.GAME_OVER : [ State.MAIN ]
|
||||
}
|
||||
|
||||
signal state_changed(previous, current)
|
||||
|
||||
export(State) var current_state = State.MAIN
|
||||
|
||||
func current_state_name() -> String:
|
||||
return State.keys()[current_state]
|
||||
|
||||
func transition_to(state):
|
||||
assert(state in transitions[current_state])
|
||||
var previous_state = current_state
|
||||
current_state = state
|
||||
emit_signal("state_changed", previous_state, current_state)
|
Loading…
Add table
Add a link
Reference in a new issue