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_network_master is tree-recursive by default.
  15. if get_tree().is_network_server():
  16. # For the server, give control of player 2 to the other peer.
  17. player2.set_network_master(get_tree().get_network_connected_peers()[0])
  18. else:
  19. # For the client, give control of player 2 to itself.
  20. player2.set_network_master(get_tree().get_network_unique_id())
  21. print("Unique id: ", get_tree().get_network_unique_id())
  22. remotesync func update_score(add_to_left):
  23. if add_to_left:
  24. score_left += 1
  25. score_left_node.set_text(str(score_left))
  26. else:
  27. score_right += 1
  28. score_right_node.set_text(str(score_right))
  29. var game_ended = false
  30. if score_left == SCORE_TO_WIN:
  31. winner_left.show()
  32. game_ended = true
  33. elif score_right == SCORE_TO_WIN:
  34. winner_right.show()
  35. game_ended = true
  36. if game_ended:
  37. $ExitGame.show()
  38. $Ball.rpc("stop")
  39. func _on_exit_game_pressed():
  40. emit_signal("game_finished")