add project files
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using OHM.UnityToolkit.Inventory;
|
||||
|
||||
public class CustomizableController<Def, Data> where Def : CustomizableDef where Data : CustomizableDataBase, new()
|
||||
{
|
||||
public class CustomizationEvent : UnityEvent<Def>
|
||||
{
|
||||
}
|
||||
|
||||
public delegate List<Def> GetDefListDelegate();
|
||||
|
||||
public delegate CustomizablesDataBase<Data> GetSavedDataDelegate(SavedData savedData);
|
||||
|
||||
public delegate string GetCurrentDelegate(SavedData savedData);
|
||||
|
||||
public delegate void SetCurrentDelegate(SavedData savedData, string name);
|
||||
|
||||
public delegate void ApplyDelegate(Def def);
|
||||
|
||||
public delegate bool PayDelegate(TransactionUnit<SavedDataTUType> cost);
|
||||
|
||||
public delegate bool IsPremiumDelegate();
|
||||
|
||||
public delegate bool CanSetDefault();
|
||||
|
||||
public Def ForcedValue;
|
||||
|
||||
[HideInInspector]
|
||||
public UnityEvent onChanged = new UnityEvent();
|
||||
|
||||
public CustomizationEvent onPurchased = new CustomizationEvent();
|
||||
|
||||
private SavedDataController _savedDataController;
|
||||
|
||||
protected GetDefListDelegate getDefListDelegate;
|
||||
|
||||
private GetSavedDataDelegate _getSavedDataDelegate;
|
||||
|
||||
private ApplyDelegate _applyDelegate;
|
||||
|
||||
private PayDelegate _payDelegate;
|
||||
|
||||
private IsPremiumDelegate isPremiumDelegate;
|
||||
|
||||
public CustomizableController(SavedDataController _savedDataController, GetDefListDelegate getDefListDelegate, GetSavedDataDelegate _getSavedDataDelegate, ApplyDelegate _applyDelegate, PayDelegate _payDelegate, IsPremiumDelegate isPremiumDelegate)
|
||||
{
|
||||
this._savedDataController = _savedDataController;
|
||||
this.getDefListDelegate = getDefListDelegate;
|
||||
this._getSavedDataDelegate = _getSavedDataDelegate;
|
||||
this._applyDelegate = _applyDelegate;
|
||||
this._payDelegate = _payDelegate;
|
||||
this.isPremiumDelegate = isPremiumDelegate;
|
||||
}
|
||||
|
||||
public List<Def> GetDefList()
|
||||
{
|
||||
return getDefListDelegate();
|
||||
}
|
||||
|
||||
public Def GetCurrent()
|
||||
{
|
||||
if (ForcedValue != null)
|
||||
{
|
||||
return ForcedValue;
|
||||
}
|
||||
Def saved = GetSavedValue();
|
||||
if (saved == null)
|
||||
{
|
||||
SetDefault();
|
||||
return GetSavedValue();
|
||||
}
|
||||
return saved;
|
||||
}
|
||||
|
||||
private Def GetSavedValue()
|
||||
{
|
||||
string currentId = _getSavedDataDelegate(_savedDataController.SavedData).currentId;
|
||||
return GetDefList().Find((Def def) => def.name == currentId);
|
||||
}
|
||||
|
||||
public Data GetCurrentData()
|
||||
{
|
||||
return GetData(GetCurrent());
|
||||
}
|
||||
|
||||
public Data GetData(Def skin)
|
||||
{
|
||||
CustomizablesDataBase<Data> saved = _getSavedDataDelegate(_savedDataController.SavedData);
|
||||
Data data = saved.customizablesList.Find((Data d) => d.id == skin.name);
|
||||
if (data == null)
|
||||
{
|
||||
data = new Data();
|
||||
data.id = skin.name;
|
||||
saved.customizablesList.Add(data);
|
||||
_savedDataController.SaveData();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public virtual void SetDefault()
|
||||
{
|
||||
Def first = GetDefList()[0];
|
||||
GetData(first).unlocked = true;
|
||||
SetCurrentValue(first);
|
||||
}
|
||||
|
||||
public void ApplyCurrent()
|
||||
{
|
||||
_applyDelegate(GetCurrent());
|
||||
}
|
||||
|
||||
public void SetCurrentValue(Def skin)
|
||||
{
|
||||
GetData(skin);
|
||||
_getSavedDataDelegate(_savedDataController.SavedData).currentId = skin.name;
|
||||
_applyDelegate(skin);
|
||||
_savedDataController.SaveData();
|
||||
onChanged.Invoke();
|
||||
}
|
||||
|
||||
public virtual List<Def> GetUnlockedList()
|
||||
{
|
||||
return GetDefList().FindAll(IsUnlocked);
|
||||
}
|
||||
|
||||
public bool CanPurchase(Def skin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Purchase(Def skin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetUnlocked(Def def)
|
||||
{
|
||||
GetData(def).unlocked = true;
|
||||
_savedDataController.SaveData();
|
||||
}
|
||||
|
||||
public bool IsUnlocked(Def def)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsUnlocked(string id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsNewSkin(Def def)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsNewSkin(string id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user