您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

418 行
16 KiB

  1. package commands;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.fasterxml.jackson.databind.node.ObjectNode;
  4. import akka.actor.ActorRef;
  5. import play.libs.Json;
  6. import structures.basic.Card;
  7. import structures.basic.EffectAnimation;
  8. import structures.basic.Player;
  9. import structures.basic.Tile;
  10. import structures.basic.Unit;
  11. import structures.basic.UnitAnimation;
  12. import structures.basic.UnitAnimationType;
  13. /**
  14. * This is a utility class that simply provides short-cut methods for
  15. * running the basic command set for the game.
  16. *
  17. * @author Dr. Richard McCreadie
  18. *
  19. */
  20. public class BasicCommands {
  21. private static ObjectMapper mapper = new ObjectMapper(); // Jackson Java Object Serializer, is used to turn java objects to Strings
  22. // An alternative class with a 'tell' implementation can be given if writing unit tests
  23. // and need to have a null ActorRef. This should be null during normal operation.
  24. public static DummyTell altTell = null;
  25. /**
  26. * You can consider the contents of the user’s browser window a canvas that can be drawn upon. drawTile will draw
  27. * the image of a board tile on the board. This command takes as input a Tile object and a visualisation mode (an
  28. * integer) that specifies which version of the tile to render (each tile has multiple versions, e.g. normal vs.
  29. * highlighted). This command can be used multiple times to change the visualisation mode for a tile.
  30. * @param out
  31. * @param tile
  32. * @param mode
  33. */
  34. @SuppressWarnings({"deprecation"})
  35. public static void drawTile(ActorRef out, Tile tile, int mode) {
  36. try {
  37. ObjectNode returnMessage = Json.newObject();
  38. returnMessage.put("messagetype", "drawTile");
  39. returnMessage.put("tile", mapper.readTree(mapper.writeValueAsString(tile)));
  40. returnMessage.put("mode", mode);
  41. if (altTell!=null) altTell.tell(returnMessage);
  42. else out.tell(returnMessage, out);
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. /**
  48. * drawUnit will draw the sprite for a unit (a picture of that unit with its attack and health values) on the board.
  49. * This command takes as input a target Tile (a ‘square’ of the main game grid) to place the unit’s sprite upon,
  50. * and the instance of the Unit (which holds the needed information about how to draw that unit).
  51. * @param out
  52. * @param unit
  53. * @param tile
  54. */
  55. @SuppressWarnings({"deprecation"})
  56. public static void drawUnit(ActorRef out, Unit unit, Tile tile) {
  57. try {
  58. ObjectNode returnMessage = Json.newObject();
  59. returnMessage.put("messagetype", "drawUnit");
  60. returnMessage.put("tile", mapper.readTree(mapper.writeValueAsString(tile)));
  61. returnMessage.put("unit", mapper.readTree(mapper.writeValueAsString(unit)));
  62. if (altTell!=null) altTell.tell(returnMessage);
  63. else out.tell(returnMessage, out);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. /**
  69. * This command changes the visualised attack value just under a unit’s sprite to a value between 0
  70. * and 20. The command takes in a unit instance. The associated values are read from the unit object.
  71. * @param out
  72. * @param unit
  73. * @param attack
  74. */
  75. @SuppressWarnings({"deprecation"})
  76. public static void setUnitAttack(ActorRef out, Unit unit, int attack) {
  77. try {
  78. ObjectNode returnMessage = Json.newObject();
  79. returnMessage.put("messagetype", "setUnitAttack");
  80. returnMessage.put("unit", mapper.readTree(mapper.writeValueAsString(unit)));
  81. returnMessage.put("attack", attack);
  82. if (altTell!=null) altTell.tell(returnMessage);
  83. else out.tell(returnMessage, out);
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. /**
  89. * This command changes the visualised health value just under a unit’s sprite to a value between 0
  90. * and 20. The command takes in a unit instance. The associated values are read from the unit object.
  91. * @param out
  92. * @param unit
  93. * @param health
  94. */
  95. @SuppressWarnings({"deprecation"})
  96. public static void setUnitHealth(ActorRef out, Unit unit, int health) {
  97. try {
  98. ObjectNode returnMessage = Json.newObject();
  99. returnMessage.put("messagetype", "setUnitHealth");
  100. returnMessage.put("unit", mapper.readTree(mapper.writeValueAsString(unit)));
  101. returnMessage.put("health", health);
  102. if (altTell!=null) altTell.tell(returnMessage);
  103. else out.tell(returnMessage, out);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. /**
  109. * This command moves a unit sprite from one tile to another. It takes in the unit’s object and the target Tile.
  110. * Note that this command will start the movement, it may take multiple seconds for the movement to complete.
  111. * @param out
  112. * @param unit
  113. * @param tile
  114. */
  115. @SuppressWarnings({"deprecation"})
  116. public static void moveUnitToTile(ActorRef out, Unit unit, Tile tile) {
  117. try {
  118. ObjectNode returnMessage = Json.newObject();
  119. returnMessage.put("messagetype", "moveUnitToTile");
  120. returnMessage.put("unit", mapper.readTree(mapper.writeValueAsString(unit)));
  121. returnMessage.put("tile", mapper.readTree(mapper.writeValueAsString(tile)));
  122. if (altTell!=null) altTell.tell(returnMessage);
  123. else out.tell(returnMessage, out);
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. /**
  129. * This command moves a unit sprite from one tile to another. It takes in the unit’s object and the target Tile.
  130. * Note that this command will start the movement, it may take multiple seconds for the movement to complete.
  131. * yfirst sets whether the move should move the unit vertically first before moving horizontally
  132. * @param out
  133. * @param yfirst
  134. * @param unit
  135. * @param tile
  136. */
  137. @SuppressWarnings({"deprecation"})
  138. public static void moveUnitToTile(ActorRef out, Unit unit, Tile tile, boolean yfirst) {
  139. try {
  140. ObjectNode returnMessage = Json.newObject();
  141. returnMessage.put("messagetype", "moveUnitToTile");
  142. returnMessage.put("yfirst", yfirst);
  143. returnMessage.put("unit", mapper.readTree(mapper.writeValueAsString(unit)));
  144. returnMessage.put("tile", mapper.readTree(mapper.writeValueAsString(tile)));
  145. if (altTell!=null) altTell.tell(returnMessage);
  146. else out.tell(returnMessage, out);
  147. } catch (Exception e) {
  148. e.printStackTrace();
  149. }
  150. }
  151. /**
  152. * This command makes a unit play a specified animation. It takes in the unit object which
  153. * contains all of the data needed to play the animations, and a UnitAnimation that specifies
  154. * which animation to switch to.
  155. *
  156. * This method now returns an estimate for the number of milliseconds until the animation completes
  157. * playing in the browser. Ignore this if it is a looping animation.
  158. * @param out
  159. * @param unit
  160. * @param animation
  161. */
  162. @SuppressWarnings({"deprecation"})
  163. public static int playUnitAnimation(ActorRef out, Unit unit, UnitAnimationType animationToPlay) {
  164. try {
  165. unit.setAnimation(animationToPlay);
  166. ObjectNode returnMessage = Json.newObject();
  167. returnMessage.put("messagetype", "playUnitAnimation");
  168. returnMessage.put("unit", mapper.readTree(mapper.writeValueAsString(unit)));
  169. returnMessage.put("animation", animationToPlay.toString());
  170. if (altTell!=null) altTell.tell(returnMessage);
  171. else out.tell(returnMessage, out);
  172. // estimate the time needed for the animation to play
  173. UnitAnimation animation = null;
  174. if (animationToPlay.equals(UnitAnimationType.idle)) animation = unit.getAnimations().getIdle();
  175. if (animationToPlay.equals(UnitAnimationType.attack)) animation = unit.getAnimations().getAttack();
  176. if (animationToPlay.equals(UnitAnimationType.channel)) animation = unit.getAnimations().getChannel();
  177. if (animationToPlay.equals(UnitAnimationType.death)) animation = unit.getAnimations().getDeath();
  178. if (animationToPlay.equals(UnitAnimationType.hit)) animation = unit.getAnimations().getHit();
  179. if (animationToPlay.equals(UnitAnimationType.move)) animation = unit.getAnimations().getMove();
  180. if (animation==null) return 0;
  181. return ((1000*(animation.getFrameStartEndIndices()[1]-animation.getFrameStartEndIndices()[0]))/animation.getFps())+50;
  182. } catch (Exception e) {
  183. e.printStackTrace();
  184. return 0;
  185. }
  186. }
  187. /**
  188. * This will delete a unit instance from the board. It takes as input the unit object of the unit.
  189. * @param out
  190. * @param unit
  191. */
  192. @SuppressWarnings({"deprecation"})
  193. public static void deleteUnit(ActorRef out, Unit unit) {
  194. try {
  195. ObjectNode returnMessage = Json.newObject();
  196. returnMessage.put("messagetype", "deleteUnit");
  197. returnMessage.put("unit", mapper.readTree(mapper.writeValueAsString(unit)));
  198. if (altTell!=null) altTell.tell(returnMessage);
  199. else out.tell(returnMessage, out);
  200. } catch (Exception e) {
  201. e.printStackTrace();
  202. }
  203. }
  204. /**
  205. * This command changes the visualised health value in the player’s information card to a value between 0
  206. * and 20. The command takes in a basic player instance. The associated values are read from the basic player
  207. * object.
  208. * @param out
  209. * @param player
  210. */
  211. @SuppressWarnings({"deprecation"})
  212. public static void setPlayer1Health(ActorRef out, Player player) {
  213. try {
  214. ObjectNode returnMessage = Json.newObject();
  215. returnMessage.put("messagetype", "setPlayer1Health");
  216. returnMessage.put("player", mapper.readTree(mapper.writeValueAsString(player)));
  217. if (altTell!=null) altTell.tell(returnMessage);
  218. else out.tell(returnMessage, out);
  219. } catch (Exception e) {
  220. e.printStackTrace();
  221. }
  222. }
  223. /**
  224. * This command changes the visualised health value in the player’s information card to a value between 0
  225. * and 20. The command takes in a basic player instance. The associated values are read from the basic player
  226. * object.
  227. * @param out
  228. * @param player
  229. */
  230. @SuppressWarnings({"deprecation"})
  231. public static void setPlayer2Health(ActorRef out, Player player) {
  232. try {
  233. ObjectNode returnMessage = Json.newObject();
  234. returnMessage.put("messagetype", "setPlayer2Health");
  235. returnMessage.put("player", mapper.readTree(mapper.writeValueAsString(player)));
  236. if (altTell!=null) altTell.tell(returnMessage);
  237. else out.tell(returnMessage, out);
  238. } catch (Exception e) {
  239. e.printStackTrace();
  240. }
  241. }
  242. /**
  243. * This command changes the visualised mana value in the player’s information card to a value between 0
  244. * and 9. The command takes in a basic player instance. The associated values are read from the basic player
  245. * object.
  246. * @param out
  247. * @param player
  248. */
  249. @SuppressWarnings({"deprecation"})
  250. public static void setPlayer1Mana(ActorRef out, Player player) {
  251. try {
  252. ObjectNode returnMessage = Json.newObject();
  253. returnMessage.put("messagetype", "setPlayer1Mana");
  254. returnMessage.put("player", mapper.readTree(mapper.writeValueAsString(player)));
  255. if (altTell!=null) altTell.tell(returnMessage);
  256. else out.tell(returnMessage, out);
  257. } catch (Exception e) {
  258. e.printStackTrace();
  259. }
  260. }
  261. /**
  262. * This command changes the visualised mana value in the player’s information card to a value between 0
  263. * and 9. The command takes in a basic player instance. The associated values are read from the basic player
  264. * object.
  265. * @param out
  266. * @param player
  267. */
  268. @SuppressWarnings({"deprecation"})
  269. public static void setPlayer2Mana(ActorRef out, Player player) {
  270. try {
  271. ObjectNode returnMessage = Json.newObject();
  272. returnMessage.put("messagetype", "setPlayer2Mana");
  273. returnMessage.put("player", mapper.readTree(mapper.writeValueAsString(player)));
  274. if (altTell!=null) altTell.tell(returnMessage);
  275. else out.tell(returnMessage, out);
  276. } catch (Exception e) {
  277. e.printStackTrace();
  278. }
  279. }
  280. /**
  281. * This command renders a card in the player’s hand. It takes as input a hand position (a value between 1-6), a
  282. * Card (which is an object containing basic information needed to visualise that card) and a visualisation mode
  283. * (similarly to a tile). This command can be issued multiple times to change the visualisation mode of a card.
  284. * @param out
  285. * @param card
  286. * @param position
  287. * @param mode
  288. */
  289. @SuppressWarnings({"deprecation"})
  290. public static void drawCard(ActorRef out, Card card, int position, int mode) {
  291. try {
  292. ObjectNode returnMessage = Json.newObject();
  293. returnMessage.put("messagetype", "drawCard");
  294. returnMessage.put("card", mapper.readTree(mapper.writeValueAsString(card)));
  295. returnMessage.put("position", position);
  296. returnMessage.put("mode", mode);
  297. if (altTell!=null) altTell.tell(returnMessage);
  298. else out.tell(returnMessage, out);
  299. } catch (Exception e) {
  300. e.printStackTrace();
  301. }
  302. }
  303. /**
  304. * This command deletes a card in the player’s hand. It takes as input a hand position (a value between 1-6).
  305. * @param out
  306. * @param position
  307. */
  308. public static void deleteCard(ActorRef out, int position) {
  309. try {
  310. ObjectNode returnMessage = Json.newObject();
  311. returnMessage.put("messagetype", "deleteCard");
  312. returnMessage.put("position", position);
  313. if (altTell!=null) altTell.tell(returnMessage);
  314. else out.tell(returnMessage, out);
  315. } catch (Exception e) {
  316. e.printStackTrace();
  317. }
  318. }
  319. /**
  320. * Plays a specified EffectAnimation (such as an explosion) centred on a particular Tile. It takes as input an
  321. * EffectAnimation (an object with information about rendering the effect) and a target Tile.
  322. *
  323. * This method has been updated to provide an estimate of the time until the animation will finish playing
  324. * @param out
  325. * @param effect
  326. * @param tile
  327. */
  328. @SuppressWarnings({"deprecation"})
  329. public static int playEffectAnimation(ActorRef out, EffectAnimation effect, Tile tile) {
  330. try {
  331. ObjectNode returnMessage = Json.newObject();
  332. returnMessage.put("messagetype", "playEffectAnimation");
  333. returnMessage.put("effect", mapper.readTree(mapper.writeValueAsString(effect)));
  334. returnMessage.put("tile", mapper.readTree(mapper.writeValueAsString(tile)));
  335. if (altTell!=null) altTell.tell(returnMessage);
  336. else out.tell(returnMessage, out);
  337. return ((1000*effect.getAnimationTextures().size())/effect.getFps())+50;
  338. } catch (Exception e) {
  339. e.printStackTrace();
  340. return 0;
  341. }
  342. }
  343. /**
  344. * This command creates a notification box next to the portrait for the player 1 which contains
  345. * the specified text. It will be displayed for a number of seconds before being removed.
  346. * object.
  347. * @param out
  348. * @param text
  349. * @param displayTimeSeconds
  350. */
  351. public static void addPlayer1Notification(ActorRef out, String text, int displayTimeSeconds) {
  352. try {
  353. ObjectNode returnMessage = Json.newObject();
  354. returnMessage.put("messagetype", "addPlayer1Notification");
  355. returnMessage.put("text", text);
  356. returnMessage.put("seconds", displayTimeSeconds);
  357. if (altTell!=null) altTell.tell(returnMessage);
  358. else out.tell(returnMessage, out);
  359. } catch (Exception e) {
  360. e.printStackTrace();
  361. }
  362. }
  363. /**
  364. * Plays a projectile fire animation between two tiles
  365. * @param out
  366. * @param effect
  367. * @param tile
  368. */
  369. @SuppressWarnings({"deprecation"})
  370. public static void playProjectileAnimation(ActorRef out, EffectAnimation effect, int mode, Tile startTile, Tile targetTile) {
  371. try {
  372. ObjectNode returnMessage = Json.newObject();
  373. returnMessage.put("messagetype", "drawProjectile");
  374. returnMessage.put("effect", mapper.readTree(mapper.writeValueAsString(effect)));
  375. returnMessage.put("tile", mapper.readTree(mapper.writeValueAsString(startTile)));
  376. returnMessage.put("targetTile", mapper.readTree(mapper.writeValueAsString(targetTile)));
  377. returnMessage.put("mode", mapper.readTree(mapper.writeValueAsString(mode)));
  378. if (altTell!=null) altTell.tell(returnMessage);
  379. else out.tell(returnMessage, out);
  380. } catch (Exception e) {
  381. e.printStackTrace();
  382. }
  383. }
  384. }