AprilGodotGame/scripts/player.gd

162 lines
4.5 KiB
GDScript3
Raw Normal View History

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:
2025-05-06 01:06:50 +00:00
handle_input()
2025-05-06 01:06:50 +00:00
update_movement(delta)
update_states()
update_animation()
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!")
current_state = State.MELEE
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
# Handle jump.
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.
direction = Input.get_axis("ui_left", "ui_right")
2025-05-06 01:06:50 +00:00
func update_movement(delta)-> void:
# Add the gravity.
if current_state == State.JUMP:
# apply normal gravity
velocity += get_gravity() * delta
else:
#apply hard gravity
velocity += get_gravity() * hard_gravity * delta
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:
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)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * bump_power)
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
var collider = right_cast.get_collider()
if collider is Node && collider is RigidBody2D:
pushTarget = collider
pushEnabled = true
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D:
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