-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateGroup.php
53 lines (43 loc) · 1.16 KB
/
StateGroup.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
<?php
namespace FSMgasm;
class StateGroup extends StateHolder
{
protected function onStart(): void
{
foreach ($this->states as $state) {
$state->start();
}
}
protected function onUpdate(): void
{
foreach ($this->states as $state) {
$state->update();
}
$allEnded = array_reduce($this->states, function($carry, $state) {
return $carry && $state->hasEnded();
}, true); //TODO: need test
if ($allEnded) {
$this->end();
}
}
protected function onEnd(): void
{
foreach ($this->states as $state) {
$state->end();
}
}
public function isReadyToEnd(): bool
{
return array_reduce($this->states, function($carry, $state) {
return $carry && $state->isReadyToEnd();
}, true); //TODO: need test
}
protected function getDuration(): int
{
$statesDuration = array_map(function ($state) {
return $state->getDuration();
}, $this->states);
$maxDuration = max($statesDuration);
return empty($statesDuration) ? 0 : $maxDuration;
}
}