Advertisement
RecklessDarph

Plant

Mar 28th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. package jumpingalien.model;
  2.  
  3. import jumpingalien.util.Sprite;
  4.  
  5. /**
  6. * A class for creating the plants in the game and getting the imageSprites and coordinates.
  7. * Plants can move from left to right, or from right to left.
  8. * After leaving the visible game world, the plants will be terminated.
  9. * Plants are food to be eaten by Mazub.
  10. *
  11. * @version 2.0
  12. * @author Thierry Klougbo & Jordi De Pau
  13. */
  14. public class Plant extends Entity{
  15.  
  16. /**
  17. *Create an instance of Plant with given pixel position and given sprites.
  18. *The actual position of the new plant will correspond with the coordinates
  19. *of the left-bottom corner of the left-bottom pixel occupied by the new plant.
  20. *
  21. * @param pixelLeftX
  22. * @param pixelBottomY
  23. * @param sprites
  24. */
  25. public Plant(int pixelLeftX, int pixelBottomY, Sprite... sprites) {
  26. super(pixelLeftX, pixelBottomY, sprites);
  27. setSPRITES(sprites);
  28. setActualPosition(pixelLeftX, pixelBottomY);
  29. setPixelPosition(pixelLeftX, pixelBottomY);
  30. setHitPoint(1);
  31. }
  32.  
  33.  
  34. /*
  35. ************
  36. * Position *
  37. ************
  38. */
  39. public double[] actualPosition=new double[2];
  40. @Override
  41. public void setActualPosition(double X, double Y) {
  42. actualPosition[0]=(X/100); actualPosition[1]= (Y/100);
  43. // actualPosition[0]=getPixelPosition()[0]/100; actualPosition[1]=getPixelPosition()[0]/100;
  44. }
  45.  
  46. /**
  47. * Returns the actual position of the given game object.
  48. * Returns an array of 2 doubles {x, y} that represents the coordinates of the
  49. * bottom-left corner of the given game object.
  50. * Coordinates are expressed in meters.
  51. *
  52. */
  53. @Override
  54. public double[] getActualPosition() {
  55. return actualPosition;
  56. }
  57.  
  58.  
  59. public int[] pixelPosition=new int[2];
  60. @Override
  61. public void setPixelPosition(int pixelLeftX, int pixelBottomY) {
  62. // pixelPosition[0] = pixelLeftX; pixelPosition[1]=pixelBottomY;
  63. pixelPosition[0]=(int)(getActualPosition()[0]*100); pixelPosition[1]=(int)(getActualPosition()[1]*100);
  64. }
  65.  
  66. @Override
  67. public int[] getPixelPosition() {return pixelPosition;}
  68.  
  69. /*
  70. ****************
  71. * Orientation *
  72. ****************
  73. */
  74. public int Orientation;
  75. @Override
  76. public void setOrientation(int OrientationPlant) {Orientation = OrientationPlant;}
  77.  
  78. public int getOrientation() {return Orientation;}
  79.  
  80.  
  81. /*
  82. ************
  83. * Movement *
  84. ************
  85. */
  86. @Override
  87. public void startMovingLeft(){
  88. setOrientation(-1);
  89. setVelocity(0.5, 0);}
  90.  
  91. @Override
  92. public void startMovingRight() {
  93. setOrientation(1);
  94. setVelocity(0.5, 0);}
  95.  
  96. /**
  97. *
  98. * @return boolean for if plant is moving left or not.
  99. * |if (getPlantVelocity()[0]< 0) {return true;} else {return false;}
  100. */
  101. public boolean IsMovingLeftPlant() {
  102. if (getVelocity()[0]< 0 || getVelocity()[0]==0) {return true;} else {return false;}}
  103.  
  104. /**
  105. *
  106. * @return a boolean for if plant is moving right or not.
  107. * | if(getPlantVelocity()>0) {return true;} else {return false;}
  108. */
  109. public boolean IsMovingRightPlant() {
  110. if(getVelocity()[0]>0 || getVelocity()[0]==0) {return true;} else {return false;}}
  111.  
  112. public void HorizontalMovement(double dt) {
  113. double posx=getActualPosition()[0]+(getVelocity()[0]*dt);
  114. setActualPosition(posx, getActualPosition()[1]);
  115. }
  116.  
  117. /*
  118. ************
  119. * Velocity *
  120. ************
  121. */
  122.  
  123. public double[] Velocity=new double[2];
  124. @Override
  125. public void setVelocity(double velocityX, double velocityY) {Velocity[0] = velocityX*getOrientation(); Velocity[1]=velocityY;}
  126.  
  127. @Override
  128. public double[] getVelocity() {return Velocity;}
  129.  
  130. /*
  131. *********************
  132. * Sprites of plants *
  133. *********************
  134. */
  135. public Sprite[] SpriteS;
  136. public void setSPRITES(Sprite[] sPrites) {SpriteS=sPrites;}
  137.  
  138. public Sprite[] getSPRITES() {return SpriteS;}
  139.  
  140.  
  141.  
  142. public Sprite SPRITES=null;
  143. /**
  144. * @return the current sprite of the plant.
  145. * |result=SPRITES
  146. * @post...
  147. * | if(IsMovingLeftPlant()==true){SPRITES[0]=currentSpritePlant[0];}
  148. * @post...
  149. * | if(IsMovingRightPlant()==true){SPRITES[0]=currentSpritePlant[1];}
  150. */
  151. public Sprite getCurrentSpritePlant() {
  152. if(IsMovingLeftPlant()==true){SPRITES=getSPRITES()[0];}
  153. if(IsMovingRightPlant()==true){SPRITES=getSPRITES()[1];}
  154. return SPRITES;
  155. }
  156.  
  157. /*
  158. ***************
  159. *AdvencedTime *
  160. ***************
  161. */
  162.  
  163. /*
  164. * Advance the state of the given game object by the given time period.
  165. */
  166. @Override
  167. public void advanceTime(double time) {
  168. double Time=time;
  169. double LeftTime=0;
  170. if (Time%0.5==0) {LeftTime=Time; startMovingLeft();HorizontalMovement(time);}
  171. if (Time==LeftTime+0.5 ) {startMovingRight();HorizontalMovement(time);}
  172. if(Time==10) {setHitPoint(0);isDeadGameObject();}
  173. }
  174.  
  175.  
  176. /*
  177. **************
  178. * Terminated *
  179. **************
  180. */
  181. public void terminateGameObject() {
  182.  
  183. }
  184.  
  185. @Override
  186. public boolean isDeadGameObject() {
  187. if(getHitpoint()== 0) {return true;}
  188. else {return false;}
  189. }
  190.  
  191. /*
  192. ************
  193. * Hitpoints*
  194. ************
  195. *
  196. */
  197. public int Hitpoint=0;
  198.  
  199. /**
  200. * Sets the hitpoint of the plant
  201. */
  202. @Override
  203. public void setHitPoint(int hitpoint) {
  204. if(Hitpoint==0) {isDeadGameObject();}
  205. Hitpoint=hitpoint;
  206. }
  207.  
  208. /**
  209. *
  210. * @param Hitpoint
  211. * @return
  212. */
  213. @Override
  214. public int getHitpoint() {
  215. return Hitpoint;
  216. }
  217.  
  218.  
  219. /*
  220. **************
  221. * Get World
  222. **************
  223. *
  224. */
  225. World thisWorld;
  226. @Override
  227. public void setWorld(World world) {
  228. thisWorld = world;
  229. }
  230.  
  231. @Override
  232. public World getWorld() {
  233. return thisWorld;
  234. }
  235.  
  236.  
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement