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

46 lines
1.0 KiB
GDScript3
Raw Normal View History

2018-03-12 16:49:09 +01:00
extends Area2D
signal hit
2019-03-17 01:26:56 +01:00
export var speed = 400
var screen_size
2018-03-12 16:49:09 +01:00
func _ready():
2019-03-17 01:26:56 +01:00
screen_size = get_viewport_rect().size
2018-03-12 16:49:09 +01:00
hide()
2018-03-12 16:49:09 +01:00
func _process(delta):
2019-03-17 01:26:56 +01:00
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")
2018-03-12 16:49:09 +01:00
if velocity.length() > 0:
2019-03-17 01:26:56 +01:00
velocity = velocity.normalized() * speed
2018-03-12 16:49:09 +01:00
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
position += velocity * delta
2019-03-17 01:26:56 +01:00
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
2018-03-12 16:49:09 +01:00
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
2019-03-17 01:26:56 +01:00
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
func _on_Player_body_entered(_body):
hide()
emit_signal("hit")
$CollisionShape2D.set_deferred("disabled", true)