class_name Player extends CharacterBody2D @onready var raycast_left: RayCast2D = $raycastLeft @onready var raycast_right: RayCast2D = $raycastRight @onready var left_marker: Marker2D = $left_marker @onready var right_marker: Marker2D = $right_marker @onready var graphic: AnimatedSprite2D = $graphic 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 enum State { IDLE, RUN, JUMP, APEX, FALL } var state: State = State.IDLE var up_jump:bool = false func _physics_process(delta: float): handle_input() handle_movement(delta) handle_state() handle_animation() 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 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) # 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 graphic.flip_h = true elif direction > 0: facing = FaceDirection.RIGHT graphic.flip_h = false 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: if raycast_right.get_collider() is RigidBody2D: shove_target = raycast_right.get_collider() elif raycast_left.is_colliding() and facing == FaceDirection.LEFT: if raycast_left.get_collider() is RigidBody2D: shove_target = raycast_left.get_collider() 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) func handle_state(): match state: State.IDLE when velocity.x != 0: 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: state = State.IDLE func handle_animation(): match state: State.IDLE: graphic.play("idle") State.RUN: graphic.play("run") 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