[TASK] Show collected duck stickers
This commit is contained in:
58
Assets/DuckStickerManager.cs
Normal file
58
Assets/DuckStickerManager.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
[Serializable]
|
||||
public class DuckStickerEvent : UnityEvent<DuckStickerData>
|
||||
{
|
||||
}
|
||||
|
||||
public class DuckStickerManager : MonoBehaviour
|
||||
{
|
||||
public List<DuckStickerData> duckStickers;
|
||||
|
||||
public DuckStickerEvent stickerCollected;
|
||||
|
||||
private readonly Dictionary<DuckStickerData, int> _collectedStickers = new Dictionary<DuckStickerData, int>();
|
||||
|
||||
private static DuckStickerManager _instance;
|
||||
public static DuckStickerManager Instance
|
||||
{
|
||||
get { return _instance; }
|
||||
private set { _instance = value; }
|
||||
}
|
||||
|
||||
public IEnumerable<DuckStickerData> CollectedStickers
|
||||
{
|
||||
get { return duckStickers.Where(d => GetStickerCollectedCount(d) > 0); }
|
||||
}
|
||||
|
||||
public DuckStickerManager()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
foreach (var sticker in duckStickers)
|
||||
{
|
||||
_collectedStickers.Add(sticker, PlayerPrefs.GetInt("sticker." + sticker.Id));
|
||||
}
|
||||
}
|
||||
|
||||
public void OnStickerCollected(DuckStickerData sticker)
|
||||
{
|
||||
_collectedStickers[sticker]++;
|
||||
PlayerPrefs.SetInt("sticker." + sticker.Id, _collectedStickers[sticker]);
|
||||
|
||||
stickerCollected.Invoke(sticker);
|
||||
}
|
||||
|
||||
public int GetStickerCollectedCount(DuckStickerData sticker)
|
||||
{
|
||||
return _collectedStickers.ContainsKey(sticker) ? _collectedStickers[sticker] : 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user