61 lines
2.2 KiB
GDScript
61 lines
2.2 KiB
GDScript
@tool
|
|
@warning_ignore_start("integer_division")
|
|
|
|
## Class that helps to manage construction sites
|
|
extends Marker2D
|
|
|
|
class_name PlacementZone
|
|
|
|
## Rect that used for bounds check and conversions
|
|
@export var building_rect : Rect2:
|
|
set(value):
|
|
building_rect = value
|
|
if Engine.is_editor_hint():
|
|
queue_redraw()
|
|
get:
|
|
return building_rect
|
|
|
|
@export var entity_holder : GridController
|
|
|
|
func _ready() -> void:
|
|
if not Engine.is_editor_hint():
|
|
RuntimePlayerData.build_zones.append(self)
|
|
|
|
func _draw() -> void:
|
|
if Engine.is_editor_hint() and EditorInterface.get_inspector().get_edited_object() == self:
|
|
draw_rect(building_rect,Color.CYAN,false,1)
|
|
|
|
## Returns true if point (local coordinates) in rect, else return false
|
|
func is_point_in_zone(point : Vector2) -> bool:
|
|
return building_rect.has_point(point)
|
|
|
|
## Returns true if point (global coordinates) in rect, else return false
|
|
func is_global_point_in_zone(point: Vector2) -> bool:
|
|
return is_point_in_zone(to_local(point))
|
|
|
|
## Returns index of point (local coordinates) to be used in array
|
|
func indexify_point(point : Vector2) -> int:
|
|
if is_point_in_zone(point) == false:
|
|
return -1
|
|
return int(point.x) / int(Globals.GRID_SIZE.x) + int(building_rect.size.x/Globals.GRID_SIZE.x)*(int(point.y) / int(Globals.GRID_SIZE.y))
|
|
|
|
## Returns index of point (global coordinates) to be used in array
|
|
func indexify_global_point(point : Vector2) -> int:
|
|
return indexify_point(to_local(point))
|
|
|
|
## Inverses indexification of point, returning snapped position
|
|
func inverse_index(index: int) -> Vector2:
|
|
return to_global(Vector2(index%int(building_rect.size.x/Globals.GRID_SIZE.x)*Globals.GRID_SIZE.x,index / int(building_rect.size.x/Globals.GRID_SIZE.x) * Globals.GRID_SIZE.y))
|
|
|
|
## Returns snapped position of point (global coordinates).
|
|
## Equivalent of [code]
|
|
## inverse_index(indexify_global_point(point))
|
|
## [/code]
|
|
func get_placement_position(point : Vector2) -> Vector2:
|
|
if is_global_point_in_zone(point) == false:
|
|
return Vector2.ZERO
|
|
return to_global((to_local(point) / Globals.GRID_SIZE).floor()*Globals.GRID_SIZE + Globals.GRID_SIZE/2.0)
|
|
|
|
## Returns capacity of building zone to be used in the array
|
|
func get_capacity() -> int:
|
|
return int(building_rect.size.x/Globals.GRID_SIZE.x)*int(building_rect.size.y/Globals.GRID_SIZE.y)
|