Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jumpingalien.model;
- import java.math.BigDecimal;
- import jumpingalien.util.Sprite;
- /**
- * A class for creating the plants in the game and getting the imageSprites and coordinates.
- * Plants can move from left to right, or from right to left.
- * After leaving the visible game world, the plants will be terminated.
- * Plants are food to be eaten by Mazub.
- *
- * @version 2.0
- * @author Thierry Klougbo & Jordi De Pau
- */
- public class Plant extends Entity{
- /**
- *Create an instance of Plant with given pixel position and given sprites.
- *The actual position of the new plant will correspond with the coordinates
- *of the left-bottom corner of the left-bottom pixel occupied by the new plant.
- *
- * @param pixelLeftX
- * @param pixelBottomY
- * @param sprites
- */
- public Plant(int pixelLeftX, int pixelBottomY, Sprite... sprites) throws Exception {
- super(pixelLeftX, pixelBottomY, sprites);
- if(sprites.length!=2) {throw new Exception("Invalid sprites");}
- startMoveLeft();
- }
- /*
- ************
- * Movement *
- ************
- */
- @Override
- public void startMoveRight(){
- setOrientation(1);
- setVelocity(0.5, 0);}
- @Override
- public void startMoveLeft() {
- setOrientation(-1);
- setVelocity(-0.5, 0);}
- /**
- *
- * @return boolean for if plant is moving left or not.
- * |if (getPlantVelocity()[0]< 0) {return true;} else {return false;}
- */
- public boolean IsMovingLeftPlant() {
- if (getVelocity()[0]< 0) {return true;} else {return false;}}
- /**
- *
- * @return a boolean for if plant is moving right or not.
- * | if(getPlantVelocity()>0) {return true;} else {return false;}
- */
- public boolean IsMovingRightPlant() {
- if(getVelocity()[0]>0) {return true;} else {return false;}}
- public void HorizontalMovement(double dt) {
- double posx=getActualPosition()[0]+(getVelocity()[0]*dt);
- setActualPosition(posx, getActualPosition()[0]);
- }
- /*
- *********************
- * Sprites of plants *
- *********************
- */
- /**
- * @return the current sprite of the plant.
- * |result=SPRITES
- * @post...
- * | if(IsMovingLeftPlant()==true){SPRITES[0]=currentSpritePlant[0];}
- * @post...
- * | if(IsMovingRightPlant()==true){SPRITES[0]=currentSpritePlant[1];}
- */
- @Override
- public Sprite getCurrentSprite() {
- if(getOrientation()==1){currentSprite=getSprites()[1];}
- if(getOrientation()==-1){currentSprite=getSprites()[0];}
- return currentSprite;
- }
- /*
- ***************
- *AdvencedTime *
- ***************
- */
- public BigDecimal time= BigDecimal.valueOf(0.000000);
- /*
- * Advance the state of the given game object by the given time period.
- */
- @Override
- public void advanceTime(double dt) {
- if(time.doubleValue()>=0.5) {
- if (IsMovingLeftPlant()) {startMoveRight();time = BigDecimal.valueOf(0);}
- if (IsMovingRightPlant()) {startMoveLeft();time = BigDecimal.valueOf(0);}}
- else {time.add(BigDecimal.valueOf(dt));}
- running(dt);
- if(dt>=10.0) {super.setHitPoint(0);}
- }
- public void running(double time) {
- BigDecimal newpos = BigDecimal.valueOf(getActualPosition()[0]);
- setActualPosition(newpos.add(BigDecimal.valueOf(getVelocity()[0]).multiply(BigDecimal.valueOf(time))).doubleValue(), getActualPosition()[1]);}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement