@tool @warning_ignore_start("integer_division") ## Class that helps to manage construction sites extends Marker2D class_name BuildZone const GRID_SIZE : Vector2 = Vector2(16,16) ## 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 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(GRID_SIZE.x) + int(building_rect.size.x/GRID_SIZE.x)*(int(point.y) / int(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/GRID_SIZE.x)*GRID_SIZE.x,index / int(building_rect.size.x/GRID_SIZE.x) * 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) / GRID_SIZE).floor()*GRID_SIZE + 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/GRID_SIZE.x)*int(building_rect.size.y/GRID_SIZE.y)