From 72ac64f6d040024a4c78660ec01230a58cf34f09 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Mon, 15 Nov 2021 01:17:32 -0600 Subject: [PATCH] Game: Implement player setup --- godot_ship/scenes/Game/Player.tscn | 6 ++++- godot_ship/script/game/Gameplay/Board.gd | 5 +++-- godot_ship/script/game/Gameplay/Game.gd | 15 ++++++++++--- godot_ship/script/game/Gameplay/Player.gd | 21 ++++++++++++------ godot_ship/script/game/Gameplay/Ship.gd | 8 +++---- godot_ship/script/game/Setup.gd | 27 +++++++---------------- godot_ship/script/game/SetupShip.gd | 6 +++++ 7 files changed, 52 insertions(+), 36 deletions(-) diff --git a/godot_ship/scenes/Game/Player.tscn b/godot_ship/scenes/Game/Player.tscn index a0e1be0..f48de99 100644 --- a/godot_ship/scenes/Game/Player.tscn +++ b/godot_ship/scenes/Game/Player.tscn @@ -1,8 +1,12 @@ -[gd_scene format=2] +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://script/game/Gameplay/Player.gd" type="Script" id=1] [node name="Player" type="Control"] anchor_right = 1.0 anchor_bottom = 1.0 +mouse_filter = 2 +script = ExtResource( 1 ) __meta__ = { "_edit_use_anchors_": false } diff --git a/godot_ship/script/game/Gameplay/Board.gd b/godot_ship/script/game/Gameplay/Board.gd index 9096a6f..894ad19 100644 --- a/godot_ship/script/game/Gameplay/Board.gd +++ b/godot_ship/script/game/Gameplay/Board.gd @@ -1,7 +1,7 @@ extends Node2D # Path to Ship class, for instantiating new Ships in code -onready var Ship = load("res://script/game/Gameplay/Ship.gd") +var Ship = preload("res://scenes/Game/Ship.tscn") # Consts and enums const NO_SHIP = -1 @@ -62,7 +62,8 @@ func fire(pos, res): # Place a ship on the board at board-space coordinates func place_ship(in_position, in_size, in_orientation, in_variant = 0): - var ship = Ship.new(in_position, in_size, in_orientation, in_variant) + var ship = Ship.instance() + ship._init(in_position, in_size, in_orientation, in_variant) for pos in ship.get_extent(): bottom_board[pos.x][pos.y] = [ships.size(), READY] ships.append(ship) diff --git a/godot_ship/script/game/Gameplay/Game.gd b/godot_ship/script/game/Gameplay/Game.gd index 6245fad..4a4fd84 100644 --- a/godot_ship/script/game/Gameplay/Game.gd +++ b/godot_ship/script/game/Gameplay/Game.gd @@ -11,7 +11,7 @@ onready var Victory = preload("res://scenes/Game/Victory.tscn") # Array of instances of the Player class; stores the Players -var players # = player1, player2, ... +var players = [] # = player1, player2, ... # turn counter var turn = 0 # Variable transporting hit state between players @@ -32,14 +32,23 @@ func _ready(): var _errno = 0; _errno += OptionsController.connect("change_theme", self, "_on_change_theme") _on_change_theme(OptionsController.get_theme()) + game_start() -# TODO: Move Setup into the Player. -func game_setup(_ships): +func game_setup(): print_debug("Congrats! Setup complete.") # Member functions: # game_start: starts the game func game_start(): + # Create a player 1 + var player = Player.instance() + # TODO: Create valid callback for player_ready + # It shouldn't connect to game_setup + player.connect("player_ready", self, "game_setup") + # Add player to scene tree + add_child(player) + # Add player to players + players.append(player) pass # victory_screen: display the victory screen diff --git a/godot_ship/script/game/Gameplay/Player.gd b/godot_ship/script/game/Gameplay/Player.gd index 81e8856..0855916 100644 --- a/godot_ship/script/game/Gameplay/Player.gd +++ b/godot_ship/script/game/Gameplay/Player.gd @@ -1,23 +1,26 @@ extends Node # Path to Board class, for instantiating new Boards in code -var Board = "res://script/game/Gameplay/Board.gd" +var Board = preload("res://scenes/Game/Board.tscn") # Preloaded assets, to be used later # TODO: Move Setup into the Player. It's just here, for now, so that it can be tested and the game doesn't appear broken -onready var Setup = preload("res://scenes/Game/Setup.tscn") +var Setup = preload("res://scenes/Game/Setup.tscn") # TODO: Move Fire into the Player. See above. -onready var Fire = preload("res://scenes/Game/Fire.tscn") +var Fire = preload("res://scenes/Game/Fire.tscn") + +signal player_ready + # Player ID of this player var pid # board (an instance of the Board class) -onready var board = Board.new() +onready var board = Board.instance() # Called when the node enters the scene tree for the first time. func _ready(): var setup = Setup.instance() - setup.connect("game_ready", self, "game_setup") + setup.connect("board_ready", self, "set_up") add_child(setup) # Member functions: @@ -39,10 +42,14 @@ func place_ship(pos, size, orientation, variant): # setUp: set up the board given the placed ship locations # translates the ship positions in the Setup UI to board-space, then places each ship -# ships: a list of lists of ship properties {{position, orientation, size, variant}, ...} +# ships: a list of lists of ship properties [[position, orientation, size, variant], ...] func set_up(ships): + # Place all the ships for i in ships: - place_ship(ships[i].Position, ships[i].Size, ships.Orientation, ships[i].Variant) + place_ship(i[0], i[1], i[2], i[3]) + emit_signal("player_ready") + # Add the board to the tree + add_child(board) # turnStart: start player's turn # Initiates the player's turn, and blocks until the player selects a location to fire upon diff --git a/godot_ship/script/game/Gameplay/Ship.gd b/godot_ship/script/game/Gameplay/Ship.gd index 2432fb6..bddc046 100644 --- a/godot_ship/script/game/Gameplay/Ship.gd +++ b/godot_ship/script/game/Gameplay/Ship.gd @@ -16,7 +16,7 @@ var sunk = false # Orientation of the ship (see enum Orientation) var orientation = Orientation.Y # array of spots thats been hit -var hit = [] +var hits = [] # Variable storing the positions of each piece of the ship var extents = [] @@ -50,7 +50,7 @@ func hit(pos): # If that position exists: if (index > -1): # Hit the ship piece at that location - hit[index] = true + hits[index] = true res = HIT # Decrement its health health -= 1 @@ -85,7 +85,7 @@ func get_extent(): # Update textures func texture(index): var state = 0 # ready - if(hit[index]): + if(hits[index]): state = 1 # hit var textureSize = 32 # It's okay to create a new texture every time, as resources are refcounted @@ -140,7 +140,7 @@ func _init(in_position = Vector2(0,0), in_size = 0, in_orientation = Orientation # Set the ship's variant(A, B, ... ) variant = in_variant # Resize the size-based arrays - hit.resize(in_size) + hits.resize(in_size) sprites.resize(in_size) # Update the extents and draw the textures update() diff --git a/godot_ship/script/game/Setup.gd b/godot_ship/script/game/Setup.gd index ce7074b..1c5cc7b 100644 --- a/godot_ship/script/game/Setup.gd +++ b/godot_ship/script/game/Setup.gd @@ -1,11 +1,9 @@ extends Control -signal game_ready +signal board_ready onready var Ships = ["2Ship", "3ShipA", "3ShipB", "4Ship", "5Ship"] -onready var Victory = preload("res://scenes/Game/Player.tscn") - var light_theme = load("res://light_theme.tres") var dark_theme = load("res://dark_theme.tres") @@ -40,25 +38,16 @@ func _on_Confirm_Placement_pressed(): get_node("PlaceShipDialog").popup() else: #Saves the location of ships and length of ship into an array - var shipLocation = [] + var ship_data = [] for ship in Ships: - var shipdata = ShipData.new() - shipdata.Position = get_node(ship).position - shipdata.Length = get_node(ship).get("ship_length") - shipdata.Orientation = get_node(ship).get("vertical") - match ship: - "3ShipB": - shipdata.Variant = 1 - _: - shipdata.Variant = 0 - shipLocation.append(shipdata) - + ship = get_node(ship) + var data = ship.get_shipdata() + ship_data.append(data) #print out the array for testing - for x in shipLocation: - print("Ship Length: ", x.Length, ", Ship Orientation: ", x.Orientation, ", Ship Position: ", x.Position) - + for x in ship_data: + print_debug("Ship Position: ", x[0], ", Ship Length: ", x[1], ", Ship Orientation: ", x[2], ", Variant: ", x[3]) # Return the shipLocation array to those listening on game_ready - emit_signal("game_ready", shipLocation) + emit_signal("board_ready", ship_data) queue_free() return valid # Replace with function body. diff --git a/godot_ship/script/game/SetupShip.gd b/godot_ship/script/game/SetupShip.gd index 6d923a2..10373fb 100644 --- a/godot_ship/script/game/SetupShip.gd +++ b/godot_ship/script/game/SetupShip.gd @@ -146,6 +146,12 @@ func ship_stacked(_body): func ship_unstacked(_body): collision = false +func get_shipdata(): + var shipdata = [world_to_board_space(position), ship_length, int(vertical)] + var variant = int(name.match("*B")) + shipdata.push_back(variant) + return shipdata + # Calculate the extents (front to back) of the ship and check whether they're on the board # Returns how many squares to move the ship along its orientation axis (positive or negative) func check_extents(center, orientation, length):