1
0
mirror of https://github.com/JohnBreaux/Boat-Battle.git synced 2024-11-15 13:25:58 +00:00
Boat-Battle/godot_ship/script/game/Gameplay/Player.gd

93 lines
2.6 KiB
GDScript3
Raw Normal View History

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")
# Members
var pid # Player ID
2021-11-18 15:00:15 +00:00
var board # Board
2021-11-15 05:50:23 +00:00
2021-11-18 15:00:15 +00:00
var fire_at_position # Position to fire at
2021-11-08 13:40:11 +00:00
# Called when the node enters the scene tree for the first time.
func _ready():
2021-11-18 15:00:15 +00:00
# Set the player ID according to which network peer ID we are
pid = int(name)
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)
board = Board.instance()
2021-11-08 13:40:11 +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
func hit(pos):
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
func mark(pos, value):
2021-11-18 15:00:15 +00:00
# Mark the position on the top board
board.fire(pos, value)
2021-11-08 13:40:11 +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
2021-11-18 15:00:15 +00:00
# setup: set up the board given the placed ship locations
# 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)
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
# returns: fire = [player id, target coordinates]
func turn_start():
2021-11-18 15:00:15 +00:00
print("turn_start")
var fire = Fire.instance()
add_child(fire)
var pos = yield(fire, "fire_at")
return pos
2021-11-08 13:40:11 +00:00
# getBoard: returns the player's board
# returns: board
func getBoard():
return board
# 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():
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()
2021-11-08 13:40:11 +00:00
2021-11-18 15:00:15 +00:00
func _on_fire_at(pos):
fire_at_position = pos