2026-05-22 17:45:31 +03:00

73 lines
1.8 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);
Debug.Log("Систкема инициализирована");
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
}
}