GodotGameWorkshop/Scripts/player.gd
2025-12-15 19:10:59 -05:00

234 lines
6.8 KiB
GDScript

class_name Player extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@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 player_graphic: AnimatedSprite2D = $PlayerGraphic
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
enum State{IDLE, RUN, JUMP, FALLING, HURT, DEATH}
var current_state:State = State.IDLE
var pushTarget
var pushEnabled = false
var direction
var upJump = false
var gravity := ProjectSettings.get_setting("physics/2d/default_gravity") as float
var can_mount_stairs := false
var is_on_stairs := false
var stairs_z_above := 0 # filled by stairs trigger
var stairs_z_below := -1 # filled by stairs trigger
var stairs_node: Node = null
signal deathAnimationCompleteSignal
func _physics_process(delta: float) -> void:
#game loop
handle_input()
#calculate the movement
# --- STAIRS OPT-IN LOGIC ---
if can_mount_stairs and not is_on_stairs:
print("Player: inside stairs trigger, waiting for up/down")
if Input.is_action_just_pressed("ui_up"):
print("Player: ui_up pressed")
if Input.is_action_just_pressed("ui_down"):
print("Player: ui_down pressed")
if Input.is_action_just_pressed("ui_up") or Input.is_action_just_pressed("ui_down"):
print("Player: START stairs mode requested")
_start_stairs_mode()
# ---------------------------
handle_movement(delta)
#change states
update_states()
#play animations
update_animation()
#collision with objects, raycasts
# Gravity
if not is_on_floor():
velocity.y += gravity * delta
move_and_slide()
handle_collisions()
func update_states():
match current_state:
#idle when movement in x
State.IDLE when velocity.x !=0:
current_state = State.RUN
State.RUN:
if velocity.x ==0:
current_state = State.IDLE
#jumping when reaching apex
State.JUMP when 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
func update_animation():
match current_state:
State.IDLE:
player_graphic.play("idle")
State.RUN:
player_graphic.play("run")
State.JUMP:
if upJump:
player_graphic.play("jump")
State.FALLING:
player_graphic.play("falling")
State.HURT:
player_graphic.play("hurt")
State.DEATH:
player_graphic.play("death")
func handle_movement(delta):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if direction:
velocity.x = direction * SPEED
if direction <0:
facing = FaceDirection.LEFT
player_graphic.flip_h = true
if direction >0:
facing = FaceDirection.RIGHT
player_graphic.flip_h = false
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
func handle_input():
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
current_state = State.JUMP
upJump = true
if Input.is_action_just_pressed("shove") && pushEnabled:
print("shove pressed")
if facing == FaceDirection.RIGHT:
pushTarget.apply_central_impulse(Vector2(1,0)*700)
pushEnabled = false
if facing == FaceDirection.LEFT:
pushTarget.apply_central_impulse(Vector2(-1,0)*700)
pushEnabled = false
if Input.is_action_just_pressed("shoot"):
print("shoot a bullet")
if facing == FaceDirection.RIGHT:
%SceneManager.makeBullet(right_spawn.global_transform, 700)
if facing == FaceDirection.LEFT:
%SceneManager.makeBullet(left_spawn.global_transform, -700)
# 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("ui_left", "ui_right")
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)
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
#get the thing I am colliding with
var collider = right_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print("I can shove this to the right")
pushTarget = collider
pushEnabled = true
if not right_cast.is_colliding() && not left_cast.is_colliding():
pushEnabled = false
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print("I can shove this to the left")
pushTarget = collider
pushEnabled = true
if not left_cast.is_colliding() && not right_cast.is_colliding():
pushEnabled = false
const STAIRS_LAYER := 3 # correct layer number for "stairs"
func _ready() -> void:
_set_stair_collision(false)
# Allow climbing up steeper slopes (default is ~45°)
floor_max_angle = deg_to_rad(60) # you can increase or decrease as needed
func _set_stair_collision(enabled: bool) -> void:
print("[Player] set stair collision to:", enabled)
set_collision_mask_value(STAIRS_LAYER, enabled)
func _start_stairs_mode() -> void:
print("[Player] _start_stairs_mode")
is_on_stairs = true
_set_stair_collision(true)
# When "on" the stairs, draw above (or tune based on your art)
if stairs_node and stairs_node.has_method("get_above_z_index"):
z_index = stairs_node.get_above_z_index()
else:
z_index = 5 # fallback
func _end_stairs_mode() -> void:
print("[Player] _end_stairs_mode")
is_on_stairs = false
_set_stair_collision(false)
# When not on stairs, draw behind (to appear "behind" stair art if overlapping)
if stairs_node and stairs_node.has_method("get_below_z_index"):
z_index = stairs_node.get_below_z_index()
else:
z_index = 0 # fallback
stairs_node = null
# Called by the stairs trigger via signals:
func on_stairs_trigger_enter(stairs: Node, above_z: int, below_z: int) -> void:
print("[Player] on_stairs_trigger_enter from:", stairs.name)
can_mount_stairs = true
stairs_node = stairs
stairs_z_above = above_z
stairs_z_below = below_z
# By default when *not* mounted, draw behind:
z_index = below_z
func on_stairs_trigger_exit(stairs: Node) -> void:
print("[Player] on_stairs_trigger_exit from:", stairs.name)
if stairs_node == stairs:
can_mount_stairs = false
func on_stairs_top_reached(stairs: Node) -> void:
print("[Player] on_stairs_top_reached from:", stairs.name)
if stairs_node == stairs and is_on_stairs:
_end_stairs_mode()
func _on_animation_finished() -> void:
match current_state:
State.JUMP:
upJump = false
State.HURT:
current_state = State.IDLE
State.DEATH:
deathAnimationCompleteSignal.emit()
func playerTakesDamage(health):
print("Player sees remaining health "+str(health))
current_state = State.HURT
func playerDies():
print("Player sees they died.")
current_state = State.DEATH