Added loading and saving of areas using javascript.
[alterverse.git] / src / org / alterverse / world / GameObject.java
blob8ea72b43b083e4e0f84ae4db239ed60666b7f271
1 /***************************************************************************
2 * Copyright (C) 2010 by the Alterverse team *
3 * email: rynkruger@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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;
22 import java.io.*;
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 {
29 Vector<Shape> shapes;
30 double x;
31 double y;
32 double z;
33 double prevX;
34 double prevY;
35 double prevZ;
36 ArrayList<Sound> sounds;
37 boolean solid;
38 GameObject parent=null;
39 Area myArea;
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>();
50 addShape(shape);
51 solid=true;
52 sounds=new ArrayList<Sound>();
53 myArea=area;
54 prevX=x;
55 prevY=y;
56 prevZ=z;
57 children = new Vector<GameObject>();
60 public GameObject(Area area,double x,double y,double z,Shape shape) {
61 this.x=x;
62 this.y=y;
63 this.z=z;
64 shapes = new Vector<Shape>();
65 addShape(shape);
66 solid=true;
67 sounds=new ArrayList<Sound>();
68 myArea = area;
69 prevX=x;
70 prevY=y;
71 prevZ=z;
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))
79 return true;
80 return false;
83 public void onBump(GameObject obj) {
87 public boolean canEnter(GameObject othr) {
88 return !(this.isSolid()&&othr.isSolid());
91 public boolean isSolid() {
92 return solid;
95 public void setSolid(boolean solid) {
96 this.solid=solid;
99 public boolean getSolid() {
100 return solid;
103 public void addSound(Sound sound) {
104 sounds.add(sound);
107 public void removeSound(Sound sound) {
108 sounds.remove(sound);
111 public ArrayList<Sound> getSounds() {
112 return sounds;
115 public boolean setPosition(double x, double y, double z) {
116 prevX=this.x;
117 prevY=this.y;
118 prevZ=this.z;
119 this.x = x;
120 this.y = y;
121 this.z = z;
122 return true;
125 public double getX() {
126 return x;
129 public double getY() {
130 return y;
133 public double getZ() {
134 return z;
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) {
153 setPosition(x,y,z);
156 public void setY(double y) {
157 setPosition(x,y,z);
160 public void setZ(double z) {
161 setPosition(x,y,z);
164 public Shape getShape() {
165 if (shapes.size()==0)
166 return null;
167 return shapes.get(0);
170 public void setShape(Shape shape) {
171 shapes.clear();
172 addShape(shape);
175 public Vector<Shape> getShapes() {
176 return shapes;
179 public void addShape(Shape shape) {
180 shapes.add(shape);
183 public Shape getShape(int i) {
184 if (i < shapes.size())
185 return shapes.get(i);
186 return null;
189 public void process() {
193 public double minX() {
194 if (shapes.size() == 0)
195 return 0;
196 double min = 0;
197 for (Shape shape: shapes)
198 if (shape.minX()<min)
199 min=shape.minX();
200 return min;
203 public double maxX() {
204 if (shapes.size() == 0)
205 return 0;
206 double max = 0;
207 for (Shape shape: shapes)
208 if (shape.maxX()>max)
209 max=shape.maxX();
210 return max;
213 public double minY() {
214 if (shapes.size() == 0)
215 return 0;
216 double min = 0;
217 for (Shape shape: shapes)
218 if (shape.minY()<min)
219 min=shape.minY();
220 return min;
223 public double maxY() {
224 if (shapes.size() == 0)
225 return 0;
226 double max = 0;
227 for (Shape shape: shapes)
228 if (shape.maxY()>max)
229 max=shape.maxY();
230 return max;
233 public double minZ() {
234 if (shapes.size() == 0)
235 return 0;
236 double min = 0;
237 for (Shape shape: shapes)
238 if (shape.minZ()<min)
239 min=shape.minZ();
240 return min;
243 public double maxZ() {
244 if (shapes.size() == 0)
245 return 0;
246 double max = 0;
247 for (Shape shape: shapes)
248 if (shape.maxZ()>max)
249 max=shape.maxZ();
250 return max;
253 public void addChild(GameObject child) {
254 children.add(child);
255 myArea.add(child);
256 child.setParent(this);
259 public void onRemove() {
260 for (GameObject obj: children)
261 myArea.remove(obj);
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);
273 return null;
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>();
284 int num=0;
285 BoundBox tester = new BoundBox(plx,ply,plz,pux,puy,puz);
286 for (Shape shape: shapes)
287 if (shape.isTouching(tester)) {
288 num++;
289 tmps.clear();
290 remlist.add(shape);
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()));
303 for (Shape s: tmps)
304 if (s.getVolume()>0.0) {
305 adlist.add(s);
306 org.alterverse.speech.tts.speak("added");
309 for (Shape s: adlist)
310 addShape(s);
311 for (Shape s: remlist)
312 shapes.remove(s);
313 if (num>0)
314 return true;
315 return false;
318 public void setParent(GameObject obj) {
319 parent=obj;
322 public GameObject getParent() {
323 return parent;
326 public String toJs(String varname) {
327 String ret = "var "+varname+" = "+this.getClass().getName()+"(area);\n";
328 ret+=varname+".setPosition("+x+","+y+","+z+");\n";
329 if (getSolid())
330 ret+=varname+".setSolid(true);\n";
331 else
332 ret+=varname+".setSolid(false);\n";
333 for (Shape s:shapes)
334 ret+=varname+".addShape("+s.toJs()+");\n";
335 return ret;