-
Notifications
You must be signed in to change notification settings - Fork 0
/
MechanicInfo.cs
204 lines (180 loc) · 6.89 KB
/
MechanicInfo.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
namespace Bos {
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class MechanicInfoSave {
public int id;
public int count;
//public float interval;
[JsonConverter(typeof(Bos.Json.StringEnumConverter))]
public MechanicState state;
public float timer;
//public AddInfoSave adSave;
}
public class MechanicInfo : GameElement {
private const int kMaxCountOfAds = 4;
public int Id { get; private set; }
public int Count { get; private set; }
public float Interval {
get {
float val = 1f / Services.MechanicService.ServiceSpeed;
if(val < 10 ) {
val = 10;
}
return val;
}
}
public MechanicState State { get; private set; }
//private readonly AdInfo adInfo = new AdInfo(kMaxCountOfAds);
private float timer = 0f;
private float MoveInterval
=> Interval * 0.2f;
private float LoadInterval
=> Interval * 0.3f;
private float UnloadInterval
=> Interval * 0.3f;
public float Timer
=> timer;
public void AddMechanic(int cnt) {
int oldCount = Count;
Count += cnt;
if(oldCount != Count ) {
GameEvents.OnMechanicAdded(this);
}
}
public MechanicInfo(int id, int count) {
this.Id = id;
this.Count = count;
//this.Interval = 1f / speed;
//if (Interval < 10) {
// Interval = 10;
//}
this.State = MechanicState.MoveToLoad;
}
public MechanicInfo(MechanicInfoSave save ) {
this.Id = save.id;
this.Count = save.count;
//this.Interval = save.interval;
this.State = save.state;
this.timer = save.timer;
//if(save.adSave != null ) {
// adInfo.Load(save.adSave);
//}
}
public MechanicInfoSave GetSave()
=> new MechanicInfoSave {
id = Id,
count = Count,
//interval = Interval,
state = State,
timer = timer,
//adSave = adInfo.GetSave()
};
private void AddOnSleep() {
if (timer >= Interval) {
int cnt = (int)(timer / Interval);
timer -= cnt * Interval;
GameEvents.OnMechanicWorkCircleCompleted(this, cnt);
}
}
public void Update(float dt, ISpeedModifier speedModifier) {
//adInfo.Update(dt);
float deltaTime = dt * speedModifier.GetSpeedMult(Id); //* adInfo.CurrentMultiplier;
timer += deltaTime;
AddOnSleep();
switch (State) {
case MechanicState.MoveToLoad: {
if(timer >= MoveInterval) {
ChangeState(MechanicState.Loading);
}
}
break;
case MechanicState.Loading: {
if (timer >= LoadInterval) {
ChangeState(MechanicState.MoveToUnload);
}
}
break;
case MechanicState.MoveToUnload: {
if (timer >= MoveInterval) {
ChangeState(MechanicState.Unloading);
}
}
break;
case MechanicState.Unloading: {
if (timer >= UnloadInterval) {
ChangeState(MechanicState.Completed);
}
}
break;
case MechanicState.Completed: {
if (timer >= 1) {
ChangeState(MechanicState.MoveToLoad);
}
}
break;
}
}
private void ChangeState(MechanicState newState) {
MechanicState oldState = State;
if(State != newState ) {
if(newState == MechanicState.MoveToLoad ) {
timer = 0;
State = MechanicState.MoveToLoad;
GameEvents.OnMechanicStateChanged(oldState, newState, this);
} else if (newState == MechanicState.Loading) {
timer -= MoveInterval;
State = MechanicState.Loading;
GameEvents.OnMechanicStateChanged(oldState, newState, this);
if(timer >= LoadInterval ) {
ChangeState(MechanicState.MoveToUnload);
}
} else if(newState == MechanicState.MoveToUnload ) {
timer -= LoadInterval;
State = MechanicState.MoveToUnload;
GameEvents.OnMechanicStateChanged(oldState, newState, this);
if (timer >= MoveInterval) {
ChangeState(MechanicState.Unloading);
}
} else if(newState == MechanicState.Unloading ) {
timer -= MoveInterval;
State = MechanicState.Unloading;
GameEvents.OnMechanicStateChanged(oldState, newState, this);
if(timer > UnloadInterval ) {
ChangeState(MechanicState.Completed);
}
} else if(newState == MechanicState.Completed ) {
State = MechanicState.Completed;
timer = 0;
GameEvents.OnMechanicStateChanged(oldState, newState, this);
GameEvents.OnMechanicWorkCircleCompleted(this, 1);
}
}
}
public float NormalizedTimer {
get {
switch (State) {
case MechanicState.MoveToUnload:
case MechanicState.MoveToLoad: {
return Mathf.Clamp01(timer / MoveInterval);
}
case MechanicState.Loading: {
return Mathf.Clamp01(timer / LoadInterval);
}
case MechanicState.Unloading: {
return Mathf.Clamp01(timer / UnloadInterval);
}
}
return 0;
}
}
/*
public bool IsAdValid
=> adInfo.IsValidForApply;
public void ApplyAd()
=> adInfo.Apply();*/
}
public enum MechanicState { MoveToLoad, Loading, MoveToUnload, Unloading, Completed }
}