mirror of
https://github.com/JohnBreaux/Boat-Battle.git
synced 2025-02-04 12:28:35 +00:00
Game logic (#14)
* Create game classes * Pushing what I've done of the game logic. * Commented my code Deals with ship placement on board and ship rotation on board * Comment game logic code, and edit for style. * Added Clear board button and Confirm Placement button * Fix up some stuff * Pass * I accidentally the buttons + Fix some hitbox mistakes. * Improve hitbox leniency, and don't allow ships to rotate into each other. * Fixed Dialog Box that pops up for Confirm Placement * Update Gameplay.tscn * Partial fire functionality added. Currently hides all ship placement UI and makes visible all fire functionality. The crosshair currently does not snap to a grid. The ships are saved in an area of location objects holding the length, orientation, and coordinates of the ship. * Crosshair now snaps into place * Created Sprites for 2Ship and 3ShipA * Created Sprite for Ship3B * Created Sprite for Ship4 * Created Sprite for Ship5 * Created Sprite for "Hit" * Created Sprites for Ship2 Destroyed and Ship3A Destroyed * Created Sprite for Ship3B Destroyed * Created Sprite for Ship4 Destroyed * Created Sprite for Ship5 Destroyed * Created Sprite for "Miss" * Fixed blurry pixel art Changed texture rendering * Added temporary title screen text * Major restructuring to better support object-oriented design of the main game logic and UI. This was necessary, I swear. * Accidentally offset the Setup menu by a handful of pixels. Whoops. * Setup: remove unused signal * Comment on which parts of Game are only there for testing purposes. * Fixed up the Title Screen text imported font * Fixed Crosshair pixel rendering * Consolidate backgrounds * Added more SFX sounds * Created Sprite for Light Mode * Fix null deref when entering singleplayer after exiting multiplayer * Hien's work * Revert "Hien's work" This reverts commitf099ed6c80. * Stuff * Make git happy by pushing gdengine imports * Put Background in main * Removed option background * Added Confirmation Dialog for Forfeit * Revert "Merge branch 'game-logic' of https://github.com/StatewideInk/Group12 into game-logic" This reverts commit104ffaab0a, reversing changes made to63dec75688. * Fix title screen never going (whoops * Oops I made the background invisible * removed option background again * Added dark/light mode functionality * Added signal for audio SFX * Load the theme on startup * Added Confirmation dialog for Forfeit * Fixed Texture Rendering for Light Mode Background * Sounds no longer scale linearly * Remove unused resourse main.tres * Added victory screen Added a victory screen. Since there is no current way to win, there is a 'win' button added to the top left of the game screen. Once clicked, victory text will appear and the player will be given the option to return to the main menu. * restart button is invisible Co-authored-by: John Breaux <JohnBreaux@my.unt.edu> Co-authored-by: JohnBreaux <89870642+JohnBreaux@users.noreply.github.com> Co-authored-by: hpham474 <HienPham@my.unt.edu> Co-authored-by: Chance Atkinson <chancewatkinson@gmail.com>
This commit is contained in:
54
godot_ship/script/game/Gameplay/Board.gd
Normal file
54
godot_ship/script/game/Gameplay/Board.gd
Normal file
@@ -0,0 +1,54 @@
|
||||
extends Node
|
||||
|
||||
# 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
|
||||
|
||||
# a board is square. This is its side lengths
|
||||
var board_len = 10
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
ships = []
|
||||
ship_count = 0
|
||||
|
||||
# TODO: What state?
|
||||
func getState():
|
||||
pass
|
||||
|
||||
# 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 getBottomBoard():
|
||||
pass
|
||||
|
||||
func getShipCount():
|
||||
return ship_count
|
||||
|
||||
func _init():
|
||||
# Initialize the bottom_board to a 10x10 array
|
||||
for _row in range(board_len):
|
||||
bottom_board.append([])
|
||||
for column in bottom_board:
|
||||
column.resize(10)
|
||||
# Initialize the top_board to a 10x10 array
|
||||
for _row in range(board_len):
|
||||
top_board.append([])
|
||||
for column in top_board:
|
||||
column.resize(board_len)
|
||||
|
||||
# worldspace_to_boardspace: convert a Vector2 in world-space to board-space
|
||||
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
|
||||
58
godot_ship/script/game/Gameplay/Crosshair.gd
Normal file
58
godot_ship/script/game/Gameplay/Crosshair.gd
Normal file
@@ -0,0 +1,58 @@
|
||||
extends Sprite
|
||||
|
||||
|
||||
var snapped = false #when snapped if true crosshair stops following mouse
|
||||
const world_offset = Vector2(36,36)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
# Move the cursor to 0,0
|
||||
position = board_to_world_space(Vector2(-2,-2))
|
||||
pass # Replace with function body.
|
||||
|
||||
func _physics_process(_delta):
|
||||
var mousePos = get_global_mouse_position()
|
||||
# If the cursor is not snapped, and the mouse is over the board
|
||||
if snapped == false and validate_position(mousePos):
|
||||
# Snap the crosshair to the grid, but following the mouse
|
||||
position = (mousePos - world_offset).snapped(Vector2(32,32)) + world_offset
|
||||
|
||||
func _input(event):
|
||||
# Check if left click is being clicked and the sprite is visible (i.e only checks for inputs after ship positions are confirmed)
|
||||
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and visible and not event.is_pressed():
|
||||
# Make a noise
|
||||
AudioBus.emit_signal("button_clicked")
|
||||
# Locks the position of the crosshair with left click release
|
||||
if validate_position(position) == true:
|
||||
# rounds the board position to the nearest integer
|
||||
snapped = true
|
||||
position.x = int(round(world_to_board_space(position).x))
|
||||
position.y = int(round(world_to_board_space(position).y))
|
||||
position = board_to_world_space(position)
|
||||
# Check if left click is being clicked and the sprite is visible (i.e only checks for inputs after ship positions are confirmed)
|
||||
elif event is InputEventMouseButton and event.button_index == BUTTON_LEFT and visible == true:
|
||||
# Unlocks the position of the crosshair with left click
|
||||
snapped = false
|
||||
|
||||
func validate_position(vector):
|
||||
# rounds the board position to the nearest integer
|
||||
var board = world_to_board_space(vector)
|
||||
# Checks if the board position is within bounds of the board
|
||||
if board.x < 9.5 and board.x >= -0.5 and board.y < 9.5 and board.y >= -0.5:
|
||||
# changes the position of the crosshair
|
||||
return true
|
||||
else:
|
||||
# unlocks the crosshair if not within bounds
|
||||
return false
|
||||
|
||||
# Convert the world-space coordinates to positions on the board
|
||||
func world_to_board_space(vector):
|
||||
# Do math
|
||||
var res = (vector - world_offset) / 32 # Basically Fahrenheit/Celcius conversion, but in 2D
|
||||
return res
|
||||
|
||||
# Inverse of the above function.
|
||||
func board_to_world_space(vector):
|
||||
# Do math
|
||||
var res = (vector * 32) + world_offset #Invert the above function
|
||||
return res #Truncate decimals
|
||||
30
godot_ship/script/game/Gameplay/Fire.gd
Normal file
30
godot_ship/script/game/Gameplay/Fire.gd
Normal file
@@ -0,0 +1,30 @@
|
||||
extends Control
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
# Signal to pass the fire location back to yet-unknown nodes
|
||||
signal fire_at
|
||||
|
||||
|
||||
func _on_Fire_pressed():
|
||||
var crosshair = get_node("Crosshair")
|
||||
# hides crosshair
|
||||
crosshair.visible = false
|
||||
if crosshair.validate_position(crosshair.position) == true:
|
||||
var crosshair_pos = crosshair.world_to_board_space(crosshair.position)
|
||||
# fires at position
|
||||
print("Fire at position: ", crosshair_pos)
|
||||
emit_signal("fire_at", crosshair_pos)
|
||||
# Close the Firing menu
|
||||
queue_free()
|
||||
else:
|
||||
#if invalid position popup appears
|
||||
var dialog = get_node("FireDialog")
|
||||
dialog.popup_centered()
|
||||
pass # Replace with function body.
|
||||
|
||||
func _on_FireDialog_confirmed():
|
||||
get_node("Crosshair").visible = true
|
||||
pass # Replace with function body.
|
||||
75
godot_ship/script/game/Gameplay/Game.gd
Normal file
75
godot_ship/script/game/Gameplay/Game.gd
Normal file
@@ -0,0 +1,75 @@
|
||||
extends Node
|
||||
|
||||
class ShipData:
|
||||
var Coor: Vector2
|
||||
var Length: int
|
||||
var Orientation: bool #vertical is true, (Trueship = vertical) (Falseship = horizontal)
|
||||
|
||||
# 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
|
||||
onready var Setup = preload("res://scenes/Game/Setup.tscn")
|
||||
# TODO: Move Fire into the Player. See above.
|
||||
onready var Fire = preload("res://scenes/Game/Fire.tscn")
|
||||
|
||||
# Path to Player class, for instantiating new Players in code
|
||||
onready var Player = preload("res://scenes/Game/Player.tscn")
|
||||
|
||||
onready var Victory = preload("res://scenes/Game/Victory.tscn")
|
||||
|
||||
|
||||
# Array of instances of the Player class; stores the Players
|
||||
var players # = player1, player2, ...
|
||||
# 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():
|
||||
# TODO: Move Setup into the Player.
|
||||
var setup = Setup.instance()
|
||||
setup.connect("game_ready", self, "game_setup")
|
||||
add_child(setup)
|
||||
|
||||
get_node("ConfirmationDialog").get_ok().text = "Yes"
|
||||
get_node("ConfirmationDialog").get_cancel().text = "No"
|
||||
|
||||
# TODO: Move Setup into the Player.
|
||||
func game_setup(_ships):
|
||||
print_debug("Congrats! Setup complete.")
|
||||
# TODO: Move Fire into the Player.
|
||||
add_child(Fire.instance())
|
||||
|
||||
# Member functions:
|
||||
# game_start: starts the game
|
||||
func game_start():
|
||||
pass
|
||||
|
||||
# victory_screen: display the victory screen
|
||||
func victory_screen():
|
||||
# TODO: Create the victory screen, fill it with knowledge
|
||||
pass
|
||||
|
||||
# display_turn(): display which turn it is on the screen
|
||||
func display_turn():
|
||||
# TODO: Update the turn display, if there is one?
|
||||
pass
|
||||
|
||||
func _on_Forfeit_pressed():
|
||||
AudioBus.emit_signal("button_clicked")
|
||||
get_node("ConfirmationDialog").popup()
|
||||
|
||||
func end():
|
||||
queue_free()
|
||||
|
||||
func _on_Button_button_down():
|
||||
AudioBus.emit_signal("button_clicked")
|
||||
var victory = Victory.instance()
|
||||
add_child(victory)
|
||||
victory.connect("exit_main", self, "end")
|
||||
|
||||
func _on_ConfirmationDialog_confirmed():
|
||||
end()
|
||||
55
godot_ship/script/game/Gameplay/Player.gd
Normal file
55
godot_ship/script/game/Gameplay/Player.gd
Normal file
@@ -0,0 +1,55 @@
|
||||
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)
|
||||
onready var board = Board.new()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
# Member functions:
|
||||
# hit: Called when opponent fires on us.
|
||||
# Update internal state, and return bool hit/miss
|
||||
func hit():
|
||||
pass
|
||||
|
||||
# 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 {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 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 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
|
||||
# returns: board
|
||||
func getBoard():
|
||||
return board
|
||||
|
||||
# forfeit: ends game for player
|
||||
# Sinks all ships
|
||||
func forfeit():
|
||||
pass
|
||||
|
||||
# getShipCount: get the number of ships the player has left alive
|
||||
func getShipCount():
|
||||
pass
|
||||
|
||||
55
godot_ship/script/game/Gameplay/Ship.gd
Normal file
55
godot_ship/script/game/Gameplay/Ship.gd
Normal file
@@ -0,0 +1,55 @@
|
||||
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}
|
||||
|
||||
# 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
|
||||
# Orientation of the ship (see enum Orientation)
|
||||
var orientation = Orientation.Y
|
||||
|
||||
# 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.
|
||||
|
||||
# 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
|
||||
|
||||
# _init: called on object initialization. Accepts args if called via <Ship>.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
|
||||
Reference in New Issue
Block a user