2025-03-25 10:46:22 +08:00

129 lines
3.1 KiB
C#
Raw Blame History

using QFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using XMLTool;
public class ScoreController : MonoSingleton<ScoreController>
{
public class Data
{
public Dictionary<string, ScoreStep> scoreDict;
}
public Dictionary<string, Data> moduleDict;
public override void OnSingletonInit()
{
base.OnSingletonInit();
InitData();
}
public void Init()
{
if (Global.Instance.curModule.type == "Exam" || Global.Instance.curModule.type == "All")
{
TypeEventSystem.Global.Register<OnModuleStart>(OnStart);
TypeEventSystem.Global.Register<OnModuleQuit>(OnQuit);
}
}
public void InitData()
{
moduleDict = new Dictionary<string, Data>();
foreach (var item in Global.Instance.appData.Modules)
{
if (item.type == "Exam" || item.type == "All")
{
Data data = new Data() { scoreDict = new Dictionary<string, ScoreStep>() };
moduleDict.Add(item.ModuleName, data);
}
}
}
private void OnStart(OnModuleStart start)
{
var data = moduleDict[Global.Instance.curModule.ModuleName];
data.scoreDict.Clear();
foreach (var item in Global.Instance.curModule.score.scores)
{
item.value = 0;
data.scoreDict.Add(item.step + item.name, item);
}
}
public Dictionary<string, ScoreStep> GetCurScoreData()
{
var data = moduleDict[Global.Instance.curModule.ModuleName];
return data.scoreDict;
}
public void Add(string key, float value)
{
var data = moduleDict[Global.Instance.curModule.ModuleName];
var scoreDict = data.scoreDict;
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;
}
}
else
{
Debug.LogError(<><C3BB><EFBFBD>ҵ<EFBFBD> <20><><EFBFBD>ֵ㣺" + key);
}
}
public void Set(string key, float value)
{
var data = moduleDict[Global.Instance.curModule.ModuleName];
var scoreDict = data.scoreDict;
if (scoreDict.ContainsKey(key))
{
scoreDict[key].value = value;
}
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ܷ<EFBFBD>
/// </summary>
public float GetCurScore()
{
float score = 0;
var data = moduleDict[Global.Instance.curModule.ModuleName];
foreach (var item in data.scoreDict)
{
score += item.Value.value;
}
return score;
}
private void OnQuit(OnModuleQuit quit)
{
TypeEventSystem.Global.UnRegister<OnModuleStart>(OnStart);
TypeEventSystem.Global.UnRegister<OnModuleQuit>(OnQuit);
}
}