Advertisement
RedVinck

Plant 4.0

May 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package jumpingalien.model;
  2.  
  3. import java.math.BigDecimal;
  4.  
  5. import jumpingalien.util.Sprite;
  6.  
  7. /**
  8.  * A class for creating the plants in the game and getting the imageSprites and coordinates.
  9.  * Plants can move from left to right, or from right to left.
  10.  * After leaving the visible game world, the plants will be terminated.
  11.  * Plants are food to be eaten by Mazub.
  12.  *
  13.  * @version  2.0
  14.  * @author   Thierry Klougbo & Jordi De Pau
  15.  */
  16. public class Plant extends Entity{
  17.  
  18. /**
  19.  *Create an instance of Plant with given pixel position and given sprites.
  20.  *The actual position of the new plant will correspond with the coordinates
  21.  *of the left-bottom corner of the left-bottom pixel occupied by the new plant.
  22.  *
  23.  * @param pixelLeftX
  24.  * @param pixelBottomY
  25.  * @param sprites
  26.  */
  27. public Plant(int pixelLeftX, int pixelBottomY, Sprite... sprites) throws Exception {
  28.     super(pixelLeftX, pixelBottomY, sprites);
  29.     if(sprites.length!=2) {throw new Exception("Invalid sprites");}
  30.     startMoveLeft();
  31. }
  32.  
  33.     /*
  34.      ************
  35.      * Movement *
  36.      ************
  37.      */
  38.     @Override
  39.     public void startMoveRight(){
  40.         setOrientation(1);
  41.         setVelocity(0.5, 0);}
  42.    
  43.     @Override
  44.     public void startMoveLeft() {
  45.         setOrientation(-1);
  46.         setVelocity(-0.5, 0);}
  47.  
  48.     /**
  49.      *
  50.      * @return  boolean for if plant is moving left or not.
  51.      *      |if (getPlantVelocity()[0]< 0) {return true;} else {return false;}
  52.      */
  53.     public boolean IsMovingLeftPlant() {
  54.         if (getVelocity()[0]< 0) {return true;} else {return false;}}
  55.    
  56.     /**
  57.      *
  58.      * @return a boolean for if plant is moving right or not.
  59.      *      | if(getPlantVelocity()>0) {return true;} else {return false;}
  60.      */
  61.     public boolean IsMovingRightPlant() {
  62.         if(getVelocity()[0]>0) {return true;} else {return false;}}
  63.    
  64.     public void HorizontalMovement(double dt) {
  65.         double posx=getActualPosition()[0]+(getVelocity()[0]*dt);
  66.         setActualPosition(posx, getActualPosition()[0]);
  67.     }
  68.    
  69.     /*
  70.      *********************
  71.      * Sprites of plants *
  72.      *********************
  73.      */
  74.     /**
  75.      * @return the current sprite of the plant.
  76.      *      |result=SPRITES
  77.      * @post...
  78.      *          | if(IsMovingLeftPlant()==true){SPRITES[0]=currentSpritePlant[0];}
  79.      * @post...
  80.      *          | if(IsMovingRightPlant()==true){SPRITES[0]=currentSpritePlant[1];}
  81.      */
  82.     @Override
  83.     public Sprite getCurrentSprite() {
  84.          if(getOrientation()==1){currentSprite=getSprites()[1];}
  85.          if(getOrientation()==-1){currentSprite=getSprites()[0];}
  86.          return currentSprite;
  87.     }
  88.    
  89.     /*
  90.      ***************
  91.      *AdvencedTime *
  92.      ***************
  93.      */
  94.    public BigDecimal time= BigDecimal.valueOf(0.000000);
  95.     /*
  96.      * Advance the state of the given game object by the given time period.
  97.      */
  98.     @Override
  99.     public void advanceTime(double dt) {
  100.         if(time.doubleValue()>=0.5) {
  101.         if (IsMovingLeftPlant()) {startMoveRight();time = BigDecimal.valueOf(0);}
  102.         if (IsMovingRightPlant()) {startMoveLeft();time = BigDecimal.valueOf(0);}}
  103.         else {time.add(BigDecimal.valueOf(dt));}
  104.         running(dt);
  105.         if(dt>=10.0) {super.setHitPoint(0);}
  106.            
  107.         }
  108.  
  109.     public void running(double time) {
  110.         BigDecimal newpos = BigDecimal.valueOf(getActualPosition()[0]);
  111.        
  112.         setActualPosition(newpos.add(BigDecimal.valueOf(getVelocity()[0]).multiply(BigDecimal.valueOf(time))).doubleValue(), getActualPosition()[1]);}
  113.        
  114.        
  115.    
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement