74 lines
1.8 KiB
C#
74 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public class Shelves : MonoBehaviour, IDataPersistence<GameData>
|
|
{
|
|
public GameObject ShelfPrefab;
|
|
|
|
private List<Shelf> items = new List<Shelf>();
|
|
|
|
void Awake()
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
public void LoadData(GameData data)
|
|
{
|
|
Clear();
|
|
foreach (ShelfData shelfData in data.shelves)
|
|
{
|
|
Add(shelfData);
|
|
}
|
|
}
|
|
|
|
public void SaveData(GameData data)
|
|
{
|
|
data.shelves.Clear();
|
|
|
|
foreach (Shelf shelf in GetComponentsInChildren<Shelf>())
|
|
{
|
|
ShelfData shelfData = new ShelfData();
|
|
shelf.SaveData(shelfData);
|
|
|
|
data.shelves.Add(shelfData);
|
|
}
|
|
}
|
|
|
|
public void Add(ShelfData data)
|
|
{
|
|
GameObject instance = Instantiate(ShelfPrefab, new Vector3(data.location.x, data.location.y, 0), Quaternion.Euler(0, 0, data.angle), transform);
|
|
Shelf shelf = instance.GetComponent<Shelf>();
|
|
shelf.LoadData(data);
|
|
|
|
items.Add(shelf);
|
|
}
|
|
|
|
private void Clear()
|
|
{
|
|
items.Clear();
|
|
|
|
while (transform.childCount > 0)
|
|
DestroyImmediate(transform.GetChild(0).gameObject);
|
|
}
|
|
|
|
[ContextMenu("Test Initialize")]
|
|
void TestInitialize()
|
|
{
|
|
Clear();
|
|
|
|
for (int i = 0; i < UnityEngine.Random.Range(3, 5); i++)
|
|
{
|
|
ShelfData shelf = new ShelfData()
|
|
{
|
|
type = "random shelf",
|
|
location = new Vector2(UnityEngine.Random.value * 20 - 10, UnityEngine.Random.value * 10 - 5),
|
|
angle = 0,
|
|
};
|
|
shelf.inventory.capacity = UnityEngine.Random.Range(10, 40);
|
|
shelf.inventory.items.Add("123", UnityEngine.Random.Range(5, 10));
|
|
Add(shelf);
|
|
}
|
|
}
|
|
} |