player state
This commit is contained in:
1 parent
5fb367e082
commit
2e494502a8
8 files changed
+361
-7
No files matched your search
+59
-7
@@ -6,6 +6,7 @@ signal fireSignal(markerPosition, direction, speed)
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Marker2D = $RightSpawn
|
||||
@onready var left_spawn: Marker2D = $LeftSpawn
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
@@ -17,15 +18,28 @@ const JUMP_VELOCITY = -400.0
|
||||
|
||||
enum FaceDirection {RIGHT,LEFT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
#State Handling
|
||||
enum PlayerState {IDLE, RUNNING, JUMPING, FALLING}
|
||||
var current_player_state:PlayerState = PlayerState.IDLE
|
||||
var upJump:bool = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
#States and Animation
|
||||
handle_update_states()
|
||||
handle_update_animation()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
|
||||
func handle_input()->void:
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
current_player_state = PlayerState.JUMPING
|
||||
upJump = true
|
||||
# Handle shove.
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
print("I want to shove")
|
||||
@@ -47,7 +61,7 @@ func _physics_process(delta: float) -> void:
|
||||
FaceDirection.RIGHT:
|
||||
shootSignal.emit(right_spawn.global_transform, 800)
|
||||
FaceDirection.LEFT:
|
||||
shootSignal.emit(left_spawn.global_transform, 800)
|
||||
shootSignal.emit(left_spawn.global_transform, -800)
|
||||
#Handle fire rocket
|
||||
if Input.is_action_just_pressed("fire"):
|
||||
print("Ima fire rocket")
|
||||
@@ -63,12 +77,17 @@ func _physics_process(delta: float) -> void:
|
||||
velocity.x = direction * SPEED
|
||||
if direction >0:
|
||||
facing = FaceDirection.RIGHT
|
||||
player_graphic.flip_h = false
|
||||
else:
|
||||
facing = FaceDirection.LEFT
|
||||
player_graphic.flip_h = true
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
func handle_movement(delta)->void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
func handle_collisions()->void:
|
||||
if right_cast.is_colliding():
|
||||
print("Right raycast collision")
|
||||
var collider = right_cast.get_collider()
|
||||
@@ -94,3 +113,36 @@ func _physics_process(delta: float) -> void:
|
||||
if c.get_collider() is RigidBody2D:
|
||||
#c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
||||
pushSignal.emit(c.get_collider(), -c.get_normal() * 100)
|
||||
|
||||
#New functions for state handling
|
||||
func handle_update_states()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE when velocity.x !=0:
|
||||
current_player_state = PlayerState.RUNNING
|
||||
PlayerState.RUNNING when velocity.x == 0:
|
||||
current_player_state = PlayerState.IDLE
|
||||
PlayerState.JUMPING when velocity.y>0:
|
||||
current_player_state = PlayerState.FALLING
|
||||
PlayerState.FALLING when is_on_floor():
|
||||
if velocity.x == 0:
|
||||
current_player_state = PlayerState.IDLE
|
||||
else:
|
||||
current_player_state = PlayerState.RUNNING
|
||||
|
||||
func handle_update_animation()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE:
|
||||
player_graphic.play("idle")
|
||||
PlayerState.RUNNING:
|
||||
player_graphic.play("running")
|
||||
PlayerState.JUMPING:
|
||||
if upJump:
|
||||
player_graphic.play("jumping")
|
||||
PlayerState.FALLING:
|
||||
player_graphic.play("falling")
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_player_state:
|
||||
PlayerState.JUMPING:
|
||||
upJump = false
|
||||
Reference in new issue
Block a user