Player throw back the ball from walk or idle state
This commit is contained in:
parent
1aff1c2e38
commit
332c0fb0e1
16 changed files with 165 additions and 25 deletions
|
@ -1,6 +1,8 @@
|
|||
class_name Player
|
||||
extends CharacterBody2D
|
||||
|
||||
signal hit
|
||||
signal collide_with_ball
|
||||
@export var speed = 120
|
||||
var y_spawn_offset = -8
|
||||
@onready var animation_player = $AnimationPlayer
|
||||
|
@ -11,3 +13,9 @@ func _ready():
|
|||
var spawn_cell: Vector2i = tile_map.get_bottom_spawn_cell()
|
||||
position = tile_map.map_to_local(spawn_cell)
|
||||
position.y += y_spawn_offset
|
||||
|
||||
|
||||
func _on_area_2d_body_entered(body: Node2D):
|
||||
# As player’s Area2D only collide with balls
|
||||
# We only enter this function after colliding with a ball
|
||||
collide_with_ball.emit()
|
||||
|
|
|
@ -1,13 +1,23 @@
|
|||
class_name PlayerIdleState
|
||||
extends PlayerState
|
||||
|
||||
var collide_with_ball = false
|
||||
|
||||
func enter(_msg := {}):
|
||||
player.velocity = Vector2.ZERO
|
||||
player.animation_player.play("idle")
|
||||
|
||||
func update(_delta):
|
||||
if collide_with_ball && Input.is_action_pressed("hit"):
|
||||
collide_with_ball = false
|
||||
state_machine.transition_to("Throw")
|
||||
|
||||
if get_input_direction() != Vector2.ZERO:
|
||||
state_machine.transition_to("Walk")
|
||||
|
||||
func get_input_direction():
|
||||
return Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
||||
|
||||
|
||||
func _on_player_collide_with_ball():
|
||||
collide_with_ball = true
|
||||
|
|
20
scripts/player/states/player_throw_state.gd
Normal file
20
scripts/player/states/player_throw_state.gd
Normal file
|
@ -0,0 +1,20 @@
|
|||
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
|
|
@ -4,13 +4,15 @@ extends PlayerState
|
|||
func enter(_msg := {}):
|
||||
player.animation_player.play("walk")
|
||||
|
||||
func physics_update(_delta):
|
||||
func physics_update(delta):
|
||||
var direction = get_input_direction()
|
||||
player.velocity = direction * player.speed
|
||||
var velocity = direction * player.speed
|
||||
|
||||
var collision = player.move_and_collide(velocity * delta)
|
||||
if collision and Input.is_action_pressed("hit"):
|
||||
state_machine.transition_to("Throw")
|
||||
|
||||
player.move_and_slide() # This method calculate with delta, we don't need to do it.
|
||||
|
||||
if player.velocity == Vector2.ZERO:
|
||||
if velocity == Vector2.ZERO:
|
||||
state_machine.transition_to("Idle")
|
||||
|
||||
func get_input_direction():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue