/*
RSK World – Tower Defense 3D (Grid)
For support, licensing, or custom development inquiries: help@rskworld.in • Phone/WhatsApp: +91 9330539277
Website: RSK World • Business Inquiries: help@rskworld.in
🤝 Contributing: Suggestions and improvements are welcome!
*/

export class Grid {
	constructor(cellSize = 1.2) {
		this.cellSize = cellSize;
		this.occupied = new Set();
	}

	_cellKey(cx, cz) { return `${cx},${cz}`; }
	worldToCell(x, z) { return { cx: Math.round(x / this.cellSize), cz: Math.round(z / this.cellSize) }; }
	cellToWorld(cx, cz) { return { x: cx * this.cellSize, z: cz * this.cellSize }; }

	canPlaceAt(x, z, span = 1) {
		const { cx, cz } = this.worldToCell(x, z);
		const half = Math.floor((span - 1) / 2);
		for (let dx = -half; dx <= half; dx++) {
			for (let dz = -half; dz <= half; dz++) {
				const key = this._cellKey(cx + dx, cz + dz);
				if (this.occupied.has(key)) return false;
			}
		}
		return true;
	}

	occupyAt(x, z, span = 1) {
		const { cx, cz } = this.worldToCell(x, z);
		const half = Math.floor((span - 1) / 2);
		for (let dx = -half; dx <= half; dx++) {
			for (let dz = -half; dz <= half; dz++) {
				const key = this._cellKey(cx + dx, cz + dz);
				this.occupied.add(key);
			}
		}
	}
}
