37 lines
728 B
C#
37 lines
728 B
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class ScoreService : IScoreService
|
|
{
|
|
public event Action<int> OnScoreChange;
|
|
public int score { get; private set; }
|
|
|
|
public int highScore { get; private set; } = PlayerPrefs.GetInt("HighScore");
|
|
|
|
public void AddScore()
|
|
{
|
|
score += 5;
|
|
if (score>highScore)
|
|
{
|
|
SetNewHighScore();
|
|
}
|
|
OnScoreChange?.Invoke(score);
|
|
}
|
|
|
|
private void SetNewHighScore()
|
|
{
|
|
highScore = score;
|
|
PlayerPrefs.SetInt("HighScore", highScore);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
}
|
|
|
|
public interface IScoreService
|
|
{
|
|
int score { get; }
|
|
int highScore { get; }
|
|
event Action<int> OnScoreChange;
|
|
void AddScore();
|
|
}
|