paddle.gd 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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_multiplayer_authority():
  10. _motion = Input.get_axis(&"move_up", &"move_down")
  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. set_pos_and_motion.rpc(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. @rpc("unreliable")
  25. func set_pos_and_motion(pos, motion):
  26. position = pos
  27. _motion = motion
  28. func _hide_you_label():
  29. _you_hidden = true
  30. get_node(^"You").hide()
  31. func _on_paddle_area_enter(area):
  32. if is_multiplayer_authority():
  33. # Random for new direction generated checked each peer.
  34. area.bounce.rpc(left, randf())