paddle.gd 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. extends Area2D
  2. export var left=false
  3. const MOTION_SPEED=150
  4. var motion = 0
  5. var you_hidden = false
  6. onready var screen_size = get_viewport_rect().size
  7. #synchronize position and speed to the other peers
  8. slave func set_pos_and_motion(p_pos, p_motion):
  9. position = p_pos
  10. motion = p_motion
  11. func _hide_you_label():
  12. you_hidden = true
  13. get_node("you").hide()
  14. func _process(delta):
  15. #is the master of the paddle
  16. if is_network_master():
  17. motion = 0
  18. if Input.is_action_pressed("move_up"):
  19. motion -= 1
  20. elif Input.is_action_pressed("move_down"):
  21. motion += 1
  22. if not you_hidden and motion != 0:
  23. _hide_you_label()
  24. motion *= MOTION_SPEED
  25. #using unreliable to make sure position is updated as fast as possible, even if one of the calls is dropped
  26. rpc_unreliable("set_pos_and_motion", position, motion)
  27. else:
  28. if not you_hidden:
  29. _hide_you_label()
  30. translate( Vector2(0,motion*delta) )
  31. # set screen limits
  32. var pos = position
  33. if pos.y < 0:
  34. position = Vector2(pos.x, 0)
  35. elif pos.y > screen_size.y:
  36. position = Vector2(pos.x, screen_size.y)
  37. func _on_paddle_area_enter( area ):
  38. if is_network_master():
  39. area.rpc("bounce", left, randf()) #random for new direction generated on each peer