Source code: org/meowers/cide/data/GameData.java
1 /*
2 * GameData
3 *
4 * Please see CVS for version information
5 *
6 * 10 Feb 2002
7 *
8 * Copyright 2002 Adam Miezianko
9 *
10 */
11
12 package org.meowers.cide.data;
13
14 import java.io.*;
15 import java.util.*;
16
17 /**
18 * Stores all the data necessary for a game to be run, or at least provides
19 * wrappers to other storage classes.
20 *
21 * @author Adam Miezianko
22 * @version %I%, %G%
23 */
24 public class GameData extends GameObject {
25
26 private ArrayList[] stores = new ArrayList[GameObject.MAX_TYPE];
27 private int tileSize;
28
29 /**
30 * Creates new GameData.
31 */
32 public GameData() {
33 type = GameObject.GAME_DATA;
34 for (int i = 0; i < GameObject.MAX_TYPE; i++)
35 stores[i] = new ArrayList();
36 }
37
38 /**
39 * Checks to see if the type of object is one that is storable here. In
40 * particular, it must be a valid type and not a <code>GameData</code>
41 * type either.
42 *
43 * @param objType the type of object requested.
44 * @returns if the specified object type is valid here.
45 */
46 private boolean isGameObjectTypeValid(int objType) {
47 if ((objType > 0)
48 && (objType < GameObject.MAX_TYPE)
49 && (objType != GameObject.GAME_DATA)) {
50 return true;
51 }
52
53 return false;
54 }
55
56 /**
57 * Adds and <code>GameObject</code> object to store in the game data.
58 *
59 * @param gameObj the <code>GameObject</code> to store.
60 */
61 public void addGameObject(GameObject gameObj) {
62 int objType = gameObj.getType();
63 if (isGameObjectTypeValid(objType)) {
64 stores[objType].add(gameObj);
65 }
66 }
67
68 /**
69 * Returns the number of <code>GameObject</code>s of the specified type
70 * are stored here. The the type specified is not valid, returns zero.
71 *
72 * @param objType the type of object to count.
73 * @return the number of objects of the specified type stored here, or
74 * zero for an invalid type specified.
75 */
76 public int getGameObjectCount(int objType) {
77 if (isGameObjectTypeValid(objType)) {
78 return stores[objType].size();
79 }
80
81 return 0;
82 }
83
84 /**
85 * Gets a <code>GameObject</code> at a specified index of a specified
86 * type. Returns <code>null</code> if something went wrong.
87 *
88 * @param objType the type of <code>GameObject</code> to get.
89 * @param index the index from which to get the object.
90 * @returns the <code>GameObject</code> gotten, or <code>null</code> if
91 * something went wrong.
92 */
93 public GameObject getGameObject(int objType, int index) {
94 if (isGameObjectTypeValid(objType)) {
95 if ((index >= 0) && (index < stores[objType].size())) {
96 return (GameObject) stores[objType].get(index);
97 }
98 }
99 return null;
100 }
101
102 /**
103 * Checks all the elements of an array of objects, and for each one of
104 * class <code>GameObject</code> it checks if its dirty. Returns <code>true
105 * </code> if any of the array's <code>GameObject</code> type elements are
106 * dirty.
107 *
108 * @param list the list to check.
109 * @return <code>true</code> if there are any dirty <code>GameObject</code>
110 * elements in the list.
111 */
112 private boolean isArrayDirty(ArrayList list) {
113 GameObject gameObj;
114 Object obj;
115 for (int i = 0; i < list.size(); i++) {
116 obj = list.get(i);
117 if (GameObject.class.isInstance(obj)) {
118 gameObj = (GameObject) obj;
119 if (gameObj.dirty) {
120 return true;
121 }
122 }
123 }
124
125 return false;
126 }
127
128 /**
129 * Returns the dirty state of this game data. This also does a deep check
130 * of all contained data objects.
131 *
132 * @return whether this <code>GameData</code> is dirty or not.
133 */
134 public boolean isDirty() {
135
136 /* check the tiles */
137 //if (isArrayDirty(tiles)) {
138 // dirty = true;
139 //}
140
141 /* check the maps */
142 //if (isArrayDirty(maps)) {
143 // dirty = true;
144 //}
145
146 return dirty;
147 }
148
149 /**
150 * Returns the size of tiles used in this game.
151 *
152 * @return the size of tiles used in this game.
153 */
154 public int getTileSize() {
155 return tileSize;
156 }
157
158 /**
159 * Set the size of tiles used in this game. If tiles exist, the operation
160 * fails unless the <code>destroy</code> parameter is <code>true</code>,
161 * in which case the existing tiles are discarded.
162 *
163 * NOTE: Soon a method of changing the tile size non-destructivley should
164 * be provided for maximum editing ability.
165 *
166 * @param tileSize the new size for tiles in this game.
167 * @param destroy whether to destroy the existing tiles and force the tile
168 * size change.
169 * @return <code>true</code> if the operation succeeded, <code>false</code>
170 * otherwise.
171 */
172 public boolean setTileSize(int tileSize, boolean destroy) {
173 if (stores[GameObject.TILE].isEmpty() || destroy) {
174 this.tileSize = tileSize;
175 dirty = true;
176 return true;
177 }
178
179 /* the operation failed */
180 return false;
181 }
182 }