98 lines
2.7 KiB
GDScript
98 lines
2.7 KiB
GDScript
@tool
|
|
extends Node2D
|
|
|
|
## Game object that interact with other structures in its grid space
|
|
class_name Structure
|
|
|
|
enum Facing {
|
|
UNDIRECTIONAL = 0,
|
|
RIGHT = 1,
|
|
DOWN = 2,
|
|
LEFT = 3,
|
|
UP = 4
|
|
}
|
|
|
|
signal switched_facing(to: Facing)
|
|
|
|
## Dimensions of structure in grid tiles
|
|
@export var dimensions : Rect2i = Rect2i(0,0,1,1):
|
|
set(value):
|
|
dimensions = value
|
|
if Engine.is_editor_hint():
|
|
queue_redraw()
|
|
get:
|
|
return dimensions
|
|
|
|
## Inventory of this structure
|
|
@export var inventory : Inventory
|
|
@export var facing : Facing
|
|
|
|
static func facing_to_vector(face : Facing) -> Vector2:
|
|
match face:
|
|
Facing.RIGHT:
|
|
return Vector2.RIGHT
|
|
Facing.LEFT:
|
|
return Vector2.LEFT
|
|
Facing.UP:
|
|
return Vector2.UP
|
|
Facing.DOWN:
|
|
return Vector2.DOWN
|
|
_:
|
|
return Vector2.ZERO
|
|
|
|
## Debug draw of points
|
|
func _draw() -> void:
|
|
if Engine.is_editor_hint():
|
|
for x in range(dimensions.size.x):
|
|
for y in range(dimensions.size.y):
|
|
draw_circle((dimensions.position+Vector2i(x,y)) * Vector2i(Globals.GRID_SIZE),2,Color.AQUA)
|
|
|
|
## Get structure at tile coordinates relative to this structure [br]
|
|
## dv : Vector2 - get position in tiles [br]
|
|
func get_relative(dv : Vector2) -> Structure:
|
|
if get_parent() is GridController:
|
|
return get_parent().get_at(global_position+dv*Globals.GRID_SIZE)
|
|
return null
|
|
|
|
## Check structure is in zone and does not collide with any other structures [br]
|
|
## Returns true if structure is in zone and does not collide with any other structure [br]
|
|
## Returns false if structure is not in zone or does not collide with any other structure
|
|
func can_be_placed(zone : PlacementZone) -> bool:
|
|
for dp in get_dimension_points():
|
|
var point = global_position + dp
|
|
if zone.is_global_point_in_zone(point) == false:
|
|
return false
|
|
if zone.grid_controller.is_point_occupied(point):
|
|
return false
|
|
return true
|
|
|
|
## Tries to place structure in zone's grid controller
|
|
## Returns false if structure is not in zone or collides with any structure
|
|
func try_place(zone : PlacementZone) -> bool:
|
|
if can_be_placed(zone) == false:
|
|
return false
|
|
|
|
return zone.grid_controller.add_structure(self)
|
|
|
|
## Returns array of integer points in dimensions
|
|
func get_dimension_points() -> Array[Vector2]:
|
|
var result : Array[Vector2] = []
|
|
result.resize(dimensions.size.x*dimensions.size.y)
|
|
for x in range(dimensions.size.x):
|
|
for y in range(dimensions.size.y):
|
|
result[x + y * dimensions.size.x] = (Vector2(x,y)*Globals.GRID_SIZE + Vector2(dimensions.position))
|
|
return result
|
|
|
|
func set_facing(to : Facing) -> void:
|
|
facing = to
|
|
switched_facing.emit(facing)
|
|
|
|
func cycle_up_facing() -> void:
|
|
if facing == Facing.UNDIRECTIONAL:
|
|
return
|
|
set_facing(wrapi(facing+1,1,5))
|
|
|
|
func cycle_down_facing() -> void:
|
|
if facing == Facing.UNDIRECTIONAL:
|
|
return
|
|
set_facing(wrapi(facing-1,1,5))
|