-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.php
executable file
·106 lines (87 loc) · 3.36 KB
/
map.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
<?php
class TileMapConverter {
private $map = array();
private $sprites = array();
public function convert($file) {
if (!file_exists($file))
return "file not exists";
$xml = simplexml_load_file($file);
$this->createMap($xml);
$this->createAssets($xml);
$this->pr($this->map);
$this->pr($this->sprites);
}
private function createAssets($xml) {
$r = array();
foreach ($xml->tileset as $tileset) {
$ignore = false;
foreach ($tileset->properties as $property) {
foreach ($property as $p) {
if ($p['name'] == 'ignore')
$ignore = false;
}
}
if (!$ignore) {
$offsetX = 0;
$offsetY = 0;
$offsetX = (int) $tileset->tileoffset['x'];
$offsetY = (int) $tileset->tileoffset['y'];
$w = $tileset['tilewidth'] ;
$h = $tileset['tileheight'];
$id = (int) $tileset['firstgid'];
$img = (string) $tileset->image['source'];
$maxX = round((int) $tileset->image['width'] / $w);
$maxY = round((int) $tileset->image['height'] / $h);
$data = array();
for ($y = 0; $y < $maxY; $y++) {
for ($x = 0; $x < $maxX; $x++) {
$data[] = sprintf('%d:[%d,%d]', $id, $x, $y);
$id++;
}
}
if($offsetX != 0 || $offsetY != 0){
$r[] = sprintf('Crafty.sprite(%d,%d,"%s",{' . "\n" . '%s' . "\n" . '},0,0,%d,%d);', $w, $h, $img, implode(",\n", $data), $offsetX, $offsetY);
}else{
$r[] = sprintf('Crafty.sprite(%d,%d,"%s",{' . "\n" . '%s' . "\n" . '});', $w, $h, $img, implode(",\n", $data));
}
}
// $this->pr($tileset);
}
$this->sprites = implode("\n", $r);
//$this->sprites = ($r);
}
private function createMap($xml) {
$r = array(
'metadata' => array(
'orientation' => (string) $xml['orientation'],
'width' => (string) $xml['width'],
'height' => (string) $xml['height'],
'tilewidth' => (string) $xml['tilewidth'],
'tileheight' => (string) $xml['tileheight']
)
);
foreach ($xml->properties as $property) {
foreach ($property as $p) {
if ($p['name'] == 'title')
$r['metadata']['title'] = (string) $p['value'];
}
}
foreach ($xml->layer as $layer) {
if (!isset($layer['visible'])) {
foreach ($layer->data as $data) {
//ignore attributes, take just the data
$d = array_values((array) $data);
$d = str_replace("\n", "", ($d[count($d) - 1]));
}
$r['layers'][(string) $layer['name']] = $d;
}
}
$this->map = json_encode($r);
}
public function save($output) {
// file_put_contents($output, $this->map);
}
private function pr($value) {
echo '<pre>' . print_r($value, true) . '</pre>';
}
}