Advertisement
RedVinck

World with most of comments beter

May 23rd, 2019
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.96 KB | None | 0 0
  1. package jumpingalien.model;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.util.ArrayList;
  6. import java.util.Currency;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.Set;
  10. import java.util.Timer;
  11.  
  12. import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
  13.  
  14. import org.junit.FixMethodOrder;
  15.  
  16. //import com.sun.javafx.scene.traversal.WeightedClosestCorner;
  17.  
  18. import be.kuleuven.cs.som.annotate.Raw;
  19. import jdk.nashorn.internal.runtime.ECMAException;
  20. import jumpingalien.facade.Facade;
  21. import jumpingalien.model.Mazub;
  22. import jumpingalien.util.ModelException;
  23. import sun.net.www.content.audio.x_aiff;
  24.  
  25.  
  26. /**
  27. * A class for creating the game world, get the images and coordinates, and make the world change depending on the pixelposition of Mazub
  28. * and the advance time.
  29. * The world has passable and impassable terains.
  30. *
  31. * @version 2.0
  32. * @author Thierry Klougbo & Jordi De Pau
  33. */
  34. public class World {
  35. // Precision constants for checks involving floating point numbers.
  36. public final static double HIGH_PRECISION = 0.1E-10;
  37. public final static double LOW_PRECISION = 0.01;
  38.  
  39.  
  40. public int VisibleWindowX, VisibleWindowY, TileLength ,nmbTilesX, nmbTilesY;
  41. /**
  42. *
  43. * @param pixelSize
  44. * The actual size of the pixels.
  45. * @param nbTilesX
  46. * The number of tiles in the visible window of the world's x-axis.
  47. * @param nbTilesY
  48. * The number of tiles in the visible window of the world's y-axis.
  49. * @param targetTileCoordinate
  50. * @param visibleWindowWidth
  51. * The actual width of the visible window.
  52. * @param visibleWindowHeight
  53. * The actual height of the visible window.
  54. * @param geologicalFeatures
  55. * An integer for representing every geological feature:
  56. * AIR = 0;
  57. * SOLID_GRO;UND = 1;
  58. * WATER = 2;
  59. * MAGMA = 3;
  60. * GAS = 4;.
  61. * ICE = 5;
  62. */
  63. public World(int pixelSize, int nbTilesX, int nbTilesY, int[] targetTileCoordinate, int visibleWindowWidth, int visibleWindowHeight, int...geologicalFeatures) throws Exception{
  64. setTileLength(pixelSize);
  65. setSizeInPixels(nbTilesX*pixelSize,nbTilesY*pixelSize);
  66. setVisibleWindowDimension(visibleWindowWidth,visibleWindowHeight);
  67. setTargetTileCoordinate(targetTileCoordinate);
  68. setGeologicalFeaturesSize(nbTilesX, nbTilesY);
  69.  
  70. createGeologicalFeatures(nbTilesX, nbTilesY, geologicalFeatures);}
  71.  
  72. /**
  73. * Sets the Tile Length to the given pixelsize.
  74. *
  75. * @param pixelSize
  76. * given size of the length of a Tile
  77. * @effect if the given pixelsize is larger then zero the tile length will be set to the given pixelsize
  78. * | if(pixelSize >0)
  79. * | then TileLength = pixelSize
  80. * @effect else the given pixelsize is lower or equal then/to zero the tile length will be set to 1
  81. * |TileLenght = 1
  82. *
  83. */
  84. private void setTileLength(int pixelSize) {
  85. if(pixelSize >0) {TileLength=pixelSize;}
  86. else {TileLength=1;}
  87.  
  88. }
  89. /*
  90. *******************************
  91. * *Tiles Total programming. *
  92. * *****************************
  93. */
  94.  
  95. /**
  96. * SizeInPixels= Pixel size of the world with an array of 2.
  97. */
  98. public int [] SizeInPixels= new int[2] ;
  99.  
  100. /**
  101. * Returns the size of the game world in pixels.
  102. *
  103. * @post SizeInPixels contains respectively the visible window length and the
  104. * visible window height.
  105. * | SizeInPixels[0]=VisibleWindowX; SizeInPixels[1]=VisibleWindowY;
  106. * @return SizeInPixels
  107. */
  108. public int[] getSizeInPixels() {return SizeInPixels;}
  109.  
  110. /**
  111. * sets the given width and Height inside an integer array of index 2 with name SizeInPixels
  112. *
  113. * @param Width
  114. * given width of the given world in integers.
  115. * @param Height
  116. * given height of the given world in integers.
  117. * @effect
  118. * If the width is lower then zero the will be zero
  119. * | if(Width<0)
  120. * | then SizeInPixels[0]=0
  121. * @effect
  122. * If the width is equal or higher then zero this SizeinPixels horizontal will be set to the given width
  123. * |SizeInPixels[0]=Width
  124. * @effect
  125. * If the Height is lower then zero the will be zero
  126. * | if(Width<0)
  127. * | then SizeInPixels[1]=0
  128. * @effect
  129. * If the Height is equal or higher then zero this SizeinPixels horizontal will be set to the given width
  130. * |SizeInPixels[1]=Height
  131. *
  132. */
  133. public void setSizeInPixels(int Width, int Height) {
  134. if(Width<0) {SizeInPixels[0]=0;}
  135. else {SizeInPixels[0] =Width;}
  136. if(Height<0){SizeInPixels[1] = 0;}
  137. else{SizeInPixels[1] =Height;}}
  138.  
  139. /**
  140. * @return the length of a tile
  141. * |return TileLength
  142. */
  143. public int getTileLength(){return TileLength;}
  144. /*
  145. * ***********************
  146. * Geological features *
  147. * ***********************/
  148.  
  149. /**
  150. * Is an Integer array of the world's geologicalFeatures in Tile's
  151. */
  152. public int[][] GeologicalFeaturesCanvas;
  153.  
  154. /**
  155. * Sets the GeologicalCanvas with coloms and rows
  156. *
  157. * @param GeoRow
  158. * Is the value of how many rows there has to be in GeologicalFeatures
  159. * @param GeoCol
  160. * Is the value of how many Coloms there has to be in GeologicalFeatuers
  161. * @effect
  162. * sets GeologicalFeaturesCanvas with the rows and coloms.
  163. * |this.GeologicalFeaturesCanvas = new int[GeoRow][GeoCol]
  164. */
  165. public void setGeologicalFeaturesSize(int GeoRow,int GeoCol) {this.GeologicalFeaturesCanvas = new int[GeoRow][GeoCol];}
  166.  
  167. /**
  168. * sets the geologicalfeatures of the world by putting every position with a feature
  169. *
  170. * @param X
  171. * a given X value that is a pixel & Integer.
  172. * @param Y
  173. * a given Y value that is a pixel & Integer
  174. * @param geoFeature
  175. * a geological Feature that is Integer
  176. * @effect
  177. * if the given x (that is calculatet to a tile) or y(that is calculatet to a tile) is outside the border the feature should be 0
  178. * |if(X > ((SizeInPixels[0]/this.getTileLength())-1) || Y > ((SizeInPixels[0]/this.getTileLength())-1)
  179. * |then this.GeologicalFeaturesCanvas[GeoX][GeoY] =0
  180. *@effect
  181. * else the given x (that is calculatet to a tile) and y(that is calculatet to a tile) is set with the given feature in geologicalCanvas
  182. * |this.GeologicalFeaturesCanvas[GeoX][GeoY] = geoFeature
  183. */
  184. public void setGeologicalFeatures(int X, int Y, int geoFeature) {
  185. int GeoX = calculateInFeature(X);
  186. int GeoY = calculateInFeature(Y);
  187. if (X > ((SizeInPixels[0]/this.getTileLength())-1) || Y > ((SizeInPixels[0]/this.getTileLength())-1) || X<0 || Y<0) {this.GeologicalFeaturesCanvas[GeoX][GeoY] =0;}
  188. this.GeologicalFeaturesCanvas[GeoX][GeoY] = geoFeature;
  189. }
  190.  
  191. /**
  192. * returns the geological feature with the given x and y calculated in a tile.
  193. *
  194. * @param pixelX
  195. * is the value of the pixel x-value
  196. * @param pixelY
  197. * is the value of the pixel y-value
  198. * @return
  199. * if the x value or the y value is larger then the world itself it returns 0
  200. * |return 0
  201. * @return
  202. * returns feature of the position calculated in tile. (T
  203. * |GeologicalFeaturesCanvas[GeoX][GeoY]
  204. */
  205. public int getGeologicalFeatures(int pixelX, int pixelY) {
  206. if (pixelX >= this.getSizeInPixels()[0] || pixelY >= this.getSizeInPixels()[1]) {return 0;}
  207. int GeoX = calculateInFeature(pixelX);
  208. int GeoY = calculateInFeature(pixelY);
  209.  
  210. return GeologicalFeaturesCanvas[GeoX][GeoY];
  211. }
  212.  
  213.  
  214. /**
  215. * @param X
  216. * is the given value of the pixel of x-value
  217. * @param Y
  218. * is the given value of the pixel of y-value
  219. * @param F
  220. * is the given feature
  221. *@effect
  222. * sets the positions feature
  223. * |this.GeologicalFeaturesCanvas[X][Y] = F
  224. */
  225. public void setFeature(int X, int Y, int F) {
  226. this.GeologicalFeaturesCanvas[X][Y] = F;
  227. }
  228.  
  229.  
  230. /**
  231. * calculates the pixel position into the position of the tile.
  232. *
  233. * @param aPixel
  234. * is the given value of a pixel
  235. * @effect
  236. * sets the variable resultpixel in the pixel divide by the tile length
  237. * |int resultPixel = (int)((aPixel)/getTileLength()
  238. * @return
  239. * the result of the pixel
  240. */
  241. public int calculateInFeature(int aPixel) {
  242. int resultPixel = (int)((aPixel)/getTileLength());
  243. return resultPixel;}
  244.  
  245.  
  246. /**
  247. * sets all the geological in canvas with the given amount tiles in x and the given amount tiles in y with the given feature in an array.
  248. *
  249. * @param nbTilesX
  250. * is the given amount of tiles for x value.
  251. * @param nbTilesY
  252. * is the given amount of tiles for y-value
  253. * @param geologicalFeatures
  254. * is an array of feature
  255. *@effect
  256. * if the geologicalfeature length of the array is bigger then zero sets all the features are set in a nested loop for each position
  257. * |if(geologicalFeatures.length>0)
  258. * |then for (int y= 0; y < nbTilesY; y++) {
  259. * | for (int x = 0; x< nbTilesX; x++) {
  260. * int counter =y*nbTilesX + x;
  261. * setFeature(x, y,geologicalFeatures[counter]);}}
  262. *@effect
  263. * if the geologicalfeature length of the array is zero sets all the features to zero in a nested loop for each position
  264. * |if(geologicalFeatures.length==0)
  265. * |then for (int y= 0; y < nbTilesY; y++) {
  266. * | for (int x = 0; x< nbTilesX; x++) {
  267. * int counter =y*nbTilesX + x;
  268. * setFeature(x, y,geologicalFeatures[0]);}}
  269. *
  270. */
  271. public void createGeologicalFeatures(int nbTilesX, int nbTilesY,int... geologicalFeatures) {
  272. if(geologicalFeatures.length>0) {
  273. for (int y= 0; y < nbTilesY; y++) {
  274. for (int x = 0; x< nbTilesX; x++) {
  275. int counter =y*nbTilesX + x;
  276. setFeature(x, y,geologicalFeatures[counter]);}}}
  277.  
  278. else {for (int y= 0; y < nbTilesY; y++) {
  279. for (int x = 0; x< nbTilesX; x++) {
  280. setFeature(x, y,0);}}}
  281. }
  282.  
  283. /*
  284. **********************************
  285. *Visible Window Defensive program*
  286. **********************************
  287. */
  288. //FIXME To do 400 pixel groter dan mazub
  289. //FIXME Make adaptor in advanced time
  290.  
  291. /**
  292. * VisibleWindowDimension is the dimension that is visible for your screen
  293. */
  294. public int[] VisibleWindowDimension=new int[2];
  295.  
  296. /**
  297. * sets the visible window with the given weight and height
  298. * @param weight
  299. * is the given weight of the window position
  300. * @param height
  301. * is the given height of the window position
  302. * @throws Exception
  303. * if the weight or the height is bigger then the dimension of the world
  304. * |if(weight>getSizeInPixels()[0]||height>getSizeInPixels()[1])
  305. * |then throw new Exception("To big window compared to actualworld");
  306. * @throws Exception
  307. * if the weight or the height is lower then the universe (= the universe is everything above 0 for both x & y)
  308. * |if(weight <0 && height <0)
  309. * |then throw new Exception("Illegal Window");
  310. * @effect
  311. * sets the given visible dimensioen with the weight and the height
  312. * |if(weight >0 && height >0)
  313. * |then weight;VisibleWindowDimension[1] = height;
  314. */
  315. public void setVisibleWindowDimension(int weight , int height) throws Exception{
  316. if(weight>getSizeInPixels()[0]||height>getSizeInPixels()[1]) {throw new Exception("To big window compared to actualworld");}
  317. else if(weight >0 && height >0) {
  318. VisibleWindowDimension[0] = weight;VisibleWindowDimension[1] = height;}
  319. else {throw new Exception("Illegal Window");}}
  320. /**
  321. * gets visible window dimensions.
  322. * @return
  323. * |result=VisibleWindowDimension
  324. */
  325. public int[] getVisibleWindowDimension() {
  326. return VisibleWindowDimension;
  327. }
  328.  
  329.  
  330. /**
  331. * gets the visible window dimension
  332. *
  333. * @return
  334. * if there isn't a mazub return a dimension with values of zero's
  335. *
  336. * @return
  337. * if mazub pixel position is smaller then the world size of x sets x window position to the visibleposition adaptor
  338. * | if(mazub.getPixelPosition()[0]<getSizeInPixels()[0]-mazub.getCurrentSprite().getWidth()&& visibleMazubCanMove(0))
  339. * | then VisibleWindowPosition[0]=(int)(VisiblePositionAdaptor);
  340. * @return
  341. * if mazub pixel position is smaller then the world size of y sets y window position to the visibleposition adaptor
  342. * | if(mazub.getPixelPosition()[1]<getSizeInPixels()[1]-mazub.getCurrentSprite().getWidth()&& visibleMazubCanMove(1))
  343. * | then VisibleWindowPosition[1]=(int)(VisiblePositionAdaptor);
  344. * @return
  345. * if the size is bigger then the world size x sets the windows position to max possible
  346. * |if(VisibleWindowPosition[0] >SizeInPixels[0]-getVisibleWindowDimension()[0])
  347. * |then VisibleWindowPosition[0] =SizeInPixels[0]-getVisibleWindowDimension()[0];
  348. * @return
  349. * if the size is bigger then the world size y sets the windows position to max possible
  350. * |if(VisibleWindowPosition[1] >SizeInPixels[1]-getVisibleWindowDimension()[1])
  351. * |then VisibleWindowPosition[1] =SizeInPixels[1]-getVisibleWindowDimension()[1];
  352. *
  353. */
  354. public int[] getVisibleWindowsPosition() {
  355. if(mazub == null) {VisibleWindowPosition[0] =0; VisibleWindowPosition[1]=0; return VisibleWindowPosition;}
  356. if(mazub.getPixelPosition()[0]<getSizeInPixels()[0]-mazub.getCurrentSprite().getWidth()&& visibleMazubCanMove(0)){
  357. VisibleWindowPosition[0]=(int)(VisiblePositionAdaptor);}
  358. if (mazub.getPixelPosition()[1]<getSizeInPixels()[1] &&visibleMazubCanMove(1)) {
  359. VisibleWindowPosition[1]=(int)(VisiblePositionAdaptor);}
  360. if(VisibleWindowPosition[0] >SizeInPixels[0]-getVisibleWindowDimension()[0]) {VisibleWindowPosition[0] =SizeInPixels[0]-getVisibleWindowDimension()[0];}
  361. if(VisibleWindowPosition[1] >SizeInPixels[1]-getVisibleWindowDimension()[1]) {VisibleWindowPosition[1] =SizeInPixels[1]-getVisibleWindowDimension()[1];}
  362. return VisibleWindowPosition;
  363. }
  364.  
  365. /**
  366. * Is the new windows position that sometimes is the windows position or -200 of the windows position that mazub is not near the wall while moving
  367. */
  368. public int VisiblePositionAdaptor;
  369.  
  370.  
  371. /**
  372. * is the position of the visible window.
  373. */
  374. public int[]VisibleWindowPosition=new int[2];
  375.  
  376.  
  377. /**
  378. * returns if mazub its visibleposition can move but also adapts visiblepositionadaptor
  379. *
  380. * @param direction
  381. * is 0 or 1 that equals x or y
  382. * @return
  383. * if the mazub is not near 200 pixels near the world borders visiblePositionAdaptor will be the position minus 200 and returns true
  384. * |if(newPos > 200 && newPos < SizeInPixels[direction]-200 )
  385. * |then VisiblePositionAdaptor = newPos -200; return true;
  386. * @return
  387. * if the mazub is 200 or less pixels near the wall and returns false
  388. * |if(newPos <= 200 && newPos >= SizeInPixels[direction]-200 )
  389. * |then VisiblePositionAdaptor = 0; return false;
  390. */
  391. public boolean visibleMazubCanMove(int direction) {
  392. int newPos = mazub.getPixelPosition()[direction];
  393. if(newPos > 200 && newPos < SizeInPixels[direction]-200 ) {VisiblePositionAdaptor = newPos -200; return true;}
  394. else {VisiblePositionAdaptor = 0; return false;}
  395. }
  396.  
  397.  
  398. /**
  399. *sets the visibleadaptor with the given integer visiblePositionAdaptor
  400. *
  401. * @param visiblePositionAdaptor
  402. *@effect
  403. * sets the visibleadaptor with the given integer visiblePositionAdaptor
  404. * |VisiblePositionAdaptor = visiblePositionAdaptor;
  405. */
  406. public void setVisiblePositionAdaptor(int visiblePositionAdaptor) {
  407. VisiblePositionAdaptor = visiblePositionAdaptor;
  408. }
  409.  
  410. /*
  411. ************************************
  412. | Target tiles Nominal programming |
  413. ************************************
  414. */
  415.  
  416. /**
  417. * sets the targetTileCoordinates
  418. */
  419. public int[] targetTileCoordinates = new int[2];
  420.  
  421.  
  422. /**
  423. * Set the coordinate of the target tile in the given world to the given
  424. * tile coordinate.
  425. */
  426. public void setTargetTileCoordinate(int[] tileCoordinate) throws Exception{
  427. if(tileCoordinate.length == 2 || tileCoordinate ==null) {
  428. targetTileCoordinates = tileCoordinate;}
  429. else {throw new Exception("Invalid coordinates");}
  430.  
  431.  
  432. }
  433.  
  434. /**
  435. * Return the coordinate of the target tile in the given world.
  436. * Returns an array of 2 integers {x, y} that represents the tile coordinate
  437. * of the target tile.
  438. * */
  439. public int[] getTargetTileCoordinate() throws ModelException{
  440. return targetTileCoordinates;}
  441. /*
  442. ************************************
  443. | Collision |
  444. ************************************
  445. */
  446.  
  447.  
  448. /**
  449. * returns of the the given currentEntity interactes with the given geologicalFeature
  450. *
  451. * @param currentEntity
  452. * is the given Entity
  453. * @param GeologicalFeature
  454. * is the given geologicalFeature
  455. * @return
  456. * if the Entity interactes with the given geologicalfeature by calculateing the corners of the given Entity returns true
  457. * |if(getGeologicalFeatures(X, Y)==GeologicalFeature
  458. * | || getGeologicalFeatures((X+SpriteX), Y + SpriteY)==GeologicalFeature
  459. * | || getGeologicalFeatures((X+SpriteX), Y)==GeologicalFeature
  460. * | || getGeologicalFeatures(X+SpriteX, Y+SpriteY)==GeologicalFeature
  461. * |then touch=true;
  462. * @return
  463. * if the Entity is not interactes with the given geologicalfeature by calculateing the corners of the given Entity returns false
  464. * |if(getGeologicalFeatures(X, Y)!=GeologicalFeature
  465. * | || getGeologicalFeatures((X+SpriteX), Y + SpriteY)!=GeologicalFeature
  466. * | || getGeologicalFeatures((X+SpriteX), Y)!=GeologicalFeature
  467. * | || getGeologicalFeatures(X+SpriteX, Y+SpriteY)!=GeologicalFeature
  468. * |then touch=false;
  469. */
  470. public boolean Element(Entity currentEntity,int GeologicalFeature) {
  471. boolean touch=false;
  472. int X=currentEntity.getPixelPosition()[0],Y =currentEntity.getPixelPosition()[1],SpriteX= currentEntity.getCurrentSprite().getWidth()-1,SpriteY=currentEntity.getCurrentSprite().getHeight()-1;
  473. if(getGeologicalFeatures(X, Y)==GeologicalFeature
  474. || getGeologicalFeatures((X+SpriteX), Y + SpriteY)==GeologicalFeature
  475. || getGeologicalFeatures((X+SpriteX), Y)==GeologicalFeature
  476. || getGeologicalFeatures(X+SpriteX, Y+SpriteY)==GeologicalFeature
  477. ){touch=true;}
  478. else {touch=false;}
  479. return touch;
  480. }
  481.  
  482.  
  483. /**
  484. * calculates if the entity will collide with earth with the given time
  485. *
  486. * @param currentEntity
  487. * is a given Entity
  488. * @param time
  489. * is a given time in double
  490. * @return
  491. * if the entity will collide with earth with the given time
  492. *
  493. */
  494. public boolean futureEarth(Entity currentEntity,double time) throws Exception {
  495. int[] pos =
  496. {mazub.calculateToPixel(BigDecimal.valueOf(
  497. (BigDecimal.valueOf(currentEntity.actualPosition[0])
  498. .add (BigDecimal.valueOf(currentEntity.Velocity[0]).multiply(BigDecimal.valueOf(time)))
  499. .add(BigDecimal.valueOf(currentEntity.getAcceleration()[0]).multiply(BigDecimal.valueOf(0.5))
  500. .multiply(BigDecimal.valueOf(Math.pow(time, 2))))).doubleValue()).doubleValue()),
  501. mazub.calculateToPixel(BigDecimal.valueOf(
  502. (BigDecimal.valueOf(currentEntity.actualPosition[1])
  503. .add (BigDecimal.valueOf(currentEntity.Velocity[1]).multiply(BigDecimal.valueOf(time)))
  504. .add(BigDecimal.valueOf(currentEntity.getAcceleration()[1]).multiply(BigDecimal.valueOf(0.5))
  505. .multiply(BigDecimal.valueOf(Math.pow(time, 2))))).doubleValue()).doubleValue())};
  506.  
  507. boolean touch=false;
  508. int X=pos[0]+1,Y = pos[1]+1,SpriteX=currentEntity.getCurrentSprite().getWidth()-2,SpriteY= currentEntity.getCurrentSprite().getHeight()-2;
  509. if(getGeologicalFeatures(X, Y)==1
  510. || getGeologicalFeatures(X+SpriteX, Y)== 1
  511. || getGeologicalFeatures(X, Y+SpriteY)==1
  512. || getGeologicalFeatures(X+SpriteX, Y+SpriteY)==1
  513. ){touch=true;}
  514. else {touch=false;}
  515. return touch;
  516. }
  517.  
  518.  
  519. /**
  520. * Collides with earth
  521. *
  522. * @param currentEntity
  523. * @throws Exception
  524. */
  525. public void Earth(Entity currentEntity) throws Exception {
  526. int X=currentEntity.getPixelPosition()[0]+1,Y =currentEntity.getPixelPosition()[1]+1,SpriteX= currentEntity.getCurrentSprite().getWidth()-2,SpriteY= currentEntity.getCurrentSprite().getHeight()-2;
  527. //TopPixels = Left Up & Right Up
  528. if((getGeologicalFeatures(X, Y+SpriteY)==1||getGeologicalFeatures(X+SpriteX,Y+SpriteY)==1 ) && mazub.isJumping()) {
  529. currentEntity.falling();}
  530. else {currentEntity.stopDuckingIsEnabled();}
  531. //BottomPixels = Left Bottom & Right Bottom
  532. if((getGeologicalFeatures(X, Y-1)==1 || getGeologicalFeatures(X+SpriteX, Y-1)==1) && currentEntity.getVelocity()[1]<0 ) {
  533. currentEntity.endJump();}
  534. else {currentEntity.falling();} //;throw new Exception(Integer.toString(X-1)+" "+Integer.toString(Y-1));}
  535. //RightPixels= alleen rechtsboven want als je linksonder doet kan hij niet meer bewegen door het verplaatsung
  536. if((getGeologicalFeatures(X+SpriteX, Y+SpriteX-1)==1 || getGeologicalFeatures(X+SpriteX, Y)==1) && currentEntity.getOrientation()==1 && currentEntity.getVelocity()[0]>=0) {
  537. currentEntity.endMove();throw new Exception(Integer.toString(X-1)+" "+Integer.toString(Y-1));
  538. }
  539. //LeftPixels
  540. if((getGeologicalFeatures(X, Y+SpriteY-1)==1||getGeologicalFeatures(X, Y)==1 )&& currentEntity.getOrientation()==-1) {
  541. currentEntity.endMove();throw new Exception(Integer.toString(X-1)+" "+Integer.toString(Y-1));}
  542.  
  543.  
  544. }
  545.  
  546.  
  547. /**
  548. * returns a boolean if mazub is in the targetcoordinate
  549. *
  550. * @param currentEntity
  551. * is a given Entity
  552. *
  553. * @return
  554. * if mazub's corners are touching the tile of the targetcoordinate
  555. * |if(calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
  556. * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
  557. * | || calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1]
  558. * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1])
  559. * |then return true
  560. * @return
  561. * if mazub's corners are not touching the tile of the targetcoordinate
  562. * |if(calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y) != getTargetTileCoordinate()[1]
  563. * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y) != getTargetTileCoordinate()[1]
  564. * | || calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) != getTargetTileCoordinate()[1]
  565. * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) != getTargetTileCoordinate()[1])
  566. * |then return false
  567. *
  568. */
  569. public boolean mazubInTarget(Entity currentEntity) {
  570. boolean touch=false;
  571. int X=currentEntity.getPixelPosition()[0]+1,Y = currentEntity.getPixelPosition()[1]+1,SpriteX= currentEntity.getCurrentSprite().getWidth()-2,SpriteY= currentEntity.getCurrentSprite().getHeight()-2;
  572. if(calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
  573. || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
  574. || calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1]
  575. || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1]
  576. )
  577. {touch=true;}
  578. else {touch=false;}
  579. return touch;
  580. }
  581.  
  582.  
  583. /*
  584. ************************************
  585. | Advenced time Defensieve program |
  586. ************************************
  587. */
  588. //After Ducking
  589. //FIXME if(!(mazub.isDucking()) && getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==1) {mazub.Ducking=true;}
  590. //FIXME if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==0 && mazub.isDucking()==true ) {mazub.endDucking();}
  591.  
  592.  
  593. /**
  594. * In-game-time timer
  595. */
  596. public double AFithOfAsecond=0.0;
  597. /**
  598. * Advances the time for the world and all its objects by the given amount.
  599. * @param dt
  600. * The in game time.
  601. * @throws IllegalArgumentException
  602. */
  603.  
  604. public double lessThenTwo = 0.0;
  605. public boolean canBeDamaged=true;
  606. public void advancedTime(double dt) throws Exception { if(dt<0 || dt > 0.2) {throw new IllegalArgumentException();}
  607. Iterator<Object> itr = object_set.iterator();
  608. while(itr.hasNext()){
  609. Entity currentEntity=(Entity)itr.next();
  610. if(currentEntity instanceof Mazub) {
  611. BigDecimal time = timePerCm(dt, currentEntity);
  612. for (BigDecimal i = BigDecimal.valueOf(0.00); i.compareTo(BigDecimal.valueOf(dt))==-1; i = BigDecimal.valueOf(i.add(time).doubleValue()) ) {
  613. if(hashAsGameObject(currentEntity)) {
  614. if(mazubInTarget(currentEntity)==true) {didPlayerReachedTarget=true;}else {didPlayerReachedTarget=false;}
  615.  
  616. currentEntity.advanceTime(time.doubleValue());Earth(currentEntity);
  617. isGameOver();
  618.  
  619. }
  620. if(currentEntity.getWorld()!= null && OutsideWorldBorder(currentEntity.getActualPosition())) {terminatedGameObject(currentEntity);;}
  621.  
  622.  
  623. // if(mazub.isMoving() || mazub.isJumping()) {time = CollisionDetectTime(time,currentEntity);}
  624. if(OutsideWorldBorder(currentEntity.getActualPosition())) {terminatedGameObject(currentEntity);;}}}
  625. else {currentEntity.advanceTime(dt);}
  626. if(OutsideWorldBorder(currentEntity.getActualPosition())) {currentEntity.setHitPoint(0);}
  627. }
  628.  
  629. }
  630.  
  631.  
  632.  
  633.  
  634. public int hitsChanged = 0;
  635. public int totalChanged = 0;
  636.  
  637.  
  638. /**
  639. * is collision detection with geologicalfeatures
  640. */
  641. public int ElementCollison(Entity currentEntity) {
  642. if(Element(currentEntity,3)==true){hitsChanged+=-50;totalChanged+=-50;}// 3=Magma
  643. else if(Element(currentEntity,5)==true){hitsChanged+=-4;totalChanged+=-4;}
  644. else if(Element(currentEntity,2)==true){hitsChanged+=-2;totalChanged+=-2;} // 2=Water
  645.  
  646. return hitsChanged;
  647. }
  648. /*
  649. * *****************
  650. * Collision Detect*
  651. * *****************
  652. */
  653.  
  654.  
  655. /**
  656. * Boolean value that is true if Mazub will collide with something, and false if not.
  657. */
  658. public boolean Overlap;
  659.  
  660. /**
  661. * calculates the given time with the entity to a double that gives a time for each cm that changed
  662. *
  663. * @param time
  664. * is the given time that has to be calculated
  665. * @param currentEntity
  666. * is the given Entity where his time per cm
  667. * @return
  668. * the time per centimeter
  669. *
  670. */
  671. public BigDecimal timePerCm(double time,Entity currentEntity) {
  672.  
  673. //Calculation of time fragments with given acceleration, velocity and time.
  674. BigDecimal currentVelocityX= BigDecimal.valueOf(currentEntity.getVelocity()[0]), currentVelocityY= BigDecimal.valueOf(currentEntity.getVelocity()[1]), currentAccelerationX= BigDecimal.valueOf(currentEntity.getAcceleration()[0]),currentAccelerationY= BigDecimal.valueOf(currentEntity.getAcceleration()[1]);
  675. BigDecimal cm= BigDecimal.valueOf(0.01);
  676. BigDecimal timedt = BigDecimal.valueOf(time);
  677. BigDecimal mathsqr1=BigDecimal.valueOf(Math.sqrt((currentAccelerationX.pow(2).add(currentAccelerationY.pow(2))).doubleValue()));
  678. BigDecimal mathsqr2=BigDecimal.valueOf(Math.sqrt((currentVelocityX.pow(2).add(currentVelocityY.pow(2))).doubleValue()));
  679. BigDecimal divider = mathsqr1.add(mathsqr2.multiply(timedt));
  680. if(divider.compareTo(BigDecimal.valueOf(0.0))==0) {return BigDecimal.valueOf(time);}
  681. BigDecimal dt= BigDecimal.valueOf(cm.divide((divider), 16, RoundingMode.HALF_UP).doubleValue());
  682. return dt;
  683.  
  684. }
  685.  
  686.  
  687.  
  688. /*
  689. * *************
  690. * Game Objects*
  691. * *************
  692. */
  693. // public boolean isDeadGameObject()
  694.  
  695. /**
  696. * is the set of all objects inside the world.
  697. */
  698. public Set<Object> object_set = new HashSet<Object>();
  699.  
  700. /**
  701. * @return
  702. * returns a set of all game objects
  703. * |return object_set;
  704. */
  705. public Set<Object> getAllGameObjects(){return object_set;}
  706.  
  707.  
  708. /**
  709. * sets the set of all game objects of this world to the given set
  710. *
  711. * @param newSet
  712. * a set of objects
  713. * @effect
  714. * sets the set of all game objects of this world to the given set
  715. * |object_set = newSet;
  716. */
  717. public void setAllGameObjects(Set<Object> newSet){object_set = newSet;}
  718.  
  719. /**
  720. * Remove the given object to the given world.
  721. * @effect
  722. * Remove the given object to the given world.
  723. * |object_set.remove(object);
  724. */
  725. public void removeGameObject(Object object){
  726. object_set.remove(object);}
  727.  
  728.  
  729. /**
  730. * add a gameObject inside the set of all given objects.
  731. *
  732. * @param gameObject
  733. * a given gameObject
  734. * |
  735. * @param world
  736. * @effect
  737. * if the gameObject is an instance of mazub and mazub
  738. * @throws Exception
  739. * if the world already has 100 gameObject throw a exception
  740. * |if(object_set.size()>=100)
  741. * |then throw new Exception("Already 100 gameObjects");
  742. * @effect
  743. * if the world has less then 100 gameObject add the gameObject inside the set of all given objects and if gameObject has no world,
  744. * set the world of the gameObject to this world
  745. * | if(object_set.size()<100)
  746. * | then object_set.add(gameObject);
  747. * | if(gameObject.getWorld()!=null)
  748. * | then gameObject.setWorld(this);
  749. */
  750. public void addGameObject(Entity gameObject,World world) throws Exception {
  751. if (gameObject instanceof Mazub&&(mazub == null ||gameObject==null)) {mazub = (Mazub)gameObject;}
  752. if(object_set.size()<100) {object_set.add(gameObject);gameObject.setWorld(world);}
  753. else {throw new Exception("Already 100 gameObjects");}
  754. }
  755.  
  756. // public void DeathGameObject() {
  757. // if(mazub.getPixelPosition()[0]>getSizeInPixels()[0] || mazub.getPixelPosition()[1]>getSizeInPixels()[1]) {
  758. // mazub.setHitPoint(0);}}
  759.  
  760. /**
  761. * returns if the game is over
  762. *
  763. * @return
  764. * if the mazub is dead return true
  765. * |if(mazub.isDeadGameObject())
  766. * |then result = true;
  767. * @return
  768. * if the mazub reached the target variable
  769. * |if(didPlayerReachedTarget)
  770. * |then result = true;
  771. * @return
  772. * else the game isn't over
  773. * |result = false
  774. *
  775. */
  776. public boolean isGameOver() {
  777. if(mazub.isDeadGameObject()) {return true;}
  778. if(didPlayerReachedTarget) {return true;}
  779. return false;
  780. }
  781.  
  782. /**
  783. * returns that the object is inside this world
  784. *
  785. * @param object
  786. * a given object
  787. * @return
  788. * if the object is inside the set of all game object
  789. * |if(object_set.contains(object)) then result = true
  790. * @return
  791. * if the object is not inside of the set of all game object
  792. * |if(!object_set.contains(object)) then result = false
  793. */
  794. public boolean hashAsGameObject(Object object) {if(object_set.contains(object)) {return true;}else {return false;}}
  795. /*
  796. *********************
  797. * Terminate*
  798. *********************
  799. */
  800. /**
  801. * Start a game in the given world.
  802. */
  803. public void startGame() throws ModelException{}
  804.  
  805. /*
  806. *********************
  807. * Mazub*
  808. *********************
  809. */
  810. /**
  811. * Variable storing the Mazzub of this world
  812. */
  813. private Mazub mazub;
  814. private boolean didPlayerReachedTarget=false;
  815.  
  816. /**
  817. * returns this mazub
  818. *
  819. * @return
  820. * returns this mazub
  821. * |result = this.mazub
  822. */
  823. public Mazub getMazub() {
  824. return mazub;
  825. }
  826.  
  827. /**
  828. * set this mazub of the world to the given newmazub
  829. *
  830. * @param newMazub
  831. * @effect
  832. * set this mazub of the world to the given newmazub
  833. * |mazub=newmazub
  834. */
  835. public void setMazub(Mazub newMazub) {
  836. mazub=newMazub;
  837. }
  838.  
  839. /**
  840. *returns if the player has won
  841. *
  842. * @return
  843. * if the played reached the target return true
  844. * |if(didPlayerReachedTarget) then result = true
  845. * @return
  846. * if the mazubb is dead return false
  847. * |else if(mazub.isDeadGameObject()==true) then result= false
  848. * @return
  849. * otherwise false
  850. * |else result= false
  851. *
  852. */
  853. public boolean DidPlayerWin() {
  854. if(didPlayerReachedTarget) {return true;}
  855. else if(mazub.isDeadGameObject()==true) {return false;}
  856. else {return false;}
  857. }
  858.  
  859. /*
  860. *********************
  861. * School*
  862. *********************
  863. */
  864.  
  865.  
  866. /**
  867. * results either false or true weither the gameobject is removed or not
  868. *
  869. * @param gameObject
  870. * a given object
  871. * @return
  872. * if given object is terminated and it doesn't contain in the world
  873. * | if(((Entity)gameObject).isTerminatedGameObject()==true||hashAsGameObject(gameObject)) then result= false
  874. * @return
  875. * if the given object is terminated or it does contain in the world
  876. * |if(((Entity)gameObject).isTerminatedGameObject()==false||!hashAsGameObject(gameObject)){return false
  877. */
  878. public boolean isTerminatedGameObject(Object gameObject) {
  879. if(((Entity)gameObject).isTerminatedGameObject()==true||hashAsGameObject(gameObject)){return false;}
  880. else {return true;}}
  881.  
  882. /**
  883. * terminate the game object
  884. *
  885. * @param gameObject
  886. *
  887. * @effect
  888. * remove the
  889. * |removeGameObject(gameObject);
  890. * @effect
  891. *
  892. * |
  893. */
  894. public void terminatedGameObject(Entity gameObject) {
  895. if(gameObject instanceof Mazub) {if(gameObject.getWorld()!=null) {((Mazub) gameObject).terminateGameObject();}
  896. mazub = null;removeGameObject(gameObject);}
  897. else {if(gameObject.getWorld()!=null) {gameObject.terminateGameObject();}removeGameObject(gameObject);}
  898. // removeGameObject(gameObject);
  899. }
  900.  
  901. /**
  902. * @param newpos
  903. * @return
  904. */
  905. public boolean calculateInsideOfWorld(int[] newpos) {
  906. if(newpos[0]<0||newpos[0]>getSizeInPixels()[0]||newpos[1]<0||newpos[1]>getSizeInPixels()[1]) {return true;}
  907. else {return false;}
  908. }
  909.  
  910. /**
  911. * @post...
  912. * |new.getSchool().contains(school)==true
  913. */
  914. public Set<School> SchoolSet=new HashSet<School>();
  915. public void setSchools(Set<School> school) {
  916. SchoolSet=school;
  917. }
  918. public void addSchool(School newSchool)throws Exception {
  919. if(SchoolSet.size()<10) {
  920. SchoolSet.add(newSchool);}
  921. else {throw new Exception("To many School in world.");}
  922. }
  923.  
  924. /**
  925. *
  926. * @return...
  927. * |result ==SchoolSet
  928. */
  929. public Set<School> getAllSchools() {
  930. return SchoolSet;
  931. }
  932.  
  933.  
  934. public boolean OutsideWorldBorder(double[] newPosition){
  935. if(newPosition[0] < 0 || newPosition[0] > calculateToActual(getSizeInPixels()[0]) || newPosition[1] <0 || newPosition[1] > calculateToActual(getSizeInPixels()[1]) ) {return true;}
  936. else {return false;}}
  937. public void terminateWorld() {
  938. Iterator<Object> itr = object_set.iterator();
  939. while(itr.hasNext()){
  940. Entity currentEntity=(Entity)itr.next();
  941. (currentEntity).terminateGameObject();}
  942. object_set.clear();
  943. }
  944. public int calculateToPixel (double actual) {return ((int)(actual*100));}
  945. public double calculateToActual (int pixel) {return ((double)(pixel/100));}
  946.  
  947. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement