class_name Player extends CharacterBody2D const SPEED = 300.0 const JUMP_VELOCITY = -400.0 @onready var right_spawn: Marker2D = $RightSpawn @onready var left_spawn: Marker2D = $LeftSpawn @onready var player_graphic: AnimatedSprite2D = $PlayerGraphic var direction enum FaceDirection{LEFT,RIGHT} var facing:FaceDirection = FaceDirection.RIGHT enum PlayerState{IDLE,RUNNING,JUMPING,FALLING} var current_player_state:PlayerState = PlayerState.IDLE var upjump:bool = false func _physics_process(delta: float) -> void: handle_input() handle_state() handle_animation() # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. if direction: velocity.x = direction * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) move_and_slide() for i in get_slide_collision_count(): var c = get_slide_collision(i) if c.get_collider() is RigidBody2D: #deliver the impact c.get_collider().apply_central_impulse(-c.get_normal() * 100) func handle_input()->void: if Input.is_action_just_pressed("Chuck"): print("chuck a grenade") %SceneManager.makeGrenade(right_spawn.global_transform, 1) if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY upjump = true current_player_state = PlayerState.JUMPING # 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 player_graphic.flip_h = true if direction>0: facing = FaceDirection.RIGHT player_graphic.flip_h = false func handle_state()->void: match current_player_state: PlayerState.IDLE when velocity.x !=0: #change to running current_player_state = PlayerState.RUNNING PlayerState.RUNNING when velocity.x ==0: current_player_state = PlayerState.IDLE PlayerState.JUMPING when velocity.y > 0: current_player_state = PlayerState.FALLING PlayerState.FALLING when is_on_floor(): if velocity.x ==0: current_player_state = PlayerState.IDLE else: current_player_state=PlayerState.RUNNING func handle_animation()->void: match current_player_state: PlayerState.IDLE: player_graphic.play("idle") PlayerState.RUNNING: player_graphic.play("run") PlayerState.JUMPING: if upjump: player_graphic.play("jump") PlayerState.FALLING: player_graphic.play("fall") func _on_animation_finished() -> void: match current_player_state: PlayerState.JUMPING: upjump = false