1
0
mirror of https://github.com/JohnBreaux/Boat-Battle.git synced 2025-02-04 12:28:35 +00:00

Game: Implement player setup

This commit is contained in:
2021-11-15 01:17:32 -06:00
parent 36cc6896b1
commit 72ac64f6d0
7 changed files with 52 additions and 36 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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()