diff --git a/src/main/java/cactusServer/entities/Bullet.java b/src/main/java/cactusServer/entities/Bullet.java new file mode 100644 index 0000000..c81b447 --- /dev/null +++ b/src/main/java/cactusServer/entities/Bullet.java @@ -0,0 +1,49 @@ +package cactusServer.entities; + +import org.ntlab.radishforandroidstudio.framework.model3D.Position3D; +import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D; + +public class Bullet extends Entity3D { + private String playerID; + private Position3D position; + private Quaternion3D angle; + + private Bullet() { + // JSON�G���R�[�h���̌Ăяo���p + } + + public Bullet(String playerID, Position3D position, Quaternion3D angle) { + this.playerID = playerID; + this.position = position; + this.angle = angle; + } + + public String getPlayerID() { + return playerID; + } + + public Position3D getPosition() { + return position; + } + + public Quaternion3D getAngle() { + return angle; + } + + public void setPlayerID(String playerID) { + this.playerID = playerID; + } + + public void setPosition(Position3D positon) { + this.position = positon; + } + + public void setAngle(Quaternion3D angle) { + this.angle = angle; + } + + public void update(Position3D position, Quaternion3D angle) { + setPosition(position); + setAngle(angle); + } +} diff --git a/src/main/java/cactusServer/entities/Instance.java b/src/main/java/cactusServer/entities/Instance.java index 5874cce..8d850bb 100644 --- a/src/main/java/cactusServer/entities/Instance.java +++ b/src/main/java/cactusServer/entities/Instance.java @@ -36,6 +36,7 @@ private HashMap areaMap = new HashMap<>(); private HashMap objMap = new HashMap<>(); private HashMap characterMap = new HashMap<>(); + private HashMap> bulletMap = new HashMap<>(); @JSONHint(ignore = true) public static final int UNIQUE_ID_LENGTH = 12; @@ -116,6 +117,10 @@ public Character getCharacter(String characterId) { return characterMap.get(characterId); } + + public HashMap> getBullets() { + return bulletMap; + } public void setName(String name) { this.name = name; @@ -161,6 +166,15 @@ returnedMap.put(id, character); return returnedMap; } + + public HashMapcreateBullet(String playerID, String bulletID, Position3D position, Quaternion3D angle) { + if (!bulletMap.containsKey(playerID)) { + bulletMap.put(playerID, new HashMap()); + } + HashMap map = bulletMap.get(playerID); + map.put(bulletID, new Bullet(playerID, position, angle)); + return map; + } public Instance update(Instance.State state) { setState(state); diff --git a/src/main/java/cactusServer/resources/BulletsRest.java b/src/main/java/cactusServer/resources/BulletsRest.java new file mode 100644 index 0000000..cf6f3e3 --- /dev/null +++ b/src/main/java/cactusServer/resources/BulletsRest.java @@ -0,0 +1,63 @@ +package cactusServer.resources; + +import java.util.HashMap; + +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; + +import org.ntlab.radishforandroidstudio.framework.model3D.Position3D; +import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D; + +import cactusServer.entities.Bullet; +import cactusServer.entities.Instance; +import cactusServer.models.Instances; +import net.arnx.jsonic.JSON; + +@Path("/instances/{instanceId}/bullets") +public class BulletsRest { + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String getBullets(@PathParam("instanceId") String instanceId) { + return JSON.encode(Instances.getInstance().getInstance(instanceId).getBullets()); + } + + @Path("/{playerId}") + @POST + @Produces(MediaType.TEXT_PLAIN) + public String createBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId, + @FormParam("bulletId") String bulletId, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle) { + Instance instance = Instances.getInstance().getInstance(instanceId); + HashMap idMap = instance.createBullet(playerId, bulletId, position, angle); + HashMap uriMap = new HashMap<>(); + for (String id : idMap.keySet()) { + String uri = (InstancesRest.INSTANCES_URI + "/" + instanceId + "/bullets/") + playerId + "/" + id; + uriMap.put(uri, idMap.get(id)); + } + return JSON.encode(uriMap); + } + + @Path("/{playerId}/{bulletId}") + @PUT + @Produces(MediaType.TEXT_PLAIN) + 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); + } + throw new WebApplicationException(400); + } +} diff --git a/src/main/java/cactusServer/utils/App.java b/src/main/java/cactusServer/utils/App.java index 2c2aa6c..0eec1bc 100644 --- a/src/main/java/cactusServer/utils/App.java +++ b/src/main/java/cactusServer/utils/App.java @@ -86,6 +86,10 @@ // �_�~�[�I�u�W�F�N�g Instances.getInstance().getInstance("test1").createObject(new Position3D(0, 0, 0), new Velocity3D(), new AngularVelocity3D(), new Quaternion3D(), new Attribute(true, 1), 0); + // �_�~�[�o���b�g + Instances.getInstance().getInstance("test1").createBullet("player1", "testBulletId1", new Position3D(), new Quaternion3D()); + Instances.getInstance().getInstance("test1").createBullet("player1", "testBulletId2", new Position3D(), new Quaternion3D()); + // // �m�F�p // String player1Encode = JSON.encode(player1, true); // String player2Encode = JSON.encode(player2, true);