Advertisement
nessessence

Untitled

Nov 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. /* *****************************************
  2. * CSCI205 - Software Engineering and Design
  3. * Spring 2016
  4. *
  5. * Name: Tongyu Yang, Peter Unrein, Hung Giang
  6. * Date: Apr 16, 2016
  7. * Time: 11:42:27 PM
  8. *
  9. * Project: csci205FinalProject
  10. * Package: GameMain
  11. * File: CollisionUtility
  12. * Description: Utility class for collision detection and handling
  13. *
  14. * ****************************************
  15. */
  16. package GameMain;
  17.  
  18. import SpriteClasses.Animation;
  19. import SpriteClasses.Base;
  20. import SpriteClasses.Block;
  21. import SpriteClasses.Bullet;
  22. import SpriteClasses.ExplodingTank;
  23. import SpriteClasses.Explosion;
  24. import SpriteClasses.Tank;
  25. import SpriteClasses.TankAI;
  26. import SpriteClasses.TankShield;
  27. import javafx.scene.shape.Rectangle;
  28.  
  29. import java.util.ArrayList;
  30.  
  31. /**
  32. * Utility class for collision detection and handling
  33. *
  34. * @author Adrian Berg
  35. */
  36. public class CollisionUtility {
  37. // Instance variable that indicated the x, y coordinates of the powerUp
  38. public static int powerUpX = 0;
  39. public static int powerUpY = 0;
  40. private static ArrayList<Block> blocks;
  41. private static ArrayList<Animation> explosions;
  42. // Instance variable that tracks the number of enemy tanks being destroyed
  43. private static int[] enemyTankNum = {0, 0, 0, 0};
  44.  
  45. /**
  46. * Load blocks and explosion animation from the input array list
  47. *
  48. * @param inblocks input blocks
  49. * @param inexplosion
  50. */
  51. static public void loadCollisionUtility(ArrayList<Block> inblocks,
  52. ArrayList<Animation> inexplosion) {
  53. blocks = inblocks;
  54. explosions = inexplosion;
  55. }
  56.  
  57. /**
  58. * Reset the score of the game after game over.
  59. */
  60. public static void resetScore() {
  61. enemyTankNum = new int[]{0, 0, 0, 0};
  62. }
  63.  
  64. /**
  65. * Helper method for collision between bullets and blocks
  66. *
  67. * @param bullet
  68. * @param block
  69. */
  70. public static void CollisionBulletsBlocksHelper(Bullet bullet, Block block) {
  71. BlockType type = BlockType.getTypeFromInt(block.getType());
  72. if (type.equals(BlockType.BRICK)) {
  73. block.lowerHealth();
  74. bullet.setVisible(false);
  75. }
  76. if (type.equals(BlockType.STEEL) && bullet.getUpgrade()) {
  77. block.lowerHealth();
  78. bullet.setVisible(false);
  79. }
  80. if (type.equals(BlockType.STEEL) && bullet.getUpgrade() == false) {
  81. bullet.setVisible(false);
  82. }
  83. if (type.equals(BlockType.BASE) && block.health != 0) {
  84. bullet.setVisible(false);
  85. Base b = (Base) block;
  86. b.gameOver = true;
  87. explosions.add(new ExplodingTank(block.x, block.y));
  88. SoundUtility.explosion2();
  89. Board.setEndGame();
  90. SoundUtility.gameOver();
  91.  
  92. }
  93. if (block.getHealth() == 0) {
  94. SoundUtility.explosion2();
  95. block.vis = false;
  96. explosions.add(new Explosion(block.x, block.y));
  97.  
  98. }
  99. if (block.getHealth() == 0) {
  100. block.vis = false;
  101.  
  102. }
  103. }
  104.  
  105. /**
  106. * Check collision between tank and blocks
  107. *
  108. * @param r3 Rectangle
  109. * @return a boolean represents if there is a collision
  110. */
  111. public static boolean checkCollisionTankBlocks(Rectangle r3) {
  112. for (Block block : blocks) {
  113. Rectangle r2 = block.getBounds();
  114. BlockType type = BlockType.getTypeFromInt(block.getType());
  115. if (type.equals(BlockType.TREE)) {
  116. } else if (r3.intersects(r2.getBoundsInLocal())) {
  117. return true;
  118. }
  119.  
  120. }
  121. return false;
  122. }
  123.  
  124. /**
  125. * Check collision between bullets and blocks
  126. *
  127. * @param bullets array list for bullets
  128. * @param blocks array list for blocks
  129. */
  130. public static void checkCollisionBulletsBlocks(ArrayList<Bullet> bullets,
  131. ArrayList<Block> blocks) {
  132.  
  133. for (int x = 0; x < bullets.size(); x++) {
  134. Bullet b = bullets.get(x);
  135. Rectangle r1 = b.getBounds();
  136.  
  137. for (int i = 0; i < blocks.size(); i++) {
  138. Block aBlock = blocks.get(i);
  139. Rectangle r2 = aBlock.getBounds();
  140.  
  141. if (r1.intersects(r2.getBoundsInLocal())) {
  142. SoundUtility.BulletHitBrick();
  143. CollisionBulletsBlocksHelper(b, aBlock);
  144. }
  145. }
  146. }
  147. }
  148.  
  149. /**
  150. * Check collision between bullets and the player tank
  151. *
  152. * @param bullets array list for bullets
  153. * @param tank
  154. */
  155. public static void checkCollisionBulletsTank(ArrayList<Bullet> bullets,
  156. Tank tank) {
  157. Rectangle r2 = tank.getBounds();
  158. for (int x = 0; x < bullets.size(); x++) {
  159. Bullet b = bullets.get(x);
  160. Rectangle r1 = b.getBounds();
  161. if (r1.intersects(r2.getBoundsInLocal()) && b.isEnemy == true) {
  162. b.vis = false;
  163. if (tank.shield == false) {
  164. SoundUtility.explosion1();
  165. explosions.add(new ExplodingTank(tank.x, tank.y));
  166. tank.downHealth();
  167. resetTankPosition(tank, 1);
  168. } else {
  169. SoundUtility.BulletHitTank();
  170. }
  171. }
  172. }
  173. }
  174.  
  175. /**
  176. * Check collision between bullets and enemy tanks
  177. *
  178. * @param bullets array list for bullets
  179. * @param TankAIs array list for Tank AIs
  180. */
  181. public static void checkCollisionBulletsTankAI(ArrayList<Bullet> bullets,
  182. ArrayList<TankAI> TankAIs) {
  183. for (int x = 0; x < bullets.size(); x++) {
  184. Bullet b = bullets.get(x);
  185. Rectangle r1 = b.getBounds();
  186.  
  187. for (int i = 0; i < TankAIs.size(); i++) {
  188. TankAI tankAI = TankAIs.get(i);
  189. Rectangle r2 = tankAI.getBounds();
  190.  
  191. if (r1.intersects(r2.getBoundsInLocal()) && b.isEnemy == false) {
  192. tankAI.decreaseHP();
  193. b.vis = false;
  194. SoundUtility.BulletHitTank();
  195. if (tankAI.getHealth() < 1) {
  196. incrementNum(tankAI);
  197. if (tankAI.hasPowerUp()) {
  198. powerUpX = tankAI.getX();
  199. powerUpY = tankAI.getY();
  200. }
  201. tankAI.vis = false;
  202. Board.decrementEnemies(1);
  203. explosions.add(new ExplodingTank(tankAI.x, tankAI.y));
  204. SoundUtility.explosion1();
  205. }
  206. }
  207. }
  208. }
  209. }
  210.  
  211. /**
  212. * Increment number of the tankAI being destroyed
  213. *
  214. * @param tankAI a given tankAI
  215. */
  216. public static void incrementNum(TankAI tankAI) {
  217. String type = tankAI.getType();
  218. switch (type) {
  219. case "basic":
  220. enemyTankNum[0] += 1;
  221. break;
  222. case "fast":
  223. enemyTankNum[1] += 1;
  224. break;
  225. case "power":
  226. enemyTankNum[2] += 1;
  227. break;
  228. case "armor":
  229. enemyTankNum[3] += 1;
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235.  
  236. /**
  237. * Get the array that stores the number of each enemy tank being destroyed
  238. *
  239. * @return enemyTankNum the array that stores the number of each enemy tank
  240. * being destroyed
  241. */
  242. public static int[] getEnemyTankNum() {
  243. return enemyTankNum;
  244. }
  245.  
  246. /**
  247. * Reset the position of the tank
  248. *
  249. * @param atank
  250. * @param type
  251. */
  252. public static void resetTankPosition(Tank atank, int type) {
  253. atank.x = 10 * 16;
  254. atank.y = (Map.level0.length - 3) * 16;
  255. atank.shield = true;
  256. explosions.add(new TankShield(atank, 2));
  257. if (type == 1) {
  258. atank.starLevel = 0;
  259. } else {
  260. atank.shield = false;
  261. }
  262.  
  263. }
  264.  
  265. /**
  266. * Check collision between the player and enemy tanks
  267. *
  268. * @param TankAIs array list for Tank AIs
  269. * @param atank the player tank
  270. */
  271. public static void checkCollisionTankTankAI(ArrayList<TankAI> TankAIs,
  272. Tank atank) {
  273. Rectangle r1 = atank.getBounds();
  274. for (int i = 0; i < TankAIs.size(); i++) {
  275. TankAI tankAI = TankAIs.get(i);
  276. Rectangle r2 = tankAI.getBounds();
  277. if (r1.intersects(r2.getBoundsInLocal())) {
  278. if (atank.shield == false) {
  279. explosions.add(new ExplodingTank(atank.x, atank.y));
  280. atank.downHealth();
  281. resetTankPosition(atank, 1);
  282. } else if (atank.shield == true) {
  283. incrementNum(tankAI);
  284. Board.decrementEnemies(1);
  285. tankAI.vis = false;
  286. explosions.add(new ExplodingTank(atank.x, atank.y));
  287. }
  288.  
  289. }
  290. }
  291. }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement