randomgeon/utility/CoordUtility.gd
Dan Baker 02b3be35b0 Sets up initial project structure
Initializes the project with core files including:

- Editor configuration (.editorconfig, .gitattributes, .gitignore, .vscode/settings.json)
- Log.gd addon for enhanced debugging
- Loggie addon for advanced logging
- Project assets folder
2025-04-29 17:35:39 +01:00

80 lines
No EOL
2.3 KiB
GDScript

extends Node
func _ready() -> void:
Log.pr("CoordUtility ready")
func all_cells(start : Vector2i, end : Vector2i) -> Array:
# Returns all cells between start and end
var cells = []
for y in range(start.y, end.y + 1):
for x in range(start.x, end.x + 1):
cells.append(Vector2i(x, y))
cells.sort()
return cells
## Return all the perimeter cells between vector2i points
func perimeter_cells(start: Vector2i, end: Vector2i, inclusive: bool = true, width: int = 1) -> Array[Vector2i]:
# Returns cells on the perimeter between start and end
# inclusive: if true, includes the area cells, if false, excludes them
# width: the thickness of the perimeter (default: 1)
var cells: Array[Vector2i] = []
var min_x: int = min(start.x, end.x)
var max_x: int = max(start.x, end.x)
var min_y: int = min(start.y, end.y)
var max_y: int = max(start.y, end.y)
# Calculate the inner and outer bounds based on inclusive flag and width
var inner_min_x: int
var inner_min_y: int
var inner_max_x: int
var inner_max_y: int
var outer_min_x: int
var outer_min_y: int
var outer_max_x: int
var outer_max_y: int
if inclusive:
# For inclusive, inner bounds are the original rectangle
inner_min_x = min_x
inner_min_y = min_y
inner_max_x = max_x
inner_max_y = max_y
# Outer bounds expand by width-1 in each direction
outer_min_x = min_x - (width - 1)
outer_min_y = min_y - (width - 1)
outer_max_x = max_x + (width - 1)
outer_max_y = max_y + (width - 1)
else:
# For exclusive, inner bounds are the original rectangle
inner_min_x = min_x
inner_min_y = min_y
inner_max_x = max_x
inner_max_y = max_y
# For exclusive, outer bounds expand by exact width
outer_min_x = min_x - width
outer_min_y = min_y - width
outer_max_x = max_x + width
outer_max_y = max_y + width
# Add all cells in the outer rectangle
for x in range(outer_min_x, outer_max_x + 1):
for y in range(outer_min_y, outer_max_y + 1):
var pos: Vector2i = Vector2i(x, y)
# For inclusive, include cells on or outside inner bounds
# For exclusive, only include cells that are outside inner bounds
if inclusive:
if x <= inner_min_x or x >= inner_max_x or y <= inner_min_y or y >= inner_max_y:
cells.append(pos)
else:
if x < inner_min_x or x > inner_max_x or y < inner_min_y or y > inner_max_y:
cells.append(pos)
return cells