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
;
38 GameObject parent
=null;
40 Vector
<GameObject
> children
;
41 public GameObject(Area area
) {
42 this(area
,0.0,0.0,0.0,new Shape());
45 public GameObject(Area area
, Shape shape
) {
46 this.x
=(shape
.minX()+shape
.maxX())/2;
47 this.y
=(shape
.minY()+shape
.maxY())/2;
48 this.z
=(shape
.minZ()+shape
.maxZ())/2;
49 shapes
= new Vector
<Shape
>();
52 sounds
=new ArrayList
<Sound
>();
57 children
= new Vector
<GameObject
>();
60 public GameObject(Area area
,double x
,double y
,double z
,Shape shape
) {
64 shapes
= new Vector
<Shape
>();
67 sounds
=new ArrayList
<Sound
>();
72 children
= new Vector
<GameObject
>();
75 public boolean isTouching(GameObject other
) {
76 for (Shape shape
:shapes
)
77 for (Shape shape2
: other
.getShapes())
78 if (shape
.isTouching(shape2
))
83 public void onBump(GameObject obj
) {
87 public boolean canEnter(GameObject othr
) {
88 return !(this.isSolid()&&othr
.isSolid());
91 public boolean isSolid() {
95 public void setSolid(boolean solid
) {
99 public boolean getSolid() {
103 public void addSound(Sound sound
) {
107 public void removeSound(Sound sound
) {
108 sounds
.remove(sound
);
111 public ArrayList
<Sound
> getSounds() {
115 public boolean setPosition(double x
, double y
, double z
) {
125 public double getX() {
129 public double getY() {
133 public double getZ() {
137 public void updateData() {
138 for (Sound sound
: sounds
) {
139 sound
.setPosition(x
,y
,z
);
143 public void updateShape() {
144 for (Shape shape
: shapes
) {
145 double rx
= shape
.getX()-prevX
;
146 double ry
= shape
.getY()-prevY
;
147 double rz
= shape
.getZ()-prevZ
;
148 shape
.setPosition(this.x
+rx
,this.y
+ry
,this.z
+rz
);
152 public void setX(double x
) {
156 public void setY(double y
) {
160 public void setZ(double z
) {
164 public Shape
getShape() {
165 if (shapes
.size()==0)
167 return shapes
.get(0);
170 public void setShape(Shape shape
) {
175 public Vector
<Shape
> getShapes() {
179 public void addShape(Shape shape
) {
183 public Shape
getShape(int i
) {
184 if (i
< shapes
.size())
185 return shapes
.get(i
);
189 public void process() {
193 public double minX() {
194 if (shapes
.size() == 0)
197 for (Shape shape
: shapes
)
198 if (shape
.minX()<min
)
203 public double maxX() {
204 if (shapes
.size() == 0)
207 for (Shape shape
: shapes
)
208 if (shape
.maxX()>max
)
213 public double minY() {
214 if (shapes
.size() == 0)
217 for (Shape shape
: shapes
)
218 if (shape
.minY()<min
)
223 public double maxY() {
224 if (shapes
.size() == 0)
227 for (Shape shape
: shapes
)
228 if (shape
.maxY()>max
)
233 public double minZ() {
234 if (shapes
.size() == 0)
237 for (Shape shape
: shapes
)
238 if (shape
.minZ()<min
)
243 public double maxZ() {
244 if (shapes
.size() == 0)
247 for (Shape shape
: shapes
)
248 if (shape
.maxZ()>max
)
253 public void addChild(GameObject child
) {
256 child
.setParent(this);
259 public void onRemove() {
260 for (GameObject obj
: children
)
264 public void removeChild(GameObject child
) {
265 children
.remove(child
);
266 myArea
.remove(child
);
267 child
.setParent(null);
270 public GameObject
getChild(int i
) {
271 if (i
< children
.size())
272 return children
.get(i
);
276 public int numChildren() {
277 return children
.size();
280 public boolean split(double plx
, double ply
, double plz
, double pux
, double puy
, double puz
) {
281 Vector
<Shape
> tmps
= new Vector
<Shape
>();
282 Vector
<Shape
> remlist
= new Vector
<Shape
>();
283 Vector
<Shape
> adlist
= new Vector
<Shape
>();
285 BoundBox tester
= new BoundBox(plx
,ply
,plz
,pux
,puy
,puz
);
286 for (Shape shape
: shapes
)
287 if (shape
.isTouching(tester
)) {
291 double lx
= plx
< shape
.minX()? shape
.minX() : plx
;
292 double ly
= ply
< shape
.minY()? shape
.minY():ply
;
293 double lz
= plz
< shape
.minZ()?shape
.minZ():plz
;
294 double ux
= pux
> shape
.maxX()?shape
.maxX():pux
;
295 double uy
= puy
>shape
.maxY()?shape
.maxY():puy
;
296 double uz
= puz
> shape
.maxZ()?shape
.maxZ():puz
;
297 tmps
.add(new BoundBox(shape
.minX(),shape
.minY(),shape
.minZ(),lx
,shape
.maxY(),shape
.maxZ()));
298 tmps
.add(new BoundBox(ux
,shape
.minY(),shape
.minZ(),shape
.maxX(),shape
.maxY(),shape
.maxZ()));
299 tmps
.add(new BoundBox(lx
,shape
.minY(),shape
.minZ(),ux
,ly
,shape
.maxZ()));
300 tmps
.add(new BoundBox(lx
,uy
,shape
.minZ(),ux
,shape
.maxY(),shape
.maxZ()));
301 tmps
.add(new BoundBox(lx
,ly
,shape
.minZ(),ux
,uy
,lz
));
302 tmps
.add(new BoundBox(lx
,ly
,uz
,ux
,uy
,shape
.maxZ()));
304 if (s
.getVolume()>0.0) {
306 org
.alterverse
.speech
.tts
.speak("added");
309 for (Shape s
: adlist
)
311 for (Shape s
: remlist
)
318 public void setParent(GameObject obj
) {
322 public GameObject
getParent() {
326 public String
toJs(String varname
) {
327 String ret
= "var "+varname
+" = "+this.getClass().getName()+"(area);\n";
328 ret
+=varname
+".setPosition("+x
+","+y
+","+z
+");\n";
330 ret
+=varname
+".setSolid(true);\n";
332 ret
+=varname
+".setSolid(false);\n";
334 ret
+=varname
+".addShape("+s
.toJs()+");\n";