Files
HelixJump/Assets/Scripts/ScoreService/ScoreService.cs
2025-12-15 17:49:08 -08:00

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();
}