2025-04-15 00:58:01 +00:00
|
|
|
class_name Player extends CharacterBody2D
|
2025-04-08 01:00:21 +00:00
|
|
|
|
2025-04-15 00:58:01 +00:00
|
|
|
@onready var right_cast: RayCast2D = $RightCast
|
|
|
|
@onready var left_cast: RayCast2D = $LeftCast
|
|
|
|
@onready var right_spawn: Node2D = $RightSpawn
|
|
|
|
@onready var left_spawn: Node2D = $LeftSpawn
|
2025-04-08 01:00:21 +00:00
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
|
2025-04-15 00:58:01 +00:00
|
|
|
@export var bump_power = 80
|
|
|
|
@export var shove_power = 500
|
|
|
|
|
|
|
|
enum FaceDirection{LEFT, RIGHT}
|
|
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
|
|
|
|
|
|
|
var pushTarget:RigidBody2D
|
|
|
|
var pushEnabled:bool = false
|
2025-04-08 01:00:21 +00:00
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
2025-04-15 00:58:01 +00:00
|
|
|
|
|
|
|
handle_input()
|
|
|
|
move_and_slide()
|
|
|
|
handle_collisions()
|
|
|
|
|
|
|
|
func handle_input() -> void:
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
|
|
print("Shoot a bullet")
|
|
|
|
if facing == FaceDirection.LEFT:
|
|
|
|
%SceneManager.makeBullet(left_spawn.global_transform, -700)
|
|
|
|
if facing == FaceDirection.RIGHT:
|
|
|
|
%SceneManager.makeBullet(right_spawn.global_transform, 700)
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("shove") && pushEnabled:
|
|
|
|
print("Shove attack!")
|
|
|
|
if facing == FaceDirection.LEFT:
|
|
|
|
print("facing left")
|
|
|
|
pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power)
|
|
|
|
pushEnabled = false
|
|
|
|
if facing == FaceDirection.RIGHT:
|
|
|
|
print("facing right")
|
|
|
|
pushTarget.apply_central_impulse(Vector2(1,0) * shove_power)
|
|
|
|
pushEnabled = false
|
2025-04-08 01:00:21 +00:00
|
|
|
# Handle jump.
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
|
|
var direction := Input.get_axis("ui_left", "ui_right")
|
|
|
|
if direction:
|
|
|
|
velocity.x = direction * SPEED
|
2025-04-15 00:58:01 +00:00
|
|
|
if direction <0:
|
|
|
|
facing = FaceDirection.LEFT
|
|
|
|
if direction > 0:
|
|
|
|
facing = FaceDirection.RIGHT
|
2025-04-08 01:00:21 +00:00
|
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
2025-04-15 00:58:01 +00:00
|
|
|
|
|
|
|
func update_movement()-> void:
|
|
|
|
pass
|
|
|
|
|
|
|
|
func update_states()->void:
|
|
|
|
pass
|
2025-04-08 01:00:21 +00:00
|
|
|
|
2025-04-15 00:58:01 +00:00
|
|
|
func handle_collisions()->void:
|
2025-04-08 01:00:21 +00:00
|
|
|
for i in get_slide_collision_count():
|
|
|
|
var c = get_slide_collision(i)
|
|
|
|
if c.get_collider() is RigidBody2D:
|
2025-04-15 00:58:01 +00:00
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * bump_power)
|
|
|
|
|
|
|
|
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
|
|
|
|
print("right raycast is hitting something")
|
|
|
|
var collider = right_cast.get_collider()
|
|
|
|
if collider is Node && collider is RigidBody2D:
|
|
|
|
print("I think it is a crate")
|
|
|
|
pushTarget = collider
|
|
|
|
pushEnabled = true
|
|
|
|
|
|
|
|
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
|
|
|
|
print("left raycast is hitting something")
|
|
|
|
var collider = left_cast.get_collider()
|
|
|
|
if collider is Node && collider is RigidBody2D:
|
|
|
|
print("I think it is a crate on the left")
|
|
|
|
pushTarget = collider
|
|
|
|
pushEnabled = true
|
|
|
|
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
|
|
|
pushEnabled = false
|