From 6ff4df4a1d3f5d56ba7607620cfd49474a4e141c Mon Sep 17 00:00:00 2001 From: John Breaux Date: Wed, 24 Nov 2021 03:05:45 -0600 Subject: [PATCH] Game: Begin state machine implementation; use Net-mail for host<->guest communication. --- godot_ship/script/game/Gameplay/Game.gd | 83 +++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/godot_ship/script/game/Gameplay/Game.gd b/godot_ship/script/game/Gameplay/Game.gd index 5fb28f3..e16cd43 100644 --- a/godot_ship/script/game/Gameplay/Game.gd +++ b/godot_ship/script/game/Gameplay/Game.gd @@ -1,8 +1,11 @@ extends Control -var light_theme = load("res://light_theme.tres") -var dark_theme = load("res://dark_theme.tres") +# warning-ignore-all:unused_signal +# Signals +signal fire # fire(position) +signal hit # hit (state: see Miss/Ready/Hit/Sunk enum in Board.gd) +signal win # win (): sent when opponent player lost # Path to Player class, for instantiating new Players in code var Player = preload("res://scenes/Game/Player.tscn") @@ -11,7 +14,7 @@ var Victory = preload("res://scenes/Game/Victory.tscn") # Array of instances of the Player class; stores the Players -var players = [] # = player1, player2, ... +var player var players_ready = [] # turn counter var turn = 0 @@ -37,6 +40,7 @@ func _ready(): # Function used to keep track of which players are ready # TODO: Change this to keep track of ready states only func player_ready(): + players_ready.append(Net.get_network_id()) pass # Member functions: @@ -49,15 +53,84 @@ sync func game_setup(): network_id = Net.get_network_id() pass -# game_start: +# game_start: Runs on host. Controls the game. func game_start(): + var state = "P1_fire" # Make sure we're the server + while true: + match state: + "P1_fire": + # Tell local player to fire + + # Wait for result + + # Send fire REQUEST to P2 + pass + "P2_check": + # Wait for hit + var ret = yield(self, "hit") + # Record the hit + + # + pass + "P2_fire": + pass + "P1_check": + # Check if + pass + "P1_win": + pass + "P2_win": + pass pass +func fire_on(id, pos:Vector2): + # REQUEST fire on opponent + Net.send(id, ["fire", pos], Net.REQUEST) + # Wait for REPLY + +func return_hit(id, ship_status): + + Net.send(id, ["hit", ship_status], Net.REPLY) + +func _on_win(): + pass + +func _on_Net_incoming(mail): + if mail.size() == 3: + var sender = mail[0] + var message = mail[1] + var mailtype = mail[2] + match mailtype: + # if message is a REQUEST (to perform an action) + Net.REQUEST: + match message[0]: + # Opponent asks for player.fire() + "fire": + emit_signal("fire", message[1]) + # Opponent asks for hit(pos) + "hit": + pass + _: + pass + Net.REPLY: + # message is a REPLY (return value) + match message[0]: + "fire": + emit_signal("hit", message[1]) + # Return value of + "hit": + pass + pass + Net.READY: + # Add player to the ready array + players_ready.append(sender) + pass + # _on_player_ready: Player Ready signal handler func _on_player_ready(): print ("_on_player_ready") - Net.send(1, ["player_ready", Net.Mail.REPLY]) + Net.send(1, [], Net.READY) # victory_screen: display the victory screen func victory_screen():