상세 컨텐츠

본문 제목

1204 TIL - 도전과제 구현 중

카테고리 없음

by lucar 2024. 12. 4. 23:02

본문

enums.cs

public enum AchievementType
{
    None,
    KillMonster,
    ClearStage,
    CollectGold,
    CollectSoul,
    Time
}

public enum ActionType
{
    Kill,
    Gold,
    Stage,
    Soul,
    Time
}

 

EventManager.cs 이벤트버스

public class EventManager : SingletonDDOL<EventManager>
{
    private Dictionary<Channel, List<Delegate>> Events = new Dictionary<Channel, List<Delegate>>();

    public void Subscribe(Channel channel, UnityAction listener)
    {
        if(Events?.ContainsKey(channel) == false)
        {
            Events[channel] = new List<Delegate>();
        }

        Events?[channel].Add(listener);
    }

    public void Subscribe<T>(Channel channel, UnityAction<T> listener) where T : UnityEngine.Object
    {
        if (Events?.ContainsKey(channel) == false)
        {
            Events[channel] = new List<Delegate>();
        }

        Events?[channel].Add(listener);
    }

    public void Unsubscribe(Channel channel, UnityAction listener)
    {
        if(Events.TryGetValue(channel, out var UnityActions))
        {
            UnityActions.Remove(listener);
        }
    }

    public void Unsubscribe<T>(Channel channel, UnityAction<T> listener) where T : UnityEngine.Object
    {
        if (Events.TryGetValue(channel, out var UnityActions))
        {
            UnityActions.Remove(listener);
        }
    }

    public void Publish<T>(Channel channel, T parameter)
    {
        if (Events?.ContainsKey(channel) == false) return;

        var UnityActions = Events?[channel];
        if (UnityActions == null) return;

        foreach (var action in UnityActions)
        {
            if(action.GetType() == typeof(UnityAction<T>))
            {
                ((UnityAction<T>)action)(parameter);
            }
        }
    }
}

 

AchieveData.cs

public class AchieveData : MonoBehaviour
{
    public string Name;
    public string Description;

    public float progress;
    public float Goal;
    public int ID;

    public string iconPath;

    public Enums.ActionType Action;
    public AchievementType AchievementType;

    public AchieveData(AchieveData data)
    {
        Name = data.Name;
        Description = data.Description;

        progress = 0;
        Goal = data.Goal;
        ID = data.ID;

        iconPath = data.iconPath;
        Action = data.Action;
        AchievementType = data.AchievementType;
    }

    public AchieveData(AchieveDB data)
    {
        Name = data.Name;
        Description = data.Description;

        progress = 0;
        Goal = data.Goal;
        ID = data.ID;

        iconPath = data.iconPath;
        Action = (Enums.ActionType)data.Action;
        AchievementType = (AchievementType)data.AchievementType;
    }

    public void AddProgress(float value)
    {
        progress += value;
    }
}

 

AchieveEvent.cs

public class AchieveEvent : MonoBehaviour
{
    public ActionType Action;
    public AchievementType Type;
    public int ID;
    public int Value;

    public AchieveEvent(ActionType action, AchievementType type, int value, int iD = 0)
    {
        Action = action;
        Type = type;
        Value = value;
        ID = iD;
    }
}

 

AchievementManager.cs 

public class AchievementManager : SingletonDDOL<AchievementManager>
{
    private Dictionary<AchievementType, List<AchieveData>> achievements;
    private List<AchieveDB> achievementDB;
    public Animation anime;

    protected override void Awake()
    {
        base.Awake();
        achievements = new Dictionary<AchievementType, List<AchieveData>>();
        achievementDB = DataManager.Instance.AchieveDB.AchieveList;

        foreach (var item in achievementDB)
        {
            AchieveData data = new AchieveData(item);
            if(achievements?.ContainsKey((AchievementType)item.AchievementType) == false)
            {
                achievements[(AchievementType)item.AchievementType] = new List<AchieveData>();
            }
            achievements[(AchievementType)item.AchievementType].Add(data);
        }
        foreach (KeyValuePair<AchievementType,List<AchieveData>> pair in achievements)
        {
            EventManager.Instance.Subscribe<AchieveEvent>(Channel.Achievement, OnTriggerAction);
        }
    }

    public void OnTriggerAction(AchieveEvent data)
    {
        //작성중
    }

    public List<AchieveData> MakeAchieveList(ActionType action, AchievementType type, int key)
    {
    	//작성중
        return null;
    }
}

 

너무 피곤하다