48 lines
2.0 KiB
C#
48 lines
2.0 KiB
C#
using UnityEngine;
|
|
namespace FlappyBird.Core
|
|
{
|
|
/// <summary>
|
|
/// Pure C# service — all scoring logic lives here.
|
|
/// No MonoBehaviour, no Unity lifecycle. Injected via Zenject.
|
|
/// High score is persisted with PlayerPrefs.
|
|
/// </summary>
|
|
public sealed class ScoreManager : IScoreManager
|
|
{
|
|
private const string HIGH_SCORE_KEY = "HighScore";
|
|
// ── IScoreManager ──────────────────────────────────────────────────
|
|
/// <inheritdoc/>
|
|
public int CurrentScore { get; private set; }
|
|
/// <inheritdoc/>
|
|
public int HighScore { get; private set; }
|
|
/// <inheritdoc/>
|
|
public event System.Action<int> OnScoreChanged;
|
|
/// <inheritdoc/>
|
|
public event System.Action<int> OnHighScoreBeaten;
|
|
// ── Constructor ────────────────────────────────────────────────────
|
|
public ScoreManager()
|
|
{
|
|
HighScore = PlayerPrefs.GetInt(HIGH_SCORE_KEY, 0);
|
|
}
|
|
// ── IScoreManager ──────────────────────────────────────────────────
|
|
/// <inheritdoc/>
|
|
public void AddScore(int points)
|
|
{
|
|
if (points <= 0) return;
|
|
CurrentScore += points;
|
|
OnScoreChanged?.Invoke(CurrentScore);
|
|
if (CurrentScore > HighScore)
|
|
{
|
|
HighScore = CurrentScore;
|
|
PlayerPrefs.SetInt(HIGH_SCORE_KEY, HighScore);
|
|
PlayerPrefs.Save();
|
|
OnHighScoreBeaten?.Invoke(HighScore);
|
|
}
|
|
}
|
|
/// <inheritdoc/>
|
|
public void ResetScore()
|
|
{
|
|
CurrentScore = 0;
|
|
OnScoreChanged?.Invoke(CurrentScore);
|
|
}
|
|
}
|
|
} |