1 /***************************************************************************
2 * Copyright (C) 2010 by the Alterverse team *
3 * email: rynkruger@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write see: *
17 * <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
20 package org
.alterverse
.world
;
23 import org
.alterverse
.shapes
.*;
24 import org
.alterverse
.sound
.*;
25 import java
.util
.ArrayList
;
26 import java
.util
.Vector
;
28 public class GameObject
implements Serializable
{
36 ArrayList
<Sound
> sounds
;
39 Vector
<GameObject
> children
;
40 public GameObject(Area area
) {
41 this(area
,0.0,0.0,0.0,new Shape());
44 public GameObject(Area area
, Shape shape
) {
45 this.x
=(shape
.minX()+shape
.maxX())/2;
46 this.y
=(shape
.minY()+shape
.maxY())/2;
47 this.z
=(shape
.minZ()+shape
.maxZ())/2;
48 shapes
= new Vector
<Shape
>();
51 sounds
=new ArrayList
<Sound
>();
56 children
= new Vector
<GameObject
>();
59 public GameObject(Area area
,double x
,double y
,double z
,Shape shape
) {
63 shapes
= new Vector
<Shape
>();
66 sounds
=new ArrayList
<Sound
>();
71 children
= new Vector
<GameObject
>();
74 public boolean isTouching(GameObject other
) {
75 for (Shape shape
:shapes
)
76 for (Shape shape2
: other
.getShapes())
77 if (shape
.isTouching(shape2
))
82 public void onBump(GameObject obj
) {
86 public boolean canEnter(GameObject othr
) {
87 return !(this.isSolid()&&othr
.isSolid());
90 public boolean isSolid() {
94 public void setSolid(boolean solid
) {
98 public boolean getSolid() {
102 public void addSound(Sound sound
) {
106 public void removeSound(Sound sound
) {
107 sounds
.remove(sound
);
110 public ArrayList
<Sound
> getSounds() {
114 public boolean setPosition(double x
, double y
, double z
) {
124 public double getX() {
128 public double getY() {
132 public double getZ() {
136 public void updateData() {
137 for (Sound sound
: sounds
) {
138 sound
.setPosition(x
,y
,z
);
142 public void updateShape() {
143 for (Shape shape
: shapes
) {
144 double rx
= shape
.getX()-prevX
;
145 double ry
= shape
.getY()-prevY
;
146 double rz
= shape
.getZ()-prevZ
;
147 shape
.setPosition(this.x
+rx
,this.y
+ry
,this.z
+rz
);
151 public void setX(double x
) {
155 public void setY(double y
) {
159 public void setZ(double z
) {
163 public Shape
getShape() {
164 if (shapes
.size()==0)
166 return shapes
.get(0);
169 public void setShape(Shape shape
) {
174 public Vector
<Shape
> getShapes() {
178 public void addShape(Shape shape
) {
182 public Shape
getShape(int i
) {
183 if (i
< shapes
.size())
184 return shapes
.get(i
);
188 public void process() {
192 public double minX() {
193 if (shapes
.size() == 0)
196 for (Shape shape
: shapes
)
197 if (shape
.minX()<min
)
202 public double maxX() {
203 if (shapes
.size() == 0)
206 for (Shape shape
: shapes
)
207 if (shape
.maxX()>max
)
212 public double minY() {
213 if (shapes
.size() == 0)
216 for (Shape shape
: shapes
)
217 if (shape
.minY()<min
)
222 public double maxY() {
223 if (shapes
.size() == 0)
226 for (Shape shape
: shapes
)
227 if (shape
.maxY()>max
)
232 public double minZ() {
233 if (shapes
.size() == 0)
236 for (Shape shape
: shapes
)
237 if (shape
.minZ()<min
)
242 public double maxZ() {
243 if (shapes
.size() == 0)
246 for (Shape shape
: shapes
)
247 if (shape
.maxZ()>max
)
252 public void addChild(GameObject child
) {
257 public void onRemove() {
258 for (GameObject obj
: children
)
262 public void removeChild(GameObject child
) {
263 children
.remove(child
);
264 myArea
.remove(child
);
267 public GameObject
getChild(int i
) {
268 if (i
< children
.size())
269 return children
.get(i
);
273 public int numChildren() {
274 return children
.size();
277 public boolean split(double plx
, double ply
, double plz
, double pux
, double puy
, double puz
) {
278 Vector
<Shape
> tmps
= new Vector
<Shape
>();
279 Vector
<Shape
> remlist
= new Vector
<Shape
>();
280 Vector
<Shape
> adlist
= new Vector
<Shape
>();
282 BoundBox tester
= new BoundBox(plx
,ply
,plz
,pux
,puy
,puz
);
283 for (Shape shape
: shapes
)
284 if (shape
.isTouching(tester
)) {
288 double lx
= plx
< shape
.minX()? shape
.minX() : plx
;
289 double ly
= ply
< shape
.minY()? shape
.minY():ply
;
290 double lz
= plz
< shape
.minZ()?shape
.minZ():plz
;
291 double ux
= pux
> shape
.maxX()?shape
.maxX():pux
;
292 double uy
= puy
>shape
.maxY()?shape
.maxY():puy
;
293 double uz
= puz
> shape
.maxZ()?shape
.maxZ():puz
;
294 tmps
.add(new BoundBox(shape
.minX(),shape
.minY(),shape
.minZ(),lx
,shape
.maxY(),shape
.maxZ()));
295 tmps
.add(new BoundBox(ux
,shape
.minY(),shape
.minZ(),shape
.maxX(),shape
.maxY(),shape
.maxZ()));
296 tmps
.add(new BoundBox(lx
,shape
.minY(),shape
.minZ(),ux
,ly
,shape
.maxZ()));
297 tmps
.add(new BoundBox(lx
,uy
,shape
.minZ(),ux
,shape
.maxY(),shape
.maxZ()));
298 tmps
.add(new BoundBox(lx
,ly
,shape
.minZ(),ux
,uy
,lz
));
299 tmps
.add(new BoundBox(lx
,ly
,uz
,ux
,uy
,shape
.maxZ()));
301 if (s
.getVolume()>0.0) {
303 org
.alterverse
.speech
.tts
.speak("added");
306 for (Shape s
: adlist
)
308 for (Shape s
: remlist
)