61 lines
1.5 KiB
C#
Raw Normal View History

2024-12-14 18:27:59 +08:00
using QFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XMLTool;
public class ScoreController : MonoSingleton<ScoreController>
{
public Dictionary<string, ScoreStep> scoreDict;
public override void OnSingletonInit()
{
base.OnSingletonInit();
scoreDict = new Dictionary<string, ScoreStep>();
TypeEventSystem.Global.Register<OnModuleStart>(OnStart).UnRegisterWhenGameObjectDestroyed(gameObject);
TypeEventSystem.Global.Register<OnModuleQuit>(OnQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
}
private void OnStart(OnModuleStart start)
{
foreach (var item in Global.Instance.curModule.score.scores)
{
item.value = 0;
scoreDict.Add(item.step + item.name, item);
}
}
public void Add(string key, float value)
{
if (scoreDict.ContainsKey(key))
{
scoreDict[key].value += value;
var sumScore = float.Parse(scoreDict[key].sum);
if (scoreDict[key].value > sumScore)
{
scoreDict[key].value = sumScore;
}
if (scoreDict[key].value <= 0)
{
scoreDict[key].value = 0;
}
}
}
public void Set(string key, float value)
{
if (scoreDict.ContainsKey(key))
{
scoreDict[key].value = value;
}
}
private void OnQuit(OnModuleQuit quit)
{
scoreDict.Clear();
}
}