summaryrefslogtreecommitdiff
path: root/src/entities/actors/snake/scripts/state_machine.gd
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-05 09:18:35 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-05 09:18:35 -0600
commit4b42a8ba26f21e2c6c766fa747c8b93a115a53b2 (patch)
treea688e3be103942c1a1057b65ca61ea26f57dd5ee /src/entities/actors/snake/scripts/state_machine.gd
parente4423cc8490b7f5ec3449f568bf64e81f4d03248 (diff)
added new tiles to ground tilemap, moved player to state machine paradigm
Diffstat (limited to 'src/entities/actors/snake/scripts/state_machine.gd')
-rw-r--r--src/entities/actors/snake/scripts/state_machine.gd72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/entities/actors/snake/scripts/state_machine.gd b/src/entities/actors/snake/scripts/state_machine.gd
new file mode 100644
index 0000000..b63f272
--- /dev/null
+++ b/src/entities/actors/snake/scripts/state_machine.gd
@@ -0,0 +1,72 @@
+class_name StateMachine
+extends Node
+
+const DEBUG: bool = false
+
+var player: KinematicBody2D
+var state: Node
+var history: Array = []
+
+
+func _ready() -> void:
+ player = get_parent()
+ state = get_child(0)
+ _enter_state()
+
+
+func change_to(new_state: String) -> void:
+ history.append(state.name)
+ state = get_node(new_state)
+ _enter_state()
+
+
+func back() -> void:
+ if history.size() > 0:
+ state = get_node(history.pop_back())
+ _enter_state()
+
+
+func _enter_state() -> void:
+ if DEBUG:
+ print("Entering state %s" % state.name)
+ state.fsm = self
+ state.enter()
+
+
+# routing game loop functions
+func _process(delta: float) -> void:
+ if state.has_method("process"):
+ state.process(delta)
+
+
+func _physics_process(delta: float) -> void:
+ if Input.is_action_pressed("move_left"):
+ player.rotate_to(player.LEFT)
+ if Input.is_action_pressed("move_right"):
+ player.rotate_to(player.RIGHT)
+
+ # state specific code, move_and_slide is called here
+ if state.has_method("physics_process"):
+ state.physics_process(delta)
+
+ handle_slow_speeds()
+
+ player.handle_time_elapsed(delta)
+
+
+func slow_down_on_collisions(speed_backup: float):
+ if player.get_last_slide_collision():
+ Global.SNAKE_SPEED = player.velocity.length()
+ else:
+ Global.SNAKE_SPEED = speed_backup
+
+
+func handle_slow_speeds() -> void:
+ if Global.SNAKE_SPEED <= Global.SNAKE_SPEED_BACKUP / 4.0:
+ Global.SNAKE_SPEED = Global.SNAKE_SPEED_BACKUP
+ Event.emit_signal("game_over")
+
+
+func _input(event: InputEvent) -> void:
+ if state.has_method("input"):
+ state.input(event)