extends CharacterBody2D const SPEED = 300.0 const JUMP_VELOCITY = -600.0 enum FaceDirection{LEFT, RIGHT} @export var BUMP_POWER = 50 @export var SHOVE_POWER = 200 var facing : FaceDirection = FaceDirection.RIGHT var direction : float = 0.0 var push_target : RigidBody2D var push_enabled : bool = false @onready var right_cast: RayCast2D = $RightCast @onready var left_cast: RayCast2D = $LeftCast @onready var right_spawn: Marker2D = $RightSpawn @onready var left_spawn: Marker2D = $LeftSpawn func _physics_process(delta: float) -> void: handle_input() handle_movement(delta) move_and_slide() handle_collisions() func handle_input() -> void: # Handle jumping. if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY direction = Input.get_axis("move_left", "move_right") if direction < 0: facing = FaceDirection.LEFT if direction > 0: facing = FaceDirection.RIGHT # Handle shoving. if Input.is_action_just_pressed("shove") and push_enabled == true: var shove_direction : int match facing: FaceDirection.RIGHT: shove_direction = 1 FaceDirection.LEFT: shove_direction = -1 push_target.apply_central_impulse(Vector2(shove_direction, 0) * SHOVE_POWER) # Handle shooting.eee if Input.is_action_just_pressed("shoot"): print("pew-pew") match facing: FaceDirection.RIGHT: %SceneManager.make_bullet(right_spawn.global_transform, 700) print("shoot right") FaceDirection.LEFT: %SceneManager.make_bullet(left_spawn.global_transform, -700) print("shoot left") if Input.is_action_just_pressed("throw"): print("grenade!") match facing: FaceDirection.RIGHT: %SceneManager.make_grenade(right_spawn.global_transform, 1.0) print("throwing right") FaceDirection.LEFT: %SceneManager.make_grenade(left_spawn.global_transform, -1.0) print("throwing left") func handle_movement(delta) -> void: # Left-right movement. if direction: velocity.x = direction * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) # Add gravity. if not is_on_floor(): velocity += get_gravity() * delta func handle_collisions() -> void: 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() * BUMP_POWER) if right_cast.is_colliding() and facing == FaceDirection.RIGHT: var collider = right_cast.get_collider() # check if this is OK if collider is Node and collider is RigidBody2D and collider.is_in_group("pushable"): push_target = collider push_enabled = true if left_cast.is_colliding() and facing == FaceDirection.LEFT: var collider = left_cast.get_collider() if collider is Node and collider is RigidBody2D and collider.is_in_group("pushable"): push_target = collider push_enabled = true if not right_cast.is_colliding() and not left_cast.is_colliding(): push_enabled = false