-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPulseControl.cs
626 lines (584 loc) · 18.2 KB
/
PulseControl.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// Copyright 2016 Raymond Neilson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// TODO: add pulse pausing (or is that already doable with StopPulseCold(), and I just need a ResumePulse?)
// TODO: add startup delay
// TODO: move most functionality into a PulseTimer class for multi-component use
// TODO: make PulseControl a wrapper for above
// TODO: switch counter to double (?)
public class PulseControl : MonoBehaviour {
public float timeToTarget = 0.5f;
public float timeAtTarget = 0.5f;
public float timeFromTarget = 0.5f;
public float timeAfterTarget = 0.5f;
public bool autoStart = false;
public bool returnToStart = true;
public bool looping = false;
public bool debugInfo = false;
public PulseMode pulseMode = PulseMode.Linear;
public PulseUpdate pulseUpdate = PulseUpdate.UseUpdate;
private float counter;
private float phase;
private int loops;
private PulseState currentState;
private PulseState previousState;
private const float halfPi = Mathf.PI / 2.0f;
public PulseState State {
get { return currentState; }
}
public PulseState LastState {
get { return previousState; }
}
public PulseMode Mode {
get { return pulseMode; }
}
public float Phase {
get {
if (pulseMode == PulseMode.Linear) {
return phase;
}
else if (pulseMode == PulseMode.Sine) {
return Mathf.Sin(phase * halfPi);
}
else {
// We should, by all rights, never, *ever* be here
Debug.LogError("PulseControl in unknown mode: " + pulseMode.ToString(), gameObject);
return 1.0f;
}
}
}
public float PhaseRaw {
get { return phase; }
}
public float PulseTime {
get {
if (returnToStart) {
return timeToTarget + timeAtTarget + timeFromTarget + timeAfterTarget;
}
else {
return timeToTarget;
}
}
}
public float Counter {
get { return counter; }
}
public float Countdown {
get {
switch (currentState) {
case PulseState.Starting:
return timeToTarget;
case PulseState.ToTarget:
return timeToTarget - counter;
case PulseState.AtTarget:
return timeAtTarget - counter;
case PulseState.FromTarget:
return timeFromTarget - counter;
case PulseState.AfterTarget:
return timeAfterTarget - counter;
case PulseState.Stopped:
return 0.0f;
default:
// We should, by all rights, never, *ever* be here
Debug.LogError("PulseControl in unknown state: " + currentState.ToString(), gameObject);
return this.PulseTime;
}
}
}
public float CountdownTotal {
get {
switch (currentState) {
case PulseState.Starting:
return this.PulseTime;
case PulseState.ToTarget:
return this.PulseTime - counter;
case PulseState.AtTarget:
return timeAtTarget + timeFromTarget + timeAfterTarget - counter;
case PulseState.FromTarget:
return timeFromTarget + timeAfterTarget - counter;
case PulseState.AfterTarget:
return timeAfterTarget - counter;
case PulseState.Stopped:
return 0.0f;
default:
// We should, by all rights, never, *ever* be here
Debug.LogError("PulseControl in unknown state: " + currentState.ToString(), gameObject);
return this.PulseTime;
}
}
}
public float CountFraction {
get {
switch (currentState) {
case PulseState.Starting:
return 0.0f;
case PulseState.ToTarget:
return counter / timeToTarget;
case PulseState.AtTarget:
return counter / timeAtTarget;
case PulseState.FromTarget:
return counter / timeFromTarget;
case PulseState.AfterTarget:
return counter / timeAfterTarget;
case PulseState.Stopped:
return 1.0f;
default:
// We should, by all rights, never, *ever* be here
Debug.LogError("PulseControl in unknown state: " + currentState.ToString(), gameObject);
return 1.0f;
}
}
}
public bool AutoStart {
get {return autoStart; }
}
public bool ReturnToStart {
get {return returnToStart; }
}
public bool Looping {
get {return looping; }
}
public int Loops {
get { return loops; }
}
// PLEASE NOTE SUBTLE DIFFERENCES BETWEEN THESE (STARTED/RUNNING/CHANGING)
/* Since PulseControl runs before anything else, whatever is starting a new pulse is likely doing setup, but also might
not want to actual run any (possibly expensive) code based on the phase if the phase hasn't changed in the same
frame in which the new pulse was started. Therefore, "started" is defined as anything after calling StartNewPulse(),
"running" is ToTarget and onward (at least one frame), and "changing" is when the phase value is not constant (ie
not holding in place). This allows the user of PulseControl to fine-tune if and when the phase is used.
This is especially valuable, I think, given the staggered execution order and message sending. For example, if
something calls StartNewPulse() during its Start(), we'll switch to ToTarget during our LateUpdate() -- the caller
sees us at Starting during its Update(), and we'll spend at least one frame running. Or, as another example of
staggered execution, if a MaterialPulse has its StartPulse() called by Scorer, MaterialPulse will swap out its material,
and then later in the same frame, it will have its own Update() called -- and it may wish to differentiate, since both
run Update() after PulseControl.
If, for whatever reason, you'd like a running state immediately, call QuickStart() after StartNewPulse, which will
immediately set the state to ToTarget (and thus to running, as opposed to starting). The phase will remain at 0, and
thus can be used right away (accurately reflecting a lack of dT between Starting and ToTarget). */
public bool IsStarted {
get {
switch (currentState) {
// If we're starting, we've started -- contrast with below
case PulseState.Starting:
case PulseState.ToTarget:
case PulseState.AtTarget:
case PulseState.FromTarget:
case PulseState.AfterTarget:
return true;
case PulseState.Stopped:
return false;
default:
// We should, by all rights, never, *ever* be here
Debug.LogError("PulseControl in unknown state: " + currentState.ToString(), gameObject);
return false;
}
}
}
public bool IsRunning {
get {
switch (currentState) {
case PulseState.ToTarget:
case PulseState.AtTarget:
case PulseState.FromTarget:
case PulseState.AfterTarget:
return true;
// Here's a litte weirdness: if we're start*ing*, we haven't actually start*ed*, so we're not running yet
case PulseState.Starting:
case PulseState.Stopped:
return false;
default:
// We should, by all rights, never, *ever* be here
Debug.LogError("PulseControl in unknown state: " + currentState.ToString(), gameObject);
return false;
}
}
}
public bool IsChanging {
get {
switch (currentState) {
case PulseState.ToTarget:
case PulseState.FromTarget:
return true;
// Here's another litte weirdness: if we're starting, we're not changing *yet*
case PulseState.Starting:
case PulseState.AtTarget:
case PulseState.AfterTarget:
case PulseState.Stopped:
return false;
default:
// We should, by all rights, never, *ever* be here
Debug.LogError("PulseControl in unknown state: " + currentState.ToString(), gameObject);
return false;
}
}
}
// END SUBTLE DIFFERENCES (AGAIN, NOTE!!!)
// Most setup goes here -- we might need it ASAP
void Awake () {
phase = 0.0f;
counter = 0.0f;
loops = 0;
// Sanity checks
timeToTarget = (timeToTarget < 0.0f) ? 0.0f : timeToTarget;
timeAtTarget = (timeAtTarget < 0.0f) ? 0.0f : timeAtTarget;
timeFromTarget = (timeFromTarget < 0.0f) ? 0.0f : timeFromTarget;
timeAfterTarget = (timeAfterTarget < 0.0f) ? 0.0f : timeAfterTarget;
// Initialize to stopped
currentState = PulseState.Stopped;
previousState = PulseState.Stopped;
}
// Use this for initialization
void Start () {
// If autostarting, this frame we'll switch from Starting to ToTarget, and only next frame have the
// possibility of stopping
if (autoStart) {
StartNewPulse(); // State now started, but not yet running (will be later this frame)
}
}
// Update is called once per frame
void Update () {
if (pulseUpdate == PulseUpdate.UseUpdate) {
UpdateState(Time.deltaTime);
}
}
void FixedUpdate () {
if (pulseUpdate == PulseUpdate.UseFixedUpdate) {
UpdateState(Time.fixedDeltaTime);
}
}
void LateUpdate () {
// Here's where the previous state gets brought up to current
previousState = currentState;
if (currentState == PulseState.Starting) {
// If we were started earlier this frame (or late last), we can now switch from Starting
ChangeState(PulseState.ToTarget);
}
else {
// I'm not sure what kind of wonkiness happens when using LateUpdate, but yanno...
if (pulseUpdate == PulseUpdate.UseLateUpdate) {
UpdateState(Time.deltaTime);
}
}
}
void ChangeState (PulseState toState) {
// Print debug
if (debugInfo) {
Debug.Log("Switching currentState to " + toState.ToString()
+ ", counter: " + counter.ToString()
+ ", phase: " + phase.ToString(),
gameObject);
}
currentState = toState;
// TODO: add notifications
}
// State and counter update, only meant to be called while running
// Please see big distinction rant above, just before IsStarted
void UpdateState (float dT) {
if (this.IsRunning) {
// Only change to other states if there's been some actual time since last frame
// This means there will always be at least one frame from Starting to Stopped
// (More if game time passage is zero)
if (dT > 0.0f) {
// Add dt to counter
counter += dT;
// Check if time reached (and *which* time), and set currentState accordingly
// Note that there is fall-through, so if timeAtTarget and timeFromTarget are
// both zero, we'll go straight to Stopped (if at least some small amount of
// time has passed after Starting), or loop around to ToTarget (if that's what
// you're going for)
if (currentState == PulseState.ToTarget) {
if (counter >= timeToTarget) {
// Check if we're returnToStart from target
if (returnToStart) {
// Check if we're holding at target
if (timeAtTarget > 0.0f) {
// Hold state (sets phase to 1)
HoldPulseAt();
}
else {
// Switch currentState
ChangeState(PulseState.FromTarget);
}
}
else {
// Stop and set phase to 1, since we're done
StopPulse(1.0f);
}
// Either way, set time less any overshoot
counter = counter - timeToTarget;
}
}
if (currentState == PulseState.AtTarget) {
if (counter >= timeAtTarget) {
// Switch currentState
ChangeState(PulseState.FromTarget);
// Set time to target->initial, less any overshoot
counter = counter - timeAtTarget;
}
}
if (currentState == PulseState.FromTarget) {
if (counter >= timeFromTarget) {
// Check if we're looping
if (looping) {
if (timeAfterTarget > 0.0f) {
// Hold state (sets phase to 0)
HoldPulseAfter();
}
else {
// Switch currentState
ChangeState(PulseState.ToTarget);
// Advance loop counter
loops++;
}
}
else {
// Stop and set phase to 0, since we're done
StopPulse(0.0f);
}
// Set time, less any overshoot
counter = counter - timeFromTarget;
}
}
if (currentState == PulseState.AfterTarget) {
if (counter >= timeAfterTarget) {
// Switch currentState
ChangeState(PulseState.ToTarget);
// Advance loop counter
loops++;
// Set time to target->initial, less any overshoot
counter = counter - timeAfterTarget;
}
}
}
// Update phase info regardless of dT (was previously called separately)
UpdatePhase();
}
}
// Update phase value according to cycle
void UpdatePhase () {
// Calulate phase depending on whether we're (currently) going to/from target
if (currentState == PulseState.ToTarget) {
phase = counter / timeToTarget;
}
else if (currentState == PulseState.FromTarget) {
phase = 1.0f - (counter / timeFromTarget);
}
}
// Start pulse cycle
bool StartNewPulse (bool resetLoops) {
// Only start if we're stopped
if (currentState == PulseState.Stopped) {
ChangeState(PulseState.Starting);
phase = 0.0f;
counter = 0.0f;
// If we're not resetting loops, it'll be advanced in stopphase()
if (resetLoops) {
ResetLoops();
}
if (debugInfo) {
Debug.Log("Starting pulse, counter: " + counter.ToString() + ", phase: "
+ phase.ToString(), gameObject);
}
return true;
}
else if (currentState == PulseState.Starting) {
if (debugInfo) {
Debug.LogWarning("StartNewPulse() called on already-started PulseControl", gameObject);
}
return false;
}
else {
// We should really never be here
if (debugInfo) {
Debug.LogWarning("StartNewPulse() called on running PulseControl", gameObject);
}
return false;
}
}
// Quickie version, default to resetting loops
bool StartNewPulse () {
return StartNewPulse(true);
}
// Switch to running state immediately, instead of waiting until LateUpdate() (probably meaning
// next frame, for most callers)
// Returns true if was starting and is now running
public bool QuickStart () {
if (currentState == PulseState.Starting) {
// If we were started earlier this frame (or late last), we can now switch from Starting
ChangeState(PulseState.ToTarget);
return true;
}
else {
return false;
}
}
// Restarts if stopped, doesn't reset counter, and basically acts as if looping=true even if it wasn't
public bool RollingRestart () {
float tmpCounter;
if (currentState == PulseState.Stopped) {
if (counter > 0.0f) {
if (debugInfo) {
Debug.Log("Restarting pulse, counter: " + counter.ToString() + ", phase: "
+ phase.ToString(), gameObject);
}
// Here we temp-save the counter and force-update after resetting
// This is mostly to line everything up as if the post-stop time hadn't happened
tmpCounter = counter;
counter = 0.0f;
ChangeState(PulseState.ToTarget);
loops++;
return ForceUpdate(tmpCounter);
}
else {
// No idea how we'd get here, but y'know...
return StartNewPulse(false);
}
}
else {
if (debugInfo) {
Debug.LogWarning("RollingRestart() called on already-started PulseControl", gameObject);
}
return false;
}
}
// Basically emulates an Update() call with given dT and sets phase accordingly -- might be useful
// for using PulseControl outside of the game loop or...something?
// (Well, it's useful for RollingStart() above...)
// Returns true if running (and thus if the call does anything)
public bool ForceUpdate (float dT) {
if (this.IsRunning) {
UpdateState(dT);
return true;
}
else {
return false;
}
}
// When you want to go back to the start, but don't actually want a new pulse
public void ForceRollover () {
ChangeState(PulseState.ToTarget);
ResetCounter();
phase = 0.0f;
loops++;
}
// Hold pulse cycle at target
void HoldPulseAt () {
// Switch currentState
ChangeState(PulseState.AtTarget);
// Set phase to one, since we're at target
phase = 1.0f;
}
// Hold pulse cycle after target
void HoldPulseAfter () {
// Switch currentState
ChangeState(PulseState.AfterTarget);
// Set phase to zero, since we're back at start
phase = 0.0f;
}
// Stop pulse cycle at given phase
public void StopPulse (float finalPhase) {
// Switch currentState
ChangeState(PulseState.Stopped);
// Set phase to given value, since we're done
phase = finalPhase;
//loops++;
}
// For lazy public consumption
public float StopPulse () {
if (returnToStart) {
StopPulse(0.0f);
}
else {
StopPulse(1.0f);
}
return phase;
}
public float StopPulseCold () {
// Switch currentState
ChangeState(PulseState.Stopped);
return phase;
}
// Stop and reset everything to initial
public void StopAndResetPulse() {
StopPulse(0.0f);
ResetCounter();
ResetLoops();
}
// Start new pulse, possibly interrupting
public void NewPulse (float newTimeTo, float newTimeAt, float newTimeFrom, float newTimeAfter,
bool newReturn, bool newLoop, bool resetLoops, PulseMode newMode) {
// Debug
if (debugInfo) {
if (this.IsStarted) {
Debug.Log("New pulse started, interrupting current one", gameObject);
}
else {
Debug.Log("New pulse started, previous one already stopped", gameObject);
}
Debug.Log("Current state: " + currentState.ToString()
+ ", counter: " + counter.ToString() + ", phase: " + phase.ToString(),
gameObject);
}
if (this.IsStarted) {
// Stop current/previous pulse
StopPulse(0.0f);
}
// Set new values
timeToTarget = newTimeTo;
timeAtTarget = newTimeAt;
timeFromTarget = newTimeFrom;
timeAfterTarget = newTimeAfter;
returnToStart = newReturn;
looping = newLoop;
pulseMode = newMode;
// Start new pulse, and away we go
StartNewPulse(resetLoops);
}
// Lazy version, keeps present values -- essentially a reset
public void NewPulse (bool resetLoops) {
NewPulse(timeToTarget, timeAtTarget, timeFromTarget, timeAfterTarget, returnToStart, looping, resetLoops, pulseMode);
}
// Even lazier version, keeps present values and assumes loop counter reset -- essentially an active reset
public void NewPulse () {
NewPulse(timeToTarget, timeAtTarget, timeFromTarget, timeAfterTarget, returnToStart, looping, true, pulseMode);
}
// For if you've stopped a pulse or something
public void ResetLoops () {
loops = 0;
}
// Same but for counter
public void ResetCounter () {
counter = 0;
}
}
public enum PulseMode : byte {
Linear = 0,
Sine
}
public enum PulseState : byte {
Stopped = 0,
Starting,
ToTarget,
FromTarget,
AtTarget,
AfterTarget
}
public enum PulseUpdate : byte {
UseUpdate = 0,
UseFixedUpdate,
UseLateUpdate
}