class_name Player extends CharacterBody2D @onready var scene_manager: SceneManager = %SceneManager @onready var right_cast: RayCast2D = $RightCast @onready var left_cast: RayCast2D = $LeftCast @onready var right_spawn: Node2D = $RightSpawn @onready var left_spawn: Node2D = $LeftSpawn @onready var jump_buffer: Timer = $JumpBuffer @onready var player_graphic: AnimatedSprite2D = $PlayerGraphic const SHOVE_STRENGTH = 700 const SPEED = 300.0 const JUMP_VELOCITY = -450.0 var acceleration: int = 50 var direction: float enum FaceDirection{LEFT, RIGHT} var facing: FaceDirection = FaceDirection.RIGHT enum PossibleStates{IDLE, RUN, JUMP, FALL, DAMAGE} var current_state: PossibleStates = PossibleStates.IDLE var prev_state: PossibleStates = PossibleStates.IDLE var upJump: bool = false var pushTarget var life: int = 100 func _physics_process(delta: float) -> void: handle_input() handle_movement(delta) handle_states() handle_animation() move_and_slide() handle_collisions() func handle_states(): match current_state: PossibleStates.IDLE: if velocity.x != 0 and is_on_floor(): current_state = PossibleStates.RUN if not is_on_floor() && velocity.y < 0: current_state = PossibleStates.JUMP PossibleStates.RUN: if velocity.x == 0 and is_on_floor(): current_state = PossibleStates.IDLE # player steps on ledge if not is_on_floor() && velocity.y > 0: current_state = PossibleStates.FALL if not is_on_floor() && velocity.y < 0: current_state = PossibleStates.JUMP PossibleStates.FALL when is_on_floor(): if velocity.x == 0: current_state = PossibleStates.IDLE else: current_state = PossibleStates.RUN PossibleStates.JUMP: if velocity.y > 0: current_state = PossibleStates.FALL func handle_animation(): match current_state: PossibleStates.IDLE: player_graphic.play("idle") PossibleStates.RUN: player_graphic.play("run") PossibleStates.JUMP: if upJump: player_graphic.play("jump") PossibleStates.FALL: player_graphic.play("fall") PossibleStates.DAMAGE: player_graphic.play("damage") func handle_movement(delta: float): if direction == 0: velocity.x = move_toward(velocity.x, 0, acceleration) else: velocity.x = move_toward(velocity.x, direction*SPEED, acceleration) # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta if is_on_floor() && jump_buffer.time_left > 0: velocity.y = JUMP_VELOCITY upJump = true jump_buffer.stop() func handle_input(): # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): jump_buffer.start() # 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("ui_left", "ui_right") if direction > 0: facing = FaceDirection.RIGHT player_graphic.flip_h = false elif direction < 0: facing = FaceDirection.LEFT player_graphic.flip_h = true var is_shoving = Input.is_action_just_pressed("shove") if is_shoving and pushTarget is RigidBody2D: var shoveDirection: int match facing: FaceDirection.RIGHT: shoveDirection = 1 FaceDirection.LEFT: shoveDirection = -1 pushTarget.apply_central_impulse(Vector2i(shoveDirection, 0)*SHOVE_STRENGTH) var is_shooting = Input.is_action_just_pressed("shoot") if is_shooting: match facing: FaceDirection.RIGHT: scene_manager.make_bullet(right_spawn.global_transform, SPEED) FaceDirection.LEFT: scene_manager.make_bullet(left_spawn.global_transform, -1*SPEED) func handle_collisions(): for i in get_slide_collision_count(): var collision := get_slide_collision(i) var collider := collision.get_collider() if collider is RigidBody2D: collider.apply_central_impulse(-collision.get_normal()*100) if right_cast.is_colliding() && facing == FaceDirection.RIGHT: collider = right_cast.get_collider() if collider is Node and collider.is_in_group("shovable"): pushTarget = collider elif left_cast.is_colliding() && facing == FaceDirection.LEFT: collider = left_cast.get_collider() if collider is Node and collider.is_in_group("shovable"): pushTarget = collider else: pushTarget = null func _on_animation_finished() -> void: if current_state == PossibleStates.JUMP: upJump = false if current_state == PossibleStates.DAMAGE: current_state = prev_state func hurt_player(): print("in player hurt") life -= 5 prev_state = current_state current_state = PossibleStates.DAMAGE