70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public static class CountersSystem
|
|
{
|
|
public static List<CounterRecord> Records;
|
|
|
|
private static bool initialized = false;
|
|
public static void Init(CountersConfig config)
|
|
{
|
|
Records = new();
|
|
foreach (var counter in config.Counters)
|
|
Records.Add(counter);
|
|
initialized = true;
|
|
}
|
|
|
|
public static int GetCount(CounterRecord record) => PlayerPrefs.GetInt(record.PrefsKey, 0);
|
|
|
|
public static void Add(CounterType counterType, int amount)
|
|
{
|
|
if(!initialized)
|
|
{
|
|
Debug.Log("CountersSystem не проинициализирован");
|
|
return;
|
|
}
|
|
|
|
var filtred = Records.FindAll(c => c.Type == counterType);
|
|
foreach (var rec in filtred)
|
|
{
|
|
int currentValue = PlayerPrefs.GetInt(rec.PrefsKey, 0);
|
|
PlayerPrefs.SetInt(rec.PrefsKey, currentValue + amount);
|
|
}
|
|
}
|
|
|
|
public static void Increment(CounterType counterType) => Add(counterType, 1);
|
|
|
|
public static void Reset()
|
|
{
|
|
if (!initialized)
|
|
{
|
|
Debug.Log("CountersSystem не проинициализирован");
|
|
return;
|
|
}
|
|
foreach (var rec in Records)
|
|
rec.Reset();
|
|
}
|
|
|
|
[Serializable]
|
|
public class CounterRecord
|
|
{
|
|
public string Name;
|
|
public CounterType Type;
|
|
public string PrefsKey;
|
|
public bool Resettable;
|
|
public void Reset()
|
|
{
|
|
if(Resettable)
|
|
PlayerPrefs.SetInt(PrefsKey, 0);
|
|
}
|
|
}
|
|
|
|
public enum CounterType
|
|
{
|
|
StartsFromMenu,
|
|
StartsFromRevenge,
|
|
TotalStarts,
|
|
Revenue
|
|
}
|
|
} |