RecklessDarph

World

Mar 16th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.43 KB | None | 0 0
  1. package jumpingalien.model;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6.  
  7. import com.sun.javafx.scene.traversal.WeightedClosestCorner;
  8.  
  9. import be.kuleuven.cs.som.annotate.Raw;
  10. import jumpingalien.facade.Facade;
  11. import jumpingalien.model.Mazub;
  12. import jumpingalien.util.ModelException;
  13.  
  14.  
  15. /**
  16. * A class for creating the game world, get the images and coordinates, and make the world change depending on the pixelposition of Mazub
  17. * and the advance time.
  18. * The world has passable and impassable terains.
  19. *
  20. * @version 2.0
  21. * @author Thierry Klougbo & Jordi De Pau
  22. */
  23. public class World {
  24.  
  25.  
  26. public int VisibleWindowX, VisibleWindowY, TileLength ,nmbTilesX, nmbTilesY;
  27. /**
  28. *
  29. * @param pixelSize
  30. * The actual size of the pixels.
  31. * @param nbTilesX
  32. * The number of tiles in the visible window of the world's x-axis.
  33. * @param nbTilesY
  34. * The number of tiles in the visible window of the world's y-axis.
  35. * @param targetTileCoordinate
  36. * @param visibleWindowWidth
  37. * The actual width of the visible window.
  38. * @param visibleWindowHeight
  39. * The actual height of the visible window.
  40. * @param geologicalFeatures
  41. * An integer for representing every geological feature:
  42. * AIR = 0;
  43. * SOLID_GRO;UND = 1;
  44. * WATER = 2;
  45. * MAGMA = 3;
  46. */
  47. public World(int pixelSize, int nbTilesX, int nbTilesY, int[] targetTileCoordinate, int visibleWindowWidth, int visibleWindowHeight, int...geologicalFeatures){
  48. nmbTilesX=nbTilesX/100;
  49. nmbTilesY=nbTilesY/100;
  50. TileLength=pixelSize;
  51. setVisibleWindowDimension(visibleWindowWidth,visibleWindowHeight);
  52. setTargetTileCoordinate(targetTileCoordinate);
  53. setGeologicalFeaturesSize(nbTilesX, nbTilesY);
  54. setSizeInPixels(nbTilesX*pixelSize,nbTilesY*pixelSize);
  55.  
  56. for (int y= 0; y < nbTilesY; y++) {
  57. for (int x = 0; x< nbTilesX; x++) {
  58. int counter =y*nbTilesX + x;
  59. setFeature(x, y,geologicalFeatures[counter]);
  60. }
  61. }
  62. }
  63.  
  64. /*
  65. * Terminate the given world.
  66. */
  67. public void terminateWorld(World world) throws ModelException{
  68. }
  69.  
  70. /*
  71. *******************************
  72. * *Tiles Total programming. *
  73. * *****************************
  74. */
  75. public int [] SizeInPixels ;
  76. /**
  77. * Returns the size of the game world in pixels.
  78. * @post SizeInPixels contains respectively the visible window length and the
  79. * visible window height.
  80. * | SizeInPixels[0]=VisibleWindowX; SizeInPixels[1]=VisibleWindowY;
  81. * @return SizeInPixels
  82. */
  83. public int[] getSizeInPixels() {return SizeInPixels;}
  84.  
  85. public void setSizeInPixels(int Width, int Height) {
  86. SizeInPixels = new int[2];
  87. SizeInPixels[0] =Width;
  88. SizeInPixels[1] =Height;
  89. }
  90.  
  91.  
  92. public int getTileLength(){
  93. return TileLength;
  94. }
  95.  
  96.  
  97. /*
  98. * ***********************
  99. * Geological features *
  100. * ***********************/
  101.  
  102. public int[][] GeologicalFeaturesCanvas;
  103. public void setGeologicalFeaturesSize(int GeoRow,int GeoCol) {this.GeologicalFeaturesCanvas = new int[GeoRow][GeoCol];}
  104.  
  105. public void setGeologicalFeatures(int X, int Y, int geoFeature) {
  106. if (X > ((SizeInPixels[0]/this.getTileLength())-1) || Y > ((SizeInPixels[0]/this.getTileLength())-1) || X<0 || Y<0) {return;} ///Problem
  107. int GeoX = calculateInFeature(X);
  108. int GeoY = calculateInFeature(Y);
  109. this.GeologicalFeaturesCanvas[GeoX][GeoY] = geoFeature;
  110. }
  111.  
  112. public int getGeologicalFeatures(int pixelX, int pixelY) {
  113. if (pixelX >= this.getSizeInPixels()[0] || pixelY >= this.getSizeInPixels()[1]) {return 0;}
  114. int GeoX = calculateInFeature(pixelX);
  115. int GeoY = calculateInFeature(pixelY);
  116. return GeologicalFeaturesCanvas[GeoX][GeoY];
  117. }
  118.  
  119. public void setFeature(int X, int Y, int F) {
  120. this.GeologicalFeaturesCanvas[X][Y] = F;
  121. }
  122. public int calculateInFeature(int aPixel) {
  123. int resultPixel = (int)(aPixel/getTileLength());
  124. return resultPixel;
  125.  
  126. }
  127.  
  128. /*
  129. **********************************
  130. *Visible Window Defensive program*
  131. **********************************
  132. */
  133.  
  134. public int[] VisibleWindowDimension=new int[2];
  135. public void setVisibleWindowDimension(int weight , int height){
  136. VisibleWindowDimension[0] = weight;
  137. VisibleWindowDimension[1] = height;
  138. }
  139.  
  140. /**
  141. * gets visible window dimensions.
  142. * @return
  143. * |result=VisibleWindowDimension
  144. */
  145. public int[] getVisibleWindowDimension() {
  146. return VisibleWindowDimension;
  147. }
  148.  
  149. int LEFT, DOWN;
  150.  
  151. public int[] getVisibleWindowsPosition() {
  152. int[]VisibleWindowPosition=new int[2];
  153. getSizeInPixels();
  154. if(mazub.getPixelPosition()[0]!=getSizeInPixels()[0]&& visibleMazubCanMove(0) ){
  155. this.LEFT=(int)(VisiblePositionAdaptor);}
  156. if (mazub.getPixelPosition()[1]!=getSizeInPixels()[1] &&visibleMazubCanMove(1)) {
  157. this.DOWN=(int)(VisiblePositionAdaptor);}
  158.  
  159. VisibleWindowPosition[0]=LEFT;VisibleWindowPosition[1]=DOWN;
  160. return VisibleWindowPosition;
  161.  
  162.  
  163. }
  164. public int VisiblePositionAdaptor;
  165.  
  166.  
  167. public boolean visibleMazubCanMove(int direction) {
  168. int newPos = mazub.getPixelPosition()[direction];
  169.  
  170.  
  171. if(newPos<= 400 ||newPos >SizeInPixels[direction]-400) { VisiblePositionAdaptor = 0; return false;}
  172. else if(newPos <= newPos + (getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) || newPos > newPos +(getVisibleWindowDimension()[direction]-400)) {VisiblePositionAdaptor = newPos -400; return true;}
  173. else if(newPos <= newPos + (getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) || newPos > newPos +(getVisibleWindowDimension()[direction])) {VisiblePositionAdaptor = 0; return false;}
  174. else {return false;}
  175. /*
  176. if(prevPos >= 0 && prevPos<200) { VisiblePositionAdaptor =prevPos +0;return false;}
  177. else if(prevPos >= 200 && prevPos >getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) { VisiblePositionAdaptor =prevPos;return true;}
  178. else if(prevPos >= (getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) && prevPos<getVisibleWindowDimension()[direction]-400) {VisiblePositionAdaptor=prevPos-400 ;return false;}
  179. else if(prevPos >= getVisibleWindowDimension()[direction]-400 && prevPos >(getSizeInPixels()[direction]-200)) {VisiblePositionAdaptor = prevPos ; return true;}
  180. else if(prevPos >= getSizeInPixels()[direction]-200 && prevPos<getSizeInPixels()[direction]) {VisiblePositionAdaptor= prevPos -0 ;return false;}
  181. else {return false;
  182. */
  183. }
  184.  
  185.  
  186. // public void setVisiblePositionAdaptor(int visiblePositionAdaptor) {
  187. // VisiblePositionAdaptor = visiblePositionAdaptor;
  188. // }
  189.  
  190.  
  191.  
  192. /*
  193. ************************************
  194. | Target tiles Nominal programming |
  195. ************************************
  196. */
  197.  
  198. public int[] targetTileCoordinates = new int[2];
  199. /*
  200. * Set the coordinate of the target tile in the given world to the given
  201. * tile coordinate.
  202. */
  203. public void setTargetTileCoordinate(int[] tileCoordinate) throws ModelException{
  204. targetTileCoordinates = tileCoordinate;}
  205.  
  206. /*
  207. * Return the coordinate of the target tile in the given world.
  208. * Returns an array of 2 integers {x, y} that represents the tile coordinate
  209. * of the target tile.
  210. * */
  211. public int[] getTargetTileCoordinate() throws ModelException{
  212. return targetTileCoordinates;}
  213.  
  214. /*
  215. * Start a game in the given world.
  216. */
  217. public void startGame() throws ModelException{
  218.  
  219. }
  220.  
  221.  
  222. /*
  223. ************************************
  224. | Advenced time Defensieve program |
  225. ************************************
  226. */
  227.  
  228. /**
  229. * In-game-time
  230. */
  231.  
  232. public double a1, a2, v1, v2;
  233.  
  234. /**
  235. * Advances the time for the world and all its objects by the given amount.
  236. * @param dt
  237. * The in game time.
  238. * @throws IllegalArgumentException
  239. */
  240. public void advanceWorldTime(double dt) throws IllegalArgumentException { if(dt<0) {throw new IllegalArgumentException();}
  241. DeathGameObject();
  242. double time=dt;
  243. int PosMZx = mazub.getPixelPosition()[0]; int PosMZy=mazub.getPixelPosition()[1];
  244. int pixelX=mazub.getSpriteDimention()[0]; int pixelY=mazub.getSpriteDimention()[1];
  245.  
  246.  
  247. //All still need to be improved so they conclude all pixels of MAzub and not only the bottom ones.
  248. /*
  249. * EARTH
  250. */
  251. //BottomPixels
  252. if(getGeologicalFeatures((PosMZx+(mazub.getSpriteDimention()[0])), PosMZy)==1 && mazub.getVelocity()[1]<=0 ) {
  253. mazub.endJump();
  254. // mazub.setPixelPosition(PosMZx,PosMZy );
  255. // mazub.verticalChanging(dt);
  256. }
  257. //TopPixels
  258. if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==1 && mazub.getVelocity()[1]>0) {
  259. // mazub.endJump();
  260. mazub.setVelocity(mazub.getVelocity()[0],0);
  261. mazub.setAcceleration(mazub.getAcceleration()[0],0);
  262. // if(mazub.getVelocity()[1]>0) {mazub.setVelocity(mazub.getVelocity()[0], 0); mazub.setAcceleration(mazub.getAcceleration()[0], -10);}
  263. }
  264. //RightPixels
  265. if((getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), (PosMZy)+(mazub.getSpriteDimention()[1]-2))==1) && mazub.getOrientation()==1 && mazub.getVelocity()[1]<=0) {
  266. mazub.endJump();
  267. // mazub.setAcceleration(0, mazub.getAcceleration()[1]);
  268. mazub.setVelocity(0, mazub.getVelocity()[1]);
  269. if(mazub.getVelocity()[1]>0) {mazub.setVelocity(mazub.getVelocity()[0], 0); mazub.setAcceleration(0, -10);}
  270. }
  271. //LeftPixels
  272. if(getGeologicalFeatures(PosMZx, (PosMZy)+(mazub.getSpriteDimention()[1]-2))==1 && mazub.getOrientation()==-1) {
  273. mazub.endJump();
  274. // mazub.setOrientation(0);
  275. mazub.setAcceleration(0, mazub.getAcceleration()[1]);
  276. mazub.setVelocity(0, mazub.getVelocity()[1]);
  277. }
  278. //After Ducking
  279. // if(!(mazub.isDucking()) && getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==1) {mazub.Ducking=true;}
  280. // if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==0 && mazub.isDucking()==true ) {mazub.endDucking();}
  281.  
  282. /*
  283. * WATER
  284. */
  285. if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-2), PosMZy)==2
  286. || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy)==2
  287. || getGeologicalFeatures((PosMZx+(mazub.getSpriteDimention()[0]-1)), PosMZy+(mazub.getSpriteDimention()[1]-2))==2
  288. || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==2
  289. ){
  290. if(time%0.2!=0) {mazub.setHitPoint(-2);}
  291. if(time==0.2) {time=0;}
  292. time=+dt;
  293. }
  294.  
  295. /*
  296. * MAGMA
  297. */
  298. if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-2), PosMZy)==3
  299. || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy)==3
  300. || getGeologicalFeatures((PosMZx+(mazub.getSpriteDimention()[0]-1)), PosMZy+(mazub.getSpriteDimention()[1]-2))==3
  301. || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==3
  302. ){
  303. if(time%0.2!=0) {mazub.setHitPoint(-50);}
  304. if(time==0.2) {time=0;}
  305. time=+dt;
  306. }
  307.  
  308.  
  309. if(mazub.isMoving() || mazub.isJumping()) {CollisionDetect(time);}
  310. if(Overlap && (mazub.isMoving() || mazub.isJumping())) {mazub.endMove(); mazub.endJump();}
  311.  
  312. /*
  313. * AdvanceTime Objects
  314. */
  315. if(time%0.2!=0) {
  316. mazub.advanceTime(time);
  317. plant.advanceTime(time);
  318. if(time==0.2) {time=0;}
  319. time=+dt;}
  320. }
  321.  
  322. /*
  323. * *****************
  324. * Collision Detect*
  325. * *****************
  326. */
  327.  
  328. public int WidthTiles, HeigthTiles;
  329. /**
  330. * Boolean value that is true if Mazub will collide with something, and false if not.
  331. */
  332. public boolean Overlap;
  333. /**
  334. * Detects if Mazub collides with anything, and determines if Mazub should the be able to make the initial move.
  335. * @param time
  336. */
  337. public void CollisionDetect(double time) {
  338.  
  339. //Calculation of time fragments with given acceleration, velocity and time.
  340. this.v1=mazub.getVelocity()[0]; this.v2=mazub.getVelocity()[1]; this.a1=mazub.getAcceleration()[0]; this.a2=mazub.getAcceleration()[1];
  341. double dt=0.01/(Math.sqrt(Math.pow(v1,2)+Math.pow(v2,2))+(Math.sqrt(Math.pow(a1,2)+Math.pow(a2,2)))*time);
  342. WidthTiles=54; HeigthTiles=27;
  343.  
  344. //Voor andere voorwerpen getSpriteSize proberen te maken in Entity.
  345. if(time%dt==0) {
  346. VirtualMoving(time);
  347. }
  348.  
  349. if((mazub.getPixelPosition()[0]+(mazub.getSpriteDimention()[0]-1)<VirtualPos[0]
  350. || VirtualPos[0]+(WidthTiles-1)<mazub.getPixelPosition()[0]
  351. || mazub.getPixelPosition()[1]+(mazub.getSpriteDimention()[1]-1)<VirtualPos[1]
  352. || VirtualPos[1]+(HeigthTiles-1)<mazub.getPixelPosition()[1]))
  353. {this.Overlap=false;}
  354. else {Overlap=true;}
  355.  
  356. }
  357.  
  358. int[] VirtualPos=new int[2];
  359. /**
  360. * Method that calculates the "Virtual" position of Mazub after moving. This to be used in collision detect to see if it collides with something.
  361. * @param time
  362. * @return
  363. * result==VirtualPos
  364. */
  365. public int[] VirtualMoving(double T) {
  366. int posx=(int)(mazub.getActualPosition()[0]+(mazub.getVelocity()[0]*T)+(0.5*mazub.getAcceleration()[0]*T*T));
  367. int posy=(int)(mazub.getActualPosition()[1]+ (mazub.getVelocity()[1]*T)+(0.5*mazub.getAcceleration()[1]*T*T));
  368. VirtualPos[0]=posx; VirtualPos[1]=posy;
  369. return VirtualPos; }
  370.  
  371. /*
  372. * *************
  373. * Game Objects*
  374. * *************
  375. */
  376.  
  377. public Set<Object> object_set = new HashSet<Object>();
  378.  
  379. public void setGameObjects(Object gameobject){object_set.add(gameobject);}
  380.  
  381. public Set<Object> getGameObjects(){return object_set;}
  382.  
  383. /*
  384. * Remove the given object to the given world.
  385. */
  386. public void removeGameObject(Object object){
  387. getGameObjects().remove(object);
  388. }
  389.  
  390. /**
  391. * Variable storing the Mazub of this World
  392. */
  393. private Mazub mazub;
  394. /**
  395. * Variable storing the Plant of this world
  396. */
  397. private Plant plant;
  398. /**
  399. * @param gameobject
  400. */
  401. public void addGameObject(Object gameobject) {
  402. if(gameobject instanceof Plant) {this.plant=(Plant)gameobject; setGameObjects(plant);}
  403. if(gameobject instanceof Mazub) {this.mazub=(Mazub)gameobject; setGameObjects(mazub);}
  404. }
  405.  
  406. // public double[] getActualPosition(Object gameobject) {
  407. // double[] position=new double[2];
  408. // if(gameobject instanceof Plant) {position[0]=plant.getActualPosition()[0];position[1]=plant.getActualPosition()[1];}
  409. // if(gameobject instanceof Mazub) {position[0]=mazub.getActualPosition()[0]; position[1]=mazub.getActualPosition()[1];}
  410. // return position;
  411. // }
  412. //
  413. // public int[] getPixelPosition(Object gameobject) {
  414. // int[] PixelPosition=new int[2];
  415. // if(gameobject instanceof Plant) {PixelPosition=plant.getPixelPosition();}
  416. // if(gameobject instanceof Mazub) {PixelPosition[0]=mazub.getPixelPosition()[0];PixelPosition[1]=mazub.getPixelPosition()[1];}
  417. // return PixelPosition;
  418. // }
  419.  
  420. public void DeathGameObject() {
  421. if(mazub.getPixelPosition()[0]>getSizeInPixels()[0] || mazub.getPixelPosition()[1]>getSizeInPixels()[1]) {
  422. mazub.setHitPoint(0);}
  423. }
  424.  
  425. public boolean GoodGame;
  426. public boolean DidPlayerWin() {
  427. if(mazub.isDeadGameObject()==false) {GoodGame=true;}
  428. else if(mazub.isDeadGameObject()==true) {GoodGame=false;}
  429. return GoodGame;
  430. }
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438. }
Advertisement
Add Comment
Please, Sign In to add comment