Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jumpingalien.model;
- import jumpingalien.util.ModelException;
- import jumpingalien.util.Sprite;
- import static org.junit.Assert.assertNotEquals;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import be.kuleuven.cs.som.annotate.*;
- public class Mazub{
- /*
- **************
- * Game World *
- **************
- */
- private double World_Width= 10.24;
- private double World_Height= 7.68;
- /*
- ************
- * Position *
- ************
- */
- //Variables
- private double[] ActualPositon;
- private int[] PixelPosition;
- //facade
- /**
- * A method to get the Actual position
- * @return
- * |result==ActualPosition
- */
- public double[] getActualPosition() {
- return ActualPositon;
- }
- /**
- * A method to change the actual position
- * @param newPosition
- * @throws Exception
- * if the new positions are negative .
- * |if(newPosition[1] <0 || newPosition[0] <0)
- * |then (throw new Exception("A or both positions are negative"))
- * @throws Exception
- * if the new positions are bigger than the world width or height
- * |if(newPosition[0]>World_Width || newPosition[1]> World_Height)
- * | then(throw new Exception("A or both positions is bigger than world dimentions"))
- * @throws Exception
- * if the new positions are not numbers.
- * |if(Double.isNaN(newPosition[0]) ||Double.isNaN(newPosition[0]))
- * |then (throw new Exception("Invalid input type"))
- * @throws Exception
- * if the array given as parameter does not have two elements.
- * |if(newPosition.length != 2)
- * |then (throw new Exception("Invalid size of Position"))
- * @post the new actual position is equal to newPosition
- * new.getActualPosition=newPosition
- */
- public void changeActualPosition(double[] newPosition) throws Exception {
- if(newPosition[1] <0 || newPosition[0] <0) {throw new Exception("A or both positions are negative");}
- if(newPosition[0]>World_Width || newPosition[1]> World_Height) {throw new Exception("A or both positions is bigger than world dimentions");}
- else if(Double.isNaN(newPosition[0]) ||Double.isNaN(newPosition[0])) {throw new Exception("Invalid input type");}
- else if(newPosition.length != 2) {throw new Exception("Invalid size of Position");}
- else{this.ActualPositon = newPosition;}
- }
- // Helper classes
- /**
- * A method to get the pixel positions
- * @return
- * |newPixelPosition
- *
- */
- public int[] getPixelPosition() {
- int[] newPixelPosition= {(int) (getActualPosition()[0]*100),(int) (getActualPosition()[1]*100)};
- return newPixelPosition ;}
- /**
- * Method to set the actual position
- * @param x
- * @param y
- * @throws Exception
- * if the new position values are less than zero
- * |if(x <0 || y <0)
- * |then(throw new Exception("A or both positions are negative"))
- * @throws Exception
- * if the new positin values are no numbers
- * |if(Double.isNaN(x) ||Double.isNaN(y))
- * |then(throw new Exception("Invalid input type"))
- */
- public void setActualPositon(double x, double y) throws Exception {
- if(x <0 || y <0) {throw new Exception("A or both positions are negative");}
- else if(Double.isNaN(x) ||Double.isNaN(y)) {throw new Exception("Invalid input type");}
- else{
- double[] actualPositon= {x,y};
- ActualPositon = actualPositon;}
- }
- /**
- * Method to set pixselPosition
- * @param x
- * @param y
- * @throws Exception
- * if the new position values are less than zero
- * |if(x <0 || y <0)
- * |then(throw new Exception("A or both positions are negative"))
- * @post the new pixel position is set
- * |new.getPixelPosition=newPosition
- *
- */
- public void setPixelPosition(int x, int y) throws Exception {
- if(x <0 || y <0) {throw new Exception("A or both positions are negative");}
- else{
- int[] newPositon= {x,y};
- PixelPosition = newPositon;}
- }
- //helper methods
- /*
- ***********
- * Mazub *
- ***********
- */
- public Mazub(int pixelLeftX, int pixelBottomY, Sprite... sprites) throws Exception {
- setActualPositon((double)pixelLeftX/100, (double)pixelBottomY/100);
- setAllSprites(sprites);
- }
- /*
- **************
- * Orientation*
- **************
- */
- //variable
- private int Orientation=0;
- //facade
- /**
- * Returns the orientation
- * @return the orientation
- * |result==Orientation
- */
- public int getOrientation() {
- return Orientation;
- }
- //helper methods
- /**
- * Sets the orientation
- * @post
- * the new orientation is set
- * |new.getOrientation==currentOrientation
- * @param currentOrientation
- */
- public void setOrientation(int currentOrientation) {
- this.Orientation = currentOrientation;
- }
- /*
- ************
- * Velocity *
- ************
- */
- //variable
- public double[] Velocity = {0,0};
- //variables
- private double Max_Horizontal = 3.0;
- private double Min_Horizontal = 1.0;
- //facade
- /**
- * returns the velocity
- * @return
- * |result==Velocity
- */
- public double[] getVelocity() {
- return this.Velocity;
- }
- // helpermethodes
- /**
- * Sets the velocity
- * @param x
- * @param y
- * @post
- * the new velocity is set
- * |new.getVelocity==velocity
- */
- public void setVelocity(double x, double y) {
- double[] velocity= {x,y};
- Velocity = velocity;
- }
- //helper methods
- /**
- * return the maximal horizontal velocity
- * @effect
- * if the velocity is positive
- * |if(getVelocity()[0]>0)
- * |then(result==Max_Horizontal)
- * @effect
- * if the velocity is negative
- * |if(getVelocity()[0]<0)
- * |then(result==Max_Horizontal*-1)
- * @return
- * |result==Max_Horizontal
- */
- public double getMax_Horizontal() {
- if(getVelocity()[0]>0) {return Max_Horizontal;}
- if(getVelocity()[0]<0) {return Max_Horizontal*-1;}
- else {return Max_Horizontal;}
- }
- public void setMax_Horizontal(double max_Horizontal) {
- Max_Horizontal = max_Horizontal;
- }
- /*
- ****************
- * Acceleration *
- ****************
- */
- /* Acceleration *//* not sure*/
- //variable
- public double[] Acceleration = {0,0};
- //facade
- /**
- * Return the Acceleration
- * @return
- * |result==Acceleration
- */
- public double[] getAcceleration() {
- return this.Acceleration;
- }
- //not in facade
- /**
- * Sets the acceleration
- * @param x
- * @param y
- * @post
- * the new acceleration is set
- * |new.getAcceleration==acceleration
- */
- public void setAcceleration(double x, double y) {
- double[] acceleration= {x,y};
- Acceleration = acceleration;
- }
- /*
- **********************
- * Horizontal Vertical*
- **********************
- */
- /**
- * Method that sets the right velocity and acceleration for moving mazub.
- * @param dt
- * Time to be used when changing the position of mazub.
- * @effect if mazub is ducking and moving
- * |if(isDucking()&&isMoving())
- * |then (setVelocity(getMax_Horizontal(), getVelocity()[1]))
- * @effect if the horizontal velocity and the absolute value of the horizontal velocity is less than the absolute value of the minimal horizontal speed
- * |if(Math.abs(getVelocity()[0])<Math.abs(getMax_Horizontal()))
- * |then (setVelocity( getVelocity()[0]+getAcceleration()[0]*dt, getVelocity()[1]))
- * @effect if the horizontal velocity not is equal to zero and the absolute value of the horizontal velocity is more than the absolute value of the minimal horizontal speed
- * |if(getVelocity()[0]!=0 && Math.abs(getVelocity()[0])
- * |then (setVelocity(getMax_Horizontal(), getVelocity()[1]))
- *
- */
- public void horizontalMaxChanging(double dt) {
- if(isDucking()&&isMoving()) {setVelocity(getMax_Horizontal(), getVelocity()[1]);}
- else if(Math.abs(getVelocity()[0])<Math.abs(getMax_Horizontal())){setVelocity( getVelocity()[0]+getAcceleration()[0]*dt, getVelocity()[1]);}
- if(getVelocity()[0]!=0 && Math.abs(getVelocity()[0])> Math.abs(getMax_Horizontal())) {setVelocity(getMax_Horizontal(), getVelocity()[1]);}
- }
- /*
- **********************
- * Vertical Velocity*
- **********************
- */
- /**
- * Sets the new actual position of mazub.
- * @param dt
- * Time to be used when changing the position of mazub.
- * @effect if the actual position is less than zero
- * |if(getActualPosition()[1]<0)
- * |then (falling())
- * @post the new vertical acceleration of mazub is set to 0
- * |if(getActualPosition()[1]>0)
- * |then(new.getAcceleration()[1]==-10)
- * @post The new vertical velocity of mazub is set
- * |new.getVelocity()[1]==getVelocity()[1]+getAcceleration[1]*dt
- */
- public void verticalChanging(double dt) {
- if(getActualPosition()[1]<0) {falling();}
- else if(getActualPosition()[1]>0) {setAcceleration(getAcceleration()[0],-10);}
- setVelocity(getVelocity()[0], getVelocity()[1]+getAcceleration()[1]*dt);
- }
- /*
- **********
- * Running*
- **********
- */
- /**
- * Sets the right orientation, acceleration and velocity of mazub to move to the left.
- * @throws Exception
- * if mazub is moving
- * |if(isMoving())
- * |then(throw new Exception("Already moving"))
- * @post new orientation is set to -1
- * |new.getOrientation==-1
- * @post new horizontal acceleration is set to -0.9
- * |new.getAcceleration()[0]==-0.9
- * @post new horizontal velocity is set to -1
- * |new.getVelocity()[0]==-1
- * @post Moving is set to true
- * |new.IsMoving()==true
- */
- public void startMoveLeft() throws Exception {
- if(isMoving()==false) {
- setOrientation(-1);
- setAcceleration(-0.9, getAcceleration()[1]);
- setVelocity(-1, getVelocity()[1]);
- if(isDucking()==false) {setMax_Horizontal(3.0);}
- }
- else {
- throw new Exception("Already moving");
- }
- }
- /**
- *
- * @throws Exception
- * if mazub is moving
- * |if(isMoving())
- * |then(throw new Exception("Already moving"))
- * @post new orientation is set to 1
- * |new.getOrientation==1
- * @post new horizontal acceleration is set to 0.9
- * |new.getAcceleration()[0]==0.9
- * @post new horizontal velocity is set to 1
- * |new.getVelocity()[0]==1
- * @post Moving is set to true
- * |new.IsMoving()==true
- */
- public void startMoveRight() throws Exception {
- if(isMoving()==false) {
- setOrientation(1);
- setAcceleration(0.9, getAcceleration()[1]);
- setVelocity(1, getVelocity()[1]);
- if(isDucking()==false) {setMax_Horizontal(3.0);}
- }
- else {throw new Exception("Already moving");}
- }
- /**
- * End the horizontal movement of mazub.
- * @throws Exception
- * if mazub is alreadi moving
- * |if(!isMoving())
- * the(throw new Exception("Not moving"))
- * @post The current horizontal acceleration is equal to 0
- * | new.getAcceleration()[0]==0
- * @post The horizontal velocity is equal to 0
- * | new.getVelocity()[0]==0
- */
- public void endMove() throws Exception {
- if(isMoving()==true) {setAcceleration(0, getAcceleration()[1]);setVelocity(0, getVelocity()[1]);}
- else {throw new Exception("Not moving");}
- }
- /**
- *
- * @return the value of the variable Moving
- * if mazub is moving
- * | if(getVelocity()[0]!=0)
- * |then (return==true;)
- * |else (return false;)
- *
- */
- public boolean isMoving() {
- if(getVelocity()[0]!=0) {return true;}
- else {return false;}
- }
- /**
- * Sets the new actual position of mazub
- * @param time
- * time to us to calculate the new position
- * @throws Exception
- * @effect if the horizontal actual position is bigger tha zero but less than the world width
- * | if(getActualPosition()[0] >= 0 && getActualPosition()[0] <= World_Width)
- * |then(double posx=getActualPosition()[0]+getVelocity()[0]*time+getAcceleration()[0]*0.5*time*time;
- * |if(posx <0)
- * |then(posx=0)
- * |if(posx >World_Width)
- * |then(posx=World_Width)
- * |new.getActualPosition[0]==posx)
- *
- */
- public void running(double time) throws Exception {
- if(getActualPosition()[0] >= 0 && getActualPosition()[0] <= World_Width) {
- double posx=getActualPosition()[0]+getVelocity()[0]*time+getAcceleration()[0]*0.5*time*time;
- if(posx <0) {posx=0;}
- if(posx >World_Width) {posx=World_Width;}
- setActualPositon(posx, getActualPosition()[1]);
- }
- }
- /*
- *********************
- * Jumping & Falling *
- *********************
- */
- /**
- * Is true if mazub is jumping. Else is false
- */
- public boolean jumping;
- /**
- * The acceleration of mazub while jumping
- */
- public double VELOCITY_WHILE_JUMPING = 8.0;
- /**
- * The acceleration of mazub while falling
- */
- public double ACCELERATION_WHILE_FALLING = -10.0;
- /**
- * Innitiates jumping
- * @throws Exception
- * if mazub is already jumping
- * |if(isJumping())
- * | then(throw new Exception("Already Jumping"))
- * @post
- * new vertical velocity and acceleration are set
- * |new.getVelocity[1]==VELOCITY_WHILE_JUMPING
- * |new.getAcceleration[1]==ACCELERATION_WHILE_FALLING
- */
- public void startJump() throws Exception {
- if(isJumping()==false) {
- setVelocity(getVelocity()[0], VELOCITY_WHILE_JUMPING);
- setAcceleration(getAcceleration()[0], ACCELERATION_WHILE_FALLING);
- }
- else {
- throw new Exception("Already Jumping");
- }
- }
- /**
- * ends the jump
- * @throws Exception
- * @effect
- * if the vertical velocity is over 0
- * |if(getVelocity()[1]>0.0)
- * |then(setVelocity(getVelocity()[0], 0);falling())
- */
- public void endJump() throws Exception {
- if(getVelocity()[1]==0) {throw new Exception("");}
- if(getVelocity()[1]>0.0) {setVelocity(getVelocity()[0], 0);falling();}
- }
- /**
- * @post the vertical acceleration is set
- * |new.getAcceleration[1]==ACCELERATION_WHILE_FALLING
- */
- public void falling() {
- setAcceleration(getAcceleration()[0], ACCELERATION_WHILE_FALLING);
- }
- /**
- * Return is mazub is jumping or not
- * @return
- * | if(getVelocity()[1]>0)
- * |result==true
- * |else
- * |result==false
- */
- public boolean isJumping() {
- if(getVelocity()[1]>0) {return true;}
- else{return false;}
- }
- /**
- * Checks whether the given time is a valid time for any Mazub.
- * @param time
- * The time to check.
- * @return
- * | if (time == 0.2)
- * |then (return true)
- * |else (result== false)
- */
- public static boolean isValidtime(double time) {
- if (time == 0.2) {return true;}
- else {return false;}
- }
- /**
- * Innitiates Jumping
- * @param time
- * @throws Exception
- * @effect
- * |if(getActualPosition()[1] >= 0 && getActualPosition()[1] <=World_Height)
- * |then( double posy= getActualPosition()[1]+getVelocity()[1]*time+getAcceleration()[1]*0.5*time*time;
- * | if(posy <0) {posy=0;}if(posy >World_Height)
- * |then(posy=World_Height)
- * |new.getActualPosition[1]==posy)
- *
- */
- public void jumping(double time) throws Exception{
- if(getActualPosition()[1] >= 0 && getActualPosition()[1] <=World_Height) {
- double posy= getActualPosition()[1]+getVelocity()[1]*time+getAcceleration()[1]*0.5*time*time;
- if(posy <0) {posy=0;}if(posy >World_Height) {posy=World_Height;}
- setActualPositon(getActualPosition()[0], posy);
- }
- }
- /*
- ***********
- * Ducking *
- ***********
- */
- /**
- * Innitiates ducking
- * @throws Exception
- * @post
- * |new.getMax_Horizontal==1.0
- * @effect is mazub is moving
- * |if(isMoving())
- * |then(setVelocity(getMax_Horizontal(), getVelocity()[1]))
- */
- public void startDuck() throws Exception {
- setMax_Horizontal(1.0);
- if(isMoving()) {setVelocity(getMax_Horizontal(), getVelocity()[1]);}
- }
- /**
- * ends ducking
- * @throws Exception
- * @post
- * |new.getMax_Horizontal==3.0
- */
- public void endDuck() throws Exception {setMax_Horizontal(3.0);}
- /**
- *
- * @return true is mazub is ducking. False if not
- * |if(Ducking)
- * |(result==true)
- * |else (result == false)
- */
- public boolean isDucking() {
- if(Math.abs(getMax_Horizontal())==1.0) {return true;}
- else {return false;}
- }
- /*
- ****************
- * AdvancedTime *
- ****************
- */
- //Time
- public double aFitfhOFASecond = 0;
- public double milisecond= 0;
- public double HitTime=0;
- public boolean test;
- public double lessThenaSecond=0.000;
- public void advanceTime(double time) throws Exception {
- if(Double.isNaN(time)) {time=0;}
- if(getActualPosition()[1]>0) {falling();}
- running(time);
- jumping(time);
- horizontalMaxChanging(time);
- verticalChanging(time);
- if(lessThenaSecond>=1.00 && isMoving()==false) {setOrientation(0);lessThenaSecond =0;}
- if(isMoving()==false) {lessThenaSecond +=time;}
- if (isMoving() && milisecond >= 0.075) {setImagCount(getImagCount()+ 1); milisecond -= 0.075;}
- else {milisecond += time;}
- if(isMoving() == false) {setImagCount(0);}
- }
- /*
- ****************
- * Sprites *
- ****************
- */
- /**
- * Used to know what index of Sprites to use next while mazub is running
- */
- public int imagCount;
- private Sprite[] Allsprites;
- /**
- * Sets the image Count of the sprites for Mazub
- * @param imagCount
- * @post ..
- * |new.imagCount = imagCount
- */
- public void setImagCount(int imagCount){
- this.imagCount = imagCount;}
- /**
- * Gets the image Count of the sprites for Mazub
- * @return...
- * |result==imagCount
- */
- public int getImagCount() {return imagCount;}
- public Sprite sprites;
- /**
- * Gets the current sprite for Mazub while it is moving.
- * @effect...
- * |if((isDucking() == false)&&(getOrientation() ==0) && (getOrientation() == 0)) {return getSprites()[0];}
- * |else if((isDucking() == true) && (getOrientation()==0) &&(getOrientation() == 0)) {return getSprites()[1];
- * |else if((getOrientation() > 0) && (Moving == false) && (isDucking() == false)) {return getSprites()[2];}
- * |else if((getOrientation() < 0) && (Moving == false)&& (isDucking() == false)) {return getSprites()[3];}
- * |else if((getOrientation() == 1) && (isDucking() ==false) && (isJumping() == true)) {return getSprites()[4];}
- * |else if((getOrientation() ==-1) && (isDucking() == false)&& (isJumping() == true)) {return getSprites()[5];}
- * |else if((getOrientation() ==1|| getOrientation() == 1) && (isDucking() == true)) {return getSprites()[6];}
- * |else if((getOrientation() == -1|| getOrientation() == -1) &&(isDucking() ==true)) {return getSprites()[7];}
- * |else if((getOrientation() == 1) && (isDucking() == false)&& (isJumping() == false)) {return getSprites()[8+setImages()];}
- * |else if((getOrientation() == -1) && (isDucking()== false)&& (isJumping() ==false)) {return getSprites()[((SPRITES.length-8)/2+8)+(setImages())];}
- * |else {return getSprites()[1];}
- * @return
- * |result==getSprites()
- */
- public Sprite getCurrentSprite() {
- //0 is not moving horizontally, has not moved horizontally within the last second of in-game time and is not ducking.
- if(getOrientation()==0 && (isDucking() == false) && (getVelocity()[0]==0)) {sprites= getSprites()[0];}
- //1 is not moving horizontally, has not moved horizontally within the last second of in-game time and is ducking.
- else if ((isDucking() == true) && (getOrientation()==0) && (getVelocity()[0])==0) {sprites =getSprites()[1];}
- //2 is not moving horizontally but its last horizontal movement was to the right (within 1s), and the character is not ducking.
- //Looking at right
- else if (getOrientation()==1 && isMoving()==false && (isDucking() == false)) {sprites= getSprites()[2];}
- //3 is not moving horizontally but its last horizontal movement was to the left (within 1s), and the character is not ducking.
- //Looking at left
- else if (getOrientation()==-1 && (isMoving()==false) && (isDucking() == false)) {sprites= getSprites()[3];}
- //4 is moving to the right and jumping and not ducking.
- //Jumping right
- else if (getVelocity()[0]>0 && (isDucking() == false) && (isJumping() == true)) {sprites= getSprites()[4];}
- //5 is moving to the left and jumping and not ducking.
- else if (getVelocity()[0]<0 && (isDucking() == false) && (isJumping() == true)) {sprites= getSprites()[5];}
- //8..(8 + m) the character is neither ducking nor jumping and moving to the right
- else if (getVelocity()[0]>0 && (isDucking() == false) && (isJumping() == false)) {sprites= getSprites()[8+setImages()];}
- //(9 + m)..(9 + 2m) the character is neither ducking nor jumping and moving to the left.
- else if (getVelocity()[0]<0 && (isDucking()== false) && (isJumping() ==false)) {sprites= getSprites()[((getSprites().length-8)/2+8)+(setImages())];}
- //6 is ducking and moving to the right or was moving to the right (within 1s)
- else if ((isDucking() == true) && getVelocity()[0]>0||getOrientation()>0) {sprites= getSprites()[6];}
- //7 is ducking and moving to the left or was moving to the left (within 1s).
- else if (getVelocity()[0]<0||getOrientation()<0 &&(isDucking() ==true)) {sprites= getSprites()[7];}
- return sprites;
- // else {return getSprites()[0];}
- }
- /**
- * Sets the images of Mazub
- * @post...
- * |new.length=(getSprites().length-8)/2;
- * |new.m1 = getImagCount();
- * @return ...
- * |result==m1
- * @effect if the imagecount exceeds the length of the sprites array
- * |if(getImagCount() >= length)
- * |then (setImagCount(0))
- */
- public int setImages() {
- int length=(getSprites().length-8)/2;
- if(getImagCount() >= length) {setImagCount(0);}
- int m1 = getImagCount();
- return m1;
- }
- private void setAllSprites(Sprite[] sprites) throws Exception {
- if(sprites.length <10 ||sprites.length%2 ==1) {throw new Exception("Invalid sprites");}
- for (int i = 0; i < sprites.length; i++) {
- if(sprites[i]==null || sprites[i] instanceof Sprite==false) {
- throw new Exception("Illegal type sprites");}
- }
- this.Allsprites = sprites.clone();
- }
- /**
- * return the sprites
- * @return
- * |result==Allsprites.clone()
- */
- public Sprite[] getSprites() {
- return this.Allsprites.clone();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement