86 lines
4.1 KiB
C#
86 lines
4.1 KiB
C#
using UnityEngine;
|
|||
|
|
|
||
|
|
// Добавляет "Склад" и "Амбар" в список стройки кодом -- как Казармы (Meta/Build/BarracksBootstrap).
|
||
|
|
// Шаблон -- копия обычного здания без добычи; пока нет арта, отличаем цветом.
|
||
|
|
// Вызывается при загрузке сцены и перед восстановлением города из сохранения (SettlementRestoringEvent),
|
||
|
|
// иначе построенные Склады не нашлись бы при загрузке.
|
||
|
|
public static class StorageBootstrap
|
||
|
|
{
|
||
|
|
public const string WarehouseName = "Склад";
|
||
|
|
public const string GranaryName = "Амбар";
|
||
|
|
|
||
|
|
static GameObject warehouseTemplate;
|
||
|
|
static GameObject granaryTemplate;
|
||
|
|
|
||
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
|
||
|
|
static void Init()
|
||
|
|
{
|
||
|
|
Ensure();
|
||
|
|
UnityEngine.SceneManagement.SceneManager.sceneLoaded += (s, m) => Ensure();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void Ensure()
|
||
|
|
{
|
||
|
|
var bm = BuildManager.Instance != null ? BuildManager.Instance : Object.FindAnyObjectByType<BuildManager>();
|
||
|
|
if (bm == null || bm.allBuildings == null) return;
|
||
|
|
|
||
|
|
bool hasWarehouse = false, hasGranary = false;
|
||
|
|
BuildingData template = null;
|
||
|
|
foreach (var b in bm.allBuildings)
|
||
|
|
{
|
||
|
|
if (b == null) continue;
|
||
|
|
if (b.buildingName == WarehouseName) hasWarehouse = true;
|
||
|
|
if (b.buildingName == GranaryName) hasGranary = true;
|
||
|
|
if (template == null && b.prefab != null && b.prefab.GetComponent<ResourceCollector>() != null) template = b;
|
||
|
|
}
|
||
|
|
if (hasWarehouse && hasGranary) return;
|
||
|
|
if (template == null) { Debug.LogWarning("[СКЛАДЫ] Нет здания-шаблона в BuildManager"); return; }
|
||
|
|
|
||
|
|
var cfg = EconomyConfig.Load().storage;
|
||
|
|
if (!hasWarehouse)
|
||
|
|
{
|
||
|
|
if (warehouseTemplate == null) warehouseTemplate = MakeTemplate(template.prefab, "Warehouse", StorageBuilding.Kind.Warehouse, new Color(0.85f, 0.7f, 0.5f));
|
||
|
|
bm.allBuildings.Add(MakeData(WarehouseName, template, warehouseTemplate, cfg.warehouseWood, cfg.warehouseStone));
|
||
|
|
}
|
||
|
|
if (!hasGranary)
|
||
|
|
{
|
||
|
|
if (granaryTemplate == null) granaryTemplate = MakeTemplate(template.prefab, "Granary", StorageBuilding.Kind.Granary, new Color(1f, 0.92f, 0.55f));
|
||
|
|
bm.allBuildings.Add(MakeData(GranaryName, template, granaryTemplate, cfg.granaryWood, cfg.granaryStone));
|
||
|
|
}
|
||
|
|
Debug.Log("[СКЛАДЫ] Склад и Амбар добавлены в список стройки");
|
||
|
|
}
|
||
|
|
|
||
|
|
static GameObject MakeTemplate(GameObject source, string name, StorageBuilding.Kind kind, Color tint)
|
||
|
|
{
|
||
|
|
var t = Object.Instantiate(source);
|
||
|
|
t.name = name;
|
||
|
|
t.SetActive(false);
|
||
|
|
Object.DontDestroyOnLoad(t);
|
||
|
|
foreach (var c in t.GetComponents<ResourceCollector>()) Object.DestroyImmediate(c);
|
||
|
|
foreach (var c in t.GetComponents<ResourceProducer>()) Object.DestroyImmediate(c);
|
||
|
|
foreach (var c in t.GetComponents<BuildingUI>()) Object.DestroyImmediate(c);
|
||
|
|
for (int i = t.transform.childCount - 1; i >= 0; i--)
|
||
|
|
Object.DestroyImmediate(t.transform.GetChild(i).gameObject); // полоска над зданием не нужна
|
||
|
|
var sr = t.GetComponent<SpriteRenderer>();
|
||
|
|
if (sr != null) sr.color = tint;
|
||
|
|
t.AddComponent<StorageBuilding>().kind = kind;
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
static BuildingData MakeData(string title, BuildingData template, GameObject prefab, float wood, float stone)
|
||
|
|
{
|
||
|
|
var data = ScriptableObject.CreateInstance<BuildingData>();
|
||
|
|
data.name = "Data_" + prefab.name + "_Runtime";
|
||
|
|
data.buildingName = title;
|
||
|
|
data.icon = template.icon;
|
||
|
|
data.prefab = prefab;
|
||
|
|
data.requiredTownHallLevel = 1;
|
||
|
|
data.cost = new[]
|
||
|
|
{
|
||
|
|
new ResourceCost { type = ResourceType.Wood, amount = wood },
|
||
|
|
new ResourceCost { type = ResourceType.Stone, amount = stone },
|
||
|
|
};
|
||
|
|
return data;
|
||
|
|
}
|
||
|
|
}
|