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

87 lines
2.4 KiB
GDScript3
Raw Normal View History

2021-11-08 13:40:11 +00:00
extends Node
# Path to Board class, for instantiating new Boards in code
2021-11-15 07:17:32 +00:00
var Board = preload("res://scenes/Game/Board.tscn")
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
2021-11-15 07:17:32 +00:00
var Setup = preload("res://scenes/Game/Setup.tscn")
2021-11-15 05:50:23 +00:00
# TODO: Move Fire into the Player. See above.
2021-11-15 07:17:32 +00:00
var Fire = preload("res://scenes/Game/Fire.tscn")
signal player_ready
2021-11-15 05:50:23 +00:00
# Player ID of this player
2021-11-08 13:40:11 +00:00
var pid
# board (an instance of the Board class)
2021-11-15 07:17:32 +00:00
onready var board = Board.instance()
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()
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-08 13:40:11 +00:00
# Member functions:
2021-11-08 13:40:11 +00:00
# hit: Called when opponent fires on us.
# 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
# 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 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])
emit_signal("player_ready")
# Add the board to the tree
add_child(board)
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
# 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())
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
# hits every single board tile
2021-11-08 13:40:11 +00:00
func forfeit():
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():
var count = board.get_ship_count()
return count
2021-11-08 13:40:11 +00:00
pass