SessionScore.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. namespace DevilLink2.Gameplay
  3. {
  4. /// <summary>Scores successful matches only. The real-time combo clock keeps advancing while paused.</summary>
  5. public sealed class SessionScore
  6. {
  7. public const double ComboWindowSeconds = 4;
  8. private readonly IMonotonicClock clock;
  9. private double lastRead, lastMatch;
  10. private bool finished;
  11. private int comboCount;
  12. public long TotalScore { get; private set; }
  13. public int ComboCount { get { Update(); return comboCount; } }
  14. public int CurrentMultiplier => Math.Min(ComboCount, 5);
  15. public bool IsInCombo => ComboCount > 0;
  16. public double ComboRemainingTime
  17. {
  18. get { Update(); return comboCount == 0 ? 0 : Math.Max(0, ComboWindowSeconds - (lastRead - lastMatch)); }
  19. }
  20. public int ClearBonus { get; private set; }
  21. public event Action<int> MatchScored;
  22. public event Action<int> ClearBonusAwarded;
  23. public event Action ComboChanged;
  24. public SessionScore(IMonotonicClock clock)
  25. {
  26. this.clock = clock ?? throw new ArgumentNullException(nameof(clock));
  27. GameplayTime.Read(clock, ref lastRead);
  28. }
  29. public void Update()
  30. {
  31. double now = GameplayTime.Read(clock, ref lastRead);
  32. if (comboCount > 0 && now - lastMatch > ComboWindowSeconds)
  33. { comboCount = 0; ComboChanged?.Invoke(); }
  34. }
  35. public int RegisterMatch()
  36. {
  37. if (finished) return 0;
  38. Update();
  39. comboCount++; lastMatch = lastRead;
  40. int points = 100 * Math.Min(comboCount, 5);
  41. TotalScore += points;
  42. ComboChanged?.Invoke(); MatchScored?.Invoke(points);
  43. return points;
  44. }
  45. public int CompleteClear(bool hasTimer, double remainingTime)
  46. {
  47. GameplayTime.Validate(remainingTime, nameof(remainingTime));
  48. if (finished) return 0;
  49. int bonus = checked(1000 + (hasTimer ? checked((int)Math.Ceiling(remainingTime) * 10) : 0));
  50. finished = true; ClearBonus = bonus; TotalScore += bonus;
  51. ClearBonusAwarded?.Invoke(bonus);
  52. return bonus;
  53. }
  54. public void EndWithoutBonus() { finished = true; }
  55. }
  56. }