2021-11-08 13:40:11 +00:00
|
|
|
extends Node
|
|
|
|
|
2021-11-11 20:38:10 +00:00
|
|
|
# Path to Board class, for instantiating new Boards in code
|
|
|
|
var Board = "res://script/game/Gameplay/Board.gd"
|
|
|
|
|
2021-11-15 05:50:23 +00:00
|
|
|
# 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")
|
|
|
|
# TODO: Move Fire into the Player. See above.
|
|
|
|
onready var Fire = preload("res://scenes/Game/Fire.tscn")
|
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# Player ID of this player
|
2021-11-08 13:40:11 +00:00
|
|
|
var pid
|
2021-11-11 21:38:50 +00:00
|
|
|
# board (an instance of the Board class)
|
2021-11-12 02:45:38 +00:00
|
|
|
onready var board = Board.new()
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2021-11-15 05:50:23 +00:00
|
|
|
var setup = Setup.instance()
|
|
|
|
setup.connect("game_ready", self, "game_setup")
|
|
|
|
add_child(setup)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# Member functions:
|
2021-11-08 13:40:11 +00:00
|
|
|
# hit: Called when opponent fires on us.
|
2021-11-15 02:12:01 +00:00
|
|
|
# Update internal state, and return bool hit/miss, hit = true, miss = false
|
|
|
|
func hit(pos):
|
|
|
|
var res = board.hit(pos)
|
|
|
|
if res == -1:
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
return false
|
2021-11-08 13:40:11 +00:00
|
|
|
pass
|
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# place_ship: called when ships are placed.
|
2021-11-08 13:40:11 +00:00
|
|
|
# forwards Ship locations to the Board, so that it may construct a ship
|
2021-11-15 04:47:01 +00:00
|
|
|
# ship: a list of ship properties {position, orientation, size, variant}
|
|
|
|
func place_ship(pos, size, orientation, variant):
|
|
|
|
board.place_ship(pos, size, orientation, variant)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# 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
|
2021-11-15 04:47:01 +00:00
|
|
|
# ships: a list of lists of ship properties {{position, orientation, size, variant}, ...}
|
|
|
|
func set_up(ships):
|
|
|
|
for i in ships:
|
|
|
|
place_ship(ships[i].Position, ships[i].Size, ships.Orientation, ships[i].Variant)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# turnStart: start player's turn
|
|
|
|
# Initiates the player's turn, and blocks until the player selects a location to fire upon
|
2021-11-11 21:38:50 +00:00
|
|
|
# returns: fire = [player id, target coordinates]
|
2021-11-08 13:40:11 +00:00
|
|
|
func turnStart():
|
2021-11-15 05:50:23 +00:00
|
|
|
# TODO: Yielf until Fire return
|
|
|
|
add_child(Fire.instance())
|
2021-11-11 21:38:50 +00:00
|
|
|
var player_id = 0
|
|
|
|
var target = Vector2(0,0)
|
|
|
|
return [player_id, target]
|
2021-11-08 13:40:11 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
# getBoard: returns the player's board
|
|
|
|
# returns: board
|
|
|
|
func getBoard():
|
|
|
|
return board
|
|
|
|
|
|
|
|
# forfeit: ends game for player
|
|
|
|
# Sinks all ships
|
2021-11-15 02:12:01 +00:00
|
|
|
# hits every single board tile
|
2021-11-08 13:40:11 +00:00
|
|
|
func forfeit():
|
2021-11-15 02:12:01 +00:00
|
|
|
for i in 10:
|
|
|
|
for j in 10:
|
|
|
|
var pos
|
|
|
|
pos.x = i
|
|
|
|
pos.y = j
|
|
|
|
hit(pos)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# getShipCount: get the number of ships the player has left alive
|
|
|
|
func getShipCount():
|
2021-11-15 02:12:01 +00:00
|
|
|
var count = board.get_ship_count()
|
|
|
|
return count
|
2021-11-08 13:40:11 +00:00
|
|
|
pass
|
|
|
|
|