bovido/scripts/player/player.gd

43 lines
1.1 KiB
GDScript3
Raw Normal View History

class_name Player
extends CharacterBody2D
2023-07-01 13:29:18 +02:00
const y_spawn_offset = -8
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
@export var speed = 120
@onready var tile_map: TileMap = get_parent()
func _ready():
2023-07-01 13:29:18 +02:00
var spawn_cell = tile_map.get_bottom_spawn_cell()
position = tile_map.map_to_local(spawn_cell) + Vector2(0, y_spawn_offset)
2023-06-28 00:07:13 +02:00
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"):
2023-07-02 17:23:23 +02:00
$Sprite2D.flip_h = true
2023-06-28 00:07:13 +02:00
elif Input.is_action_pressed("move_right"):
2023-07-02 17:23:23 +02:00
$Sprite2D.flip_h = false
2023-06-28 21:29:55 +02:00
func play_idle_animation():
2023-07-02 17:23:23 +02:00
$AnimationPlayer.play("idle")
2023-06-28 21:29:55 +02:00
func play_walk_animation():
2023-07-02 17:23:23 +02:00
$AnimationPlayer.play("walk")
2023-06-28 21:29:55 +02:00
func play_hit_ball_animation():
2023-07-02 17:23:23 +02:00
$AnimationPlayer.speed_scale = 0.8
$AnimationPlayer.play("hit_ball")
await $AnimationPlayer.animation_finished
$AnimationPlayer.speed_scale = 1
2023-07-01 13:29:18 +02:00
func _on_area_2d_body_entered(_body):
# As players Area2D only collide with balls
# We only enter this function after colliding with a ball
ball_starts_colliding.emit()
func _on_area_2d_body_exited(_body):
ball_stops_colliding.emit()