2021-11-08 13:40:11 +00:00
|
|
|
extends Node
|
|
|
|
|
2021-11-18 15:00:15 +00:00
|
|
|
# Emitted when the player is ready
|
|
|
|
signal player_ready
|
|
|
|
|
2021-11-15 05:50:23 +00:00
|
|
|
# Preloaded assets, to be used later
|
2021-11-18 15:00:15 +00:00
|
|
|
# Path to Board class, for instantiating new Boards in code
|
|
|
|
var Board = preload("res://scenes/Game/Board.tscn")
|
|
|
|
# Path to Setup menu, so the player may set up their Board
|
2021-11-15 07:17:32 +00:00
|
|
|
var Setup = preload("res://scenes/Game/Setup.tscn")
|
2021-11-18 15:00:15 +00:00
|
|
|
# Path to Fire menu, so the player may fire on the opponent
|
2021-11-15 07:17:32 +00:00
|
|
|
var Fire = preload("res://scenes/Game/Fire.tscn")
|
|
|
|
|
2021-11-23 04:04:21 +00:00
|
|
|
# Members
|
2021-11-18 15:00:15 +00:00
|
|
|
var board # Board
|
2021-11-24 18:17:07 +00:00
|
|
|
var fire_pos = Vector2(-1,-1)
|
|
|
|
var target = Vector2(-1,-1)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2021-11-24 18:17:07 +00:00
|
|
|
pass
|
2021-11-18 15:00:15 +00:00
|
|
|
|
2021-11-23 04:04:21 +00:00
|
|
|
func set_up_begin():
|
2021-11-15 05:50:23 +00:00
|
|
|
var setup = Setup.instance()
|
2021-11-15 07:17:32 +00:00
|
|
|
setup.connect("board_ready", self, "set_up")
|
2021-11-15 05:50:23 +00:00
|
|
|
add_child(setup)
|
2021-11-23 04:04:21 +00:00
|
|
|
board = Board.instance()
|
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-18 15:00:15 +00:00
|
|
|
# Update internal state, and return hit/miss/sunk
|
2021-11-23 04:04:21 +00:00
|
|
|
func hit(pos):
|
2021-11-24 18:17:07 +00:00
|
|
|
target = pos
|
2021-11-15 02:12:01 +00:00
|
|
|
var res = board.hit(pos)
|
2021-11-18 15:00:15 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
# mark: Called when the opponent returns hit/miss/sunk
|
|
|
|
# Update internal state, return ack/nak
|
2021-11-24 18:17:07 +00:00
|
|
|
func mark(value):
|
2021-11-18 15:00:15 +00:00
|
|
|
# Mark the position on the top board
|
2021-11-24 18:17:07 +00:00
|
|
|
return board.fire(fire_pos, value)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
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):
|
2021-11-24 18:17:07 +00:00
|
|
|
return board.place_ship(pos, size, orientation, variant)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
2021-11-24 18:17:07 +00:00
|
|
|
# set_up: set up the board given the placed ship locations
|
2021-11-18 15:00:15 +00:00
|
|
|
# Places each ship onto the board
|
2021-11-15 07:17:32 +00:00
|
|
|
# ships: a list of lists of ship properties [[position, orientation, size, variant], ...]
|
2021-11-15 04:47:01 +00:00
|
|
|
func set_up(ships):
|
2021-11-15 07:17:32 +00:00
|
|
|
# Place all the ships
|
2021-11-15 04:47:01 +00:00
|
|
|
for i in ships:
|
2021-11-15 07:17:32 +00:00
|
|
|
place_ship(i[0], i[1], i[2], i[3])
|
|
|
|
# Add the board to the tree
|
|
|
|
add_child(board)
|
2021-11-18 15:50:17 +00:00
|
|
|
emit_signal("player_ready")
|
2021-11-08 13:40:11 +00:00
|
|
|
|
2021-11-18 15:00:15 +00:00
|
|
|
# turn_start: start player's turn
|
2021-11-08 13:40:11 +00:00
|
|
|
# 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-23 04:04:21 +00:00
|
|
|
func turn_start():
|
2021-11-18 15:00:15 +00:00
|
|
|
var fire = Fire.instance()
|
2021-11-24 18:17:07 +00:00
|
|
|
fire.hits = board.top_board
|
2021-11-18 15:00:15 +00:00
|
|
|
add_child(fire)
|
2021-11-24 18:17:07 +00:00
|
|
|
fire_pos = yield(fire, "fire_at")
|
|
|
|
fire.queue_free()
|
|
|
|
return fire_pos
|
|
|
|
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# getBoard: returns the player's board
|
|
|
|
# returns: board
|
2021-11-24 18:17:07 +00:00
|
|
|
func board_query(boardname):
|
|
|
|
match boardname:
|
|
|
|
"top":
|
|
|
|
return board.query_top (fire_pos)
|
|
|
|
"bottom":
|
|
|
|
return board.quert_bottom (target)
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# forfeit: ends game for player
|
|
|
|
# Sinks all ships
|
2021-11-18 15:00:15 +00:00
|
|
|
# Ensures there are no ships left behind
|
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:
|
2021-11-18 15:00:15 +00:00
|
|
|
# Hit the board
|
|
|
|
hit(Vector2(i, j))
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# getShipCount: get the number of ships the player has left alive
|
|
|
|
func getShipCount():
|
2021-11-18 15:00:15 +00:00
|
|
|
return board.get_ship_count()
|