mirror of
https://github.com/JohnBreaux/Boat-Battle.git
synced 2024-11-15 05:25:57 +00:00
Ship: Complete initial implementation. Impl's #16
This commit is contained in:
parent
6758dc048e
commit
fd51682eb0
@ -2,10 +2,5 @@
|
|||||||
|
|
||||||
[ext_resource path="res://script/game/Gameplay/Ship.gd" type="Script" id=1]
|
[ext_resource path="res://script/game/Gameplay/Ship.gd" type="Script" id=1]
|
||||||
|
|
||||||
[node name="Ship" type="Control"]
|
[node name="Ship" type="Node2D"]
|
||||||
anchor_right = 1.0
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
script = ExtResource( 1 )
|
script = ExtResource( 1 )
|
||||||
__meta__ = {
|
|
||||||
"_edit_use_anchors_": false
|
|
||||||
}
|
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
extends Control
|
extends Node2D
|
||||||
|
|
||||||
# This is the rendered element of a "ship", generated when the game transitions from the placing state to the gameplay state
|
# 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 0, Y is 1)
|
# Enum denoting the orientation (X is 0, Y is 1)
|
||||||
enum Orientation {X = 0, Y = 1}
|
enum Orientation {X = 0, Y = 1}
|
||||||
|
enum {MISS = -1, READY = 0, HIT = 1, SUNK = 2}
|
||||||
|
|
||||||
# Size of ship in board units
|
# Size of ship in board units
|
||||||
var size = 2
|
var size
|
||||||
|
var health
|
||||||
# Coordinates of ship's center. Ship extends [-(size-1 >> 1), (size/2 >> 1)]
|
# Coordinates of ship's center. Ship extends [-(size-1 >> 1), (size/2 >> 1)]
|
||||||
var boardposition = Vector2(-1,-1)
|
var boardposition = Vector2(-1,-1)
|
||||||
# Variable storing whether the ship is sunk, for rendering purposes
|
# Variable storing whether the ship is sunk, for rendering purposes
|
||||||
@ -15,11 +17,16 @@ var sunk = false
|
|||||||
var orientation = Orientation.Y
|
var orientation = Orientation.Y
|
||||||
# array of spots thats been hit
|
# array of spots thats been hit
|
||||||
var hit = []
|
var hit = []
|
||||||
|
# Variable storing the positions of each piece of the ship
|
||||||
|
var extents = []
|
||||||
|
|
||||||
# Ship sprite metadata
|
# Ship sprite metadata
|
||||||
# sprite: the texture atlas containing all ship parts
|
# atlas: the texture atlas containing all ship parts
|
||||||
var atlas # = TODO: figure out how to use one sprite for multiple textures
|
var atlas = preload("res://assets/game/TextureAtlas.png")
|
||||||
# variant: for ship 3. A is 0, B is 1
|
# sprites: the individual sprite nodes which make up the ship
|
||||||
|
var sprites = []
|
||||||
|
# variant: Ships of the same length can have different textures
|
||||||
|
# variant A is 0, variant B is 1, ...
|
||||||
var variant = 0
|
var variant = 0
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
@ -27,24 +34,43 @@ func _ready():
|
|||||||
pass # Replace with function body.
|
pass # Replace with function body.
|
||||||
|
|
||||||
# member functions:
|
# member functions:
|
||||||
# getSize: get the size of the ship, in board-units (2 for 2-ship, 3 for 3-ship, ...)
|
# getShip():
|
||||||
func getSize():
|
func get_ship():
|
||||||
return size
|
return [boardposition, size, orientation, variant]
|
||||||
|
|
||||||
# getPosition: get the position of the ship's center, in board units
|
|
||||||
func getPosition():
|
|
||||||
return boardposition
|
|
||||||
|
|
||||||
# getOrientation: get the orientation of the ship (see enum Orientation)
|
|
||||||
func getOrientation():
|
|
||||||
return orientation
|
|
||||||
|
|
||||||
# getSunk: get whether the ship is sunk
|
# getSunk: get whether the ship is sunk
|
||||||
func getSunk():
|
func get_sunk():
|
||||||
return sunk
|
return sunk
|
||||||
|
|
||||||
|
func hit(pos):
|
||||||
|
# Assume the opponent missed
|
||||||
|
var res = MISS
|
||||||
|
# Find the position in the extents
|
||||||
|
var index = extents.find(pos)
|
||||||
|
# If that position exists:
|
||||||
|
if (index > -1):
|
||||||
|
# Hit the ship piece at that location
|
||||||
|
hit[index] = true
|
||||||
|
res = HIT
|
||||||
|
# Decrement its health
|
||||||
|
health -= 1
|
||||||
|
# If there's no more health,
|
||||||
|
if health == 0:
|
||||||
|
# Sink the ship.
|
||||||
|
set_sunk()
|
||||||
|
res = SUNK
|
||||||
|
return res
|
||||||
|
|
||||||
|
# update: (re)calculates extents and textures
|
||||||
|
func update():
|
||||||
|
# Calculate the extents (shouldn't change)
|
||||||
|
extents = get_extent()
|
||||||
|
# Update the textures
|
||||||
|
for i in size:
|
||||||
|
texture(i)
|
||||||
|
|
||||||
# returns an array of the positions that the ship occupies
|
# returns an array of the positions that the ship occupies
|
||||||
func getExtent():
|
func get_extent():
|
||||||
var extent = []
|
var extent = []
|
||||||
# Find each tile of the ship
|
# Find each tile of the ship
|
||||||
for i in size:
|
for i in size:
|
||||||
@ -56,32 +82,65 @@ func getExtent():
|
|||||||
extent.push_back(Vector2(x,y))
|
extent.push_back(Vector2(x,y))
|
||||||
return extent
|
return extent
|
||||||
|
|
||||||
# generates a texture at the spot (index should start at 0)
|
# Update textures
|
||||||
func texture(index):
|
func texture(index):
|
||||||
var state = 0 # floating
|
var state = 0 # ready
|
||||||
if(hit[index]):
|
if(hit[index]):
|
||||||
state = 1 # sunk
|
state = 1 # hit
|
||||||
var textureSize = 32
|
var textureSize = 32
|
||||||
|
# It's okay to create a new texture every time, as resources are refcounted
|
||||||
var t = AtlasTexture.new()
|
var t = AtlasTexture.new()
|
||||||
t.atlas = load("res://assets/game/TextureAtlas.png")
|
t.set_atlas(atlas)
|
||||||
t.region (
|
t.margin = Rect2(0, 0, 32, 32)
|
||||||
(size * textureSize) * variant + (32 * index),
|
t.region = Rect2(
|
||||||
|
(size * variant + index) * textureSize,
|
||||||
|
#(size * textureSize) * variant + (32 * index),
|
||||||
(size - 2) * textureSize * 2 + (32 * state),
|
(size - 2) * textureSize * 2 + (32 * state),
|
||||||
textureSize,
|
textureSize,
|
||||||
textureSize
|
textureSize
|
||||||
)
|
)
|
||||||
|
# Create a new Sprite to house the texture, or use the existing sprite
|
||||||
pass
|
var sprite = sprites[index]
|
||||||
|
if sprite == null:
|
||||||
|
sprite = Sprite.new()
|
||||||
|
sprite.texture = t
|
||||||
|
# This is relative to the ship
|
||||||
|
# (index + 1) => Index, but 1-based
|
||||||
|
# (floor((size-1)/2) => Offset from edge to 'center' of ship
|
||||||
|
# Vector2(0.5,0.5) => Center the texture on the axis of rotation
|
||||||
|
# 32 => Converts from board-units to pixels
|
||||||
|
sprite.position = Vector2((index + 1) - (floor((size-1)/2) + 0.5), 0.5) * textureSize
|
||||||
|
sprite.rotation = 0
|
||||||
|
# Add the sprite to the "sprites" group, persistently
|
||||||
|
sprite.add_to_group("Ship Sprites", true)
|
||||||
|
# Add the sprite node to an array so it can be modified later, unless it's already there
|
||||||
|
if not sprites[index]:
|
||||||
|
sprites[index] = sprite
|
||||||
|
add_child(sprite)
|
||||||
|
|
||||||
# setSunk: sink the ship
|
# setSunk: sink the ship
|
||||||
func setSunk():
|
func set_sunk():
|
||||||
sunk = true
|
sunk = true
|
||||||
|
|
||||||
# _init: called on object initialization. Accepts args if called via <Ship>.new(...)
|
# _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_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_size: size of the ship, in board-units; 2 by default
|
||||||
# in_orientation: orientation of the ship (see enum Orientation); vertical 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):
|
# in_variant: Which ship is this?
|
||||||
|
func _init(in_position = Vector2(0,0), in_size = 0, in_orientation = Orientation.Y, in_variant = 0):
|
||||||
|
# Set the ship's positions
|
||||||
boardposition = in_position
|
boardposition = in_position
|
||||||
|
position = boardposition * 32
|
||||||
|
# Set the ship's size and health
|
||||||
size = in_size
|
size = in_size
|
||||||
|
health = size
|
||||||
|
# Set the ship's orientation/rotation
|
||||||
orientation = in_orientation
|
orientation = in_orientation
|
||||||
|
rotation = orientation * PI/2
|
||||||
|
# Set the ship's variant(A, B, ... )
|
||||||
|
variant = in_variant
|
||||||
|
# Resize the size-based arrays
|
||||||
|
hit.resize(in_size)
|
||||||
|
sprites.resize(in_size)
|
||||||
|
# Update the extents and draw the textures
|
||||||
|
update()
|
||||||
|
Loading…
Reference in New Issue
Block a user