pong.gd 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. extends Node2D
  2. signal game_finished()
  3. const SCORE_TO_WIN = 10
  4. var score_left = 0
  5. var score_right = 0
  6. @onready var player2 = $Player2
  7. @onready var score_left_node = $ScoreLeft
  8. @onready var score_right_node = $ScoreRight
  9. @onready var winner_left = $WinnerLeft
  10. @onready var winner_right = $WinnerRight
  11. func _ready():
  12. # By default, all nodes in server inherit from master,
  13. # while all nodes in clients inherit from puppet.
  14. # set_multiplayer_authority is tree-recursive by default.
  15. if multiplayer.is_server():
  16. # For the server, give control of player 2 to the other peer.
  17. player2.set_multiplayer_authority(multiplayer.get_peers()[0])
  18. else:
  19. # For the client, give control of player 2 to itself.
  20. player2.set_multiplayer_authority(multiplayer.get_unique_id())
  21. print("Unique id: ", multiplayer.get_unique_id())
  22. @rpc("any_peer", "call_local")
  23. func update_score(add_to_left):
  24. if add_to_left:
  25. score_left += 1
  26. score_left_node.set_text(str(score_left))
  27. else:
  28. score_right += 1
  29. score_right_node.set_text(str(score_right))
  30. var game_ended = false
  31. if score_left == SCORE_TO_WIN:
  32. winner_left.show()
  33. game_ended = true
  34. elif score_right == SCORE_TO_WIN:
  35. winner_right.show()
  36. game_ended = true
  37. if game_ended:
  38. $ExitGame.show()
  39. $Ball.stop.rpc()
  40. func _on_exit_game_pressed():
  41. emit_signal("game_finished")