Week 3: input buffer, acceleration, hardgravity, state machine
This commit is contained in:
1 parent
e3c11f0b4c
commit
9db4deba05
7 files changed
+407
-17
No files matched your search
+85
-10
@@ -1,29 +1,49 @@
|
||||
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 not is_on_floor():
|
||||
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()
|
||||
@@ -38,6 +58,7 @@ func handle_input() -> void:
|
||||
%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)
|
||||
@@ -48,23 +69,69 @@ func handle_input() -> void:
|
||||
pushEnable = 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() 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.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
facing = FaceDirection.LEFT if direction < 0 else 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)
|
||||
#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:
|
||||
pass
|
||||
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():
|
||||
@@ -90,3 +157,11 @@ func handle_collisions() -> void:
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user