62 lines
1.3 KiB
GDScript3
62 lines
1.3 KiB
GDScript3
|
extends Area2D
|
||
|
class_name Player
|
||
|
|
||
|
@export var speed := 400.
|
||
|
var _screen_size: Vector2
|
||
|
var _sprite: AnimatedSprite2D
|
||
|
var _shape: CollisionShape2D
|
||
|
|
||
|
func start(starting_position: Vector2) -> void :
|
||
|
position = starting_position
|
||
|
show()
|
||
|
_shape.disabled = false
|
||
|
pass
|
||
|
|
||
|
|
||
|
func _ready() -> void:
|
||
|
hide()
|
||
|
_screen_size = get_viewport_rect().size
|
||
|
_sprite = $Sprite as AnimatedSprite2D
|
||
|
_shape = $Shape as CollisionShape2D
|
||
|
pass # Replace with function body.
|
||
|
|
||
|
func _process(delta: float) -> void:
|
||
|
var velocity := Vector2.ZERO
|
||
|
|
||
|
if Input.is_action_pressed("move_right") :
|
||
|
velocity.x += 1
|
||
|
if Input.is_action_pressed("move_left") :
|
||
|
velocity.x -= 1
|
||
|
if Input.is_action_pressed("move_down"):
|
||
|
velocity.y += 1
|
||
|
if Input.is_action_pressed("move_up") :
|
||
|
velocity.y -= 1
|
||
|
|
||
|
if velocity.length() > 0 :
|
||
|
velocity = velocity.normalized() * speed
|
||
|
_sprite.play()
|
||
|
else:
|
||
|
_sprite.stop()
|
||
|
|
||
|
position += velocity * delta
|
||
|
|
||
|
# Make it impossible to leave the screen.
|
||
|
position = position.clamp(Vector2.ZERO, _screen_size)
|
||
|
|
||
|
if velocity.x != 0 :
|
||
|
_sprite.animation = "walk"
|
||
|
_sprite.flip_v = false
|
||
|
_sprite.flip_h = velocity.x < 0
|
||
|
elif velocity.y != 0 :
|
||
|
_sprite.animation = "up"
|
||
|
_sprite.flip_v = velocity.y > 0
|
||
|
|
||
|
pass
|
||
|
|
||
|
signal hit
|
||
|
func _on_body_entered(body: Node2D) -> void:
|
||
|
hide()
|
||
|
hit.emit()
|
||
|
_shape.set_deferred("disabled", true)
|
||
|
pass
|