185 lines
6.6 KiB
C#
185 lines
6.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
public class Saveable : MonoBehaviour
|
|
{
|
|
public enum ObjectType
|
|
{
|
|
Player,
|
|
BerryBush,
|
|
Twig,
|
|
NPC,
|
|
}
|
|
|
|
[SerializeField] private string uid = "";
|
|
[SerializeField] private ObjectType type = ObjectType.BerryBush;
|
|
|
|
#if UNITY_EDITOR
|
|
void OnValidate()
|
|
{
|
|
if (uid != "")
|
|
{
|
|
// If the UID is already set, we don't need to do anything
|
|
return;
|
|
}
|
|
// Generate a new UID based on the position of the object
|
|
uid = Guid.NewGuid().ToString();
|
|
}
|
|
#endif
|
|
private StateManager stateManager = null;
|
|
private void Awake()
|
|
{
|
|
stateManager = GameObject.FindObjectOfType<StateManager>();
|
|
// check if there is a saveState for the current object
|
|
RestoreMyself();
|
|
EnqueueSave();
|
|
}
|
|
|
|
void EnqueueSave()
|
|
{
|
|
if (stateManager == null)
|
|
return;
|
|
|
|
// add yourself to the stateManager depending on the type
|
|
if (type == ObjectType.BerryBush)
|
|
{
|
|
// check if the bush is already registered in the berrybushdata array
|
|
if (stateManager.BerryBushData.Find(e => e.uid == uid) == null)
|
|
stateManager.BerryBushData.Add(new SaveableBerryBush(uid, transform.position, transform.rotation, gameObject.GetComponent<BushInteraction>().GetBlueberryCount(), gameObject.GetComponent<BushInteraction>().AreBlueberriesPicked()));
|
|
}
|
|
else if (type == ObjectType.Twig)
|
|
{
|
|
if (stateManager.TwigData.Find(e => e.uid == uid) == null)
|
|
stateManager.TwigData.Add(new SaveableTwig(uid, transform.position, transform.rotation, gameObject.GetComponent<TwigInteraction>().IsCollected()));
|
|
}
|
|
|
|
else if (type == ObjectType.Player)
|
|
{
|
|
stateManager.PlayerData = new SaveablePlayer(uid, transform.position, transform.rotation, gameObject.GetComponent<PlayerInteraction>().GetInventoryItems(), gameObject.GetComponent<PlayerInteraction>().GetBlueberryCount(), gameObject.GetComponent<PlayerInteraction>().GetDialog());
|
|
}
|
|
|
|
else if (type == ObjectType.NPC)
|
|
{
|
|
if (stateManager.NPCData.Find(e => e.uid == uid) == null)
|
|
stateManager.NPCData.Add(new SaveableNPC(uid, transform.position, transform.rotation, gameObject.GetComponent<QuestGiver>().isActive, gameObject.GetComponent<QuestGiver>().isCompleted, gameObject.GetComponent<QuestGiver>().GetCurrentQuest()));
|
|
}
|
|
|
|
}
|
|
|
|
private void RestoreMyself()
|
|
{
|
|
if (stateManager == null)
|
|
return;
|
|
|
|
|
|
if (type == ObjectType.BerryBush)
|
|
{
|
|
//save itself to the statemanager
|
|
SaveableBerryBush bush = stateManager.BerryBushData.Find(e => e.uid == uid);
|
|
BushInteraction interaction = gameObject.GetComponent<BushInteraction>();
|
|
if (bush == null || interaction == null)
|
|
return;
|
|
interaction.SetBlueberriesPicked(bush.BlueBerriesPicked);
|
|
interaction.SetBlueberryCount(bush.blueberryCount);
|
|
}
|
|
else if (type == ObjectType.Twig)
|
|
{
|
|
SaveableTwig shtick = stateManager.TwigData.Find(e => e.uid == uid);
|
|
TwigInteraction interaction = gameObject.GetComponent<TwigInteraction>();
|
|
if (shtick == null || interaction == null)
|
|
return;
|
|
interaction.SetCollected(shtick.collected);
|
|
}
|
|
|
|
else if (type == ObjectType.Player)
|
|
{
|
|
SaveablePlayer player = stateManager.PlayerData;
|
|
PlayerInteraction interaction = gameObject.GetComponent<PlayerInteraction>();
|
|
if (player == null)
|
|
return;
|
|
interaction.SetInventoryItems(player.inventory);
|
|
interaction.SetBlueberryCount(player.blueberryCount);
|
|
interaction.SetDialog(player.dialog);
|
|
// transform.position = player.position;
|
|
// transform.rotation = player.rotation;
|
|
}
|
|
|
|
else if (type == ObjectType.NPC)
|
|
{
|
|
SaveableNPC npc = stateManager.NPCData.Find(e => e.uid == uid);
|
|
QuestGiver questGiver = gameObject.GetComponent<QuestGiver>();
|
|
if (npc == null || questGiver == null)
|
|
return;
|
|
questGiver.isActive = npc.isActive;
|
|
questGiver.isCompleted = npc.isCompleted;
|
|
questGiver.SetCurrentQuest(npc.dialog);
|
|
// delete the tree for the beaver quest if it is completed
|
|
if (questGiver.isCompleted && questGiver.rewardFunction != null)
|
|
{
|
|
questGiver.rewardFunction.RewardPlayer();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public string GetUID()
|
|
{
|
|
return uid;
|
|
}
|
|
public void SaveMyself()
|
|
{
|
|
if (stateManager == null)
|
|
return;
|
|
|
|
|
|
if (type == ObjectType.BerryBush)
|
|
{
|
|
//save itself to the statemanager
|
|
SaveableBerryBush bush = stateManager.BerryBushData.Find(e => e.uid == uid);
|
|
BushInteraction interaction = gameObject.GetComponent<BushInteraction>();
|
|
if (bush == null || interaction == null)
|
|
return;
|
|
bush.blueberryCount = interaction.GetBlueberryCount();
|
|
bush.BlueBerriesPicked = interaction.AreBlueberriesPicked();
|
|
}
|
|
else if (type == ObjectType.Twig)
|
|
{
|
|
SaveableTwig shtick = stateManager.TwigData.Find(e => e.uid == uid);
|
|
if (shtick == null)
|
|
return;
|
|
TwigInteraction interaction = gameObject.GetComponent<TwigInteraction>();
|
|
shtick.collected = interaction.IsCollected();
|
|
}
|
|
|
|
else if (type == ObjectType.Player)
|
|
{
|
|
SaveablePlayer player = stateManager.PlayerData;
|
|
PlayerInteraction interaction = gameObject.GetComponent<PlayerInteraction>();
|
|
player.blueberryCount = interaction.GetBlueberryCount();
|
|
player.inventory = interaction.GetInventoryItems();
|
|
player.dialog = interaction.GetDialog();
|
|
// player.position = transform.position;
|
|
// player.rotation = transform.rotation;
|
|
}
|
|
|
|
else if (type == ObjectType.NPC)
|
|
{
|
|
SaveableNPC npc = stateManager.NPCData.Find(e => e.uid == uid);
|
|
QuestGiver questGiver = gameObject.GetComponent<QuestGiver>();
|
|
if (npc == null || questGiver == null)
|
|
return;
|
|
npc.isActive = questGiver.isActive;
|
|
npc.isCompleted = questGiver.isCompleted;
|
|
npc.dialog = questGiver.GetCurrentQuest();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SaveMyself();
|
|
}
|
|
|
|
} |