Player.gd 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. extends Area2D
  2. signal hit
  3. export (int) var SPEED
  4. var velocity = Vector2()
  5. var screensize
  6. func _ready():
  7. hide()
  8. screensize = get_viewport_rect().size
  9. func start(pos):
  10. position = pos
  11. show()
  12. $Collision.disabled = false
  13. func _process(delta):
  14. velocity = Vector2()
  15. if Input.is_action_pressed("ui_right"):
  16. velocity.x += 1
  17. if Input.is_action_pressed("ui_left"):
  18. velocity.x -= 1
  19. if Input.is_action_pressed("ui_down"):
  20. velocity.y += 1
  21. if Input.is_action_pressed("ui_up"):
  22. velocity.y -= 1
  23. if velocity.length() > 0:
  24. velocity = velocity.normalized() * SPEED
  25. $AnimatedSprite.play()
  26. $Trail.emitting = true
  27. else:
  28. $AnimatedSprite.stop()
  29. $Trail.emitting = false
  30. position += velocity * delta
  31. position.x = clamp(position.x, 0, screensize.x)
  32. position.y = clamp(position.y, 0, screensize.y)
  33. if velocity.x != 0:
  34. $AnimatedSprite.animation = "right"
  35. $AnimatedSprite.flip_v = false
  36. $AnimatedSprite.flip_h = velocity.x < 0
  37. elif velocity.y != 0:
  38. $AnimatedSprite.animation = "up"
  39. $AnimatedSprite.flip_v = velocity.y > 0
  40. func _on_Player_body_entered( body ):
  41. $Collision.disabled = true
  42. hide()
  43. emit_signal("hit")