-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassets.php
executable file
·104 lines (84 loc) · 3.65 KB
/
assets.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
<?php
class AssetsConverter {
private $inputDir = 'src';
private $outputDir = 'result';
private $assets = array();
public function __construct() {
;
}
private function loadFiles() {
foreach (scandir($this->inputDir) as $file) {
if ($file !== '.' || $file !== '..') {
$ext = pathinfo($file);
$ext = $ext['extension'];
if ($ext == 'tsx') {
$this->assets[] = $this->inputDir . '/' . $file;
}
}
}
}
private function readFiles() {
$output = '';
if (count($this->assets) > 0) {
foreach ($this->assets as $asset) {
// $xml = simplexml_load_string(file_get_contents($asset));
$xml = simplexml_load_file($asset);
// echo $this->pr($xml);
$path = '';
if($xml->properties){
foreach($xml->properties as $prop){
if ($prop->property['name'] == 'path') {
$path = $prop->property['value'];
}
}
}
if (count($xml->tile) > 1) {
$names = array();
$id = 0;
for ($y = 0; $y < floor($xml->image['height'] / $xml['tileheight']); $y++) {
for ($x = 0; $x < floor($xml->image['width'] / $xml['tilewidth']); $x++) {
if ($xml->tile[$id]) {
foreach ($xml->tile[$id]->properties as $prop) {
if ($prop->property['name'] == 'name') {
$names[$id]= $prop->property['value'].sprintf(':[%d,%d]',$x,$y);
} else {
$names [$id]= 'unnamed'.$id.sprintf(':[%d,%d]',$x,$y);
}
}
}else{
$names [$id]= 'unnamed'.$id.sprintf(':[%d,%d]',$x,$y);
}
$id++;
}
}
$output .= sprintf('Crafty.sprite(%d,%d,"%s",{
%s
});' . "\r\n", $xml['tilewidth'], $xml['tileheight'], $path.$xml->image['source'], implode(','."\n",$names));
} else {
$names = '';
foreach ($xml->tile->properties as $prop) {
if ($prop->property['name'] == 'name') {
$names = $prop->property['value'];
} else {
$names = $xml->image['source'];
}
$names .= ':[0,0]';
}
$output .= sprintf('Crafty.sprite(%d,%d,"%s",{
%s
});' . "\n", $xml['tilewidth'], $xml['tileheight'], $path.$xml->image['source'], $names);
}
}
}
if(file_put_contents($this->outputDir.'/sprites.js', $output)) {
echo '<h1 style="color:green">Assets created</h1> <br/>'.nl2br(file_get_contents($this->outputDir.'/sprites.js'));
}
}
public function convert() {
$this->loadFiles();
$this->readFiles();
}
private function pr($val) {
echo '<pre>' . print_r($val, true) . '</pre>';
}
}