-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateSeries.php
86 lines (68 loc) · 1.75 KB
/
StateSeries.php
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
<?php
namespace FSMgasm;
class StateSeries extends StateHolder
{
protected bool $skipping = false;
public function addNext(State $state): void
{
$this->states[$this->key() + 1] = $state;
}
/**
* @param State[] $newStates
* @return void
*/
public function addNextList(array $newStates): void
{
$index = 1;
foreach ($newStates as $state) {
$this->states[$this->key() + $index] = $state;
++$index;
}
}
public function skip(): void
{
$this->skipping = true;
}
protected function onStart(): void
{
if (empty($this->states)) {
$this->end();
return;
}
$this->current()->start();
}
protected function onUpdate(): void
{
$this->current()->update();
if (($this->current()->isReadyToEnd() && !$this->current()->isFrozen()) || $this->skipping) {
if ($this->skipping) {
$this->skipping = false;
}
$this->current()->end();
$this->next();
if ($this->key() >= count($this->states)) {
$this->end();
return;
}
$this->current()->start();
}
}
public function isReadyToEnd(): bool
{
return ($this->key() == count($this->states) - 1 && $this->current()->isReadyToEnd());
}
protected function onEnd(): void
{
if ($this->key() < count($this->states)) {
$this->current()->end();
}
}
protected function getDuration(): int
{
$duration = 0;
foreach ($this->states as $state) {
$duration += $state->getDuration();
}
return $duration;
}
}