2026-04-28 01:06:17 +00:00
|
|
|
class_name Player extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
@onready var raycast_left: RayCast2D = $raycastLeft
|
|
|
|
|
@onready var raycast_right: RayCast2D = $raycastRight
|
2026-05-05 00:57:02 +00:00
|
|
|
@onready var left_marker: Marker2D = $left_marker
|
|
|
|
|
@onready var right_marker: Marker2D = $right_marker
|
|
|
|
|
@onready var graphic: AnimatedSprite2D = $graphic
|
2026-04-28 01:06:17 +00:00
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
|
var direction : float = 0
|
|
|
|
|
enum FaceDirection { LEFT, RIGHT }
|
|
|
|
|
var facing: FaceDirection = FaceDirection.LEFT
|
|
|
|
|
var shove_target: RigidBody2D
|
2026-05-11 22:57:56 +00:00
|
|
|
enum State { IDLE, RUN, JUMP, APEX, FALL }
|
2026-05-05 00:57:02 +00:00
|
|
|
var state: State = State.IDLE
|
2026-05-11 22:57:56 +00:00
|
|
|
var up_jump:bool = false
|
2026-04-28 01:06:17 +00:00
|
|
|
|
|
|
|
|
func _physics_process(delta: float):
|
|
|
|
|
handle_input()
|
|
|
|
|
handle_movement(delta)
|
2026-05-05 00:57:02 +00:00
|
|
|
handle_state()
|
|
|
|
|
handle_animation()
|
2026-04-28 01:06:17 +00:00
|
|
|
move_and_slide() # pre-calc next position from velocity
|
|
|
|
|
handle_collisions()
|
|
|
|
|
|
|
|
|
|
func handle_input():
|
|
|
|
|
if Input.is_action_just_pressed("shove"):
|
|
|
|
|
if shove_target:
|
|
|
|
|
var shove_normal = 1 if facing == FaceDirection.RIGHT else -1
|
|
|
|
|
shove_target.apply_central_impulse(Vector2(shove_normal,0)*700)
|
|
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
|
|
|
velocity.y = JUMP_VELOCITY
|
2026-05-05 00:57:02 +00:00
|
|
|
if Input.is_action_just_pressed("chuck"):
|
|
|
|
|
if facing == FaceDirection.LEFT:
|
|
|
|
|
print("Chuck grenade left")
|
|
|
|
|
%SceneManager.make_grenade(left_marker.global_transform, -1)
|
|
|
|
|
else:
|
|
|
|
|
print("Chuck grenade right")
|
|
|
|
|
%SceneManager.make_grenade(right_marker.global_transform, 1)
|
2026-04-28 01:06:17 +00:00
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
|
|
|
direction = Input.get_axis("left", "right")
|
|
|
|
|
if direction < 0:
|
|
|
|
|
facing = FaceDirection.LEFT
|
2026-05-05 00:57:02 +00:00
|
|
|
graphic.flip_h = true
|
2026-04-28 01:06:17 +00:00
|
|
|
elif direction > 0:
|
|
|
|
|
facing = FaceDirection.RIGHT
|
2026-05-05 00:57:02 +00:00
|
|
|
graphic.flip_h = false
|
2026-04-28 01:06:17 +00:00
|
|
|
|
|
|
|
|
func handle_movement(delta: float):
|
|
|
|
|
# Add the gravity.
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
if direction:
|
|
|
|
|
velocity.x = direction * SPEED
|
|
|
|
|
else:
|
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
|
|
|
|
|
|
func handle_collisions():
|
|
|
|
|
if raycast_right.is_colliding() and facing == FaceDirection.RIGHT:
|
2026-04-28 02:11:14 +00:00
|
|
|
if raycast_right.get_collider() is RigidBody2D:
|
|
|
|
|
shove_target = raycast_right.get_collider()
|
2026-04-28 01:06:17 +00:00
|
|
|
elif raycast_left.is_colliding() and facing == FaceDirection.LEFT:
|
2026-04-28 02:11:14 +00:00
|
|
|
if raycast_left.get_collider() is RigidBody2D:
|
|
|
|
|
shove_target = raycast_left.get_collider()
|
2026-04-28 01:06:17 +00:00
|
|
|
else:
|
|
|
|
|
shove_target = null
|
|
|
|
|
for i in get_slide_collision_count():
|
|
|
|
|
var c = get_slide_collision(i)
|
|
|
|
|
if c.get_collider() is RigidBody2D:
|
|
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
2026-05-05 00:57:02 +00:00
|
|
|
|
|
|
|
|
func handle_state():
|
|
|
|
|
match state:
|
|
|
|
|
State.IDLE when velocity.x != 0:
|
2026-05-11 22:57:56 +00:00
|
|
|
state = State.RUN
|
|
|
|
|
State.RUN when velocity.x == 0:
|
|
|
|
|
state = State.IDLE
|
|
|
|
|
State.RUN when velocity.y < 0:
|
|
|
|
|
state = State.JUMP
|
|
|
|
|
State.IDLE when velocity.y < 0:
|
|
|
|
|
state = State.JUMP
|
|
|
|
|
State.JUMP when velocity.y > 0:
|
|
|
|
|
state = State.APEX
|
|
|
|
|
# from APEX to FALL is handled when apex animation finishes
|
|
|
|
|
State.FALL when velocity.y == 0:
|
2026-05-05 00:57:02 +00:00
|
|
|
state = State.IDLE
|
|
|
|
|
|
|
|
|
|
func handle_animation():
|
|
|
|
|
match state:
|
|
|
|
|
State.IDLE:
|
|
|
|
|
graphic.play("idle")
|
2026-05-11 22:57:56 +00:00
|
|
|
State.RUN:
|
2026-05-05 00:57:02 +00:00
|
|
|
graphic.play("run")
|
2026-05-11 22:57:56 +00:00
|
|
|
State.JUMP:
|
|
|
|
|
graphic.play("jump")
|
|
|
|
|
State.APEX:
|
|
|
|
|
graphic.play("apex")
|
|
|
|
|
State.FALL:
|
|
|
|
|
graphic.play("fall")
|
|
|
|
|
|
|
|
|
|
func _on_graphic_animation_finished() -> void:
|
|
|
|
|
# switch state from apex to fall
|
|
|
|
|
match state:
|
|
|
|
|
State.APEX:
|
|
|
|
|
state = State.FALL
|