Documentation
This commit is contained in:
parent
e1cdf38dfb
commit
b0b659acd4
7 changed files with 51 additions and 20 deletions
|
@ -1,28 +1,42 @@
|
|||
extends Node2D
|
||||
|
||||
# TODO: Make able to hold multiple placement zones, supporting uniform forms
|
||||
|
||||
## Class that contains all structures of platform
|
||||
|
||||
class_name GridController
|
||||
|
||||
const GRID_SIZE : Vector2 = Vector2(16,16)
|
||||
|
||||
## Building zone controller uses for checks and capacity
|
||||
@export var building_zone : PlacementZone
|
||||
var constructions : Array[Structure]
|
||||
|
||||
## Internal structure storage
|
||||
var structures : Array[Structure]
|
||||
|
||||
func _ready() -> void:
|
||||
constructions.resize(building_zone.get_capacity())
|
||||
structures.resize(building_zone.get_capacity())
|
||||
|
||||
func add_construction(construction : Structure) -> bool:
|
||||
var construction_dp = construction.get_dimension_points()
|
||||
for point in construction_dp:
|
||||
if constructions[building_zone.indexify_global_point(construction.global_position + point)]:
|
||||
## Tries to add structure to its internal storage [br]
|
||||
## Returns false if has colliding structures
|
||||
func add_structure(structure : Structure) -> bool:
|
||||
var structure_dp = structure.get_dimension_points()
|
||||
for point in structure_dp:
|
||||
if structures[building_zone.indexify_global_point(structure.global_position + point)]:
|
||||
return false
|
||||
construction.reparent(self)
|
||||
construction.global_position = building_zone.get_placement_position(construction.global_position)
|
||||
for point in construction_dp:
|
||||
constructions[building_zone.indexify_global_point(construction.global_position + point)] = construction
|
||||
structure.reparent(self)
|
||||
structure.global_position = building_zone.get_placement_position(structure.global_position)
|
||||
|
||||
for point in structure_dp:
|
||||
structures[building_zone.indexify_global_point(structure.global_position + point)] = structure
|
||||
return true
|
||||
|
||||
## Returns structure at point. [br]
|
||||
## Returns null if no structure is at point
|
||||
func get_at(point : Vector2) -> Structure:
|
||||
return constructions[building_zone.indexify_global_point(point)]
|
||||
var index = building_zone.indexify_global_point(point)
|
||||
if index == -1:
|
||||
return null
|
||||
return structures[index]
|
||||
|
||||
func is_point_occupied(point : Vector2) -> bool:
|
||||
return get_at(point) != null
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
extends Node2D
|
||||
|
||||
## Currently held structure
|
||||
var held_construction : Structure
|
||||
|
||||
func _ready() -> void:
|
||||
|
|
|
@ -15,7 +15,7 @@ class_name PlacementZone
|
|||
get:
|
||||
return building_rect
|
||||
|
||||
@export var entity_holder : GridController
|
||||
@export var grid_controller : GridController
|
||||
|
||||
func _ready() -> void:
|
||||
if not Engine.is_editor_hint():
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
extends Resource
|
||||
|
||||
## Player instantiatable resource
|
||||
|
||||
class_name Prototype
|
||||
|
||||
## Scene to instantiate
|
||||
@export var scene : PackedScene
|
||||
|
||||
## UI Preview
|
||||
@export var preview : Texture2D
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
@tool
|
||||
extends Node2D
|
||||
|
||||
## Game object that interact with other structures in its grid space
|
||||
class_name Structure
|
||||
|
||||
## Dimensions of structure in grid tiles
|
||||
@export var dimensions : Rect2i = Rect2i(0,0,1,1):
|
||||
set(value):
|
||||
dimensions = value
|
||||
|
@ -11,30 +13,39 @@ class_name Structure
|
|||
get:
|
||||
return dimensions
|
||||
|
||||
## 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:
|
||||
return get_parent().get_at(global_position+dv)
|
||||
return get_parent().get_at(global_position+dv*Globals.GRID_SIZE)
|
||||
|
||||
## 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.entity_holder.is_point_occupied(point):
|
||||
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.entity_holder.add_construction(self)
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue