summaryrefslogtreecommitdiffstats
path: root/ps_forced.gd
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2025-11-28 18:42:41 +0200
committerJan Tuomi <jan@jantuomi.fi>2025-11-28 18:42:41 +0200
commit6173a0c4e1b41628532a37f0638339b0724b445e (patch)
tree56c52754cf2aa93e8678639c0ed7c91595acb40d /ps_forced.gd
parent1723938f22be657cbe8d6bc85e70039622412273 (diff)
Use state machine in Player
Diffstat (limited to 'ps_forced.gd')
-rw-r--r--ps_forced.gd46
1 files changed, 46 insertions, 0 deletions
diff --git a/ps_forced.gd b/ps_forced.gd
new file mode 100644
index 0000000..4c4c3dc
--- /dev/null
+++ b/ps_forced.gd
@@ -0,0 +1,46 @@
+class_name PlayerStateForced
+extends PlayerState
+
+var input_x: float
+var jump_coef = 1.3
+
+func initialize(p: Player, dir: String):
+ if dir == "LEFT":
+ input_x = -1.0
+ elif dir == "JUMP_LEFT":
+ p.velocity.y = -jump_coef * p.jump_strength
+ input_x = -1.0
+ elif dir == "RIGHT":
+ input_x = 1.0
+ elif dir == "JUMP_RIGHT":
+ p.velocity.y = -jump_coef * p.jump_strength
+ input_x = 1.0
+ elif dir == "JUMP":
+ p.velocity.y = -p.jump_strength
+ input_x = 0.0
+ p.gravity = 0.0
+ elif dir == "DROP":
+ input_x = 0.0
+
+func _handle(p: Player, _delta: float):
+ var as2d = p.get_node("AnimatedSprite2D")
+ if p.velocity.x > 0:
+ as2d.flip_h = false
+ elif p.velocity.x < 0:
+ as2d.flip_h = true
+
+ if not p.is_on_floor():
+ as2d.set_animation("jump")
+ elif abs(p.velocity.x) > 0.0:
+ as2d.set_animation("walk")
+ else:
+ as2d.set_animation("idle")
+
+func _handle_physics(p: Player, delta: float):
+ p.velocity.y += p.gravity * delta
+ if p.velocity.y > p.jump_strength:
+ p.velocity.y = p.jump_strength
+
+ p.velocity.x = input_x * p.speed
+
+ p.move_and_slide()