Add initial project files and configurations for Unity setup
This commit is contained in:
99
Assets/Scripts/Core/Domain/GameSession.cs
Normal file
99
Assets/Scripts/Core/Domain/GameSession.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Core.Ports;
|
||||
|
||||
namespace Core.Domain
|
||||
{
|
||||
public class GameSession
|
||||
{
|
||||
private const string HighScoreKey = "HighScore";
|
||||
|
||||
public int Score { get; private set; }
|
||||
public int HighScore { get; private set; }
|
||||
public bool IsGameOver { get; private set; }
|
||||
|
||||
public event Action<int> OnScoreChanged;
|
||||
public event Action<string> OnOrbSpawned;
|
||||
public event Action OnOrbReset;
|
||||
public event Action OnGameOver;
|
||||
|
||||
private readonly List<Tile> _tiles;
|
||||
private readonly IPersistenceService _persistenceService;
|
||||
private readonly Random _rng = new();
|
||||
private int _playerFloorIndex = 0;
|
||||
|
||||
public GameSession(List<Tile> tiles, IPersistenceService persistenceService)
|
||||
{
|
||||
_tiles = tiles;
|
||||
_persistenceService = persistenceService;
|
||||
|
||||
Score = 0;
|
||||
IsGameOver = false;
|
||||
|
||||
HighScore = _persistenceService.Load(HighScoreKey, 0);
|
||||
}
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
SpawnNextOrb();
|
||||
}
|
||||
|
||||
public void OrbCollected()
|
||||
{
|
||||
if (IsGameOver) return;
|
||||
|
||||
Score += 10;
|
||||
OnScoreChanged?.Invoke(Score);
|
||||
SpawnNextOrb();
|
||||
}
|
||||
|
||||
private void SpawnNextOrb()
|
||||
{
|
||||
var validTiles = _tiles.FindAll(t =>
|
||||
t.CurrentState == TileState.Stable &&
|
||||
t.Floor == _playerFloorIndex
|
||||
);
|
||||
|
||||
if (validTiles.Count == 0)
|
||||
{
|
||||
validTiles = _tiles.FindAll(t => t.CurrentState == TileState.Stable);
|
||||
}
|
||||
|
||||
if (validTiles.Count == 0)
|
||||
{
|
||||
EndGame();
|
||||
return;
|
||||
}
|
||||
|
||||
var pick = validTiles[_rng.Next(validTiles.Count)];
|
||||
OnOrbSpawned?.Invoke(pick.Id);
|
||||
}
|
||||
|
||||
public void EndGame()
|
||||
{
|
||||
if (IsGameOver) return;
|
||||
|
||||
IsGameOver = true;
|
||||
|
||||
if (Score > HighScore)
|
||||
{
|
||||
HighScore = Score;
|
||||
_persistenceService.Save(HighScoreKey, HighScore);
|
||||
}
|
||||
|
||||
OnGameOver?.Invoke();
|
||||
}
|
||||
|
||||
public void SetPlayerFloor(int floorIndex)
|
||||
{
|
||||
if (_playerFloorIndex == floorIndex) return;
|
||||
|
||||
_playerFloorIndex = floorIndex;
|
||||
|
||||
if (IsGameOver) return;
|
||||
|
||||
OnOrbReset?.Invoke();
|
||||
SpawnNextOrb();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user