Szczepan86

[godot] player script

Oct 6th, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. extends Area2D
  2.  
  3. signal hit
  4.  
  5. export (int) var SPEED
  6. var velocity = Vector2()
  7. var screensize
  8. var miejsce_wcisniecia = Vector2()
  9.  
  10. func _ready():
  11. hide()
  12. screensize = get_viewport_rect().size
  13.  
  14. func start(pos):
  15. position = pos
  16. show()
  17. $Collision.disabled = false
  18.  
  19. func _input(event):
  20. if event.is_action_pressed("click"):
  21. miejsce_wcisniecia = get_global_mouse_position()
  22.  
  23.  
  24. func _process(delta):
  25. velocity = Vector2()
  26.  
  27. if miejsce_wcisniecia:
  28. velocity = miejsce_wcisniecia - position
  29.  
  30. if velocity.length() > 0:
  31. velocity = velocity.normalized() * SPEED
  32. $AnimatedSprite.play()
  33. else:
  34. $AnimatedSprite.stop()
  35.  
  36. position += velocity * delta
  37. position.x = clamp(position.x, 0, screensize.x)
  38. position.y = clamp(position.y, 0, screensize.y)
  39.  
  40. if velocity.x != 0:
  41. $AnimatedSprite.animation = "right"
  42. $AnimatedSprite.flip_v = false
  43. $AnimatedSprite.flip_h = velocity.x < 0
  44. elif velocity.y != 0:
  45. $AnimatedSprite.animation = "up"
  46. $AnimatedSprite.flip_v = velocity.y > 0
  47.  
  48. func _on_Player_body_entered( body ):
  49. $Collision.disabled = true
  50. hide()
  51. emit_signal("hit")
Advertisement
Add Comment
Please, Sign In to add comment