1 16 package com.buchuki.ensmer; 17 18 import java.awt.event.KeyEvent ; 19 import static java.lang.Math.PI ; 20 import java.util.*; 21 import javax.media.j3d.Transform3D; 22 import javax.swing.event.EventListenerList ; 23 import javax.vecmath.*; 24 import com.buchuki.ensmer.builtin.*; 25 import com.buchuki.ensmer.input.InputManager; 26 import com.buchuki.ensmer.input.command.*; 27 import com.buchuki.ensmer.input.event.EnsmerInputEvent; 28 import com.buchuki.ensmer.input.event.KeyPressEvent; 29 import com.buchuki.ensmer.input.event.semantic.*; 30 import com.buchuki.ensmer.prevayler.queries.*; 31 import org.prevayler.*; 32 33 34 44 public class AreaManager { 45 46 51 public void previousArea() { 52 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 53 try { 54 Long prevID = (Long ) prevayler.execute(new PreviousAreaQuery()); 55 if (prevID != null) { 56 loadArea(prevID); 57 } 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 } 62 63 71 public void nextArea() { 72 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 73 74 try { 75 Long nextID = (Long ) prevayler.execute(new NextAreaQuery()); 76 if (nextID != null) { 77 loadArea(nextID); 78 } 79 } catch (Exception e) { 80 e.printStackTrace(); 81 } 82 } 83 84 92 public Area newArea() { 93 try { 94 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 95 Long areaID = (Long ) prevayler.execute(new GetNextAreaIDQuery()); 96 prevayler.execute(new AddAreaTransaction(areaID)); 97 Area newArea = new Area(areaID); 98 areaMap.put(areaID, newArea); 99 return newArea; 100 } catch (Exception e) { 101 e.printStackTrace(); 102 return null; 103 } 104 } 105 106 114 public void removeArea(Long areaID) { 115 if (currentArea.getAreaID().equals(areaID)) { 116 throw new IllegalStateException ("Cannot destroy the currently visible area"); 117 } 118 Area area = areaMap.remove(areaID); 119 if (area == null) { 120 return; 121 } 122 area.destroyAll(); 123 InputManager.getCommandMap().removeAllCommands(area); 124 EnsmerManager.instance().getPrevayler().execute(new RemoveAreaTransaction(areaID)); 125 } 126 127 133 public void addObjectListener(ObjectListener listener) { 134 listenerList.add(ObjectListener.class, listener); 135 } 136 137 143 public void removeObjectListener(ObjectListener listener) { 144 listenerList.remove(ObjectListener.class, listener); 145 } 146 147 152 public Area getCurrentArea() { 153 return currentArea; 154 } 155 156 174 public Long getHistoricalAreaID(int index) { 175 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 176 try { 177 return (Long ) prevayler.execute(new GetHistoricalIDQuery(index)); 178 } catch (Exception e) { 179 e.printStackTrace(); 180 return null; 181 } 182 } 183 184 193 public Long getAreaIDForObject(Long objectID) { 194 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 195 try { 196 return (Long ) prevayler.execute(new GetAreaForObjectQuery(objectID)); 197 } catch (Exception e) { 198 e.printStackTrace(); 199 return null; 200 } 201 } 202 203 210 public Area getArea(Long id) { 211 return areaMap.get(id); 212 } 213 214 224 public void setCurrentArea(Long id) { 225 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 226 prevayler.execute(new SetCurrentAreaTransaction(id)); 227 loadArea(id); 228 } 229 230 235 protected void fireObjectEvent(ObjectEvent event) { 236 Object [] listeners = listenerList.getListenerList(); 237 for (int i = listeners.length - 2; i >= 0; i -= 2) { 238 if (listeners[i] == ObjectListener.class) { 239 ((ObjectListener) listeners[i + 1]).objectStateChanged(event); 240 } 241 } 242 } 243 244 252 AreaManager() throws Exception { 253 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 254 long[] prevayledAreas = (long[]) prevayler.execute(new GetAreasQuery()); 255 256 if (prevayledAreas.length != 0) { 257 for (long id : prevayledAreas) { 258 areaMap.put(id, new Area(id)); 259 } 260 long currentAreaID = (Long ) prevayler.execute(new GetCurrentAreaQuery()); 261 currentArea = areaMap.get(currentAreaID); 262 } 263 } 264 265 271 void loadAreas() throws Exception { 272 if (areaMap.isEmpty()) { 273 setupDefaultAreas(); 274 } 275 276 282 final SpecialAreaManager spec = EnsmerManager.instance().getSpecialAreaManager(); 283 Command emptyStorageCommand = 284 new Command() { 285 public boolean execute(EnsmerInputEvent evt) { 286 getArea(spec.getAreaID(spec.STORAGE)).destroyAll(); 287 return true; 288 } 289 }; 290 Area inventory = getArea(spec.getAreaID(spec.INVENTORY)); 291 CommandMap map = InputManager.getCommandMap(); 292 map.addCommand(inventory, new RemoveFromAreaCommand(inventory.getAreaID()), new KeyPressEvent(KeyEvent.VK_R)); 293 Area storage = getArea(spec.getAreaID(spec.STORAGE)); 294 map.addCommand(storage, new RemoveFromAreaCommand(storage.getAreaID()), new KeyPressEvent(KeyEvent.VK_R)); 295 map.addCommand(storage, emptyStorageCommand, new KeyPressEvent(KeyEvent.VK_DELETE)); 296 loadArea(currentArea.getAreaID()); 297 } 298 299 307 private void loadArea(Long id) { 308 if (areaMap.containsKey(id)) { 309 currentArea = areaMap.get(id); 310 try { 311 currentArea.loadObjects(); } catch (Exception e) { 313 e.printStackTrace(); 314 } 315 InterfaceManager intf = EnsmerManager.instance().getInterfaceManager(); 316 intf.setViewPosition(currentArea.getUserPosition()); 317 intf.displayArea(currentArea); 318 EnsmerManager.instance().getSelectionManager().clearSelection(); 319 EnsmerManager.instance().getUserManager().getInputManager().systemMode(); 320 } 321 else { 322 throw new IllegalStateException ("Area " + id + " does not exist."); 323 } 324 } 325 326 334 private void setupDefaultAreas() throws Exception { 335 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 336 337 currentArea = newArea(); 339 prevayler.execute(new SetCurrentAreaTransaction(currentArea.getAreaID())); 340 currentArea.loadObjects(); 342 Transform3D trans = new Transform3D(); 344 Matrix4f mat = new Matrix4f(); 345 trans.rotY(PI / 2); trans.setTranslation(new Vector3f(0f, -1.25f, -2f)); 347 trans.get(mat); 348 Static obj = new Static(); 349 obj.setFilename("floor.wrl"); 350 currentArea.newObject(obj, mat); 351 352 trans.setIdentity(); 353 trans.setTranslation(new Vector3f(-2.99f, -.673f, -7.24f)); 354 trans.get(mat); 355 obj = new Static(); 356 obj.setFilename("bookshelf.wrl"); 357 currentArea.newObject(obj, mat); 358 359 trans.setIdentity(); 360 trans.setTranslation(new Vector3f(2.75f, -1.02f, -7.25f)); 361 trans.get(mat); 362 obj = new Static(); 363 obj.setFilename("desk.wrl"); 364 currentArea.newObject(obj, mat); 365 366 trans.setIdentity(); 367 trans.rotY(PI / 2); 368 trans.setTranslation(new Vector3f(-3.25f, -1.4f, -1f)); 369 trans.get(mat); 370 obj = new Static(); 371 obj.setFilename("table.wrl"); 372 currentArea.newObject(obj, mat); 373 374 trans.setIdentity(); 375 trans.setTranslation(new Vector3f(0f, -1.6f, -2f)); 376 trans.get(mat); 377 Background bg = new Background(); 378 currentArea.newObject(bg, mat); 379 bg.setConfigProperty("filename", "windows.gif"); 380 381 SpecialAreaManager spec = EnsmerManager.instance().getSpecialAreaManager(); 382 Area inventory = newArea(); 383 spec.setSpecialID(spec.INVENTORY, inventory.getAreaID()); 384 Area storage = newArea(); 385 spec.setSpecialID(spec.STORAGE, storage.getAreaID()); 386 } 387 388 393 private Map<Long , Area> areaMap = new HashMap<Long , Area>(); 394 395 400 private Area currentArea; 401 402 405 private EventListenerList listenerList = new EventListenerList (); 406 } 407 408 | Popular Tags |