godot-demo-projects/2d/dodge_the_creeps/Player.gd

54 lines
1.3 KiB
GDScript3
Raw Normal View History

2018-03-12 16:49:09 +01:00
extends Area2D
signal hit
export (int) var SPEED
var velocity = Vector2()
var screensize
func _ready():
hide()
screensize = get_viewport_rect().size
func start(pos):
position = pos
show()
# Must be deferred as we can't change physics properties on a physics callback
$Collision.set_deferred("disabled", false)
2018-03-12 16:49:09 +01:00
func _process(delta):
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * SPEED
$AnimatedSprite.play()
$Trail.emitting = true
else:
$AnimatedSprite.stop()
$Trail.emitting = false
position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.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 _on_Player_body_entered(_body):
# Must be deferred as we can't change physics properties on a physics callback
$Collision.set_deferred("disabled", true)
2018-03-12 16:49:09 +01:00
hide()
emit_signal("hit")