-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobjectlayer.php
executable file
·131 lines (119 loc) · 2.99 KB
/
objectlayer.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
122
123
124
125
126
127
128
129
130
131
<?php
require_once('properties.php');
require_once('map.php');
require_once('layer.php');
require_once('tileset.php');
require_once('object.php');
class ObjectLayerBase extends Layer {
//attributes
public $name='';
public $color='';
public $x=0;
public $y=0;
public $width=0;
public $height=0;
public $opacity=1;
public $visible=1;
//internal
public $ref=NULL;
private $map=NULL;
private $objects=array();
//constructors
//methods
public function setMap(Map $map) {
$this->map=$map;
}
public function getMap() {
return $this->map;
}
private function load_objects(array $xml, $ref='') {
foreach($xml as $obj) {
$ob=new MapObject();
$ob->load_from_element($obj, $ref);
$this->addObject($ob);
}
}
public function load_from_element(SimpleXMLElement $xml, $ref='', $recur=true) {
$this->name=(string)$xml['name'];
$this->color=(string)$xml['color'];
$this->x=(string)$xml['x'];
$this->y=(string)$xml['y'];
$this->width=(string)$xml['width'];
$this->height=(string)$xml['height'];
$this->opacity=(int)$xml['opacity'];
$this->visible=(int)$xml['visible'];
if((bool)$xml->properties!==false) {
$this->loadProperties_from_element($xml->properties, $ref);
}
if((bool)$xml->object!==false) {
$this->load_objects($xml->xpath('object'), $ref);
}
}
public function addObject(ObjectBase $obj) {
$this->objects[]=$obj;
}
public function getObjectCount() {
return count($this->objects);
}
public function getObject($id) {
if(array_key_exists($id, $this->objects)) {
return $this->objects[$id];
}
else {
return NULL;
}
}
public function getObjects($name='') {
$arr=array();
foreach($this->objects as $obj) {
if($obj->name==$name) {
$arr[]=$obj;
}
}
if(count($arr)==0) return NULL;
return $arr;
}
public function getAllObjects() {
return $this->objects;
}
public function isValid() {
if(!is_string($this->name)) {
throw new Exception('Incorrect objectlayer name value.');
return false;
}
if(!is_int($this->x)) {
throw new Exception('Incorrect objectlayer x value.');
return false;
}
if(!is_int($this->y)) {
throw new Exception('Incorrect objectlayer y value.');
return false;
}
if(!is_int($this->width ) || $this->width <0) {
throw new Exception('Incorrect objectlayer width .');
return false;
}
if(!is_int($this->height) || $this->height<0) {
throw new Exception('Incorrect objectlayer height.');
return false;
}
if(!is_string($this->color)) {
throw new Exception('Incorrect objectlayer color value.');
return false;
}
if(!is_int($this->opacity) || ($this->opacity!=0 && $this->opacity!=1)) {
throw new Exception('Incorrect objectlayer opacity value.');
return false;
}
if(!is_int($his->visible) || ($this->visible!=0 && $this->visible!=1)) {
throw new Exception('Incorrect objectlayer visible value.');
return false;
}
if(!is_array($this->objects)) {
throw new Exception('Incorrect objectlayer objects array.');
return false;
}
return true;
}
};
?>