Added saving of buildings
This commit is contained in:
@@ -14,8 +14,18 @@ namespace Fruitomation.Game
|
||||
|
||||
private BuildingManager Manager;
|
||||
|
||||
public void SetManager(BuildingManager manager) => Manager = manager;
|
||||
|
||||
public Vector2Int GridPosition { get; private set; }
|
||||
public Vector2Int Position { get; private set; }
|
||||
public bool IsFlipped { get; private set; }
|
||||
|
||||
public void Init(BuildingManager manager, Vector2Int gridPosition, Vector2Int position, bool isFlipped)
|
||||
{
|
||||
GridPosition = gridPosition;
|
||||
IsFlipped = isFlipped;
|
||||
Position = position;
|
||||
Manager = manager;
|
||||
}
|
||||
|
||||
[UsedImplicitly, Preserve, InspectorCallable("Click Building")] public void OnBuildingClicked()
|
||||
{
|
||||
Debug.Log($"Building clicked {gameObject.name}");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
23
Assets/Scripts/Game/Buildings/BuildingRegistry.cs
Normal file
23
Assets/Scripts/Game/Buildings/BuildingRegistry.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Fruitomation.Game
|
||||
{
|
||||
[CreateAssetMenu] public class BuildingRegistry : ScriptableObject
|
||||
{
|
||||
[System.Serializable] public class BuildingInfo
|
||||
{
|
||||
public string Name;
|
||||
public GameObject Prefab;
|
||||
}
|
||||
|
||||
[SerializeField] private List<BuildingInfo> Buildings;
|
||||
public BuildingInfo[] GetBuildings() => Buildings.ToArray();
|
||||
|
||||
public GameObject GetBuildingOf(string building)
|
||||
{
|
||||
return (from info in Buildings where info.Name == building select info.Prefab).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Game/Buildings/BuildingRegistry.cs.meta
Normal file
3
Assets/Scripts/Game/Buildings/BuildingRegistry.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 406e966552b740c1aece5036cefe9bb7
|
||||
timeCreated: 1776095957
|
||||
Reference in New Issue
Block a user