class_name Player extends CharacterBody2D @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: Timer = $jumpBufferTimer @onready var player_graphic: AnimatedSprite2D = $PlayerGraphic const SPEED = 300 const JUMP_VELOCITY = -400.0 @export var acceleration:int = 5 var upJump:bool = false var direction enum FaceDirection{LEFT, RIGHT} var facing:FaceDirection = FaceDirection.RIGHT enum State{IDLE,RUN,JUMP,FALLING} var current_state:State = State.IDLE var pushTarget var pushEnabled := false func _physics_process(delta:float) -> void: # Add the gravity. handle_input() handle_movement(delta) handle_states() handle_animation() move_and_slide() handle_collisions() func handle_states() -> void: match current_state: State.IDLE when velocity.x !=0: current_state = State.RUN State.RUN: if velocity.x == 0: current_state = State.IDLE if not is_on_floor() && velocity.y > 0: current_state = State.FALLING State.FALLING when is_on_floor(): if velocity.x == 0: current_state = State.IDLE else: current_state = State.RUN State.JUMP when velocity.y >0: current_state = State.FALLING func handle_animation() -> void: match current_state: State.IDLE: player_graphic.play("idle") State.RUN: player_graphic.play("run") State.FALLING: player_graphic.play("fall") State.JUMP: if upJump: player_graphic.play("jump") func handle_movement(delta:float) -> void: if direction == 0: #not moving velocity.x = move_toward(velocity.x,0, acceleration) else: velocity.x = move_toward(velocity.x,SPEED * direction, acceleration) if not is_on_floor(): velocity += get_gravity() * delta if is_on_floor() && jump_buffer_timer.time_left >0: velocity.y = JUMP_VELOCITY jump_buffer_timer.stop() current_state = State.JUMP upJump = true func handle_input(): if Input.is_action_just_pressed("ui_accept") and is_on_floor(): #velocity.y = JUMP_VELOCITY jump_buffer_timer.start() direction = Input.get_axis("ui_left", "ui_right") if direction <0: facing =FaceDirection.LEFT player_graphic.flip_h = true if direction > 0: facing = FaceDirection.RIGHT player_graphic.flip_h = false if Input.is_action_just_pressed("shove") && pushEnabled: var shoveDirection:int match facing: FaceDirection.RIGHT: #this is where tabbing does'nt work shoveDirection = 1 FaceDirection.LEFT: shoveDirection = -1 pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700) if Input.is_action_just_pressed("shoot"): match facing: FaceDirection.RIGHT: %SceneManager.makeBullet(right_spawn.global_transform,700) FaceDirection.LEFT: %SceneManager.makeBullet(left_spawn.global_transform,-700) func handle_collisions(): 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) # i? if right_cast.is_colliding() && facing == FaceDirection.RIGHT: var collider = right_cast.get_collider() if collider is Node && collider is RigidBody2D && collider.is_in_group("pushables"): pushTarget = collider pushEnabled = true if left_cast.is_colliding() && facing == FaceDirection.LEFT: var collider = left_cast.get_collider() if collider is Node && collider is RigidBody2D && collider.is_in_group("pushables"): pushTarget = collider pushEnabled = true if not right_cast.is_colliding() && not left_cast.is_colliding(): pushEnabled = false func _on_animation_finished() -> void: match current_state: State.JUMP: upJump = false