54 lines
1.4 KiB
GDScript
54 lines
1.4 KiB
GDScript
extends Area2D
|
|
|
|
signal pickup
|
|
signal hurt
|
|
|
|
@export var speed = 350 # pixels per second
|
|
var velocity = Vector2.ZERO
|
|
var screensize = Vector2(480, 720)
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
pass # Replace with function body.
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
# The following is an alternative to using a bunch of if statements to see if inputs are pressed.
|
|
# Will get a vector based upon what's pressed, for the 8 possible directions.
|
|
velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
|
position += velocity * speed * delta
|
|
|
|
# Keep player on screen.
|
|
position.x = clamp(position.x, 0, screensize.x)
|
|
position.y = clamp(position.y, 0, screensize.y)
|
|
|
|
if velocity.length() > 0:
|
|
$AnimatedSprite2D.animation = "run"
|
|
else:
|
|
$AnimatedSprite2D.animation = "idle"
|
|
if velocity.x != 0:
|
|
$AnimatedSprite2D.flip_h = velocity.x < 0
|
|
|
|
func start():
|
|
set_process(true)
|
|
position = screensize / 2
|
|
$AnimatedSprite2D.animation = "idle"
|
|
|
|
func die():
|
|
$AnimatedSprite2D.animation = "hurt"
|
|
set_process(false)
|
|
|
|
|
|
func _on_area_entered(area):
|
|
# Triggered when this enters the space of another Area2D.
|
|
if area.is_in_group("coins"):
|
|
area.pickup()
|
|
pickup.emit("coin")
|
|
if area.is_in_group("powerups"):
|
|
area.pickup()
|
|
pickup.emit("powerup")
|
|
if area.is_in_group("obstacles"):
|
|
hurt.emit()
|
|
die()
|