68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Fruitomation.Game
|
|
{
|
|
public class BuildingManager : MonoBehaviour
|
|
{
|
|
private class BuildingInfo
|
|
{
|
|
public BuildingInfo(BuildingBase b, RectTransform rt)
|
|
{
|
|
Building = b;
|
|
Rect = rt;
|
|
}
|
|
|
|
public BuildingBase Building { get; }
|
|
public RectTransform Rect { get; }
|
|
}
|
|
|
|
private bool[,] InhabitedCells { get; } = new bool[96, 49];
|
|
private List<BuildingInfo> Buildings { get; } = new();
|
|
|
|
public bool AddBuildingAt(Vector2Int position, GameObject prefab, bool updateCellMap = false)
|
|
{
|
|
//
|
|
GameObject go = Instantiate(prefab, transform);
|
|
BuildingInfo info = new
|
|
(
|
|
go.GetComponent<BuildingBase>(),
|
|
go.GetComponent<RectTransform>()
|
|
);
|
|
|
|
Vector2Int p0 = position * 40;
|
|
Vector2Int p1 = p0 + new Vector2Int(20, 100);
|
|
|
|
Vector2Int g0 = position + new Vector2Int(48, 24);
|
|
|
|
Vector2 off0 = info.Building.SizeOnGrid - Vector2.one;
|
|
Vector2 off1 = off0 * 20;
|
|
|
|
info.Rect.sizeDelta = info.Building.SizeOnGrid * 40;
|
|
info.Rect.anchoredPosition = p1 + off1;
|
|
|
|
Debug.Log(g0);
|
|
|
|
//
|
|
for (int x = g0.x; x < g0.x + info.Building.SizeOnGrid.x; x++)
|
|
for (int y = g0.y; y < g0.y + info.Building.SizeOnGrid.y; y++)
|
|
{
|
|
if (InhabitedCells[x, y])
|
|
{
|
|
DestroyImmediate(go);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//
|
|
for (int x = g0.x; x < g0.x + info.Building.SizeOnGrid.x; x++)
|
|
for (int y = g0.y; y < g0.y + info.Building.SizeOnGrid.y; y++)
|
|
{
|
|
InhabitedCells[x, y] = true;
|
|
}
|
|
|
|
Buildings.Add(info);
|
|
return true;
|
|
}
|
|
}
|
|
} |