2023-06-11 23:38:54 +02:00
|
|
|
|
class_name Player
|
|
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
2023-06-28 21:29:55 +02:00
|
|
|
|
signal hit_ball
|
2023-06-28 00:07:13 +02:00
|
|
|
|
signal ball_starts_colliding
|
|
|
|
|
signal ball_stops_colliding
|
|
|
|
|
const Y_SPAWN_OFFSET = -8
|
2023-06-11 23:38:54 +02:00
|
|
|
|
@export var speed = 120
|
|
|
|
|
@onready var animation_player = $AnimationPlayer
|
|
|
|
|
@onready var sprite = $Sprite2D
|
|
|
|
|
@onready var tile_map: TileMap = get_parent()
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
|
var spawn_cell: Vector2i = tile_map.get_bottom_spawn_cell()
|
2023-06-28 00:07:13 +02:00
|
|
|
|
position = tile_map.map_to_local(spawn_cell) + Vector2(0, Y_SPAWN_OFFSET)
|
2023-06-18 20:30:25 +02:00
|
|
|
|
|
2023-06-28 00:07:13 +02:00
|
|
|
|
func _on_area_2d_body_entered(_body):
|
2023-06-18 20:30:25 +02:00
|
|
|
|
# As player’s Area2D only collide with balls
|
|
|
|
|
# We only enter this function after colliding with a ball
|
2023-06-28 00:07:13 +02:00
|
|
|
|
ball_starts_colliding.emit()
|
|
|
|
|
|
|
|
|
|
func _on_area_2d_body_exited(_body):
|
|
|
|
|
ball_stops_colliding.emit()
|
|
|
|
|
|
|
|
|
|
func get_input_direction():
|
|
|
|
|
return Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
|
|
|
|
|
|
|
|
|
func flip_sprite():
|
|
|
|
|
if Input.is_action_pressed("move_left"):
|
|
|
|
|
sprite.flip_h = true
|
|
|
|
|
elif Input.is_action_pressed("move_right"):
|
|
|
|
|
sprite.flip_h = false
|
2023-06-28 21:29:55 +02:00
|
|
|
|
|
|
|
|
|
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
|