paddle.gd 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. extends Area2D
  2. const MOTION_SPEED = 150
  3. export var left = false
  4. var _motion = 0
  5. var _you_hidden = false
  6. onready var _screen_size_y = get_viewport_rect().size.y
  7. func _process(delta):
  8. # Is the master of the paddle.
  9. if is_network_master():
  10. _motion = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
  11. if not _you_hidden and _motion != 0:
  12. _hide_you_label()
  13. _motion *= MOTION_SPEED
  14. # Using unreliable to make sure position is updated as fast
  15. # as possible, even if one of the calls is dropped.
  16. rpc_unreliable("set_pos_and_motion", position, _motion)
  17. else:
  18. if not _you_hidden:
  19. _hide_you_label()
  20. translate(Vector2(0, _motion * delta))
  21. # Set screen limits.
  22. position.y = clamp(position.y, 16, _screen_size_y - 16)
  23. # Synchronize position and speed to the other peers.
  24. puppet func set_pos_and_motion(pos, motion):
  25. position = pos
  26. _motion = motion
  27. func _hide_you_label():
  28. _you_hidden = true
  29. get_node("You").hide()
  30. func _on_paddle_area_enter(area):
  31. if is_network_master():
  32. # Random for new direction generated on each peer.
  33. area.rpc("bounce", left, randf())