-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStateMachine.cs
More file actions
88 lines (77 loc) · 3.18 KB
/
GameStateMachine.cs
File metadata and controls
88 lines (77 loc) · 3.18 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
using System;
using System.Collections.Generic;
using Services;
using Services.Mono;
using Services.Mono.Sound;
using Services.UI;
namespace Infrastructure.StateMachine
{
public class GameStateMachine : IGameStateMachine
{
private readonly Dictionary<Type, IExitableState> _states;
private IExitableState _currentState;
public bool InGameLoopState => _currentState.IsGameLoopState;
public GameStateMachine(ServiceLocator serviceLocator, bool isYaGamesEnvironment)
{
_states = new Dictionary<Type, IExitableState>
{
[typeof(MainMenuState)] = new MainMenuState(
this,
serviceLocator.Get<ISceneLoader>(),
serviceLocator.Get<IUiFactory>(),
serviceLocator.Get<ISoundMonoService>(),
serviceLocator.Get<ILoadingCurtainMonoService>(),
serviceLocator.Get<IWindowService>(),
serviceLocator.Get<IYaGamesMonoService>()),
[typeof(EndlessGameLoopState)] = new EndlessGameLoopState(
serviceLocator.Get<ISceneLoader>(),
serviceLocator.Get<ILoadingCurtainMonoService>(),
serviceLocator.Get<IAssetProviderService>(),
serviceLocator.Get<IRandomService>(),
serviceLocator.Get<IStaticDataService>(),
serviceLocator.Get<ISoundMonoService>(),
serviceLocator.Get<IUpdateMonoService>(),
serviceLocator.Get<IPersistentDataService>(),
serviceLocator.Get<IUiFactory>(),
serviceLocator.Get<IWindowService>(),
serviceLocator.Get<ICoinService>(),
serviceLocator.Get<IAdsService>(),
serviceLocator.Get<ICustomizationService>()),
};
if (isYaGamesEnvironment)
{
_states.Add(typeof(LoadYaSaveDataState), new LoadYaSaveDataState(
this,
serviceLocator.Get<IPersistentDataService>(),
serviceLocator.Get<IYaGamesMonoService>()));
}
else
{
_states.Add(typeof(LoadLocalSaveDataState), new LoadLocalSaveDataState(
this,
serviceLocator.Get<IPersistentDataService>()));
}
}
public void Enter<TState>() where TState : class, IState
{
IState state = ChangeState<TState>();
state.Enter();
}
public void Enter<TState, TPayload>(TPayload payload) where TState : class, IPayloadedState<TPayload>
{
TState state = ChangeState<TState>();
state.Enter(payload);
}
private TState ChangeState<TState>() where TState : class, IExitableState
{
_currentState?.Exit();
TState state = GetState<TState>();
_currentState = state;
return state;
}
private TState GetState<TState>() where TState : class, IExitableState
{
return _states[typeof(TState)] as TState;
}
}
}