No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

BasicObjectBuilders.java 7.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package utils;
  2. import java.io.File;
  3. import java.util.List;
  4. import com.fasterxml.jackson.annotation.JsonIgnore;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import structures.basic.Card;
  7. import structures.basic.EffectAnimation;
  8. import structures.basic.Tile;
  9. import structures.basic.Unit;
  10. /**
  11. * This class contains methods for producing basic objects from configuration files
  12. *
  13. * @author Dr. Richard McCreadie
  14. *
  15. */
  16. public class BasicObjectBuilders {
  17. @JsonIgnore
  18. protected static ObjectMapper mapper = new ObjectMapper(); // Jackson Java Object Serializer, is used to read java objects from a file
  19. /**
  20. * This class produces a Card object (or anything that extends Card) given a configuration
  21. * file. Configuration files can be found in the conf/gameconfs directory. The card should
  22. * be given a unique id number. The classtype field specifies the type of Card to be
  23. * constructed, e.g. Card.class will create a default card object, but if you had a class
  24. * extending card, e.g. MyAwesomeCard that extends Card, you could also specify
  25. * MyAwesomeCard.class here. If using an extending class you will need to manually set any
  26. * new data fields.
  27. * @param configurationFile
  28. * @param id
  29. * @param classtype
  30. * @return
  31. */
  32. public static Card loadCard(String configurationFile, int id, Class<? extends Card> classtype) {
  33. try {
  34. Card card = mapper.readValue(new File(configurationFile), classtype);
  35. // If the card is a creature, add its idle animation as the card animation
  36. if (card.isCreature()) {
  37. Unit unit = loadUnit(card.getUnitConfig(), -1, Unit.class);
  38. List<String> idleAnimation = unit.getAnimations().getAllFrames().subList(unit.getAnimations().getIdle().getFrameStartEndIndices()[0], unit.getAnimations().getIdle().getFrameStartEndIndices()[1]);
  39. card.getMiniCard().setAnimationFrames(idleAnimation.toArray(new String[idleAnimation.size()]));
  40. }
  41. card.setId(id);
  42. return card;
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. return null;
  47. }
  48. /**
  49. * This class produces a EffectAnimation object given a configuration
  50. * file. Configuration files can be found in the conf/gameconfs directory.
  51. * @param configurationFile
  52. * @return
  53. */
  54. public static EffectAnimation loadEffect(String configurationFile) {
  55. try {
  56. EffectAnimation effect = mapper.readValue(new File(configurationFile), EffectAnimation.class);
  57. return effect;
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. return null;
  62. }
  63. /**
  64. * Loads a unit from a configuration file. Configuration files can be found
  65. * in the conf/gameconfs directory. The unit needs to be given a unique identifier
  66. * (id). This method requires a classtype argument that specifies what type of
  67. * unit to create.
  68. * @param configFile
  69. * @return
  70. */
  71. public static Unit loadUnit(String configFile, int id, Class<? extends Unit> classType) {
  72. try {
  73. Unit unit = mapper.readValue(new File(configFile), classType);
  74. // identify start and end frames automatically based on file names
  75. // IDLE
  76. {
  77. int startframe = 0; int endframe = 0; int index = 0; boolean inAnimation = false;
  78. for (String framename: unit.getAnimations().getAllFrames()) {
  79. if (framename.contains("_idle_")) {
  80. if (startframe==0) { startframe=index; inAnimation=true;}
  81. } else {
  82. if (inAnimation) { endframe=index-1; break;}
  83. }
  84. index++;
  85. }
  86. if (endframe==0) endframe=index;
  87. int[] frameIndexes = {startframe, endframe};
  88. if (inAnimation) unit.getAnimations().getIdle().setFrameStartEndIndices(frameIndexes);
  89. }
  90. // DEATH
  91. {
  92. int startframe = 0; int endframe = 0; int index = 0; boolean inAnimation = false;
  93. for (String framename: unit.getAnimations().getAllFrames()) {
  94. if (framename.contains("_death_")) {
  95. if (startframe==0) { startframe=index; inAnimation=true;}
  96. } else {
  97. if (inAnimation) { endframe=index-1; break;}
  98. }
  99. index++;
  100. }
  101. if (endframe==0) endframe=index;
  102. int[] frameIndexes = {startframe, endframe};
  103. if (inAnimation) unit.getAnimations().getDeath().setFrameStartEndIndices(frameIndexes);
  104. }
  105. // ATTACK
  106. {
  107. int startframe = 0; int endframe = 0; int index = 0; boolean inAnimation = false;
  108. for (String framename: unit.getAnimations().getAllFrames()) {
  109. if (framename.contains("_attack_")) {
  110. if (startframe==0) { startframe=index; inAnimation=true;}
  111. } else {
  112. if (inAnimation) { endframe=index-1; break;}
  113. }
  114. index++;
  115. }
  116. if (endframe==0) endframe=index;
  117. int[] frameIndexes = {startframe, endframe};
  118. if (inAnimation) unit.getAnimations().getAttack().setFrameStartEndIndices(frameIndexes);
  119. }
  120. // MOVE
  121. {
  122. int startframe = 0; int endframe = 0; int index = 0; boolean inAnimation = false;
  123. for (String framename: unit.getAnimations().getAllFrames()) {
  124. if (framename.contains("_run_")) {
  125. if (startframe==0) { startframe=index; inAnimation=true;}
  126. } else {
  127. if (inAnimation) { endframe=index-1; break;}
  128. }
  129. index++;
  130. }
  131. if (endframe==0) endframe=index;
  132. int[] frameIndexes = {startframe, endframe};
  133. if (inAnimation) unit.getAnimations().getMove().setFrameStartEndIndices(frameIndexes);
  134. }
  135. // CHANNEL
  136. {
  137. int startframe = 0; int endframe = 0; int index = 0; boolean inAnimation = false;
  138. for (String framename: unit.getAnimations().getAllFrames()) {
  139. if (framename.contains("_castloop_")) {
  140. if (startframe==0) { startframe=index; inAnimation=true;}
  141. } else {
  142. if (inAnimation) { endframe=index-1; break;}
  143. }
  144. index++;
  145. }
  146. if (endframe==0) endframe=index;
  147. int[] frameIndexes = {startframe, endframe};
  148. if (inAnimation) unit.getAnimations().getChannel().setFrameStartEndIndices(frameIndexes);
  149. }
  150. // HIT
  151. {
  152. int startframe = 0; int endframe = 0; int index = 0; boolean inAnimation = false;
  153. for (String framename: unit.getAnimations().getAllFrames()) {
  154. if (framename.contains("_hit_")) {
  155. if (startframe==0) { startframe=index; inAnimation=true;}
  156. } else {
  157. if (inAnimation) { endframe=index-1; break;}
  158. }
  159. index++;
  160. }
  161. if (endframe==0) endframe=index;
  162. int[] frameIndexes = {startframe, endframe};
  163. if (inAnimation) unit.getAnimations().getChannel().setFrameStartEndIndices(frameIndexes);
  164. }
  165. // add full address to animation frames
  166. for (int i =0; i<unit.getAnimations().getAllFrames().size(); i++) {
  167. unit.getAnimations().getAllFrames().set(i, unit.getAnimations().getFrameDIR()+unit.getAnimations().getAllFrames().get(i));
  168. }
  169. unit.setId(id);
  170. return unit;
  171. } catch (Exception e) {
  172. e.printStackTrace();
  173. }
  174. return null;
  175. }
  176. /**
  177. * Generates a tile object with x and y indices
  178. * @param x
  179. * @param y
  180. * @return
  181. */
  182. public static Tile loadTile(int x, int y) {
  183. int gridmargin = 5;
  184. int gridTopLeftx = 410;
  185. int gridTopLefty = 280;
  186. Tile tile = Tile.constructTile(StaticConfFiles.tileConf);
  187. tile.setXpos((tile.getWidth()*x)+(gridmargin*x)+gridTopLeftx);
  188. tile.setYpos((tile.getHeight()*y)+(gridmargin*y)+gridTopLefty);
  189. tile.setTilex(x);
  190. tile.setTiley(y);
  191. return tile;
  192. }
  193. }