Added saving of buildings
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEngine.Scripting;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Fruitomation.Game
|
||||
{
|
||||
public class BuildingManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private BuildingRegistry Registry;
|
||||
|
||||
private class BuildingInfo
|
||||
{
|
||||
public BuildingInfo(Building b, RectTransform rt)
|
||||
@@ -17,33 +24,108 @@ namespace Fruitomation.Game
|
||||
public RectTransform Rect { get; }
|
||||
}
|
||||
|
||||
[Serializable] private class SerializedBuilding
|
||||
{
|
||||
[SerializeField, Preserve, UsedImplicitly] public Vector2Int GridPosition;
|
||||
[SerializeField, Preserve, UsedImplicitly] public Vector2Int Position;
|
||||
[SerializeField, Preserve, UsedImplicitly] public string PrefabName;
|
||||
[SerializeField, Preserve, UsedImplicitly] public bool IsFlipped;
|
||||
|
||||
public SerializedBuilding(Vector2Int gridPosition, Vector2Int pos, string name, bool flipped)
|
||||
{
|
||||
GridPosition = gridPosition;
|
||||
IsFlipped = flipped;
|
||||
PrefabName = name;
|
||||
Position = pos;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable] private class SerializedBuildings
|
||||
{
|
||||
[SerializeField, Preserve, UsedImplicitly] private SerializedBuilding[] Buildings;
|
||||
public SerializedBuilding[] Get() => Buildings;
|
||||
|
||||
public SerializedBuildings(List<SerializedBuilding> buildings) =>
|
||||
Buildings = buildings.ToArray();
|
||||
}
|
||||
|
||||
private static string Filepath => Path.Combine(Application.persistentDataPath, "buildings.json");
|
||||
|
||||
private bool[,] InhabitedCells { get; } = new bool[96, 49];
|
||||
private List<Building> Buildings { get; } = new();
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (!File.Exists(Filepath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string json = File.ReadAllText(Filepath);
|
||||
SerializedBuilding[] buildings = JsonUtility.FromJson<SerializedBuildings>(json).Get();
|
||||
|
||||
foreach (SerializedBuilding building in buildings)
|
||||
{
|
||||
GameObject prefab = Registry.GetBuildingOf(building.PrefabName);
|
||||
GameObject go = Instantiate(prefab, transform);
|
||||
|
||||
BuildingInfo info = new
|
||||
(
|
||||
go.GetComponent<Building>(),
|
||||
go.GetComponent<RectTransform>()
|
||||
);
|
||||
|
||||
info.Rect.transform.localScale = new Vector3(building.IsFlipped ? -1f : 1f, 1f, 1f);
|
||||
info.Rect.sizeDelta = info.Building.SizeOnGrid * 40;
|
||||
info.Rect.anchoredPosition = building.Position;
|
||||
go.name = $"{building.PrefabName}";
|
||||
|
||||
info.Building.Init(this, building.GridPosition, building.Position, building.IsFlipped);
|
||||
Buildings.Add(info.Building);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Debug.Log($"Buildings: [{Buildings.Count}]");
|
||||
|
||||
List<SerializedBuilding> serialized = Buildings.Select(building => new SerializedBuilding
|
||||
(
|
||||
building.GridPosition,
|
||||
building.Position,
|
||||
building.name,
|
||||
building.IsFlipped
|
||||
)
|
||||
).ToList();
|
||||
|
||||
string json = JsonUtility.ToJson(new SerializedBuildings(serialized), false);
|
||||
File.WriteAllText(Filepath, json);
|
||||
}
|
||||
|
||||
public void RemoveBuilding(Building building) => Buildings.Remove(building);
|
||||
|
||||
public bool AddBuildingAt(Vector2Int position, GameObject prefab, bool isFlipped)
|
||||
public bool AddBuildingAt(Vector2Int position, BuildingRegistry.BuildingInfo building, bool isFlipped)
|
||||
{
|
||||
//
|
||||
GameObject go = Instantiate(prefab, transform);
|
||||
GameObject go = Instantiate(building.Prefab, transform);
|
||||
BuildingInfo info = new
|
||||
(
|
||||
go.GetComponent<Building>(),
|
||||
go.GetComponent<RectTransform>()
|
||||
);
|
||||
|
||||
info.Building.SetManager(this);
|
||||
go.name = $"{building.Name}";
|
||||
|
||||
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;
|
||||
Vector2Int off0 = info.Building.SizeOnGrid - Vector2Int.one;
|
||||
Vector2Int off1 = off0 * 20;
|
||||
Vector2Int p2 = p1 + off1;
|
||||
|
||||
info.Rect.sizeDelta = info.Building.SizeOnGrid * 40;
|
||||
info.Rect.anchoredPosition = p1 + off1;
|
||||
info.Rect.anchoredPosition = p2;
|
||||
|
||||
info.Rect.transform.localScale = new Vector3(isFlipped ? -1f : 1f, 1f, 1f);
|
||||
|
||||
@@ -65,7 +147,9 @@ namespace Fruitomation.Game
|
||||
InhabitedCells[x, y] = true;
|
||||
}
|
||||
|
||||
info.Building.Init(this, g0, p2, isFlipped);
|
||||
Buildings.Add(info.Building);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user