Newer
Older
CactusServer / src / main / java / cactusServer / models / Instances.java
  1. package cactusServer.models;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
  6. import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D;
  7.  
  8. import cactusServer.entities.CameraState;
  9. import cactusServer.entities.EmoteState;
  10. import cactusServer.entities.Instance;
  11. import cactusServer.entities.Player;
  12. import cactusServer.utils.RandomStringGenerator;
  13.  
  14. /**
  15. * インスタンスを管理するクラス
  16. *
  17. * @author r-isitani
  18. *
  19. */
  20. public class Instances {
  21. private static Instances theInstance = null;
  22. private HashMap<String, Instance> instanceMap = new HashMap<>(); // instanceのIDと実体を管理
  23. private HashMap<String, Player> playerMap = new HashMap<>();
  24.  
  25. private Instances() {
  26. // // ダミーコード
  27. // instanceMap.put("test1", new Instance("test1", 0));
  28. // instanceMap.put("test2", new Instance("test2", 1));
  29. }
  30.  
  31. /**
  32. * Instancesクラスを取得する (シングルトン)
  33. *
  34. * @return
  35. */
  36. public static Instances getInstance() {
  37. if (theInstance == null) {
  38. theInstance = new Instances();
  39. }
  40. return theInstance;
  41. }
  42.  
  43. public HashMap<String, Instance> createInstance(String name, int stageID) {
  44. String id = RandomStringGenerator.generateUniqueString(Instance.UNIQUE_ID_LENGTH,
  45. RandomStringGenerator.ALPHA_NUMERIC, instanceMap.keySet());
  46. Instance instance = new Instance(name, stageID);
  47. instanceMap.put(id, instance);
  48. HashMap<String, Instance> returnedMap = new HashMap<>();
  49. returnedMap.put(id, instance);
  50. return returnedMap;
  51. }
  52.  
  53. public HashMap<String, Player> createPlayer(String instanceID, String characterID, CameraState cameraState,
  54. EmoteState.EmoteType animationClassToStart) {
  55. String id = RandomStringGenerator.generateUniqueString(Player.UNIQUE_ID_LENGTH,
  56. RandomStringGenerator.ALPHA_NUMERIC, playerMap.keySet());
  57. Player player = new Player(instanceID, characterID, cameraState, animationClassToStart);
  58. playerMap.put(id, player);
  59. instanceMap.get(instanceID).getUniverse().place(player.getPlaceable());
  60. HashMap<String, Player> returnedMap = new HashMap<>();
  61. returnedMap.put(id, player);
  62. return returnedMap;
  63. }
  64.  
  65. public HashMap<String, Instance> getInstances() {
  66. return instanceMap;
  67. }
  68.  
  69. public Instance getInstance(String instanceId) {
  70. return instanceMap.get(instanceId);
  71. }
  72.  
  73. /**
  74. * 全playerを返す
  75. */
  76. public HashMap<String, Player> getPlayers() {
  77. return playerMap;
  78. }
  79.  
  80. /**
  81. * IDに対応するinstanceにいる全playerを返す
  82. *
  83. * @param instanceId
  84. */
  85. public HashMap<String, Player> getPlayers(String instanceId) {
  86. if (instanceId == null || instanceId.isEmpty()) {
  87. return getPlayers();
  88. }
  89. HashMap<String, Player> responsePlayers = new HashMap<>();
  90. for (String id : playerMap.keySet()) {
  91. Player player = playerMap.get(id);
  92. if (instanceId.equals(player.getInstanceID())) {
  93. responsePlayers.put(id, player);
  94. }
  95. }
  96. return responsePlayers;
  97. }
  98.  
  99. public Player getPlayer(String playerId) {
  100. return playerMap.get(playerId);
  101. }
  102.  
  103. public Instance updateInstance(String instanceId, Instance.State state) {
  104. Instance instance = instanceMap.get(instanceId);
  105. instance.update(state);
  106. return instance;
  107. }
  108.  
  109. public Player updatePlayer(String playerId, String characterID, Position3D position, Quaternion3D angle,
  110. CameraState cameraState, EmoteState.EmoteType animationClassToStart) {
  111. long lastUpdateTime = System.nanoTime();
  112. Player player = playerMap.get(playerId);
  113. if (player != null) {
  114. String previousCharacterID = player.getCharacterID();
  115. player.update(characterID, position, angle, cameraState, animationClassToStart, lastUpdateTime);
  116. if (!(previousCharacterID.equals(player.getCharacterID()))) {
  117. String instanceID = player.getInstanceID();
  118. instanceMap.get(instanceID).getUniverse().place(player.getPlaceable());
  119. }
  120. }
  121. return player;
  122. }
  123.  
  124. public Instance destroyInstance(String instanceId) {
  125. return instanceMap.remove(instanceId);
  126. }
  127.  
  128. public Player destroyPlayer(String playerId) {
  129. Player player = playerMap.get(playerId);
  130. if (player == null) {
  131. return null;
  132. }
  133. player.destroy();
  134. return playerMap.remove(playerId);
  135. }
  136. }