forked from trojal/php-ro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
121 lines (91 loc) · 3.82 KB
/
Action.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
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
<?php
namespace Trojal\PhpRo;
use Trojal\PhpRo\ActionLayer;
use Trojal\PhpRo\DataReader;
use Trojal\PhpRo\Sprite;
class Action
{
public function __construct(DataReader $dataReader)
{
$this->dataReader = $dataReader;
}
public function getAnimation($animationNumber, $imageType = PHPRO_IMG_GIF)
{
return $this
->requireHeaders()
->getMovement($animationNumber)
->getImage($imageType);
}
public function getSprite()
{
if (!isset($this->sprite))
throw new Exception('Sprite required and not set.');
return $this->sprite;
}
public function setSprite(Sprite $sprite)
{
$this->sprite = $sprite;
}
public function requireHeaders()
{
if (isset($this->header))
return $this;
$this->header = unpack('a2header/cversion/x/SmovementCount', $this->dataReader->read(0x10));
$this->movements = array();
for ($nMovement = 0; $nMovement < $this->header['movementCount']; $nMovement++) {
$movementHeader = unpack('LframeCount', $this->dataReader->read(0x04));
$this->movements[] = new ActionMovement($movementHeader);
for ($nFrame = 0; $nFrame < $movementHeader['frameCount']; $nFrame++) {
$this->dataReader->seek(0x20, SEEK_CUR);
$frameHeader = unpack('LlayerCount', $this->dataReader->read(0x04));
$this
->getMovement($nMovement)
->addFrame(new ActionFrame($frameHeader, $this->getSprite()));
for ($nLayer = 0; $nLayer < $frameHeader['layerCount']; $nLayer++) {
$layerHeader = unpack('lxOffset/lyOffset/LsprFrame/Lmirror/Cred/Cgreen/Cblue/Calpha', $this->dataReader->read(0x14));
if ($this->header['version'] >= 2)
$layerHeader += unpack('fxScale', $this->dataReader->read(0x04));
else
$layerHeader['xScale'] = 0;
if ($this->header['version'] >= 4)
$layerHeader += unpack('fyScale', $this->dataReader->read(0x04));
else
$layerHeader['yScale'] = $layerHeader['xScale'];
$layerHeader += unpack('Lrotation/LsprType', $this->dataReader->read(0x08));
if ($this->header['version'] >= 5)
$layerHeader += unpack('LxSize/LySize', $this->dataReader->read(0x08));
$this
->getMovement($nMovement)
->getFrame($nFrame)
->addLayer(new ActionLayer($layerHeader, $this->getSprite()));
}
$frameData = unpack('LsoundFrame/Linfo', $this->dataReader->read(0x08));
if ($frameData['info'] == 1) {
$this->dataReader->seek(0x10, SEEK_CUR);
}
$this
->getMovement($nMovement)
->getFrame($nFrame)
->addData($frameData);
}
}
$this->header += unpack('LsoundCount', $this->dataReader->read(0x04));
$this->sound = array();
for ($nSound = 0; $nSound < $this->header['soundCount']; $nSound++) {
$this->sound[] = array_pop(unpack('a40', $this->dataReader->read(0x28)));
}
for ($nMovement = 0; $nMovement < $this->header['movementCount']; $nMovement++) {
{
$this
->getMovement($nMovement)
->setDuration(array_pop(unpack('fmovementDuration', $this->dataReader->read(0x04))));
}
}
return $this;
}
public function getMovement($movementNumber)
{
return $this
->movements[$movementNumber];
}
}