46 lines
1.0 KiB
GDScript
46 lines
1.0 KiB
GDScript
extends Area2D
|
|
|
|
signal hit
|
|
|
|
export var speed = 400
|
|
var screen_size
|
|
|
|
func _ready():
|
|
screen_size = get_viewport_rect().size
|
|
hide()
|
|
|
|
|
|
func _process(delta):
|
|
var velocity = Vector2()
|
|
velocity.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
|
velocity.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
|
|
|
|
if velocity.length() > 0:
|
|
velocity = velocity.normalized() * speed
|
|
$AnimatedSprite.play()
|
|
else:
|
|
$AnimatedSprite.stop()
|
|
position += velocity * delta
|
|
position.x = clamp(position.x, 0, screen_size.x)
|
|
position.y = clamp(position.y, 0, screen_size.y)
|
|
|
|
if velocity.x != 0:
|
|
$AnimatedSprite.animation = "right"
|
|
$AnimatedSprite.flip_v = false
|
|
$AnimatedSprite.flip_h = velocity.x < 0
|
|
elif velocity.y != 0:
|
|
$AnimatedSprite.animation = "up"
|
|
$AnimatedSprite.flip_v = velocity.y > 0
|
|
|
|
|
|
func start(pos):
|
|
position = pos
|
|
show()
|
|
$CollisionShape2D.disabled = false
|
|
|
|
|
|
func _on_Player_body_entered(_body):
|
|
hide()
|
|
emit_signal("hit")
|
|
$CollisionShape2D.set_deferred("disabled", true)
|