Dimensions for constructibles
This commit is contained in:
parent
d37c4ec858
commit
6f56fffb59
18 changed files with 135 additions and 25 deletions
|
@ -6,8 +6,6 @@ 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):
|
||||
|
@ -17,6 +15,8 @@ const GRID_SIZE : Vector2 = Vector2(16,16)
|
|||
get:
|
||||
return building_rect
|
||||
|
||||
@export var entity_holder : EntityHolder
|
||||
|
||||
func _ready() -> void:
|
||||
if not Engine.is_editor_hint():
|
||||
Registry.build_zones.append(self)
|
||||
|
@ -37,7 +37,7 @@ func is_global_point_in_zone(point: Vector2) -> bool:
|
|||
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))
|
||||
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:
|
||||
|
@ -45,7 +45,7 @@ func indexify_global_point(point : Vector2) -> int:
|
|||
|
||||
## 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))
|
||||
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]
|
||||
|
@ -54,8 +54,8 @@ func inverse_index(index: int) -> Vector2:
|
|||
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)
|
||||
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/GRID_SIZE.x)*int(building_rect.size.y/GRID_SIZE.y)
|
||||
return int(building_rect.size.x/Globals.GRID_SIZE.x)*int(building_rect.size.y/Globals.GRID_SIZE.y)
|
||||
|
|
|
@ -1,6 +1,44 @@
|
|||
@tool
|
||||
extends Node2D
|
||||
|
||||
class_name Construction
|
||||
|
||||
@export var dimensions : Rect2i = Rect2i(0,0,1,1):
|
||||
set(value):
|
||||
dimensions = value
|
||||
if Engine.is_editor_hint():
|
||||
queue_redraw()
|
||||
get:
|
||||
return dimensions
|
||||
|
||||
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)
|
||||
|
||||
func get_relative(dv : Vector2) -> Construction:
|
||||
return get_parent().get_at(global_position+dv)
|
||||
|
||||
func can_be_placed(zone : BuildZone) -> 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):
|
||||
return false
|
||||
return true
|
||||
|
||||
func try_place(zone : BuildZone) -> bool:
|
||||
if can_be_placed(zone) == false:
|
||||
return false
|
||||
|
||||
return zone.entity_holder.add_construction(self)
|
||||
|
||||
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
|
||||
|
|
|
@ -13,7 +13,7 @@ func _input(event: InputEvent) -> void:
|
|||
if zone == null:
|
||||
held_construction.queue_free()
|
||||
else:
|
||||
if zone.get_parent().get_node("EntityHolder").add_construction(held_construction):
|
||||
if held_construction.try_place(zone):
|
||||
held_construction = null
|
||||
|
||||
if event.is_action_pressed("plc_cancel"):
|
||||
|
@ -27,13 +27,14 @@ func on_construction_selected(constructible : Constructible):
|
|||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if held_construction != null:
|
||||
var mouse_pos = get_global_mouse_position()
|
||||
var zone = try_get_zone(mouse_pos)
|
||||
if zone:
|
||||
global_position = zone.get_placement_position(mouse_pos)
|
||||
else:
|
||||
global_position = mouse_pos
|
||||
if held_construction == null:
|
||||
return
|
||||
var mouse_pos = get_global_mouse_position()
|
||||
var zone = try_get_zone(mouse_pos)
|
||||
if zone and held_construction.can_be_placed(zone):
|
||||
global_position = zone.get_placement_position(mouse_pos)
|
||||
else:
|
||||
global_position = mouse_pos
|
||||
|
||||
func try_get_zone(point : Vector2) -> BuildZone:
|
||||
for zone in Registry.build_zones:
|
||||
|
|
|
@ -11,12 +11,18 @@ func _ready() -> void:
|
|||
constructions.resize(building_zone.get_capacity())
|
||||
|
||||
func add_construction(construction : Construction) -> bool:
|
||||
if constructions[building_zone.indexify_global_point(construction.global_position)]:
|
||||
return false
|
||||
var construction_dp = construction.get_dimension_points()
|
||||
for point in construction_dp:
|
||||
if constructions[building_zone.indexify_global_point(construction.global_position + point)]:
|
||||
return false
|
||||
construction.reparent(self)
|
||||
construction.global_position = building_zone.get_placement_position(construction.global_position)
|
||||
constructions[building_zone.indexify_global_point(construction.global_position)] = construction
|
||||
for point in construction_dp:
|
||||
constructions[building_zone.indexify_global_point(construction.global_position + point)] = construction
|
||||
return true
|
||||
|
||||
func get_at(point : Vector2):
|
||||
func get_at(point : Vector2) -> Construction:
|
||||
return constructions[building_zone.indexify_global_point(point)]
|
||||
|
||||
func is_point_occupied(point : Vector2) -> bool:
|
||||
return get_at(point) != null
|
||||
|
|
6
scripts/globals.gd
Normal file
6
scripts/globals.gd
Normal file
|
@ -0,0 +1,6 @@
|
|||
@abstract
|
||||
extends Object
|
||||
|
||||
class_name Globals
|
||||
|
||||
const GRID_SIZE : Vector2 = Vector2(16,16)
|
1
scripts/globals.gd.uid
Normal file
1
scripts/globals.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://bnmjkc8vfd7mo
|
Loading…
Add table
Add a link
Reference in a new issue