-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameplayLevelState.cs
More file actions
90 lines (72 loc) · 3.17 KB
/
Copy pathGameplayLevelState.cs
File metadata and controls
90 lines (72 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Services;
namespace Infrastructure.StateMachine
{
public class GameplayLevelState : IPayloadedGameState<string>
{
private readonly ServiceLocator _serviceLocator;
private readonly ISceneLoader _sceneLoader;
private readonly IUiFactory _uiFactory;
private readonly ITimeService _timeService;
private string _levelCode;
public GameplayLevelState(ServiceLocator serviceLocator, ISceneLoader sceneLoader, IUiFactory uiFactory,
ITimeService timeService)
{
_serviceLocator = serviceLocator;
_sceneLoader = sceneLoader;
_uiFactory = uiFactory;
_timeService = timeService;
}
public void Enter(string levelCode)
{
_levelCode = levelCode;
CreateGameplayServices(_serviceLocator);
_sceneLoader.LoadScene(Constants.GameplaySceneName, OnSceneLoaded, true);
}
public void Exit()
{
StopGameplayServices(_serviceLocator);
}
private static void CreateGameplayServices(ServiceLocator serviceLocator)
{
IGameFactory gameFactory = new GameFactory(
serviceLocator.Get<IAssetProvider>(),
serviceLocator.Get<IStaticDataProvider>());
serviceLocator.Register(gameFactory);
IEnemyFactory enemyFactory = new EnemyFactory(
serviceLocator.Get<IRandomService>(),
serviceLocator.Get<IAssetProvider>(),
serviceLocator.Get<IStaticDataProvider>());
serviceLocator.Register(enemyFactory);
IGameplayLevelEndTracker gameEndTracker = new GameplayLevelEndTracker(
serviceLocator.Get<IRandomService>(),
serviceLocator.Get<IStaticDataProvider>(),
serviceLocator.Get<ITimeService>(),
serviceLocator.Get<IWindowService>());
serviceLocator.Register(gameEndTracker);
IEnemyService enemyService = new EnemyService(
serviceLocator.Get<IStaticDataProvider>(),
serviceLocator.Get<IRandomService>(),
serviceLocator.Get<IGameFactory>(),
serviceLocator.Get<IEnemyFactory>(),
serviceLocator.Get<ITimeService>(),
serviceLocator.Get<IGameplayLevelEndTracker>());
serviceLocator.Register(enemyService);
}
private static void StopGameplayServices(ServiceLocator serviceLocator)
{
serviceLocator.Remove<IGameplayLevelEndTracker>();
serviceLocator.Remove<IGameFactory>();
serviceLocator.Remove<IEnemyFactory>();
serviceLocator.Remove<IEnemyService>();
}
private void OnSceneLoaded()
{
_uiFactory.CreateUiRoot();
var player = _serviceLocator.Get<IGameFactory>().SpawnPlayer(Constants.PlayerSpawnLocation, _levelCode);
_uiFactory.CreateHud(player);
_serviceLocator.Get<IEnemyService>().StartSpawnEnemies(_levelCode);
_serviceLocator.Get<IGameplayLevelEndTracker>().StartTracking(_levelCode);
_timeService.SetGameSpeed(1);
}
}
}