game.gd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. extends Control
  2. const _crown = preload("res://img/crown.png")
  3. @onready var _list = $HBoxContainer/VBoxContainer/ItemList
  4. @onready var _action = $HBoxContainer/VBoxContainer/Action
  5. const ACTIONS = ["roll", "pass"]
  6. var _players = []
  7. var _turn = -1
  8. @rpc
  9. func _log(what):
  10. $HBoxContainer/RichTextLabel.add_text(what + "\n")
  11. @rpc("any_peer")
  12. func set_player_name(name):
  13. if not is_multiplayer_authority():
  14. return
  15. var sender = multiplayer.get_remote_sender_id()
  16. update_player_name.rpc(sender, name)
  17. @rpc("call_local")
  18. func update_player_name(player, name):
  19. var pos = _players.find(player)
  20. if pos != -1:
  21. _list.set_item_text(pos, name)
  22. @rpc("any_peer")
  23. func request_action(action):
  24. if not is_multiplayer_authority():
  25. return
  26. var sender = multiplayer.get_remote_sender_id()
  27. if _players[_turn] != sender:
  28. _log.rpc("Someone is trying to cheat! %s" % str(sender))
  29. return
  30. if action not in ACTIONS:
  31. _log.rpc("Invalid action: %s" % action)
  32. return
  33. do_action(action)
  34. next_turn()
  35. func do_action(action):
  36. var player_name = _list.get_item_text(_turn)
  37. var val = randi() % 100
  38. _log.rpc("%s: %ss %d" % [player_name, action, val])
  39. @rpc("call_local")
  40. func set_turn(turn):
  41. _turn = turn
  42. if turn >= _players.size():
  43. return
  44. for i in range(0, _players.size()):
  45. if i == turn:
  46. _list.set_item_icon(i, _crown)
  47. else:
  48. _list.set_item_icon(i, null)
  49. _action.disabled = _players[turn] != multiplayer.get_unique_id()
  50. @rpc("call_local")
  51. func del_player(id):
  52. var pos = _players.find(id)
  53. if pos == -1:
  54. return
  55. _players.remove_at(pos)
  56. _list.remove_item(pos)
  57. if _turn > pos:
  58. _turn -= 1
  59. if multiplayer.is_server():
  60. set_turn.rpc(_turn)
  61. @rpc("call_local")
  62. func add_player(id, pname=""):
  63. _players.append(id)
  64. if pname == "":
  65. _list.add_item("... connecting ...", null, false)
  66. else:
  67. _list.add_item(pname, null, false)
  68. func get_player_name(pos):
  69. if pos < _list.get_item_count():
  70. return _list.get_item_text(pos)
  71. else:
  72. return "Error!"
  73. func next_turn():
  74. _turn += 1
  75. if _turn >= _players.size():
  76. _turn = 0
  77. set_turn.rpc(_turn)
  78. func start():
  79. set_turn(0)
  80. func stop():
  81. _players.clear()
  82. _list.clear()
  83. _turn = 0
  84. _action.disabled = true
  85. func on_peer_add(id):
  86. if not multiplayer.is_server():
  87. return
  88. for i in range(0, _players.size()):
  89. add_player.rpc_id(id, _players[i], get_player_name(i))
  90. add_player.rpc(id)
  91. set_turn.rpc_id(id, _turn)
  92. func on_peer_del(id):
  93. if not multiplayer.is_server():
  94. return
  95. del_player.rpc(id)
  96. func _on_Action_pressed():
  97. if multiplayer.is_server():
  98. if _turn != 0:
  99. return
  100. do_action("roll")
  101. next_turn()
  102. else:
  103. request_action.rpc_id(1, "roll")