-
Notifications
You must be signed in to change notification settings - Fork 6
/
CycleProgress.cs
70 lines (58 loc) · 1.9 KB
/
CycleProgress.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
using GameLogic.Architecture;
namespace GameLogic.Commands
{
public class CycleProgress : Command
{
protected override bool Run ()
{
foreach (var room in Core.Ship.Rooms) {
BuildingProgress(room.Building);
}
return true;
}
private void BuildingProgress (Building building)
{
if (!building.Constructible.IsReady) return;
foreach (var module in building.Modules) {
ModuleProgress(module);
}
}
private void ModuleProgress (Module module)
{
if (!module.Constructible.IsReady || module.Cycle.IsFake) {
return;
}
// Добавляем прогресс только если модуль уже запущен (ресурсы были заплачены)
// Или если мы можем запустить его сейчас (заплатить ресурсы)
if (module.Cycle.IsRunning || TryStartCycle(module)) {
AddStep(module);
}
}
private void AddStep (Module module)
{
module.Cycle.AddProgress();
// Если после добавления прогресса работа модуля завершена
if (module.Cycle.IsReady) {
// Отдаем игроку его ресурсы
CycleOutput(module);
// И обнуляем прогресс, следующий раз еему придется запускаться сначала
module.Cycle.Reset();
}
}
private bool TryStartCycle (Module module)
{
if (module.Config.CycleInput == null) {
return true;
}
// Пытаемся заплатить ресурсы и если удается - модуль запущен
return new Pay(module.Config.CycleInput).Execute(Core).IsValid;
}
private void CycleOutput (Module module)
{
foreach (var item in module.Config.CycleOutput) {
// Отдаем игроку каждый ресурс, который ему был нужен
Core.Bank.Change(item.Key, item.Value);
}
}
}
}