-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStateMachine.cs
More file actions
58 lines (48 loc) · 1.73 KB
/
Copy pathGameStateMachine.cs
File metadata and controls
58 lines (48 loc) · 1.73 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
using System;
using System.Collections.Generic;
using Services;
namespace Infrastructure.StateMachine
{
public interface IGameStateMachine : IService
{
void Enter<TState, TPayload>(TPayload payload) where TState : class, IPayloadedGameState<TPayload>;
void Enter<TState>() where TState : class, IGameState;
}
public class GameStateMachine : IGameStateMachine
{
private readonly Dictionary<Type, IExitableGameState> _states;
private IExitableGameState _currentState;
public GameStateMachine(ServiceLocator serviceLocator)
{
_states = new Dictionary<Type, IExitableGameState>()
{
[typeof(GameplayLevelState)] = new GameplayLevelState(
serviceLocator,
serviceLocator.Get<ISceneLoader>(),
serviceLocator.Get<IUiFactory>(),
serviceLocator.Get<ITimeService>()),
};
}
public void Enter<TState, TPayload>(TPayload payload) where TState : class, IPayloadedGameState<TPayload>
{
var state = ChangeState<TState>();
state.Enter(payload);
}
public void Enter<TState>() where TState : class, IGameState
{
var state = ChangeState<TState>();
state.Enter();
}
private TState ChangeState<TState>() where TState : class, IExitableGameState
{
_currentState?.Exit();
var state = GetState<TState>();
_currentState = state;
return state;
}
private TState GetState<TState>() where TState : class, IExitableGameState
{
return _states[typeof(TState)] as TState;
}
}
}