73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
namespace Lemon.GenericLib.Score {
|
|
|
|
public class ScoreKeeper
|
|
{
|
|
public string HIGHSCORE_PLAYERPREF = "HighScore";
|
|
|
|
//score variables
|
|
private int score = 0;
|
|
private int scoreMultiplier = 1;
|
|
public int maxMultiplier = 7;
|
|
|
|
//score changing event
|
|
//mainly used for UI
|
|
public Action<int> OnScoreModified;
|
|
public Action<int> OnScoreMultiplierModified;
|
|
public Action<int> OnNewHighScore;
|
|
|
|
//getter setters, not advice to set score directly
|
|
public int Score { get => score; set => score = value; }
|
|
public int ScoreMultiplier { get => scoreMultiplier; set => scoreMultiplier = value; }
|
|
public int HighScore { get => PlayerPrefs.GetInt(HIGHSCORE_PLAYERPREF); set => PlayerPrefs.SetInt(HIGHSCORE_PLAYERPREF, score); }
|
|
|
|
|
|
public void ForceUpdate()
|
|
{
|
|
OnScoreModified?.Invoke(score);
|
|
OnScoreMultiplierModified?.Invoke(scoreMultiplier);
|
|
}
|
|
|
|
public void ModifyScore(int value)
|
|
{
|
|
score += (value * scoreMultiplier);
|
|
OnScoreModified?.Invoke(score);
|
|
}
|
|
|
|
public void ResetScore()
|
|
{
|
|
score = 0;
|
|
OnScoreModified?.Invoke(score);
|
|
}
|
|
|
|
public void ModifyMultiplier(int value)
|
|
{
|
|
scoreMultiplier += value;
|
|
scoreMultiplier = Mathf.Clamp(scoreMultiplier, 1, maxMultiplier);
|
|
OnScoreMultiplierModified?.Invoke(scoreMultiplier);
|
|
}
|
|
|
|
public void ResetMultiplier()
|
|
{
|
|
scoreMultiplier = 1;
|
|
OnScoreMultiplierModified?.Invoke(scoreMultiplier);
|
|
}
|
|
|
|
public bool CheckHighScore()
|
|
{
|
|
if (score > PlayerPrefs.GetInt(HIGHSCORE_PLAYERPREF))
|
|
{
|
|
PlayerPrefs.SetInt(HIGHSCORE_PLAYERPREF, score);
|
|
OnNewHighScore?.Invoke(score);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|