Clean up player scripts

This commit is contained in:
Mathilde Grapin 2023-06-28 21:29:55 +02:00
parent a1e8f9b34d
commit a08afcb147
7 changed files with 75 additions and 75 deletions

View file

@ -11,12 +11,12 @@ func _ready():
target.y += Y_OFFSET
var player = get_node("/root/Main/TileMap/Player")
assert(player)
player.hit.connect(_on_player_hit)
player.hit_ball.connect(_on_player_hit_ball)
func _physics_process(delta):
position = position.move_toward(target, delta * speed)
func _on_player_hit():
func _on_player_hit_ball():
var rand_cell: Vector2i = tile_map.get_random_top_cell()
tile_map.reset_and_set_target_cell(rand_cell)
target = tile_map.map_to_local(rand_cell) + Vector2(0, Y_OFFSET)

View file

@ -1,7 +1,7 @@
class_name Player
extends CharacterBody2D
signal hit
signal hit_ball
signal ball_starts_colliding
signal ball_stops_colliding
const Y_SPAWN_OFFSET = -8
@ -30,3 +30,15 @@ func flip_sprite():
sprite.flip_h = true
elif Input.is_action_pressed("move_right"):
sprite.flip_h = false
func play_idle_animation():
animation_player.play("idle")
func play_walk_animation():
animation_player.play("walk")
func play_hit_ball_animation():
animation_player.speed_scale = 0.8
animation_player.play("hit_ball")
await animation_player.animation_finished
animation_player.speed_scale = 1

View file

@ -0,0 +1,7 @@
class_name PlayerHitBallState
extends PlayerState
func enter(_msg := {}):
player.hit_ball.emit()
await player.play_hit_ball_animation()
state_machine.transition_to("Idle")

View file

@ -1,20 +0,0 @@
class_name PlayerThrowState
extends PlayerState
var is_animation_finished = false
func enter(_msg := {}):
player.animation_player.speed_scale = 0.8
player.animation_player.play("throw")
player.hit.emit()
func update(_delta: float):
if is_animation_finished:
state_machine.transition_to("Idle")
func exit():
is_animation_finished = false
player.animation_player.speed_scale = 1
func _on_animation_player_animation_finished(_anim_name):
is_animation_finished = true

View file

@ -5,11 +5,11 @@ var player_collide_with_ball = false
func enter(_msg := {}):
player.velocity = Vector2.ZERO
player.animation_player.play("idle")
player.play_idle_animation()
func update(_delta):
if player_collide_with_ball && Input.is_action_pressed("hit"):
state_machine.transition_to("Hit")
state_machine.transition_to("HitBall")
if player.get_input_direction() != Vector2.ZERO:
state_machine.transition_to("Walk")

View file

@ -2,17 +2,17 @@ class_name PlayerWalkState
extends PlayerState
func enter(_msg := {}):
player.animation_player.play("walk")
player.play_walk_animation()
func physics_update(delta):
var direction = player.get_input_direction()
var velocity = direction * player.speed
player.flip_sprite()
var collision = player.move_and_collide(velocity * delta)
if collision and Input.is_action_pressed("hit"):
state_machine.transition_to("Hit")
state_machine.transition_to("HitBall")
if velocity == Vector2.ZERO:
state_machine.transition_to("Idle")