Firstborn/Assets/RPG Creation Kit/Scripts/Looting System/LootingPoint.cs

138 lines
3.7 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPGCreationKit;
using RPGCreationKit.SaveSystem;
using System.Reflection;
using UnityEditor;
namespace RPGCreationKit
{
public enum ContainerLockLevel { VeryEasy, Easy, Medium, Hard, VeryHard, Impossible};
public class LootingPoint : MonoBehaviour
{
public bool hasToInitializeInventory = true;
public string ID;
public string pointName;
public Collider lCollider;
public Animation anim;
public bool isOpened = false;
[Header("Lock Data")]
public bool locked = false;
public ContainerLockLevel lockLevel;
public KeyItem key;
[Header("Container Sounds")]
public AudioClip onLocked;
public AudioClip onUnlocked;
public AudioClip openSound;
public AudioClip closeSound;
public Inventory inventory;
public Equipment equipment;
public List<ItemInInventoryUI> items;
public bool hasToLoadAndSave = true;
private void Start()
{
if (hasToInitializeInventory)
inventory.InitializeInventory();
if(hasToLoadAndSave)
TryLoadFromSavefile();
}
[ContextMenu("Regenerate ID")]
public void RegenerateGUIDStr()
{
ID = System.Guid.NewGuid().ToString();
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
#endif
}
public void OpenCloseContainer() {
if (anim == null) {
return;
}
if (!anim.isPlaying) {
if (!isOpened) {
anim.Play("Open");
isOpened = true;
} else {
anim.Play("Close");
isOpened = false;
}
}
}
public void LockContainer(ContainerLockLevel level) {
locked = true;
lockLevel = level;
SaveOnFile();
}
public void UnlockContainer() {
locked = false;
SaveOnFile();
}
public bool TryLoadFromSavefile()
{
LootingPointSaveData data;
SaveSystemManager.instance.saveFile.LootingPointsData.allLootingPoints.TryGetValue(ID, out data);
if(data != null)
{
inventory.ClearInventory();
for(int i = 0; i < data.allItems.Count; i++)
inventory.AddItem(data.allItems[i].externalItemID, data.allItems[i].Amount);
inventory.InitializeInventory();
locked = data.isLocked;
ID = data.lootingID;
lockLevel = (ContainerLockLevel)data.ContainerLockLevel;
return true;
}
return false;
}
public void SaveOnFile()
{
if (hasToLoadAndSave)
{
if (SaveSystemManager.instance.saveFile.LootingPointsData.allLootingPoints.ContainsKey(ID))
{
// Update entry
SaveSystemManager.instance.saveFile.LootingPointsData.allLootingPoints[ID] = ToSaveData();
}
else
SaveSystemManager.instance.saveFile.LootingPointsData.allLootingPoints.Add(ID, ToSaveData());
}
}
public LootingPointSaveData ToSaveData()
{
LootingPointSaveData data = new LootingPointSaveData
{
lootingID = ID,
allItems = inventory.Items,
isLocked = locked,
ContainerLockLevel = (int)lockLevel,
};
return data;
}
}
}