Totally rewroat the code for collision of solid objects. Also added an example applic...
[alterverse.git] / src / org / alterverse / world / GameObject.java
blobd1f7b8e98671508339765bd6ddfcbba51f5010cf
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;
27 public class GameObject implements Serializable {
28 Shape shape;
29 double x;
30 double y;
31 double z;
32 double prevX;
33 double prevY;
34 double prevZ;
35 ArrayList<Sound> sounds;
36 boolean solid;
37 Area myArea;
38 public GameObject(Area area) {
39 this(area,0.0,0.0,0.0,new Shape());
42 public GameObject(Area area, Shape shape) {
43 this.x=(shape.minX()+shape.maxX())/2;
44 this.y=(shape.minY()+shape.maxY())/2;
45 this.z=(shape.minZ()+shape.maxZ())/2;
46 this.shape=shape;
47 solid=true;
48 sounds=new ArrayList<Sound>();
49 myArea=area;
50 prevX=x;
51 prevY=y;
52 prevZ=z;
55 public GameObject(Area area,double x,double y,double z,Shape shape) {
56 this.x=x;
57 this.y=y;
58 this.z=z;
59 this.shape=shape;
60 solid=true;
61 sounds=new ArrayList<Sound>();
62 myArea = area;
63 prevX=x;
64 prevY=y;
65 prevZ=z;
68 public boolean isTouching(GameObject other) {
69 return this.getShape().isTouching(other.getShape());
72 public void onBump(GameObject obj) {
76 public boolean canEnter(GameObject othr) {
77 return !(this.isSolid()&&othr.isSolid());
80 public boolean isSolid() {
81 return solid;
84 public void setSolid(boolean solid) {
85 this.solid=solid;
88 public boolean getSolid() {
89 return solid;
92 public void addSound(Sound sound) {
93 sounds.add(sound);
96 public void removeSound(Sound sound) {
97 sounds.remove(sound);
100 public ArrayList<Sound> getSounds() {
101 return sounds;
104 public void setPosition(double x, double y, double z) {
105 this.x = x;
106 this.y = y;
107 this.z = z;
110 public double getX() {
111 return x;
114 public double getY() {
115 return y;
118 public double getZ() {
119 return z;
122 public void updateData() {
123 for (Sound sound : sounds) {
124 sound.setPosition(x,y,z);
128 public void updateShape() {
129 shape.setPosition(x,y,z);
132 public void setX(double x) {
133 setPosition(x,y,z);
136 public void setY(double y) {
137 setPosition(x,y,z);
140 public void setZ(double z) {
141 setPosition(x,y,z);
144 public Shape getShape() {
145 return shape;
148 public void setShape(Shape shape) {
149 this.shape=shape;
152 public void process() {
155 public double minX() {
156 return shape.minX();
159 public double maxX() {
160 return shape.maxX();
163 public double minY() {
164 return shape.minY();
167 public double maxY() {
168 return shape.maxY();
171 public double minZ() {
172 return shape.minZ();
175 public double maxZ() {
176 return shape.maxZ();