state machine, player rendering, animations, static images, sound

This commit is contained in:
OddlyTimbot committed 2025-04-28 20:58:07 -04:00
1 parent 6af1257021
commit 5e6d16f8b5
24 files changed
+673 -29

No files matched your search

+5
View File
@@ -1,10 +1,15 @@
class_name Bullet extends Area2D
var speed = 700
@onready var bullet_image: Sprite2D = $bulletImage
signal bulletHit(body, bullet)
func setSpeed(value):
speed = value
if speed <0:
bullet_image.flip_h = true
if speed >0:
bullet_image.flip_h = false
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
+2 -2
View File
@@ -11,10 +11,10 @@ func _ready() -> void:
timer.connect("timeout", secondCounter)
timer.start()
func secondCounter():
print("tick")
timeLimit -=1
if timeLimit <= 0:
print("you lose baby")
get_tree().reload_current_scene()
# Called every frame. 'delta' is the elapsed time since the previous frame.
+86 -19
View File
@@ -1,29 +1,47 @@
class_name Player extends CharacterBody2D
@onready var audio_stream_player_2d: AudioStreamPlayer2D = $AudioStreamPlayer2D
const GRASS_RUNNING = preload("res://assets/sounds/Grass Running(1).wav")
@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 jump_buffer_timer: Timer = $JumpBufferTimer
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@export var bump_power = 80
@export var shove_power = 500
@export var acceleration = 15
@export var hard_gravity = 5
enum State{IDLE, RUN, JUMP, FALLING, MELEE}
var current_state = State.IDLE
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
var direction:float = 0
var pushTarget:RigidBody2D
var pushEnabled:bool = false
var upJump:bool = false
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
if current_state == State.JUMP:
# apply normal gravity
velocity += get_gravity() * delta
else:
#apply hard gravity
velocity += get_gravity() * hard_gravity * delta
handle_input()
update_movement()
update_states()
update_animation()
move_and_slide()
handle_collisions()
@@ -37,6 +55,7 @@ func handle_input() -> void:
if Input.is_action_just_pressed("shove") && pushEnabled:
print("Shove attack!")
current_state = State.MELEE
if facing == FaceDirection.LEFT:
print("facing left")
pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power)
@@ -46,27 +65,69 @@ func handle_input() -> void:
pushTarget.apply_central_impulse(Vector2(1,0) * shove_power)
pushEnabled = false
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
if Input.is_action_just_pressed("ui_accept"):
jump_buffer_timer.start()
if is_on_floor() && jump_buffer_timer.time_left >0:
velocity.y = JUMP_VELOCITY
current_state = State.JUMP
upJump = true
jump_buffer_timer.stop()
# 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
if direction <0:
facing = FaceDirection.LEFT
if direction > 0:
facing = FaceDirection.RIGHT
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
direction = Input.get_axis("ui_left", "ui_right")
func update_movement()-> void:
pass
if direction:
velocity.x = move_toward(velocity.x, SPEED * direction, acceleration)
if direction <0:
facing = FaceDirection.LEFT
player_graphic.flip_h = true
if direction > 0:
player_graphic.flip_h = false
facing = FaceDirection.RIGHT
else:
velocity.x = move_toward(velocity.x, 0, acceleration)
func update_states()->void:
pass
match current_state:
#describe all the rules state
State.IDLE when velocity.x !=0:
current_state = State.RUN
#player stops running
State.RUN:
if velocity.x == 0:
current_state = State.IDLE
if not is_on_floor() && velocity.y > 0:
current_state = State.FALLING
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()->void:
match current_state:
State.IDLE:
player_graphic.play("idle")
State.RUN:
player_graphic.play("run")
if not audio_stream_player_2d.playing:
audio_stream_player_2d.stream=GRASS_RUNNING
audio_stream_player_2d.playing = true
State.JUMP:
if upJump:
player_graphic.play("jump")
State.FALLING:
player_graphic.play("fall")
State.MELEE:
player_graphic.play("melee")
func handle_collisions()->void:
for i in get_slide_collision_count():
var c = get_slide_collision(i)
@@ -74,19 +135,25 @@ func handle_collisions()->void:
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
func _on_animation_finished() -> void:
match current_state:
State.JUMP:
upJump = false
State.MELEE:
current_state = State.IDLE