2021-11-14 08:12:31 +00:00
|
|
|
extends Control
|
2021-11-08 13:40:11 +00:00
|
|
|
|
2021-11-11 20:38:10 +00:00
|
|
|
# This is the rendered element of a "ship", generated when the game transitions from the placing state to the gameplay state
|
|
|
|
|
2021-11-14 07:21:40 +00:00
|
|
|
# Enum denoting the orientation (X is 0, Y is 1)
|
|
|
|
enum Orientation {X = 0, Y = 1}
|
2021-11-08 13:40:11 +00:00
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# Size of ship in board units
|
2021-11-14 12:32:46 +00:00
|
|
|
var size = 2
|
2021-11-11 21:38:50 +00:00
|
|
|
# Coordinates of ship's center. Ship extends [-(size-1 >> 1), (size/2 >> 1)]
|
2021-11-14 12:32:46 +00:00
|
|
|
var boardposition = Vector2(-1,-1)
|
2021-11-11 21:38:50 +00:00
|
|
|
# Variable storing whether the ship is sunk, for rendering purposes
|
2021-11-08 13:40:11 +00:00
|
|
|
var sunk = false
|
2021-11-11 21:38:50 +00:00
|
|
|
# Orientation of the ship (see enum Orientation)
|
|
|
|
var orientation = Orientation.Y
|
2021-11-14 09:15:27 +00:00
|
|
|
# array of spots thats been hit
|
|
|
|
var hit = []
|
2021-11-08 13:40:11 +00:00
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# Ship sprite metadata
|
|
|
|
# sprite: the texture atlas containing all ship parts
|
|
|
|
var atlas # = TODO: figure out how to use one sprite for multiple textures
|
2021-11-14 09:15:27 +00:00
|
|
|
# variant: for ship 3. A is 0, B is 1
|
|
|
|
var variant = 0
|
2021-11-08 13:40:11 +00:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
|
|
|
pass # Replace with function body.
|
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# member functions:
|
|
|
|
# getSize: get the size of the ship, in board-units (2 for 2-ship, 3 for 3-ship, ...)
|
2021-11-08 13:40:11 +00:00
|
|
|
func getSize():
|
|
|
|
return size
|
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# getPosition: get the position of the ship's center, in board units
|
2021-11-08 13:40:11 +00:00
|
|
|
func getPosition():
|
2021-11-14 09:15:27 +00:00
|
|
|
return boardposition
|
2021-11-08 13:40:11 +00:00
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# getOrientation: get the orientation of the ship (see enum Orientation)
|
2021-11-08 13:40:11 +00:00
|
|
|
func getOrientation():
|
|
|
|
return orientation
|
2021-11-11 21:38:50 +00:00
|
|
|
|
|
|
|
# getSunk: get whether the ship is sunk
|
2021-11-08 13:40:11 +00:00
|
|
|
func getSunk():
|
|
|
|
return sunk
|
|
|
|
|
2021-11-14 09:15:27 +00:00
|
|
|
# returns an array of the positions that the ship occupies
|
2021-11-14 07:21:40 +00:00
|
|
|
func getExtent():
|
|
|
|
var extent = []
|
2021-11-14 13:14:35 +00:00
|
|
|
# Find each tile of the ship
|
2021-11-14 12:32:46 +00:00
|
|
|
for i in size:
|
2021-11-14 13:14:35 +00:00
|
|
|
# Calculate the x axis position
|
|
|
|
var x = boardposition.x - (1 - orientation) * ( (size - 1) / 2 - i )
|
|
|
|
# Calculate the y axis position
|
|
|
|
var y = boardposition.y - orientation * ( (size - 1) / 2 - i )
|
|
|
|
# Append the point onto the array
|
|
|
|
extent.push_back(Vector2(x,y))
|
2021-11-14 07:21:40 +00:00
|
|
|
return extent
|
2021-11-14 13:14:35 +00:00
|
|
|
|
2021-11-14 09:15:27 +00:00
|
|
|
# generates a texture at the spot (index should start at 0)
|
|
|
|
func texture(index):
|
|
|
|
var state = 0 # floating
|
|
|
|
if(hit[index]):
|
|
|
|
state = 1 # sunk
|
|
|
|
var textureSize = 32
|
|
|
|
var t = AtlasTexture.new()
|
|
|
|
t.atlas = load("res://assets/game/TextureAtlas.png")
|
|
|
|
t.region (
|
|
|
|
(size * textureSize) * variant + (32 * index),
|
|
|
|
(size - 2) * textureSize * 2 + (32 * state),
|
|
|
|
textureSize,
|
|
|
|
textureSize
|
|
|
|
)
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# setSunk: sink the ship
|
2021-11-08 13:40:11 +00:00
|
|
|
func setSunk():
|
|
|
|
sunk = true
|
|
|
|
|
2021-11-11 21:38:50 +00:00
|
|
|
# _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):
|
2021-11-14 09:15:27 +00:00
|
|
|
boardposition = in_position
|
2021-11-08 13:40:11 +00:00
|
|
|
size = in_size
|
|
|
|
orientation = in_orientation
|