From a8f96eed9de6d1d238fadbdb9c4a052db89881f1 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Thu, 11 Nov 2021 15:38:50 -0600 Subject: [PATCH] Comment game logic code, and edit for style. --- godot_ship/script/game/Gameplay/Board.gd | 22 +++++++------- godot_ship/script/game/Gameplay/Game.gd | 32 +++++++++++--------- godot_ship/script/game/Gameplay/Player.gd | 21 ++++++------- godot_ship/script/game/Gameplay/Ship.gd | 36 +++++++++++++++-------- 4 files changed, 65 insertions(+), 46 deletions(-) diff --git a/godot_ship/script/game/Gameplay/Board.gd b/godot_ship/script/game/Gameplay/Board.gd index adb134a..6e6e3aa 100644 --- a/godot_ship/script/game/Gameplay/Board.gd +++ b/godot_ship/script/game/Gameplay/Board.gd @@ -3,10 +3,10 @@ extends Node # Path to Ship class, for instantiating new Ships in code onready var Ship = load("res://script/game/Gameplay/Ship.gd") -var bottomBoard # Player board -var topBoard # Opponent board +var bottom_board # Player board +var top_board # Opponent board var ships # list of Ships -var shipCount # number of 'active' (un-sunk) ships +var ship_count # number of 'active' (un-sunk) ships # Called when the node enters the scene tree for the first time. func _ready(): @@ -29,16 +29,18 @@ func getShipCount(): pass func _init(): + # Initialize the bottom_board to a 10x10 array for i in range(10): - bottomBoard.append([]) + bottom_board.append([]) for _i in range(0, 10): - bottomBoard[i].resize(10) + bottom_board[i].resize(10) + # Initialize the top_board to a 10x10 array for i in range(10): - topBoard.append([]) + top_board.append([]) for _i in range(0, 10): - bottomBoard[i].resize(10) + top_board[i].resize(10) +# worldspace_to_boardspace: convert a Vector2 in world-space to board-space func worldspace_to_boardspace(coordinate:Vector2): - coordinate -= Vector2(36, 36) - coordinate /= Vector2(32,32) - return coordinate + # subtract 36 to get the position relative to (0,0) on the board, and integer divide by 32 + return Vector2(int(coordinate.x - 36) >> 5, int(coordinate.y-36) >> 5) diff --git a/godot_ship/script/game/Gameplay/Game.gd b/godot_ship/script/game/Gameplay/Game.gd index 1af66d2..29d7d1d 100644 --- a/godot_ship/script/game/Gameplay/Game.gd +++ b/godot_ship/script/game/Gameplay/Game.gd @@ -3,25 +3,29 @@ extends Node # Path to Player class, for instantiating new Players in code var Player = "res://script/game/Gameplay/Player.gd" +# Array of instances of the Player class; stores the Players var players # = player1, player2, ... -var turnCount = 0 -var wasHit = false -var isMultiplayer = false +# turn counter +var turn = 0 +# Variable transporting hit state between players +var hit = false +# Variable tracking whether a game is multiplayer (so that the correct Player type can be spawned) +# TODO: Multiplayer +var is_multiplayer = false # Called when the node enters the scene tree for the first time. func _ready(): - gameStart() + game_start() +# Member functions: +# game_start: starts the game +func game_start(): + pass -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass +# victory_screen: display the victory screen +func victory_screen(): + pass -func gameStart(): - pass - -func victoryScreen(): - pass - -func displayTurn(): +# display_turn(): display which turn it is on the screen +func display_turn(): pass diff --git a/godot_ship/script/game/Gameplay/Player.gd b/godot_ship/script/game/Gameplay/Player.gd index 72ca7f6..bb122bd 100644 --- a/godot_ship/script/game/Gameplay/Player.gd +++ b/godot_ship/script/game/Gameplay/Player.gd @@ -3,39 +3,40 @@ extends Node # Path to Board class, for instantiating new Boards in code var Board = "res://script/game/Gameplay/Board.gd" +# Player ID of this player var pid +# board (an instance of the Board class) var board # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass - +# Member functions: # hit: Called when opponent fires on us. # Update internal state, and return bool hit/miss func hit(): pass -# placeShip: called when ships are placed. +# place_ship: called when ships are placed. # forwards Ship locations to the Board, so that it may construct a ship -# ship: a list of ship properties {possition, orientation, size} -func placeShips(_ship): +# ship: a list of ship properties {position, orientation, size} +func place_ship(_ship): pass # 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}, ...} -func setUp(_ships): +func set_up(_ships): pass # turnStart: start player's turn # Initiates the player's turn, and blocks until the player selects a location to fire upon -# returns: fire {player, coordinates} +# returns: fire = [player id, target coordinates] func turnStart(): + var player_id = 0 + var target = Vector2(0,0) + return [player_id, target] pass # getBoard: returns the player's board diff --git a/godot_ship/script/game/Gameplay/Ship.gd b/godot_ship/script/game/Gameplay/Ship.gd index a7502f1..c6df16d 100644 --- a/godot_ship/script/game/Gameplay/Ship.gd +++ b/godot_ship/script/game/Gameplay/Ship.gd @@ -2,42 +2,54 @@ extends Node # This is the rendered element of a "ship", generated when the game transitions from the placing state to the gameplay state -# +# Enum denoting the orientation (X is 1, Y is 0) enum Orientation {X = 1, Y = 0} -var size = 0 # Size of ship in units -var position # Coordinates of ship's center. Ship extends [-(size-1 >> 1), (size/2 >> 1)] +# Size of ship in board units +var size +# Coordinates of ship's center. Ship extends [-(size-1 >> 1), (size/2 >> 1)] +var position +# Variable storing whether the ship is sunk, for rendering purposes var sunk = false -var orientation = false +# Orientation of the ship (see enum Orientation) +var orientation = Orientation.Y -# index into ship sprite table +# Ship sprite metadata +# sprite: the texture atlas containing all ship parts +var atlas # = TODO: figure out how to use one sprite for multiple textures +# texture: the offset into the texture atlas of the first part of the ship. var texture = 0 # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass - +# member functions: +# getSize: get the size of the ship, in board-units (2 for 2-ship, 3 for 3-ship, ...) func getSize(): return size +# getPosition: get the position of the ship's center, in board units func getPosition(): return position +# getOrientation: get the orientation of the ship (see enum Orientation) func getOrientation(): return orientation - + +# getSunk: get whether the ship is sunk func getSunk(): return sunk +# setSunk: sink the ship func setSunk(): sunk = true -func _init(in_position = Vector2(0,0), in_size = 2, in_orientation = Orientation.X): +# _init: called on object initialization. Accepts args if called via .new(...) +# in_position: position of the ship, in board-coordinates; (0,0) by default +# in_size: size of the ship, in board-units; 2 by default +# in_orientation: orientation of the ship (see enum Orientation); vertical by default +func _init(in_position = Vector2(0,0), in_size = 2, in_orientation = Orientation.Y): position = in_position size = in_size orientation = in_orientation