bovido/scripts/player/player.gd

33 lines
943 B
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name Player
extends CharacterBody2D
signal hit
signal ball_starts_colliding
signal ball_stops_colliding
const Y_SPAWN_OFFSET = -8
@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()
position = tile_map.map_to_local(spawn_cell) + Vector2(0, Y_SPAWN_OFFSET)
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()
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