ed-april-game/scripts/player.gd

54 lines
1.7 KiB
GDScript3
Raw Normal View History

2026-04-28 01:06:17 +00:00
class_name Player extends CharacterBody2D
@onready var raycast_left: RayCast2D = $raycastLeft
@onready var raycast_right: RayCast2D = $raycastRight
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var direction : float = 0
enum FaceDirection { LEFT, RIGHT }
var facing: FaceDirection = FaceDirection.LEFT
var shove_target: RigidBody2D
func _physics_process(delta: float):
handle_input()
handle_movement(delta)
move_and_slide() # pre-calc next position from velocity
handle_collisions()
func handle_input():
if Input.is_action_just_pressed("shove"):
if shove_target:
var shove_normal = 1 if facing == FaceDirection.RIGHT else -1
shove_target.apply_central_impulse(Vector2(shove_normal,0)*700)
if Input.is_action_just_pressed("jump") 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.
direction = Input.get_axis("left", "right")
if direction < 0:
facing = FaceDirection.LEFT
elif direction > 0:
facing = FaceDirection.RIGHT
func handle_movement(delta: float):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
func handle_collisions():
if raycast_right.is_colliding() and facing == FaceDirection.RIGHT:
shove_target = raycast_right.get_collider()
elif raycast_left.is_colliding() and facing == FaceDirection.LEFT:
shove_target = raycast_left.get_collider()
else:
shove_target = null
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)