00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 package org.classroomgaming.cgp;
00021
00022 import java.awt.*;
00023 import java.awt.event.*;
00024 import java.applet.*;
00025 import java.beans.PropertyChangeEvent;
00026 import java.util.*;
00027 import javax.swing.*;
00028 import java.net.URL;
00029 import javax.vecmath.Point2i;
00030 import org.classroomgaming.cgp.*;
00031
00038 public class MapModule extends GameModule implements GameModule.Renderable, GameModule.Configurable, GameModule.Despawnable {
00039
00040 enum MoveDirection {
00041
00042 DOWN, UP, LEFT, RIGHT
00043 };
00044 private static boolean DEBUG = true;
00045 private int mapW;
00046 private int mapH;
00047 private int appW;
00048 private int appH;
00049 private AppletContext context;
00050 private java.net.URL base;
00051 private Configurator app;
00052 private LinkedHashMap<String, Integer> myImages;
00053 private String map;
00054 private int bgID;
00055
00056 private int imageIDMap[][];
00057
00058 public MapModule(GameModule m) {
00059 parent = m;
00060 myImages = new LinkedHashMap<String, Integer>();
00061 }
00062
00063 public void init(Configurator a) {
00064
00065
00066
00067 addImages(a);
00068
00069 map = a.getParameter("MAP");
00070 app = a;
00071 appH = a.getHeight();
00072 appW = a.getWidth();
00073
00074 initDimensions(map);
00075 imageIDMap = new int[mapW + 1][mapH + 1];
00076 initImageIDMap(map);
00077 enabled = true;
00078 CameraManager.getInstance().setMapDimensions(mapW, mapH);
00079
00080 }
00081
00082 String[] parse(String string, String string2) {
00083 StringTokenizer stringtokenizer = new StringTokenizer(string, string2);
00084 String string4[] = new String[stringtokenizer.countTokens()];
00085 int i = 0;
00086 for (; i < string4.length;) {
00087 string4[i] = stringtokenizer.nextToken();
00088 i++;
00089 }
00090 return (string4);
00091 }
00092
00093 public int addImage(String id, String file) {
00094 int imageID = ImageManager.getInstance().load(file);
00095 if (!myImages.containsKey(id)) {
00096
00097 myImages.put(id, new Integer(imageID));
00098 }
00099 return imageID;
00100 }
00101
00102 private void addImages(Configurator a) {
00103 int i = 0;
00104 i = 0;
00105 while (a.getParameter(new StringBuffer("addimage").append(i).toString()) != null) {
00106 i++;
00107
00108 }
00109
00110 i = 0;
00111
00112 String stringArr4[];
00113
00114 while (a.getParameter(new StringBuffer("addimage").append(i).toString()) != null) {
00115 stringArr4 = parse(a.getParameter(new StringBuffer("addimage").append(i).toString()), a.getParameter("separator"));
00116 int image = addImage(stringArr4[0], stringArr4[1]);
00117
00118
00119 i++;
00120 }
00121 }
00122
00123 public void enable(boolean val) {
00124 enabled = val;
00125 }
00126
00127 public boolean isEnabled() {
00128 return enabled;
00129 }
00130
00131 public void ponder(float t) {
00132 }
00133
00134 public void handleEvent(EventObject e) {
00135 }
00136
00137 public void deinit() {
00138 }
00139
00140 private void initDimensions(String string) {
00141
00142 int row = 0;
00143 int col = 0;
00144 for (int i = 0; i < string.length();) {
00145 int k = 1 + i;
00146 String string7 = string.substring(i, k);
00147 while (string7 == "\n" || string7 == "\r") {
00148 ++i;
00149 string7 = string.substring(i, i + 1);
00150 }
00151 String string8 = string7.substring(0, 1);
00152 if (string8.equals(app.getParameter("separator"))) {
00153 if (row == 0) {
00154 mapW = col;
00155 }
00156
00157 ++row;
00158 col = 0;
00159 } else {
00160 ++col;
00161 }
00162
00163
00164 i = k;
00165 }
00166 mapH = row;
00167 return;
00168 }
00169
00170 private void initImageIDMap(String string) {
00171
00172 int row = 0;
00173 int col = 0;
00174 for (int i = 0; i < string.length();) {
00175
00176 String string7 = string.substring(i, i + 1);
00177 while (string7.charAt(0) == '\n' || string7.charAt(0) == '\r') {
00178 ++i;
00179 string7 = string.substring(i, i + 1);
00180 }
00181
00182 if (string7.equals(app.getParameter("separator"))) {
00183 ++row;
00184 col = 0;
00185 } else if (myImages.containsKey(string7)) {
00186 Integer id = (Integer) myImages.get(string7);
00187 imageIDMap[col][row] = (int) id;
00188 ++col;
00189 } else {
00190 imageIDMap[col][row] = bgID;
00191 ++col;
00192 }
00193
00194 ++i;
00195 }
00196 return;
00197 }
00198
00199 public void imgByPos(Graphics graphics, int i, int j) {
00200 Camera c = CameraManager.getInstance().getActiveCamera();
00201 int i2 = (i - Math.round(c.getX()) + mapW / 2) * 32;
00202 int j2 = (j - Math.round(c.getY()) + mapH / 2) * 32;
00203 ImageManager.getInstance().drawImage(graphics, imageIDMap[i][j], i2, j2);
00204
00205
00206
00207 return;
00208 }
00209
00210 public void render(Graphics graphics) {
00211
00212 for (int j = 0; j < mapH; ++j) {
00213 for (int i = 0; i < mapW; ++i) {
00214 imgByPos(graphics, i, j);
00215 }
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 }
00234
00235 public boolean followLink(String myURL) {
00236 try {
00237 java.net.URL url4 = new java.net.URL(base, myURL);
00238 context.showDocument(url4, "_self");
00239 return true;
00240 } catch (Exception exception4) {
00241 exception4.printStackTrace();
00242 return false;
00243 }
00244 }
00245
00246 @Override
00247 public void propertyChange(PropertyChangeEvent e) {
00248 if (e.getPropertyName().equals(MouseModule.MOUSE_BUTTONS_CHANGE_EVENT) ||
00249 e.getPropertyName().equals(MouseModule.MOUSE_POSITION_CHANGE_EVENT)) {
00250 try {
00251 MouseModule m = (MouseModule) e.getSource();
00252
00253 if (m.getButtons() == 0) {
00254 return;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 Point2i p = m.getPosition();
00273 int x = p.getX();
00274 int y = p.getY();
00275 float normX = (float) x / (float) appW;
00276 float normY = (float) y / (float) appH;
00277
00278
00279
00280 if (normX == normY) {
00281 return;
00282 }
00283
00284 if ((normX - .5) * (normX - .5) + (normY - .5) * (normY - .5) < .01) {
00285 return;
00286 }
00287
00288
00289 if (normY > normX) {
00290 if ((1 - normX) < normY) {
00291 movePlayer(MoveDirection.DOWN);
00292 } else {
00293 movePlayer(MoveDirection.LEFT);
00294 }
00295 } else if (normX > normY) {
00296 if ((1 - normX) < normY) {
00297 movePlayer(MoveDirection.RIGHT);
00298 } else {
00299 movePlayer(MoveDirection.UP);
00300 }
00301 }
00302
00303
00304 } catch (ClassCastException e2) {
00305 }
00306 }
00307
00308 }
00309
00310 private void movePlayer(MoveDirection d) {
00311 GameWorld p = null;
00312 try {
00313 p = (GameWorld)parent;
00314 } catch (ClassCastException e) {}
00315
00316 Entity playerObj = p.getObjectByName("Player");
00317 AnimatedSprite animMod;
00318 try {
00319 animMod = (AnimatedSprite) playerObj.getModule("AnimatedSprite");
00320 } catch (ClassCastException e2) {
00321 return;
00322 }
00323 float xpos = playerObj.getX();
00324 float ypos = playerObj.getY();
00325
00326
00327 if (d == MoveDirection.UP) {
00328 ypos = ypos - 1;
00329 animMod.play("up");
00330
00331 } else if (d == MoveDirection.DOWN) {
00332 ypos = ypos + 1;
00333 animMod.play("down");
00334
00335 } else if (d == MoveDirection.LEFT) {
00336 xpos = xpos - 1;
00337 animMod.play("left");
00338
00339 } else if (d == MoveDirection.RIGHT) {
00340 xpos = xpos + 1;
00341 animMod.play("right");
00342
00343 }
00344
00345
00346
00347 if (xpos < 0) {
00348 xpos = 0;
00349 }
00350 if (xpos > mapW) {
00351 xpos = mapW;
00352 }
00353
00354 if (ypos < 0) {
00355 ypos = 0;
00356 }
00357
00358 if (ypos > mapH) {
00359 ypos = mapH;
00360 }
00361 playerObj.setX(xpos);
00362 playerObj.setY(ypos);
00363 Camera c = CameraManager.getInstance().getActiveCamera();
00364 c.setX(xpos);
00365 c.setY(ypos);
00366
00367 }
00368 }