added player states and animations

This commit is contained in:
christophe committed 2025-08-18 21:06:02 -04:00
1 parent 0968ee75ae
commit 2461fd278a
16 files changed
+554 -14

No files matched your search

+7 -1
View File
@@ -1,5 +1,9 @@
extends Node2D
@onready var player: Player = $player
signal playerDamage
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
@@ -12,8 +16,10 @@ func _process(delta: float) -> void:
func _on_trigger(effect: Variant, body: Variant) -> void:
print("game controller sees trigger "+effect+" "+body.name)
if not body is Player:
if effect == "destroy" && not body is Player:
body.queue_free()
elif effect == "player_hurt":
playerDamage.emit()
func onBulletHitSignal(body: Node, bullet: Bullet):
body.queue_free()
+76 -5
View File
@@ -5,40 +5,97 @@ class_name Player extends CharacterBody2D
@onready var left_cast: RayCast2D = $LeftCast
@onready var right_spawn: Node2D = $RightSpawn
@onready var left_spawn: Node2D = $LeftSpawn
@onready var jump_buffer: Timer = $JumpBuffer
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
const SHOVE_STRENGTH = 700
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const JUMP_VELOCITY = -450.0
var acceleration: int = 50
var direction: float
enum FaceDirection{LEFT, RIGHT}
var facing: FaceDirection = FaceDirection.RIGHT
enum PossibleStates{IDLE, RUN, JUMP, FALL, DAMAGE}
var current_state: PossibleStates = PossibleStates.IDLE
var prev_state: PossibleStates = PossibleStates.IDLE
var upJump: bool = false
var pushTarget
var life: int = 100
func _physics_process(delta: float) -> void:
handle_input()
handle_movement(delta)
handle_states()
handle_animation()
move_and_slide()
handle_collisions()
func handle_states():
match current_state:
PossibleStates.IDLE:
if velocity.x != 0 and is_on_floor():
current_state = PossibleStates.RUN
if not is_on_floor() && velocity.y < 0:
current_state = PossibleStates.JUMP
PossibleStates.RUN:
if velocity.x == 0 and is_on_floor():
current_state = PossibleStates.IDLE
# player steps on ledge
if not is_on_floor() && velocity.y > 0:
current_state = PossibleStates.FALL
if not is_on_floor() && velocity.y < 0:
current_state = PossibleStates.JUMP
PossibleStates.FALL when is_on_floor():
if velocity.x == 0:
current_state = PossibleStates.IDLE
else:
current_state = PossibleStates.RUN
PossibleStates.JUMP:
if velocity.y > 0:
current_state = PossibleStates.FALL
func handle_animation():
match current_state:
PossibleStates.IDLE:
player_graphic.play("idle")
PossibleStates.RUN:
player_graphic.play("run")
PossibleStates.JUMP:
if upJump:
player_graphic.play("jump")
PossibleStates.FALL:
player_graphic.play("fall")
PossibleStates.DAMAGE:
player_graphic.play("damage")
func handle_movement(delta: float):
if direction:
velocity.x = direction * SPEED
if direction == 0:
velocity.x = move_toward(velocity.x, 0, acceleration)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.x = move_toward(velocity.x, direction*SPEED, acceleration)
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if is_on_floor() && jump_buffer.time_left > 0:
velocity.y = JUMP_VELOCITY
upJump = true
jump_buffer.stop()
func handle_input():
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
jump_buffer.start()
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
@@ -46,8 +103,10 @@ func handle_input():
if direction > 0:
facing = FaceDirection.RIGHT
player_graphic.flip_h = false
elif direction < 0:
facing = FaceDirection.LEFT
player_graphic.flip_h = true
var is_shoving = Input.is_action_just_pressed("shove")
if is_shoving and pushTarget is RigidBody2D:
@@ -87,3 +146,15 @@ func handle_collisions():
pushTarget = null
func _on_animation_finished() -> void:
if current_state == PossibleStates.JUMP:
upJump = false
if current_state == PossibleStates.DAMAGE:
current_state = prev_state
func hurt_player():
print("in player hurt")
life -= 5
prev_state = current_state
current_state = PossibleStates.DAMAGE
+7
View File
@@ -3,6 +3,7 @@ class_name SceneManager extends Node2D
const bullet_scene = preload("res://scenes/bullet.tscn")
@onready var game: Node2D = $".."
@onready var triggers: Node2D = $"../triggers"
var MAX_BULLET := 50
var available_bullets = []
@@ -12,6 +13,12 @@ func _ready() -> void:
var new_bullet = bullet_scene.instantiate()
new_bullet.bulletHitSignal.connect(game.onBulletHitSignal)
available_bullets.push_front(new_bullet)
if triggers:
for trig in triggers.get_children():
if trig is Trigger:
trig.areaTriggerSignal.connect(game._on_trigger)
func make_bullet(start_position: Transform2D, speed: int):
var new_bullet = bullet_factory(start_position, speed)
+1 -1
View File
@@ -1,4 +1,4 @@
extends Area2D
class_name Trigger extends Area2D
@export var effect:String