AprilGodotGame/scripts/player.gd

168 lines
4.7 KiB
GDScript

class_name Player extends CharacterBody2D
const GRASS_RUNNING = preload("res://assets/sounds/Grass Running.wav")
@onready var audio_stream_player_2d: AudioStreamPlayer2D = $AudioStreamPlayer2D
@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 = 60.0
@export var shove_power = 600.0
@export var acceleration = 50
@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.LEFT
var direction: float = 0
var pushTarget: RigidBody2D
var pushEnable: bool = false
var upJump: bool = false
func _physics_process(delta: float) -> void:
# Add the gravity.
if current_state == State.JUMP:
velocity += get_gravity() * delta
else:
velocity += get_gravity() * hard_gravity * delta
# Handle input
handle_input()
update_movement()
# Handle states and animation
update_states()
update_animation()
# Move and handle player collisions
move_and_slide()
handle_collisions()
func handle_input() -> void:
if Input.is_action_just_pressed("shoot"):
print("Shoot")
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") and pushEnable:
current_state = State.MELEE
if facing == FaceDirection.LEFT:
print("Shove attack LEFT!!!")
pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power)
pushEnable = false
if facing == FaceDirection.RIGHT:
print("Shove attack RIGHT!!!")
pushTarget.apply_central_impulse(Vector2(1,0) * shove_power)
pushEnable = false
# Handle jump.
if Input.is_action_just_pressed("ui_accept"):
jump_buffer_timer.start()
if is_on_floor() and 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")
func update_movement() -> void:
if direction:
velocity.x = move_toward(velocity.x, SPEED*direction, acceleration)
#facing = FaceDirection.LEFT if direction < 0 else FaceDirection.RIGHT
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, acceleration)
func update_states() -> void:
match current_state:
#describe all the rules for the state
State.IDLE when velocity.x != 0:
current_state = State.RUN
State.RUN:
if velocity.x == 0:
current_state = State.IDLE
if !is_on_floor() and 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() and facing == FaceDirection.RIGHT:
print("Right raycast hitting something")
var collider = right_cast.get_collider()
if collider is Node and collider is RigidBody2D:
print("I think it's a crate")
pushTarget = collider
pushEnable = true
if left_cast.is_colliding() and facing == FaceDirection.LEFT:
print("Left raycast hitting something")
var collider = left_cast.get_collider()
if collider is Node and collider is RigidBody2D:
print("I think it's a crate")
pushTarget = collider
pushEnable = true
if not right_cast.is_colliding() and not left_cast.is_colliding():
pushEnable = false
func _on_animation_finished() -> void:
match current_state:
State.JUMP:
upJump = false
State.MELEE:
current_state = State.IDLE