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 *
- **************test
- */
- private double World_Width= 1024/100;
- private double World_Height=768/100;
- /*
- ************
- * Position *
- ************
- */
- //Variables
- private double[] ActualPositon;
- private int[] PixelPosition;
- //facade
- public double[] getActualPosition() {
- return ActualPositon;
- }
- public void changeActualPosition(double[] newPosition) throws Exception {
- if(newPosition[1] <0 || newPosition[0] <0) {throw new Exception("A or both positions are negative");}
- if(Double.isNaN(newPosition[0]) ||Double.isNaN(newPosition[1])) {throw new Exception("Invalid input type");}
- else if(newPosition.length != 2) {throw new Exception("Invalid size of Position");}
- this.ActualPositon = newPosition;
- }
- // Helper classes
- public int[] getPixelPosition() {
- int[] newPixelPosition= {(int) (getActualPosition()[0]*100),(int) (getActualPosition()[1]*100)};
- return newPixelPosition ;}
- 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");}
- double[] actualPositon= {x,y}; ActualPositon = actualPositon;
- }
- 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
- public int getOrientation() {
- return Orientation;
- }
- //helper methods
- 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
- public double[] getVelocity() {
- return this.Velocity;
- }
- // helpermethodes
- public void setVelocity(double x, double y) {
- double[] velocity= {x,y};
- Velocity = velocity;
- }
- //helper methods
- 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
- public double[] getAcceleration() {
- return this.Acceleration;
- }
- //not in facade
- 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 the absolute value of het horizontla velocity is less than the maximal horizontal velocity
- * |if(Math.abs(getVelocity()[0])<Math.abs(getMAXIMUM_HORIZONTAL_SPEED()))
- * |then (setVelocity( velocity.add(acceleration.multiply(time)).doubleValue(), getVelocity()[1]))
- * @effect if the horizontal velocity is equal to zero and the absolute value of the horizontal velocity is less than the absolute value of the maximal horizontal speed
- * |if(getVelocity()[0]!=0 && Math.abs(getVelocity()[0])> Math.abs(getMAXIMUM_HORIZONTAL_SPEED()))
- * |then (setVelocity(getMAXIMUM_HORIZONTAL_SPEED(), getVelocity()[1]))
- * @effect if the horizontal velocity is equal to zero and the absolute value of the horizontal velocity is less than the absolute value of the minimal horizontal speed
- * |if(getVelocity()[0]!=0 && Math.abs(getVelocity()[0])<Math.abs(getMINIMAL_HORIZONTAL_SPEED()))
- * |then (setVelocity(getMINIMAL_HORIZONTAL_SPEED(), getVelocity()[1]);)
- *
- */
- public void horizontalMaxChanging(double dt) {
- 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 Vertical*
- **********************
- */
- /**
- * Sets the new actual position of mazub.
- * @param dt
- * Time to be used when changing the position of mazub.
- * @effect if the world is not equal to null and the current actual position of mazub is less than zero
- * |if(getWorld() != null && getActualPosition()[1]<=0)
- * |then (setAcceleration(getAcceleration()[0],0);)
- * @post the new vertical acceleration of mazub is set to 0
- * |new.getAcceleration()[1]==0;
- * @post The new vertical velocity of mazub is set to velocity.add(acceleration.multiply(time)).doubleValue()
- * |new.getVelocity()[1]==velocity.add(acceleration.multiply(time)).doubleValue();
- */
- public void verticalChanging(double dt) {
- setVelocity(getVelocity()[0], getVelocity()[1]+getAcceleration()[1]*dt);
- if(getActualPosition()[1]<=0) {setAcceleration(getAcceleration()[0],0);}
- }
- /*
- **********
- * Running*
- **********
- */
- /**
- * Sets the right orientation, acceleration and velocity of mazub to move to the left.
- * @throws Exception
- * @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]);
- }
- else {
- throw new Exception("Already moving");
- }
- }
- public void startMoveRight() throws Exception {
- if(isMoving()==false) {
- setOrientation(1);
- setAcceleration(0.9, getAcceleration()[1]);
- setVelocity(1, getVelocity()[1]);
- }
- else {
- throw new Exception("Already moving");
- }
- }
- /**
- * Set to true if mazub is moving. Else to false
- public void startMoveRight() {
- int px=getPixelPosition()[0]; int py=getPixelPosition()[1];
- setOrientation(1);
- setAcceleration(0.9, getAcceleration()[1]);
- setVelocity(1, getVelocity()[1]);
- /**
- * End the horizontal movement of mazub.
- * @throws Exception
- * @post The current orientation is equal to 0
- * | new.getOrientation() == 0
- * @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
- * | result == if(Moving)
- * |then (return true;)
- * |else (return false;)
- *
- */
- public boolean isMoving() {
- if(getVelocity()[0]!=0) {return true;}
- else {return false;}
- }
- /**
- * Initialize this new Mazub with given time.
- *
- * @param time
- * The time for this new Mazub.
- * @pre This new Mazub can have the given time as its time.
- * | canHaveAstime(time)
- * @post The time of this new Mazub is equal to the given
- * time.
- * | new.gettime() == time
- */
- /**
- * 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 equal or greater than zero and is smaller or equal to the world world dimentions
- * |if(getActualPosition()[0] >= 0 && getActualPosition()[0] <= getDimentions()[0])
- * |then(double posx=
- * BigDecimal.valueOf(actualPosition[0])
- * .add (BigDecimal.valueOf(Velocity[0]).multiply(BigDecimal.valueOf(time)))
- * .add(BigDecimal.valueOf(getAcceleration()[0]).multiply(BigDecimal.valueOf(0.5))
- * .multiply(BigDecimal.valueOf(Math.pow(time, 2)))).doubleValue();)
- * @effect if the horizontal actual postion is greater than the horizontal dimention of the world
- * |if(getActualPosition()[0]>getDimentions()[0])
- * | then setActualPosition(getDimentions()[0],getActualPosition()[1])
- * @effect if the horizontal actual postion is less than the horizontal dimention of the world
- * |if(getActualPosition()[0]< 0)
- * |then (setHitPoint(0);)
- *
- */
- 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;
- 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 ACCELERATION_WHILE_JUMPING = 8.0;
- /**
- * The acceleration of mazub while falling
- */
- public double ACCELERATION_WHILE_FALLING = -10.0;
- /**
- * Set the vertical velocity of this object_name to the given vertical velocity.
- * @throws Exception
- *
- * @pre The given vertical velocity must be a valid vertical velocity for any
- * Mazub.
- * | isValidVERTICAL_VELOCITY(setVetical_VELOCITY(ACCELERATION_WHILE_JUMPINT))
- * @post The vertical velocity of this Mazub is equal to the given
- * vertical velocity.
- * | new.getVERTICAL_VELOCITY() == setVetical_VELOCITY(ACCELERATION_WHILE_JUMPINT)
- * @post The vertical acceleration of this Mazub is equal to -10.
- * | new.getAcceleration()[1] == setVetical_ACCELERATION(-10)
- * @post
- * |new.IsJumping()==true
- *
- */
- public void startJump() throws Exception {
- if(isJumping()==false) {
- setVelocity(getVelocity()[0], ACCELERATION_WHILE_JUMPING);
- setAcceleration(getAcceleration()[0], ACCELERATION_WHILE_FALLING);
- }
- else {
- throw new Exception("Already Jumping");
- }
- }
- public void endJump() throws Exception {
- if(isJumping()) {
- falling();
- if(getVelocity()[1]>0) {setVelocity(getVelocity()[0],0);}
- }
- else {
- throw new Exception("Not Jumping");
- }
- }
- public void falling() {
- setAcceleration(getAcceleration()[0], ACCELERATION_WHILE_FALLING);
- }
- public boolean isJumping() {
- if(getVelocity()[1]!=0) {return true;}
- else{return false;}
- }
- /**
- * Check 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;}
- }
- 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-1;}
- setActualPositon(getActualPosition()[0], posy);
- }
- if(getActualPosition()[1]<=0) {setAcceleration(getAcceleration()[0],0);setVelocity(getVelocity()[0], 0);}
- }
- /*
- ***********
- * Ducking *
- ***********
- */
- public void startDuck() throws Exception {
- setMax_Horizontal(1.0);setAcceleration(0, getAcceleration()[1]);
- }
- 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;}
- 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 *
- ****************
- */
- int[] SpriteDimentions=new int[2];
- /**
- * returns the dimention of the current sprites
- * @effect..
- * new.getSpriteDimentions[0]==70
- * If is not ducking, not moving and not jumping or is jumping or moving
- * |if(!(isMoving()) && !(isDucking()) && !(isJumping()) || (isJumping()|| isMoving()))
- * |then (this.SpriteDimentions[1]=97;)
- * If mazub is ducking
- * |if(isDucking())
- * |then (this.SpriteDimentions[1]=70;)
- * @return..
- * |result==SpritDimentions
- */
- public int[] getSpriteDimention() {
- if(!(isMoving()) && !(isDucking()) && !(isJumping()) || (isJumping()|| isMoving())) {this.SpriteDimentions[1]=97;}
- if(isDucking()) {this.SpriteDimentions[1]=70;}
- SpriteDimentions[0]=70;
- return SpriteDimentions;
- }
- /**
- * Used to know what index of Sprites to use next while mazub is running
- */
- public int imagCount;
- private Sprite[] Allsprites;
- // /**
- // * Sets the sprites for Mazub
- // * @param sPRITES
- // * @post ...
- // * |SPRITES = sPRITES
- // * @throws IllegalArgumentExecption if array of sprytes is empty.
- // * |if(sPRITES==null) {throw new IllegalArgumentException();}
- // */
- /**
- * 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;}
- /**
- * 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() {
- /*
- * if(isMoving()==false) { if(isDucking()) {} if(isDucking()==false) { if
- * (getOrientation()==0) {} if (getOrientation()==1) {} if(getOrientation()==-1)
- * {} }
- *
- * } if (isMoving()==true) { if(isJumping()) {
- *
- * } if(isJumping()==false) {
- *
- * } } else {
- *
- *
- * }
- */
- //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)) {return 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) {return 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)) {return 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)) {return getSprites()[3];}
- //4 is moving to the right and jumping and not ducking.
- //Jumping right
- else if (getVelocity()[0]>0 && (isDucking() ==false) && (isJumping() == true)) {return getSprites()[4];}
- //5 is moving to the left and jumping and not ducking.
- else if (getVelocity()[0]<0 && (isDucking() == false)&& (isJumping() == true)) {return 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)) {return 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)) {return 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 ) {return 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)) {return getSprites()[7];}
- 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();
- }
- //helper methods
- public Sprite[] getSprites() {
- return this.Allsprites.clone();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement