Advertisement
Guest User

player.gd

a guest
Jan 18th, 2020
284
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 var speed = 400 # pixels/sec
  6. var screen_size
  7.  
  8. func _ready():
  9. screen_size = get_viewport_rect().size
  10. hide()
  11.  
  12. func _process(delta):
  13. var velocity = Vector2()
  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. position += velocity * delta
  28. position.x = clamp(position.x, 0, screen_size.x)
  29. position.y = clamp(position.y, 0, screen_size.y)
  30. if velocity.x !=0:
  31. $AnimatedSprite.animation = "right"
  32. $AnimatedSprite.flip_v = false
  33. $AnimatedSprite.flip_h = velocity.x < 0
  34. elif velocity.y !=0:
  35. $AnimatedSprite.animation = "up"
  36. $AnimatedSprite.flip_v = velocity.y > 0
  37.  
  38. func _on_Player_body_entered(body):
  39. hide()
  40. emit_signal("hit")
  41. $CollisionShape2D.set_deferred("disabled", true)
  42.  
  43. func start(pos):
  44. position = pos
  45. show()
  46. $CollisionShape2D.disable = false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement