diff --git a/src/main/java/cactusServer/entities/Instance.java b/src/main/java/cactusServer/entities/Instance.java index 3688f12..e273e3b 100644 --- a/src/main/java/cactusServer/entities/Instance.java +++ b/src/main/java/cactusServer/entities/Instance.java @@ -183,6 +183,18 @@ setState(state); return this; } + + public Bullet updateBullet(String playerId, String bulletId, Position3D position, Quaternion3D angle) { + HashMap map = bulletMap.get(playerId); + if (map != null) { + Bullet bullet = map.get(bulletId); + if (bullet != null) { + bullet.update(position, angle); + } + return bullet; + } + return null; + } public Character destroyCharacter(String characterId) { return characterMap.remove(characterId); @@ -195,6 +207,16 @@ public Object destroyObject(String objId) { return objMap.remove(objId); } + + public Bullet destroyBullet(String playerId, String bulletId) { + HashMap map = bulletMap.get(playerId); + if (map != null) { + Bullet removedBullet = map.remove(bulletId); + this.getUniverse().displace(removedBullet.getPlaceable()); + return removedBullet; + } + return null; + } public static enum State { AVAILABLE, MAINTENANCE; diff --git a/src/main/java/cactusServer/resources/BulletsRest.java b/src/main/java/cactusServer/resources/BulletsRest.java index 7528625..822c475 100644 --- a/src/main/java/cactusServer/resources/BulletsRest.java +++ b/src/main/java/cactusServer/resources/BulletsRest.java @@ -51,13 +51,9 @@ public String updateBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId, @PathParam("bulletId") String bulletId, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle) { Instance instance = Instances.getInstance().getInstance(instanceId); - HashMap map = instance.getBullets().get(playerId); - if (map != null) { - Bullet bullet = map.get(bulletId); - if (bullet != null) { - bullet.update(position, angle); - } - return JSON.encode(map); + Bullet bullet = instance.updateBullet(playerId, bulletId, position, angle); + if (bullet != null) { + return JSON.encode(bullet); } throw new WebApplicationException(400); } @@ -68,10 +64,8 @@ public String destroyBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId, @PathParam("bulletId") String bulletId) { Instance instance = Instances.getInstance().getInstance(instanceId); - HashMap map = instance.getBullets().get(playerId); - if (map != null) { - Bullet removedBullet = map.remove(bulletId); - instance.getUniverse().displace(removedBullet.getPlaceable()); + Bullet removedBullet = instance.destroyBullet(playerId, bulletId); + if (removedBullet != null) { return JSON.encode(removedBullet); } throw new WebApplicationException(400);