-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTurnsSystem.cs
83 lines (68 loc) · 2.44 KB
/
TurnsSystem.cs
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
using System;
using System.Collections;
using System.Linq;
using SystemsRx.Events;
using SystemsRx.Systems.Conventional;
using EcsRx.Collections;
using EcsRx.Entities;
using EcsRx.Extensions;
using EcsRx.Groups;
using EcsRx.Groups.Observable;
using EcsRx.Plugins.GroupBinding.Attributes;
using EcsRx.Systems;
using EcsRx.Unity.Extensions;
using Game.Components;
using Game.Configuration;
using Game.Events;
using UniRx;
using UnityEngine;
namespace Game.Systems
{
public class TurnsSystem : IManualSystem
{
private readonly GameConfiguration _gameConfiguration;
private readonly IEventSystem _eventSystem;
private IDisposable _updateSubscription;
private bool _isProcessing;
[FromComponents(typeof (LevelComponent))]
public IObservableGroup LevelAccessor;
[FromComponents(typeof(EnemyComponent))]
public IObservableGroup EnemyAccessor;
private IEntity _level;
public TurnsSystem(GameConfiguration gameConfiguration, IEventSystem eventSystem)
{
_gameConfiguration = gameConfiguration;
_eventSystem = eventSystem;
}
private IEnumerator CarryOutTurns()
{
_isProcessing = true;
yield return new WaitForSeconds(_gameConfiguration.TurnDelay);
if(!EnemyAccessor.Any())
{ yield return new WaitForSeconds(_gameConfiguration.TurnDelay); }
foreach (var enemy in EnemyAccessor)
{
_eventSystem.Publish(new EnemyTurnEvent(enemy));
yield return new WaitForSeconds(_gameConfiguration.MovementTime);
}
_eventSystem.Publish(new PlayerTurnEvent());
_isProcessing = false;
}
private bool IsLevelLoaded()
{
var levelComponent = _level.GetComponent<LevelComponent>();
return levelComponent != null && levelComponent.HasLoaded.Value;
}
public void StartSystem()
{
this.WaitForScene().Subscribe(x => _level = LevelAccessor.First());
_updateSubscription = Observable.EveryUpdate().Where(x => IsLevelLoaded())
.Subscribe(x => {
if (_isProcessing) { return; }
MainThreadDispatcher.StartCoroutine(CarryOutTurns());
});
}
public void StopSystem()
{ _updateSubscription.Dispose(); }
}
}