Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. extends Area2D
  2.  
  3. signal hit
  4.  
  5. export var speed = 400 # How fast the player will move (pixels/sec).
  6. var screen_size # Size of the game window.
  7.  
  8. func _ready():
  9. screen_size = get_viewport_rect().size
  10. hide()
  11.  
  12. func _process(delta):
  13. var velocity = Vector2() # The player's movement vector.
  14. if Input.is_action_pressed("ui_right"):
  15. velocity.x += 1
  16. if Input.is_action_pressed("ui_left"):
  17. velocity.x -= 1
  18. if Input.is_action_pressed("ui_down"):
  19. velocity.y += 1
  20. if Input.is_action_pressed("ui_up"):
  21. velocity.y -= 1
  22. if velocity.length() > 0:
  23. velocity = velocity.normalized() * speed
  24. $AnimatedSprite.play()
  25. else:
  26. $AnimatedSprite.stop()
  27.  
  28. position += velocity * delta
  29. position.x = clamp(position.x, 0, screen_size.x)
  30. position.y = clamp(position.y, 0, screen_size.y)
  31.  
  32. if velocity.x != 0:
  33. $AnimatedSprite.animation = "right"
  34. $AnimatedSprite.flip_v = true
  35. # See the note below about boolean assignment
  36. $AnimatedSprite.flip_h = velocity.x < 0
  37. elif velocity.y != 0:
  38. $AnimatedSprite.animation = "up"
  39. $AnimatedSprite.flip_v = velocity.y > 0
  40.  
  41.  
  42.  
  43.  
  44. func _on_Player_body_entered(body):
  45. hide() # Player disappears after being hit.
  46. emit_signal("hit")
  47. $CollisionShape2D.set_deferred("disabled", true)
  48.  
  49. func start(pos):
  50. position = pos
  51. show()
  52. $CollisionShape2D.disabled = false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement