65 lines
2.0 KiB
GDScript
65 lines
2.0 KiB
GDScript
extends Node2D
|
|
|
|
const GRID_SIZE = 64
|
|
const ROOM_WIDTH = 10
|
|
const ROOM_HEIGHT = 8
|
|
const MAIN_CHANNEL_WIDTH = 2
|
|
|
|
var grid_cells: Array = []
|
|
var occupied_positions: Array = []
|
|
|
|
func _ready():
|
|
initialize_grid()
|
|
|
|
func initialize_grid():
|
|
grid_cells.resize(ROOM_WIDTH * ROOM_HEIGHT)
|
|
for y in range(ROOM_HEIGHT):
|
|
for x in range(ROOM_WIDTH):
|
|
var index = y * ROOM_WIDTH + x
|
|
grid_cells[index] = {
|
|
"position": Vector2(x * GRID_SIZE, y * GRID_SIZE),
|
|
"occupied": false,
|
|
"type": "empty"
|
|
}
|
|
# Reserve main channel (2 cells wide, can be horizontal or vertical, here we assume vertical left side)
|
|
if x == 0 and y < ROOM_HEIGHT - 2: # Keep 2 cells at bottom free
|
|
grid_cells[index]["occupied"] = true
|
|
grid_cells[index]["type"] = "channel"
|
|
|
|
func is_position_valid(pos: Vector2) -> bool:
|
|
var x = int(pos.x / GRID_SIZE)
|
|
var y = int(pos.y / GRID_SIZE)
|
|
if x < 0 or x >= ROOM_WIDTH or y < 0 or y >= ROOM_HEIGHT:
|
|
return false
|
|
var index = y * ROOM_WIDTH + x
|
|
return not grid_cells[index]["occupied"]
|
|
|
|
func get_random_valid_position() -> Vector2:
|
|
var attempts = 0
|
|
while attempts < 50:
|
|
var x = randi() % (ROOM_WIDTH - 1) # Avoid blocking channel
|
|
var y = randi() % ROOM_HEIGHT
|
|
var pos = Vector2(x * GRID_SIZE + GRID_SIZE / 2, y * GRID_SIZE + GRID_SIZE / 2)
|
|
if is_position_valid(pos):
|
|
return pos
|
|
attempts += 1
|
|
return Vector2.ZERO
|
|
|
|
func reserve_position(pos: Vector2, obj_type: String = "object"):
|
|
var x = int(pos.x / GRID_SIZE)
|
|
var y = int(pos.y / GRID_SIZE)
|
|
if x >= 0 and x < ROOM_WIDTH and y >= 0 and y < ROOM_HEIGHT:
|
|
var index = y * ROOM_WIDTH + x
|
|
if not grid_cells[index]["occupied"]:
|
|
grid_cells[index]["occupied"] = true
|
|
grid_cells[index]["type"] = obj_type
|
|
occupied_positions.append(pos)
|
|
|
|
func free_position(pos: Vector2):
|
|
var x = int(pos.x / GRID_SIZE)
|
|
var y = int(pos.y / GRID_SIZE)
|
|
if x >= 0 and x < ROOM_WIDTH and y >= 0 and y < ROOM_HEIGHT:
|
|
var index = y * ROOM_WIDTH + x
|
|
grid_cells[index]["occupied"] = false
|
|
grid_cells[index]["type"] = "empty"
|
|
occupied_positions.erase(pos) |