feat: more multiplayer
This commit is contained in:
parent
3981646279
commit
6c6e1b3289
9 changed files with 429 additions and 104 deletions
87
main.gd
87
main.gd
|
|
@ -1,41 +1,41 @@
|
|||
extends Node
|
||||
class_name MainNode
|
||||
|
||||
const PORT = 25565
|
||||
const PORT_RANGE := 10
|
||||
const MAX_CONNECTIONS = 20
|
||||
|
||||
var counter = 0
|
||||
|
||||
@onready var before_connect_container = get_node("BeforeConnect") as Container
|
||||
@onready var host_text_edit = get_node("%HostTextEdit") as TextEdit
|
||||
@onready var connect_button = get_node("%ConnectButton") as Button
|
||||
@onready var host_button = get_node("%HostButton") as Button
|
||||
@onready var server_discovery = get_node("%ServerDiscovery") as ServerDiscovery
|
||||
|
||||
@onready var after_connect_container = get_node("%AfterConnect") as Container
|
||||
@onready var counter_label = get_node("%CounterLabel") as Label
|
||||
@onready var decrement_button = get_node("%DecrementButton") as Button
|
||||
@onready var increment_button = get_node("%IncrementButton") as Button
|
||||
@onready var after_connect_container = get_node("%AfterConnect") as Node
|
||||
@onready var disconnect_button = get_node("%DisconnectButton") as Button
|
||||
@onready var start_game_button = get_node("%StartGameButton") as Button
|
||||
|
||||
@onready var demo_game = get_node("AfterConnect/DemoGame") as DemoGame
|
||||
@onready var start_game_button = (get_node("AfterConnect/DemoGame") as DemoGame).start_game_btn
|
||||
|
||||
|
||||
var active_port: int
|
||||
var server_buttons: Dictionary = Dictionary()
|
||||
var base_seed: int
|
||||
|
||||
func _clean_room():
|
||||
# this is if someone left the room and we will just clean the game room again
|
||||
# should only called on host
|
||||
_set_counter(0)
|
||||
# need to clean for server
|
||||
pass
|
||||
|
||||
func _on_start_game_clicked():
|
||||
if !multiplayer.multiplayer_peer:
|
||||
return
|
||||
if !multiplayer.get_unique_id() != 1:
|
||||
if !multiplayer.is_server():
|
||||
return
|
||||
if (multiplayer.get_peers().size() == 0):
|
||||
return
|
||||
# do start game here, should only run on host
|
||||
_start_game.rpc()
|
||||
pass
|
||||
|
||||
func _ready() -> void:
|
||||
|
|
@ -51,13 +51,45 @@ func _ready() -> void:
|
|||
#multiplayer.connection_failed.connect(_on_connected_fail)
|
||||
multiplayer.server_disconnected.connect(_on_server_disconnected)
|
||||
|
||||
increment_button.button_up.connect(func(): _increment.rpc())
|
||||
decrement_button.button_up.connect(func(): _decrement.rpc())
|
||||
disconnect_button.button_up.connect(_on_disconnect_clicked)
|
||||
connect_button.get_parent().remove_child(connect_button)
|
||||
|
||||
server_discovery.server_added.connect(_on_server_added)
|
||||
server_discovery.server_removed.connect(_on_server_removed)
|
||||
|
||||
demo_game.own_played_card.connect(func (card): _on_own_played_card.rpc(card.id if card else ""))
|
||||
demo_game.opponent_played_card.connect(func (card): _on_opponent_played_card.rpc(card.id if card else ""))
|
||||
demo_game.own_played_rts.connect(func (move): _on_own_played_rts.rpc(move))
|
||||
demo_game.opponent_played_rts.connect(func (move): _on_opponent_played_rts.rpc(move))
|
||||
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func _on_own_played_card(card_id: String):
|
||||
demo_game.rpc_own_play_card(card_id)
|
||||
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func _on_opponent_played_card(card_id: String):
|
||||
demo_game.rpc_opponent_play_card(card_id)
|
||||
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func _on_own_played_rts(move: String):
|
||||
demo_game.rpc_own_rps_move(move)
|
||||
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func _on_opponent_played_rts(move: String):
|
||||
demo_game.rpc_opponent_rps_move(move)
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func _sync_seed(seed: int):
|
||||
seed(seed)
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func _start_game():
|
||||
if multiplayer.is_server():
|
||||
demo_game.init(MatchManager.PLAYER_1_ID)
|
||||
else:
|
||||
demo_game.init(MatchManager.PLAYER_2_ID)
|
||||
|
||||
demo_game.rpc_start_game()
|
||||
|
||||
func _on_disconnect_clicked():
|
||||
if (multiplayer.is_server()):
|
||||
|
|
@ -118,8 +150,12 @@ func _on_host_pressed():
|
|||
|
||||
_on_connected_ok()
|
||||
|
||||
start_game_button.disabled = true
|
||||
start_game_button.text = "Start Game (Not enough player)"
|
||||
|
||||
base_seed = randi()
|
||||
_sync_seed.rpc(base_seed)
|
||||
|
||||
server_discovery.enable_server(active_port)
|
||||
|
||||
func is_port_available(port: int) -> bool:
|
||||
|
|
@ -133,26 +169,13 @@ func is_port_available(port: int) -> bool:
|
|||
else:
|
||||
return false
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func _increment():
|
||||
counter += 1
|
||||
counter_label.text = str(counter)
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func _decrement():
|
||||
counter -= 1
|
||||
counter_label.text = str(counter)
|
||||
|
||||
@rpc("authority", "call_remote")
|
||||
func _set_counter(value: int):
|
||||
counter = value
|
||||
counter_label.text = str(counter)
|
||||
|
||||
func _on_player_connected(id: int):
|
||||
if multiplayer.get_unique_id() == 1:
|
||||
_set_counter.rpc_id(id, counter)
|
||||
|
||||
_sync_seed.rpc(base_seed)
|
||||
|
||||
if (multiplayer.get_peers().size() > 0):
|
||||
start_game_button.disabled = false
|
||||
start_game_button.text = "Start Game"
|
||||
server_discovery.disable_server()
|
||||
|
||||
|
|
@ -165,22 +188,20 @@ func _on_player_disconnected(id):
|
|||
|
||||
if (id != 1 and multiplayer.get_unique_id() == 1):
|
||||
_clean_room()
|
||||
start_game_button.disabled = true
|
||||
start_game_button.text = "Start Game (Not enough player)"
|
||||
server_discovery.enable_server(active_port)
|
||||
|
||||
func _on_connected_ok():
|
||||
$AfterConnect.visible = true
|
||||
$BeforeConnect.visible = false
|
||||
counter_label.text = str(counter)
|
||||
|
||||
if (multiplayer.is_server()):
|
||||
disconnect_button.text = "Disconnect (Host)"
|
||||
if start_game_button.get_parent() != after_connect_container:
|
||||
after_connect_container.add_child(start_game_button)
|
||||
start_game_button.visible = true
|
||||
else:
|
||||
disconnect_button.text = "Disconnect"
|
||||
if start_game_button.get_parent() == after_connect_container:
|
||||
after_connect_container.remove_child(start_game_button)
|
||||
start_game_button.visible = false
|
||||
|
||||
func _on_connected_fail():
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue