| C:\__todo__\tecaj.www\src\Epsilon_02\Level.java |
1 /**
2 * Level contains the Level load and paint for the Epsilon game.
3 * Epsilon is a simple space-shooter-vertical-scroller game.
4 *
5 * Epsilon is available for non-commercial use only!
6 * You can learn from this sources and you can modify them
7 * for learning purposes.
8 *
9 * @author Ziga Hajdukovic
10 * @version 1.0
11 *
12 */
13
14 import java.io.*;
15
16 import javax.microedition.lcdui.Graphics;
17
18 class Level
19 {
20
21 public static final byte MAX_LEVELS = 1;
22
23 public static final byte LEVEL_WIDTH = 16;
24 public static final byte LEVEL_HEIGHT = 64;
25
26 public static final byte TILE_SIZE = 16;
27 public static final byte TILES_PER_IMAGE_LINE = 5;
28
29 // space background position and scroll speed
30 public static int space_scroll_y;
31 public static int space_scroll_vy = -1;
32
33 // tiled background position and scroll speed
34 public static int tiles_scroll_y;
35 public static int tiles_scroll_vy = -2;
36 public static int tiles_scroll_x;
37
38 public static byte levelno;
39
40 public static byte[] tileMap;
41
42 // a translation table for level tile text representation
43 // to tile index in the tile image
44 public static char[] tileTextToImageIndex =
45 {
46 "J", "L","^","<",">",
47 "\\","/","v","O","G",
48 };
49
50 public static final byte SPRITE_ID_PLAYER = 0; // "P"
51 public static final byte SPRITE_ID_ENEMY1 = 1; // "A"
52 public static final byte SPRITE_ID_ENEMY2 = 2; // "V"
53 public static final byte SPRITE_ID_ENEMY3 = 3; // "O"
54 public static final byte SPRITE_ID_BOSS1 = 4; // "B"
55 // public static final byte SPRITE_ID_EXPLOSION = 5;
56 // public static final byte SPRITE_ID_FIRE1 = 6;
57 // public static final byte SPRITE_ID_FIRE2 = 7;
58 // public static final byte SPRITE_ID_FIRE3 = 8;
59
60 // a translation table for level sprite text representation
61 // to sprite id
62 public static char[] spriteTextToSpriteId =
63 {
64 "P","A","V","O","B",
65 };
66
67 public static byte getTileIndex(char t)
68 {
69 byte idx = 0;
70 // find tile index
71 while ( idx < tileTextToImageIndex.length && tileTextToImageIndex[idx] != t )
72 idx++;
73
74 // handle unexisting or empty tiles
75 if (idx > tileTextToImageIndex.length)
76 idx = -1;
77
78 return idx;
79 }
80
81 public static byte getSpriteId(char t)
82 {
83 byte idx = 0;
84 // find sprite id
85 while ( idx < spriteTextToSpriteId.length && spriteTextToSpriteId[idx] != t )
86 idx++;
87
88 // handle unexisting sprites
89 if (idx > spriteTextToSpriteId.length)
90 idx = -1;
91
92 return idx;
93 }
94
95 public static void loadLevel(byte level_no)
96 {
97 levelno = level_no;
98
99 // intialize the scrolling background
100 space_scroll_y = 0;
101 space_scroll_vy = 1;
102
103 InputStream inputstream = null;
104 DataInputStream datainputstream = null;
105
106 tileMap = new byte[LEVEL_HEIGHT * LEVEL_WIDTH];
107 int map_row = 0;
108 int tile_map_col = 0;
109 int sprite_map_col = 0;
110 try
111 {
112 inputstream = new Object().getClass().getResourceAsStream( "/stage"+ level_no +".txt" );
113 datainputstream = new DataInputStream(inputstream);
114
115 char ch;
116 boolean eof = false;
117 while(!eof)
118 {
119 // read level text file lines
120 try
121 {
122 // load a row of tiles
123 tile_map_col = 0;
124 ch = " ";
125 while (ch != ";")
126 {
127 ch = (char) datainputstream.readByte();
128 System.out.print(ch);
129
130 if (ch != ";")
131 {
132 tileMap[ map_row * LEVEL_WIDTH + tile_map_col ] = getTileIndex(ch);
133
134 tile_map_col++;
135 }
136 else
137 {
138 break;
139 }
140 }
141 // load a row of sprites
142 sprite_map_col = 0;
143 ch = " ";
144 while (ch != ";")
145 {
146 ch = (char) datainputstream.readByte();
147 System.out.print(ch);
148
149 if (ch != ";")
150 {
151 switch (getSpriteId(ch)) {
152 case SPRITE_ID_PLAYER:
153
154 // initialize player sprite
155 GameCanvas.player = new GameSprite(
156 GameCanvas.SPRITE_IMAGE_PLAYER,
157 GameCanvas.MAX_ENERGY_PLAYER);
158 GameCanvas.player.x = GameCanvas.canvasWidth / 2 - GameCanvas.player.w/2;
159 GameCanvas.player.y = GameCanvas.canvasHeight - GameCanvas.player.h - GameCanvas.player.h/2;
160 GameCanvas.player.frameId = GameCanvas.SPRITE_FRAME_PLAYER_CENTER;
161
162 break;
163 case SPRITE_ID_ENEMY1:
164 break;
165 case SPRITE_ID_ENEMY2:
166 break;
167 case SPRITE_ID_ENEMY3:
168 break;
169 case SPRITE_ID_BOSS1:
170 break;
171 }
172
173 sprite_map_col++;
174 }
175 else
176 {
177
178 System.out.println();
179
180 // read 0x0D and 0x0A (CR, LF)
181 ch = (char) datainputstream.readByte();
182 ch = (char) datainputstream.readByte();
183
184 break;
185 }
186 }
187
188 // go to next line of text
189 map_row++;
190 }
191 catch (EOFException eofex)
192 {
193 eof = true;
194 }
195 }
196 inputstream.close();
197 inputstream = null;
198 }
199 catch (Exception ex)
200 {
201 ex.printStackTrace();
202 }
203 }
204
205 public static void paintTiles(Graphics g)
206 {
207 // clip window coordinates
208 int cx, cy, cw, ch;
209 // image draw coordinates
210 int dx, dy;
211
212 int x;
213 int y = 0;
214
215 while (y <= GameCanvas.canvasHeight)
216 {
217 x = 0;
218 while (x <= GameCanvas.canvasWidth)
219 {
220 // get the tileMap index of the tile, located at tx, ty
221 int tx = tiles_scroll_x + x;
222 int ty = tiles_scroll_y + y;
223 int tile_idx = ty / TILE_SIZE * LEVEL_WIDTH + tx / TILE_SIZE;
224
225 // get tile index in image from tileMap
226 byte tile_image_idx = tileMap[tile_idx];
227
228 if ( tile_image_idx >= 0)
229 {
230 // get tile level coordinates from tile index
231 int tile_idx_x = (tile_idx % LEVEL_WIDTH) * TILE_SIZE;
232 int tile_idx_y = (tile_idx / LEVEL_WIDTH) * TILE_SIZE;
233
234 // translate to screen coordinates
235 tile_idx_x -= tiles_scroll_x;
236 tile_idx_y -= tiles_scroll_y;
237
238 int tile_col = tile_image_idx % TILES_PER_IMAGE_LINE;
239 int tile_row = tile_image_idx / TILES_PER_IMAGE_LINE;
240
241 cx = tile_idx_x;
242 cy = tile_idx_y;
243 cw = TILE_SIZE;
244 ch = TILE_SIZE;
245
246 // check clip window coordinates, if out of screen
247 if (cx < 0)
248 {
249 cw += cx;
250 cx = 0;
251 }
252 if (cy < 0)
253 {
254 ch += cy;
255 cy = 0;
256 }
257
258 // check clip window size, if out of screen
259 if (cx + cw > GameCanvas.canvasWidth)
260 cw -= (cx + cw) - GameCanvas.canvasWidth;
261 if (cy + ch > GameCanvas.canvasHeight)
262 ch -= (cy + ch) - GameCanvas.canvasHeight;
263
264 dx = tile_idx_x - tile_col * TILE_SIZE;
265 dy = tile_idx_y - tile_row * TILE_SIZE;
266
267 g.setClip(cx, cy, cw, ch);
268 g.drawImage(GameCanvas.tileImage, dx, dy, Graphics.TOP | Graphics.LEFT);
269 }
270
271 x += TILE_SIZE;
272 }
273
274 y += TILE_SIZE;
275 }
276 g.setClip(0, 0, GameCanvas.canvasWidth, GameCanvas.canvasHeight);
277 }
278
279 public static void paintSpaceBackground(Graphics g)
280 {
281 // fill the vast space
282 g.setColor(0x000000);
283 g.fillRect(0, 0, GameCanvas.canvasWidth, GameCanvas.canvasHeight);
284
285 // init scrolling background image tiling
286 int x = 0;
287 int y = space_scroll_y % GameCanvas.imgBackground.getHeight() - GameCanvas.imgBackground.getHeight();
288
289 // image is wide enough, so we only need to tile it in the vertical (y) direction
290 while (y < GameCanvas.canvasHeight)
291 {
292 g.drawImage(GameCanvas.imgBackground, x, y, Graphics.TOP | Graphics.LEFT);
293
294 y += GameCanvas.imgBackground.getHeight();
295 }
296 }
297
298 }
299