1 package erland.game.tileadventure;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import erland.game.GameEnvironmentInterface;
22 import erland.util.ParameterValueStorageExInterface;
23
24 import java.awt.*;
25
26
27 public abstract class GameObject implements Cloneable, MapObjectInterface {
28 /*** The block container which the block exist in */
29 private IrregularBlockContainerInterface cont;
30 /*** The x position of the block */
31 private float posX;
32 /*** The y position of the block */
33 private float posY;
34 /*** The z position of the block */
35 private float posZ;
36 /*** The game environment of the block */
37 private GameEnvironmentInterface environment;
38 /*** The object map which the object lives in */
39 private MapObjectContainerInterface objectMap;
40 /*** The object that shall take car of object actions */
41 private GameObjectMapActionInterface actionMap;
42 /*** Child objects */
43 private GameObject[] childs;
44 /*** Parent object */
45 private GameObject parent;
46 /*** Movement vector */
47 private Vector3D moveVector = new Vector3D();
48 /*** Bounding box */
49 private Box3D boundingBox;
50
51
52 public void init(GameEnvironmentInterface environment)
53 {
54 this.environment = environment;
55 }
56
57 /***
58 * Get the game environment which the block exists in
59 * @return The game environment the block exists in
60 */
61 protected GameEnvironmentInterface getEnvironment()
62 {
63 return environment;
64 }
65 /***
66 * Get the container which the block exists in
67 * @return The block container
68 */
69 public IrregularBlockContainerInterface getContainer()
70 {
71 return cont;
72 }
73
74 public void setContainer(IrregularBlockContainerInterface cont)
75 {
76 this.cont = cont;
77 }
78
79 public void setObjectMap(MapObjectContainerInterface objectMap) {
80 this.objectMap = objectMap;
81 }
82
83 public MapObjectContainerInterface getObjectMap() {
84 return objectMap;
85 }
86
87 public void setActionMap(GameObjectMapActionInterface actionMap) {
88 this.actionMap = actionMap;
89 }
90 public GameObjectMapActionInterface getActionMap() {
91 return actionMap;
92 }
93
94 public float getPosX()
95 {
96 return posX;
97 }
98
99 public float getPosY()
100 {
101 return posY;
102 }
103
104 public float getPosZ() {
105 return posZ;
106 }
107
108 public float getMovingPosX()
109 {
110 return getPosX();
111 }
112
113 public float getMovingPosY()
114 {
115 return getPosY();
116 }
117
118 public float getMovingPosZ() {
119 return getPosZ();
120 }
121
122 public int getPixelPosX() {
123 return getContainer().getPositionX(getPosX(),getPosY(),getPosZ());
124 }
125 public int getPixelPosY() {
126 return getContainer().getPositionY(getPosX(),getPosY(),getPosZ());
127 }
128 public int getDrawingPosX(float dx,float dy, float dz) {
129 return getContainer().getDrawingPositionX(getPosX()+dx,getPosY()+dy,getPosZ()+dz);
130 }
131
132 public int getDrawingPosY(float dx, float dy, float dz) {
133 return getContainer().getDrawingPositionY(getPosX()+dx,getPosY()+dy,getPosZ()+dz);
134 }
135
136 public void setPos(float x, float y, float z)
137 {
138
139
140
141
142
143
144 posX = x;
145 posY = y;
146 posZ = z;
147 if(boundingBox!=null) {
148 boundingBox.setLocation(x,y,z);
149 boundingBox.setSize(0.999f,0.999f,0.999f);
150 }
151 }
152
153 /***
154 * Check if the block needs to be redrawn
155 * @return true/false (Needs redraw/Does not need redraw)
156 */
157 public boolean getRedraw()
158 {
159 return false;
160 }
161
162 /***
163 * Clone the block
164 * @return A clone of the block
165 */
166 public Object clone()
167 {
168 GameObject b=null;
169 try {
170 b = (GameObject)super.clone();
171 } catch (CloneNotSupportedException e) {
172 e.printStackTrace();
173 }
174 return b;
175 }
176
177 public void write(ParameterValueStorageExInterface out) {
178 out.setParameter("x",Integer.toString((int)getPosX()));
179 out.setParameter("y",Integer.toString((int)getPosY()));
180 out.setParameter("z",Integer.toString((int)getPosZ()));
181 }
182
183 public void read(ParameterValueStorageExInterface in) {
184 int x = Integer.valueOf(in.getParameter("x")).intValue();
185 int y = Integer.valueOf(in.getParameter("y")).intValue();
186 int z = Integer.valueOf(in.getParameter("z")).intValue();
187 setPos(x,y,z);
188 }
189
190 protected boolean actionChild(Action action) {
191 return false;
192 }
193
194 public boolean action(Action action) {
195 if(parent!=null) {
196 return parent.action(action);
197 }else {
198 Action newAction = action;
199 if(childs!=null) {
200 for (int i = 0; i < childs.length; i++) {
201 Action a = childs[i].isActionPossible(action);
202 if(a==Action.NONE) {
203 return false;
204 }else if(!action.equals(a)) {
205 newAction = a;
206 }
207 }
208 Action a = isActionPossible(action);
209 if(a==Action.NONE) {
210 return false;
211 }else if(!action.equals(a)) {
212 newAction = a;
213 }
214 for (int i = 0; i < childs.length; i++) {
215 childs[i].actionChild(newAction);
216 }
217 }
218 return actionChild(newAction);
219 }
220 }
221
222 protected Action isActionPossible(Action action) {
223 return Action.NONE;
224 }
225 protected boolean isMovableChild(Direction direction) {
226 return false;
227 }
228 public boolean isMovable(Direction direction) {
229 if(parent!=null) {
230 return parent.isMovable(direction);
231 }else {
232 if(childs!=null) {
233 for (int i = 0; i < childs.length; i++) {
234 if(!childs[i].isMovableChild(direction)) {
235 return false;
236 }
237 }
238 }
239 return isMovableChild(direction);
240 }
241 }
242
243 protected boolean isPushableChild(Direction direction) {
244 return false;
245 }
246 public boolean isPushable(Direction direction) {
247 if(parent!=null) {
248 return parent.isPushable(direction);
249 }else {
250 if(childs!=null) {
251 for (int i = 0; i < childs.length; i++) {
252 if(!childs[i].isPushableChild(direction)) {
253 return false;
254 }
255 }
256 }
257 return isPushableChild(direction);
258 }
259 }
260 public void setParent(GameObject parent) {
261 this.parent = parent;
262 }
263 public void setChilds(GameObject[] childs) {
264 this.childs = childs;
265 }
266
267 public GameObject[] getChilds() {
268 return childs;
269 }
270
271 public GameObject getParent() {
272 return parent;
273 }
274 public void setMoveVector(Vector3D vector) {
275 moveVector = vector;
276 }
277 public Vector3D getMoveVector() {
278 return moveVector;
279 }
280
281 public boolean update() {
282
283 return true;
284 }
285
286 public Box3D getBoundingBox() {
287 if(boundingBox==null) {
288 boundingBox = new Box3D();
289 boundingBox.setLocation(posX,posY,posZ);
290 boundingBox.setSize(0.999f,0.999f,0.999f);
291 }
292 return boundingBox;
293 }
294
295 public void fillBoundingBox(float x, float y, float z, Box3D boundingBox) {
296 boundingBox.setLocation(x,y,z);
297 boundingBox.setSize(0.999f,0.999f,0.999f);
298 }
299 }
300