Adding shape splitting functionallity to objects.
[alterverse.git] / src / org / alterverse / world / GameObject.java
blobb77c56ae587349de6956f417e2365366d181f619
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 Area myArea;
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>();
49 addShape(shape);
50 solid=true;
51 sounds=new ArrayList<Sound>();
52 myArea=area;
53 prevX=x;
54 prevY=y;
55 prevZ=z;
56 children = new Vector<GameObject>();
59 public GameObject(Area area,double x,double y,double z,Shape shape) {
60 this.x=x;
61 this.y=y;
62 this.z=z;
63 shapes = new Vector<Shape>();
64 addShape(shape);
65 solid=true;
66 sounds=new ArrayList<Sound>();
67 myArea = area;
68 prevX=x;
69 prevY=y;
70 prevZ=z;
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))
78 return true;
79 return false;
82 public void onBump(GameObject obj) {
86 public boolean canEnter(GameObject othr) {
87 return !(this.isSolid()&&othr.isSolid());
90 public boolean isSolid() {
91 return solid;
94 public void setSolid(boolean solid) {
95 this.solid=solid;
98 public boolean getSolid() {
99 return solid;
102 public void addSound(Sound sound) {
103 sounds.add(sound);
106 public void removeSound(Sound sound) {
107 sounds.remove(sound);
110 public ArrayList<Sound> getSounds() {
111 return sounds;
114 public boolean setPosition(double x, double y, double z) {
115 prevX=this.x;
116 prevY=this.y;
117 prevZ=this.z;
118 this.x = x;
119 this.y = y;
120 this.z = z;
121 return true;
124 public double getX() {
125 return x;
128 public double getY() {
129 return y;
132 public double getZ() {
133 return z;
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) {
152 setPosition(x,y,z);
155 public void setY(double y) {
156 setPosition(x,y,z);
159 public void setZ(double z) {
160 setPosition(x,y,z);
163 public Shape getShape() {
164 if (shapes.size()==0)
165 return null;
166 return shapes.get(0);
169 public void setShape(Shape shape) {
170 shapes.clear();
171 addShape(shape);
174 public Vector<Shape> getShapes() {
175 return shapes;
178 public void addShape(Shape shape) {
179 shapes.add(shape);
182 public Shape getShape(int i) {
183 if (i < shapes.size())
184 return shapes.get(i);
185 return null;
188 public void process() {
192 public double minX() {
193 if (shapes.size() == 0)
194 return 0;
195 double min = 0;
196 for (Shape shape: shapes)
197 if (shape.minX()<min)
198 min=shape.minX();
199 return min;
202 public double maxX() {
203 if (shapes.size() == 0)
204 return 0;
205 double max = 0;
206 for (Shape shape: shapes)
207 if (shape.maxX()>max)
208 max=shape.maxX();
209 return max;
212 public double minY() {
213 if (shapes.size() == 0)
214 return 0;
215 double min = 0;
216 for (Shape shape: shapes)
217 if (shape.minY()<min)
218 min=shape.minY();
219 return min;
222 public double maxY() {
223 if (shapes.size() == 0)
224 return 0;
225 double max = 0;
226 for (Shape shape: shapes)
227 if (shape.maxY()>max)
228 max=shape.maxY();
229 return max;
232 public double minZ() {
233 if (shapes.size() == 0)
234 return 0;
235 double min = 0;
236 for (Shape shape: shapes)
237 if (shape.minZ()<min)
238 min=shape.minZ();
239 return min;
242 public double maxZ() {
243 if (shapes.size() == 0)
244 return 0;
245 double max = 0;
246 for (Shape shape: shapes)
247 if (shape.maxZ()>max)
248 max=shape.maxZ();
249 return max;
252 public void addChild(GameObject child) {
253 children.add(child);
254 myArea.add(child);
257 public void onRemove() {
258 for (GameObject obj: children)
259 myArea.remove(obj);
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);
270 return null;
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>();
281 int num=0;
282 BoundBox tester = new BoundBox(plx,ply,plz,pux,puy,puz);
283 for (Shape shape: shapes)
284 if (shape.isTouching(tester)) {
285 num++;
286 tmps.clear();
287 remlist.add(shape);
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()));
300 for (Shape s: tmps)
301 if (s.getVolume()>0.0) {
302 adlist.add(s);
303 org.alterverse.speech.tts.speak("added");
306 for (Shape s: adlist)
307 addShape(s);
308 for (Shape s: remlist)
309 shapes.remove(s);
310 if (num>0)
311 return true;
312 return false;