From 4730bf773387fd94e6dca5173c246130ea3ff504 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Sun, 14 Nov 2021 14:06:09 -0600 Subject: [PATCH] Board: Start initial implementation. --- godot_ship/scenes/Game/Board.tscn | 13 ++++ godot_ship/script/game/Gameplay/Board.gd | 86 ++++++++++++++++++------ 2 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 godot_ship/scenes/Game/Board.tscn diff --git a/godot_ship/scenes/Game/Board.tscn b/godot_ship/scenes/Game/Board.tscn new file mode 100644 index 0000000..3410f58 --- /dev/null +++ b/godot_ship/scenes/Game/Board.tscn @@ -0,0 +1,13 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://script/game/Gameplay/Board.gd" type="Script" id=1] +[ext_resource path="res://assets/game/board_dark.png" type="Texture" id=2] + +[node name="Board" type="Node2D"] +position = Vector2( 36, 36 ) +script = ExtResource( 1 ) + +[node name="TempBoardSprite" type="Sprite" parent="."] +texture = ExtResource( 2 ) +centered = false +offset = Vector2( -18, -18 ) diff --git a/godot_ship/script/game/Gameplay/Board.gd b/godot_ship/script/game/Gameplay/Board.gd index de71f89..9096a6f 100644 --- a/godot_ship/script/game/Gameplay/Board.gd +++ b/godot_ship/script/game/Gameplay/Board.gd @@ -1,36 +1,83 @@ -extends Node +extends Node2D # Path to Ship class, for instantiating new Ships in code onready var Ship = load("res://script/game/Gameplay/Ship.gd") -var bottom_board # Player board -var top_board # Opponent board -var ships # list of Ships -var ship_count # number of 'active' (un-sunk) ships +# Consts and enums +const NO_SHIP = -1 +enum {MISS = -1, READY = 0, HIT = 1, SUNK = 2} -# a board is square. This is its side lengths +var bottom_board:Array # Player board +var top_board:Array # Opponent board +var ships = [] # list of Ships +var ship_count = 0 # number of 'active' (un-sunk) ships + +# a board is square. This is its side length var board_len = 10 -# Called when the node enters the scene tree for the first time. -func _ready(): - ships = [] - ship_count = 0 +# The top board must be marked by textures. This is where they are stored: +var sprites = [] + +# Here are where the hit/miss textures are loaded, so that they may be used: +var hit_texture = preload("res://assets/game/Hit.png") +var miss_texture = preload("res://assets/game/Miss.png") + # TODO: What state? -func getState(): +func get_state(): pass +# Evaluate being hit by an opponent +# pos: board position opponent fired at +func hit(pos): + var res = MISS + # Get the ship-metadata for that location + var ship = bottom_board[pos.x][pos.y] + # If there's a ship there, which exists, and hasn't been hit, + if ship and ship[0] > NO_SHIP and ship[1] == READY: + # Hit the ship, and store whether HIT or SUNK + res = ships[ship[0]].hit(pos) + # TODO: display KABOOM + # Update the ship + ships[ship[0]].update() + # Mark the ship as hit + ship[1] = HIT + else: + # Mark that position as a miss, with no ship + bottom_board[pos.x][pos.y] = [NO_SHIP, MISS] + # If ship sunk, + if res == SUNK: + # remove it from the count + ship_count -= 1 + return res + +# fire: Store the results of firing on an opponent +# pos: board position fired on +# res: result of firing on the opponent +func fire(pos, res): + if top_board[pos.x][pos.y] == null: + top_board[pos.x][pos.y] = res + return true + return false + # Place a ship on the board at board-space coordinates -func placeShip(in_position, in_size, in_orientation): - ships.append(Ship.new(in_position, in_size, in_orientation)) - pass +func place_ship(in_position, in_size, in_orientation, in_variant = 0): + var ship = Ship.new(in_position, in_size, in_orientation, in_variant) + for pos in ship.get_extent(): + bottom_board[pos.x][pos.y] = [ships.size(), READY] + ships.append(ship) + ship_count += 1 + add_child(ship) -func getBottomBoard(): - pass +# Not sure why this is necessary yet +func get_bottom_board(): + return bottom_board -func getShipCount(): +# Get the number of live ships +func get_ship_count(): return ship_count +# _init: Constructor func _init(): # Initialize the bottom_board to a 10x10 array for _row in range(board_len): @@ -47,8 +94,3 @@ func _init(): func worldspace_to_boardspace(coordinate:Vector2): # 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) - -# Coordinates of ship's center. Ship extends [-(size-1 >> 1), (size/2 >> 1)] -func shiptoboard(ship:Ship): - for i in range (ship.) - pass