using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class RedpointCenter : Singleton { public event Action redpointValueChangeEvent; Dictionary redpoints = new Dictionary(); Dictionary> parentChildren = new Dictionary>(); public RedpointCenter() { } public void ResetAllRedpointState() { foreach (var redpoint in redpoints.Values) { redpoint.state = RedPointState.None; } } public void Register(Redpoint _redpoint) { var id = _redpoint.id; if (redpoints.ContainsKey(id)) { Debug.LogErrorFormat("重复注册红点:id->{0}", id); } redpoints[id] = _redpoint; var parentId = _redpoint.parent; if (parentId > 0) { List children = null; if (!parentChildren.TryGetValue(parentId, out children)) { parentChildren[parentId] = children = new List(); } if (!children.Contains(id)) { children.Add(id); } } } public void Report(Redpoint _redpoint) { var parentId = _redpoint.parent; if (redpointValueChangeEvent != null) { redpointValueChangeEvent(_redpoint.id); } if (parentId > 0) { TestParentValue(_redpoint.parent); } } public void ReportWithoutParent(Redpoint _redpoint) { if (redpointValueChangeEvent != null) { redpointValueChangeEvent(_redpoint.id); } } public RedPointState GetRedpointState(int _id) { Redpoint redpoint = null; if (this.redpoints.TryGetValue(_id, out redpoint)) { return redpoint.state; } else { return RedPointState.None; } } public int GetRedpointCount(int _id) { Redpoint redpoint = null; if (this.redpoints.TryGetValue(_id, out redpoint)) { return redpoint.count; } else { return 0; } } void TestParentValue(int _parentId) { List children = null; Redpoint parent = null; if (redpoints.TryGetValue(_parentId, out parent) && parentChildren.TryGetValue(_parentId, out children)) { var parentState = RedPointState.None; for (int i = 0; i < children.Count; i++) { Redpoint child = null; if (redpoints.TryGetValue(children[i], out child)) { if (child.state > parentState) { parentState = child.state; } } if (parentState == RedPointState.Full) { break; } } parent.state = parentState; } } }