Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jumpingalien.model;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.ArrayList;
- import java.util.Currency;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.Timer;
- import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
- import org.junit.FixMethodOrder;
- //import com.sun.javafx.scene.traversal.WeightedClosestCorner;
- import be.kuleuven.cs.som.annotate.Raw;
- import jdk.nashorn.internal.runtime.ECMAException;
- import jumpingalien.facade.Facade;
- import jumpingalien.model.Mazub;
- import jumpingalien.util.ModelException;
- import sun.net.www.content.audio.x_aiff;
- /**
- * A class for creating the game world, get the images and coordinates, and make the world change depending on the pixelposition of Mazub
- * and the advance time.
- * The world has passable and impassable terains.
- *
- * @version 2.0
- * @author Thierry Klougbo & Jordi De Pau
- */
- public class World {
- // Precision constants for checks involving floating point numbers.
- public final static double HIGH_PRECISION = 0.1E-10;
- public final static double LOW_PRECISION = 0.01;
- public int VisibleWindowX, VisibleWindowY, TileLength ,nmbTilesX, nmbTilesY;
- /**
- *
- * @param pixelSize
- * The actual size of the pixels.
- * @param nbTilesX
- * The number of tiles in the visible window of the world's x-axis.
- * @param nbTilesY
- * The number of tiles in the visible window of the world's y-axis.
- * @param targetTileCoordinate
- * @param visibleWindowWidth
- * The actual width of the visible window.
- * @param visibleWindowHeight
- * The actual height of the visible window.
- * @param geologicalFeatures
- * An integer for representing every geological feature:
- * AIR = 0;
- * SOLID_GRO;UND = 1;
- * WATER = 2;
- * MAGMA = 3;
- * GAS = 4;.
- * ICE = 5;
- */
- public World(int pixelSize, int nbTilesX, int nbTilesY, int[] targetTileCoordinate, int visibleWindowWidth, int visibleWindowHeight, int...geologicalFeatures) throws Exception{
- setTileLength(pixelSize);
- setSizeInPixels(nbTilesX*pixelSize,nbTilesY*pixelSize);
- setVisibleWindowDimension(visibleWindowWidth,visibleWindowHeight);
- setTargetTileCoordinate(targetTileCoordinate);
- setGeologicalFeaturesSize(nbTilesX, nbTilesY);
- createGeologicalFeatures(nbTilesX, nbTilesY, geologicalFeatures);}
- /**
- * Sets the Tile Length to the given pixelsize.
- *
- * @param pixelSize
- * given size of the length of a Tile
- * @effect if the given pixelsize is larger then zero the tile length will be set to the given pixelsize
- * | if(pixelSize >0)
- * | then TileLength = pixelSize
- * @effect else the given pixelsize is lower or equal then/to zero the tile length will be set to 1
- * |TileLenght = 1
- *
- */
- private void setTileLength(int pixelSize) {
- if(pixelSize >0) {TileLength=pixelSize;}
- else {TileLength=1;}
- }
- /*
- *******************************
- * *Tiles Total programming. *
- * *****************************
- */
- /**
- * SizeInPixels= Pixel size of the world with an array of 2.
- */
- public int [] SizeInPixels= new int[2] ;
- /**
- * Returns the size of the game world in pixels.
- *
- * @post SizeInPixels contains respectively the visible window length and the
- * visible window height.
- * | SizeInPixels[0]=VisibleWindowX; SizeInPixels[1]=VisibleWindowY;
- * @return SizeInPixels
- */
- public int[] getSizeInPixels() {return SizeInPixels;}
- /**
- * sets the given width and Height inside an integer array of index 2 with name SizeInPixels
- *
- * @param Width
- * given width of the given world in integers.
- * @param Height
- * given height of the given world in integers.
- * @effect
- * If the width is lower then zero the will be zero
- * | if(Width<0)
- * | then SizeInPixels[0]=0
- * @effect
- * If the width is equal or higher then zero this SizeinPixels horizontal will be set to the given width
- * |SizeInPixels[0]=Width
- * @effect
- * If the Height is lower then zero the will be zero
- * | if(Width<0)
- * | then SizeInPixels[1]=0
- * @effect
- * If the Height is equal or higher then zero this SizeinPixels horizontal will be set to the given width
- * |SizeInPixels[1]=Height
- *
- */
- public void setSizeInPixels(int Width, int Height) {
- if(Width<0) {SizeInPixels[0]=0;}
- else {SizeInPixels[0] =Width;}
- if(Height<0){SizeInPixels[1] = 0;}
- else{SizeInPixels[1] =Height;}}
- /**
- * @return the length of a tile
- * |return TileLength
- */
- public int getTileLength(){return TileLength;}
- /*
- * ***********************
- * Geological features *
- * ***********************/
- /**
- * Is an Integer array of the world's geologicalFeatures in Tile's
- */
- public int[][] GeologicalFeaturesCanvas;
- /**
- * Sets the GeologicalCanvas with coloms and rows
- *
- * @param GeoRow
- * Is the value of how many rows there has to be in GeologicalFeatures
- * @param GeoCol
- * Is the value of how many Coloms there has to be in GeologicalFeatuers
- * @effect
- * sets GeologicalFeaturesCanvas with the rows and coloms.
- * |this.GeologicalFeaturesCanvas = new int[GeoRow][GeoCol]
- */
- public void setGeologicalFeaturesSize(int GeoRow,int GeoCol) {this.GeologicalFeaturesCanvas = new int[GeoRow][GeoCol];}
- /**
- * sets the geologicalfeatures of the world by putting every position with a feature
- *
- * @param X
- * a given X value that is a pixel & Integer.
- * @param Y
- * a given Y value that is a pixel & Integer
- * @param geoFeature
- * a geological Feature that is Integer
- * @effect
- * if the given x (that is calculatet to a tile) or y(that is calculatet to a tile) is outside the border the feature should be 0
- * |if(X > ((SizeInPixels[0]/this.getTileLength())-1) || Y > ((SizeInPixels[0]/this.getTileLength())-1)
- * |then this.GeologicalFeaturesCanvas[GeoX][GeoY] =0
- *@effect
- * else the given x (that is calculatet to a tile) and y(that is calculatet to a tile) is set with the given feature in geologicalCanvas
- * |this.GeologicalFeaturesCanvas[GeoX][GeoY] = geoFeature
- */
- public void setGeologicalFeatures(int X, int Y, int geoFeature) {
- int GeoX = calculateInFeature(X);
- int GeoY = calculateInFeature(Y);
- if (X > ((SizeInPixels[0]/this.getTileLength())-1) || Y > ((SizeInPixels[0]/this.getTileLength())-1) || X<0 || Y<0) {this.GeologicalFeaturesCanvas[GeoX][GeoY] =0;}
- this.GeologicalFeaturesCanvas[GeoX][GeoY] = geoFeature;
- }
- /**
- * returns the geological feature with the given x and y calculated in a tile.
- *
- * @param pixelX
- * is the value of the pixel x-value
- * @param pixelY
- * is the value of the pixel y-value
- * @return
- * if the x value or the y value is larger then the world itself it returns 0
- * |return 0
- * @return
- * returns feature of the position calculated in tile. (T
- * |GeologicalFeaturesCanvas[GeoX][GeoY]
- */
- public int getGeologicalFeatures(int pixelX, int pixelY) {
- if (pixelX >= this.getSizeInPixels()[0] || pixelY >= this.getSizeInPixels()[1]) {return 0;}
- int GeoX = calculateInFeature(pixelX);
- int GeoY = calculateInFeature(pixelY);
- return GeologicalFeaturesCanvas[GeoX][GeoY];
- }
- /**
- * @param X
- * is the given value of the pixel of x-value
- * @param Y
- * is the given value of the pixel of y-value
- * @param F
- * is the given feature
- *@effect
- * sets the positions feature
- * |this.GeologicalFeaturesCanvas[X][Y] = F
- */
- public void setFeature(int X, int Y, int F) {
- this.GeologicalFeaturesCanvas[X][Y] = F;
- }
- /**
- * calculates the pixel position into the position of the tile.
- *
- * @param aPixel
- * is the given value of a pixel
- * @effect
- * sets the variable resultpixel in the pixel divide by the tile length
- * |int resultPixel = (int)((aPixel)/getTileLength()
- * @return
- * the result of the pixel
- */
- public int calculateInFeature(int aPixel) {
- int resultPixel = (int)((aPixel)/getTileLength());
- return resultPixel;}
- /**
- * sets all the geological in canvas with the given amount tiles in x and the given amount tiles in y with the given feature in an array.
- *
- * @param nbTilesX
- * is the given amount of tiles for x value.
- * @param nbTilesY
- * is the given amount of tiles for y-value
- * @param geologicalFeatures
- * is an array of feature
- *@effect
- * if the geologicalfeature length of the array is bigger then zero sets all the features are set in a nested loop for each position
- * |if(geologicalFeatures.length>0)
- * |then for (int y= 0; y < nbTilesY; y++) {
- * | for (int x = 0; x< nbTilesX; x++) {
- * int counter =y*nbTilesX + x;
- * setFeature(x, y,geologicalFeatures[counter]);}}
- *@effect
- * if the geologicalfeature length of the array is zero sets all the features to zero in a nested loop for each position
- * |if(geologicalFeatures.length==0)
- * |then for (int y= 0; y < nbTilesY; y++) {
- * | for (int x = 0; x< nbTilesX; x++) {
- * int counter =y*nbTilesX + x;
- * setFeature(x, y,geologicalFeatures[0]);}}
- *
- */
- public void createGeologicalFeatures(int nbTilesX, int nbTilesY,int... geologicalFeatures) {
- if(geologicalFeatures.length>0) {
- for (int y= 0; y < nbTilesY; y++) {
- for (int x = 0; x< nbTilesX; x++) {
- int counter =y*nbTilesX + x;
- setFeature(x, y,geologicalFeatures[counter]);}}}
- else {for (int y= 0; y < nbTilesY; y++) {
- for (int x = 0; x< nbTilesX; x++) {
- setFeature(x, y,0);}}}
- }
- /*
- **********************************
- *Visible Window Defensive program*
- **********************************
- */
- //FIXME To do 400 pixel groter dan mazub
- //FIXME Make adaptor in advanced time
- /**
- * VisibleWindowDimension is the dimension that is visible for your screen
- */
- public int[] VisibleWindowDimension=new int[2];
- /**
- * sets the visible window with the given weight and height
- * @param weight
- * is the given weight of the window position
- * @param height
- * is the given height of the window position
- * @throws Exception
- * if the weight or the height is bigger then the dimension of the world
- * |if(weight>getSizeInPixels()[0]||height>getSizeInPixels()[1])
- * |then throw new Exception("To big window compared to actualworld");
- * @throws Exception
- * if the weight or the height is lower then the universe (= the universe is everything above 0 for both x & y)
- * |if(weight <0 && height <0)
- * |then throw new Exception("Illegal Window");
- * @effect
- * sets the given visible dimensioen with the weight and the height
- * |if(weight >0 && height >0)
- * |then weight;VisibleWindowDimension[1] = height;
- */
- public void setVisibleWindowDimension(int weight , int height) throws Exception{
- if(weight>getSizeInPixels()[0]||height>getSizeInPixels()[1]) {throw new Exception("To big window compared to actualworld");}
- else if(weight >0 && height >0) {
- VisibleWindowDimension[0] = weight;VisibleWindowDimension[1] = height;}
- else {throw new Exception("Illegal Window");}}
- /**
- * gets visible window dimensions.
- * @return
- * |result=VisibleWindowDimension
- */
- public int[] getVisibleWindowDimension() {
- return VisibleWindowDimension;
- }
- /**
- * gets the visible window dimension
- *
- * @return
- * if there isn't a mazub return a dimension with values of zero's
- *
- * @return
- * if mazub pixel position is smaller then the world size of x sets x window position to the visibleposition adaptor
- * | if(mazub.getPixelPosition()[0]<getSizeInPixels()[0]-mazub.getCurrentSprite().getWidth()&& visibleMazubCanMove(0))
- * | then VisibleWindowPosition[0]=(int)(VisiblePositionAdaptor);
- * @return
- * if mazub pixel position is smaller then the world size of y sets y window position to the visibleposition adaptor
- * | if(mazub.getPixelPosition()[1]<getSizeInPixels()[1]-mazub.getCurrentSprite().getWidth()&& visibleMazubCanMove(1))
- * | then VisibleWindowPosition[1]=(int)(VisiblePositionAdaptor);
- * @return
- * if the size is bigger then the world size x sets the windows position to max possible
- * |if(VisibleWindowPosition[0] >SizeInPixels[0]-getVisibleWindowDimension()[0])
- * |then VisibleWindowPosition[0] =SizeInPixels[0]-getVisibleWindowDimension()[0];
- * @return
- * if the size is bigger then the world size y sets the windows position to max possible
- * |if(VisibleWindowPosition[1] >SizeInPixels[1]-getVisibleWindowDimension()[1])
- * |then VisibleWindowPosition[1] =SizeInPixels[1]-getVisibleWindowDimension()[1];
- *
- */
- public int[] getVisibleWindowsPosition() {
- if(mazub == null) {VisibleWindowPosition[0] =0; VisibleWindowPosition[1]=0; return VisibleWindowPosition;}
- if(mazub.getPixelPosition()[0]<getSizeInPixels()[0]-mazub.getCurrentSprite().getWidth()&& visibleMazubCanMove(0)){
- VisibleWindowPosition[0]=(int)(VisiblePositionAdaptor);}
- if (mazub.getPixelPosition()[1]<getSizeInPixels()[1] &&visibleMazubCanMove(1)) {
- VisibleWindowPosition[1]=(int)(VisiblePositionAdaptor);}
- if(VisibleWindowPosition[0] >SizeInPixels[0]-getVisibleWindowDimension()[0]) {VisibleWindowPosition[0] =SizeInPixels[0]-getVisibleWindowDimension()[0];}
- if(VisibleWindowPosition[1] >SizeInPixels[1]-getVisibleWindowDimension()[1]) {VisibleWindowPosition[1] =SizeInPixels[1]-getVisibleWindowDimension()[1];}
- return VisibleWindowPosition;
- }
- /**
- * Is the new windows position that sometimes is the windows position or -200 of the windows position that mazub is not near the wall while moving
- */
- public int VisiblePositionAdaptor;
- /**
- * is the position of the visible window.
- */
- public int[]VisibleWindowPosition=new int[2];
- /**
- * returns if mazub its visibleposition can move but also adapts visiblepositionadaptor
- *
- * @param direction
- * is 0 or 1 that equals x or y
- * @return
- * if the mazub is not near 200 pixels near the world borders visiblePositionAdaptor will be the position minus 200 and returns true
- * |if(newPos > 200 && newPos < SizeInPixels[direction]-200 )
- * |then VisiblePositionAdaptor = newPos -200; return true;
- * @return
- * if the mazub is 200 or less pixels near the wall and returns false
- * |if(newPos <= 200 && newPos >= SizeInPixels[direction]-200 )
- * |then VisiblePositionAdaptor = 0; return false;
- */
- public boolean visibleMazubCanMove(int direction) {
- int newPos = mazub.getPixelPosition()[direction];
- if(newPos > 200 && newPos < SizeInPixels[direction]-200 ) {VisiblePositionAdaptor = newPos -200; return true;}
- else {VisiblePositionAdaptor = 0; return false;}
- }
- /**
- *sets the visibleadaptor with the given integer visiblePositionAdaptor
- *
- * @param visiblePositionAdaptor
- *@effect
- * sets the visibleadaptor with the given integer visiblePositionAdaptor
- * |VisiblePositionAdaptor = visiblePositionAdaptor;
- */
- public void setVisiblePositionAdaptor(int visiblePositionAdaptor) {
- VisiblePositionAdaptor = visiblePositionAdaptor;
- }
- /*
- ************************************
- | Target tiles Nominal programming |
- ************************************
- */
- /**
- * sets the targetTileCoordinates
- */
- public int[] targetTileCoordinates = new int[2];
- /**
- * Set the coordinate of the target tile in the given world to the given
- * tile coordinate.
- */
- public void setTargetTileCoordinate(int[] tileCoordinate) throws Exception{
- if(tileCoordinate.length == 2 || tileCoordinate ==null) {
- targetTileCoordinates = tileCoordinate;}
- else {throw new Exception("Invalid coordinates");}
- }
- /**
- * Return the coordinate of the target tile in the given world.
- * Returns an array of 2 integers {x, y} that represents the tile coordinate
- * of the target tile.
- * */
- public int[] getTargetTileCoordinate() throws ModelException{
- return targetTileCoordinates;}
- /*
- ************************************
- | Collision |
- ************************************
- */
- /**
- * returns of the the given currentEntity interactes with the given geologicalFeature
- *
- * @param currentEntity
- * is the given Entity
- * @param GeologicalFeature
- * is the given geologicalFeature
- * @return
- * if the Entity interactes with the given geologicalfeature by calculateing the corners of the given Entity returns true
- * |if(getGeologicalFeatures(X, Y)==GeologicalFeature
- * | || getGeologicalFeatures((X+SpriteX), Y + SpriteY)==GeologicalFeature
- * | || getGeologicalFeatures((X+SpriteX), Y)==GeologicalFeature
- * | || getGeologicalFeatures(X+SpriteX, Y+SpriteY)==GeologicalFeature
- * |then touch=true;
- * @return
- * if the Entity is not interactes with the given geologicalfeature by calculateing the corners of the given Entity returns false
- * |if(getGeologicalFeatures(X, Y)!=GeologicalFeature
- * | || getGeologicalFeatures((X+SpriteX), Y + SpriteY)!=GeologicalFeature
- * | || getGeologicalFeatures((X+SpriteX), Y)!=GeologicalFeature
- * | || getGeologicalFeatures(X+SpriteX, Y+SpriteY)!=GeologicalFeature
- * |then touch=false;
- */
- public boolean Element(Entity currentEntity,int GeologicalFeature) {
- boolean touch=false;
- int X=currentEntity.getPixelPosition()[0],Y =currentEntity.getPixelPosition()[1],SpriteX= currentEntity.getCurrentSprite().getWidth()-1,SpriteY=currentEntity.getCurrentSprite().getHeight()-1;
- if(getGeologicalFeatures(X, Y)==GeologicalFeature
- || getGeologicalFeatures((X+SpriteX), Y + SpriteY)==GeologicalFeature
- || getGeologicalFeatures((X+SpriteX), Y)==GeologicalFeature
- || getGeologicalFeatures(X+SpriteX, Y+SpriteY)==GeologicalFeature
- ){touch=true;}
- else {touch=false;}
- return touch;
- }
- /**
- * calculates if the entity will collide with earth with the given time
- *
- * @param currentEntity
- * is a given Entity
- * @param time
- * is a given time in double
- * @return
- * if the entity will collide with earth with the given time
- *
- */
- public boolean futureEarth(Entity currentEntity,double time) throws Exception {
- int[] pos =
- {mazub.calculateToPixel(BigDecimal.valueOf(
- (BigDecimal.valueOf(currentEntity.actualPosition[0])
- .add (BigDecimal.valueOf(currentEntity.Velocity[0]).multiply(BigDecimal.valueOf(time)))
- .add(BigDecimal.valueOf(currentEntity.getAcceleration()[0]).multiply(BigDecimal.valueOf(0.5))
- .multiply(BigDecimal.valueOf(Math.pow(time, 2))))).doubleValue()).doubleValue()),
- mazub.calculateToPixel(BigDecimal.valueOf(
- (BigDecimal.valueOf(currentEntity.actualPosition[1])
- .add (BigDecimal.valueOf(currentEntity.Velocity[1]).multiply(BigDecimal.valueOf(time)))
- .add(BigDecimal.valueOf(currentEntity.getAcceleration()[1]).multiply(BigDecimal.valueOf(0.5))
- .multiply(BigDecimal.valueOf(Math.pow(time, 2))))).doubleValue()).doubleValue())};
- boolean touch=false;
- int X=pos[0]+1,Y = pos[1]+1,SpriteX=currentEntity.getCurrentSprite().getWidth()-2,SpriteY= currentEntity.getCurrentSprite().getHeight()-2;
- if(getGeologicalFeatures(X, Y)==1
- || getGeologicalFeatures(X+SpriteX, Y)== 1
- || getGeologicalFeatures(X, Y+SpriteY)==1
- || getGeologicalFeatures(X+SpriteX, Y+SpriteY)==1
- ){touch=true;}
- else {touch=false;}
- return touch;
- }
- /**
- * Collides with earth
- *
- * @param currentEntity
- * @throws Exception
- */
- public void Earth(Entity currentEntity) throws Exception {
- int X=currentEntity.getPixelPosition()[0]+1,Y =currentEntity.getPixelPosition()[1]+1,SpriteX= currentEntity.getCurrentSprite().getWidth()-2,SpriteY= currentEntity.getCurrentSprite().getHeight()-2;
- //TopPixels = Left Up & Right Up
- if((getGeologicalFeatures(X, Y+SpriteY)==1||getGeologicalFeatures(X+SpriteX,Y+SpriteY)==1 ) && mazub.isJumping()) {
- currentEntity.falling();}
- else {currentEntity.stopDuckingIsEnabled();}
- //BottomPixels = Left Bottom & Right Bottom
- if((getGeologicalFeatures(X, Y-1)==1 || getGeologicalFeatures(X+SpriteX, Y-1)==1) && currentEntity.getVelocity()[1]<0 ) {
- currentEntity.endJump();}
- else {currentEntity.falling();} //;throw new Exception(Integer.toString(X-1)+" "+Integer.toString(Y-1));}
- //RightPixels= alleen rechtsboven want als je linksonder doet kan hij niet meer bewegen door het verplaatsung
- if((getGeologicalFeatures(X+SpriteX, Y+SpriteX-1)==1 || getGeologicalFeatures(X+SpriteX, Y)==1) && currentEntity.getOrientation()==1 && currentEntity.getVelocity()[0]>=0) {
- currentEntity.endMove();throw new Exception(Integer.toString(X-1)+" "+Integer.toString(Y-1));
- }
- //LeftPixels
- if((getGeologicalFeatures(X, Y+SpriteY-1)==1||getGeologicalFeatures(X, Y)==1 )&& currentEntity.getOrientation()==-1) {
- currentEntity.endMove();throw new Exception(Integer.toString(X-1)+" "+Integer.toString(Y-1));}
- }
- /**
- * returns a boolean if mazub is in the targetcoordinate
- *
- * @param currentEntity
- * is a given Entity
- *
- * @return
- * if mazub's corners are touching the tile of the targetcoordinate
- * |if(calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
- * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
- * | || calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1]
- * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1])
- * |then return true
- * @return
- * if mazub's corners are not touching the tile of the targetcoordinate
- * |if(calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y) != getTargetTileCoordinate()[1]
- * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y) != getTargetTileCoordinate()[1]
- * | || calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) != getTargetTileCoordinate()[1]
- * | || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) != getTargetTileCoordinate()[1])
- * |then return false
- *
- */
- public boolean mazubInTarget(Entity currentEntity) {
- boolean touch=false;
- int X=currentEntity.getPixelPosition()[0]+1,Y = currentEntity.getPixelPosition()[1]+1,SpriteX= currentEntity.getCurrentSprite().getWidth()-2,SpriteY= currentEntity.getCurrentSprite().getHeight()-2;
- if(calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
- || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y) == getTargetTileCoordinate()[1]
- || calculateInFeature(X)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1]
- || calculateInFeature(X+SpriteX)== getTargetTileCoordinate()[0] && calculateInFeature(Y+SpriteY) == getTargetTileCoordinate()[1]
- )
- {touch=true;}
- else {touch=false;}
- return touch;
- }
- /*
- ************************************
- | Advenced time Defensieve program |
- ************************************
- */
- //After Ducking
- //FIXME if(!(mazub.isDucking()) && getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==1) {mazub.Ducking=true;}
- //FIXME if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==0 && mazub.isDucking()==true ) {mazub.endDucking();}
- /**
- * In-game-time timer
- */
- public double AFithOfAsecond=0.0;
- /**
- * Advances the time for the world and all its objects by the given amount.
- * @param dt
- * The in game time.
- * @throws IllegalArgumentException
- */
- public double lessThenTwo = 0.0;
- public boolean canBeDamaged=true;
- public void advancedTime(double dt) throws Exception { if(dt<0 || dt > 0.2) {throw new IllegalArgumentException();}
- Iterator<Object> itr = object_set.iterator();
- while(itr.hasNext()){
- Entity currentEntity=(Entity)itr.next();
- if(currentEntity instanceof Mazub) {
- BigDecimal time = timePerCm(dt, currentEntity);
- for (BigDecimal i = BigDecimal.valueOf(0.00); i.compareTo(BigDecimal.valueOf(dt))==-1; i = BigDecimal.valueOf(i.add(time).doubleValue()) ) {
- if(hashAsGameObject(currentEntity)) {
- if(mazubInTarget(currentEntity)==true) {didPlayerReachedTarget=true;}else {didPlayerReachedTarget=false;}
- currentEntity.advanceTime(time.doubleValue());Earth(currentEntity);
- isGameOver();
- }
- if(currentEntity.getWorld()!= null && OutsideWorldBorder(currentEntity.getActualPosition())) {terminatedGameObject(currentEntity);;}
- // if(mazub.isMoving() || mazub.isJumping()) {time = CollisionDetectTime(time,currentEntity);}
- if(OutsideWorldBorder(currentEntity.getActualPosition())) {terminatedGameObject(currentEntity);;}}}
- else {currentEntity.advanceTime(dt);}
- if(OutsideWorldBorder(currentEntity.getActualPosition())) {currentEntity.setHitPoint(0);}
- }
- }
- public int hitsChanged = 0;
- public int totalChanged = 0;
- /**
- * is collision detection with geologicalfeatures
- */
- public int ElementCollison(Entity currentEntity) {
- if(Element(currentEntity,3)==true){hitsChanged+=-50;totalChanged+=-50;}// 3=Magma
- else if(Element(currentEntity,5)==true){hitsChanged+=-4;totalChanged+=-4;}
- else if(Element(currentEntity,2)==true){hitsChanged+=-2;totalChanged+=-2;} // 2=Water
- return hitsChanged;
- }
- /*
- * *****************
- * Collision Detect*
- * *****************
- */
- /**
- * Boolean value that is true if Mazub will collide with something, and false if not.
- */
- public boolean Overlap;
- /**
- * calculates the given time with the entity to a double that gives a time for each cm that changed
- *
- * @param time
- * is the given time that has to be calculated
- * @param currentEntity
- * is the given Entity where his time per cm
- * @return
- * the time per centimeter
- *
- */
- public BigDecimal timePerCm(double time,Entity currentEntity) {
- //Calculation of time fragments with given acceleration, velocity and time.
- BigDecimal currentVelocityX= BigDecimal.valueOf(currentEntity.getVelocity()[0]), currentVelocityY= BigDecimal.valueOf(currentEntity.getVelocity()[1]), currentAccelerationX= BigDecimal.valueOf(currentEntity.getAcceleration()[0]),currentAccelerationY= BigDecimal.valueOf(currentEntity.getAcceleration()[1]);
- BigDecimal cm= BigDecimal.valueOf(0.01);
- BigDecimal timedt = BigDecimal.valueOf(time);
- BigDecimal mathsqr1=BigDecimal.valueOf(Math.sqrt((currentAccelerationX.pow(2).add(currentAccelerationY.pow(2))).doubleValue()));
- BigDecimal mathsqr2=BigDecimal.valueOf(Math.sqrt((currentVelocityX.pow(2).add(currentVelocityY.pow(2))).doubleValue()));
- BigDecimal divider = mathsqr1.add(mathsqr2.multiply(timedt));
- if(divider.compareTo(BigDecimal.valueOf(0.0))==0) {return BigDecimal.valueOf(time);}
- BigDecimal dt= BigDecimal.valueOf(cm.divide((divider), 16, RoundingMode.HALF_UP).doubleValue());
- return dt;
- }
- /*
- * *************
- * Game Objects*
- * *************
- */
- // public boolean isDeadGameObject()
- /**
- * is the set of all objects inside the world.
- */
- public Set<Object> object_set = new HashSet<Object>();
- /**
- * @return
- * returns a set of all game objects
- * |return object_set;
- */
- public Set<Object> getAllGameObjects(){return object_set;}
- /**
- * sets the set of all game objects of this world to the given set
- *
- * @param newSet
- * a set of objects
- * @effect
- * sets the set of all game objects of this world to the given set
- * |object_set = newSet;
- */
- public void setAllGameObjects(Set<Object> newSet){object_set = newSet;}
- /**
- * Remove the given object to the given world.
- * @effect
- * Remove the given object to the given world.
- * |object_set.remove(object);
- */
- public void removeGameObject(Object object){
- object_set.remove(object);}
- /**
- * add a gameObject inside the set of all given objects.
- *
- * @param gameObject
- * a given gameObject
- * |
- * @param world
- * @effect
- * if the gameObject is an instance of mazub and mazub
- * @throws Exception
- * if the world already has 100 gameObject throw a exception
- * |if(object_set.size()>=100)
- * |then throw new Exception("Already 100 gameObjects");
- * @effect
- * if the world has less then 100 gameObject add the gameObject inside the set of all given objects and if gameObject has no world,
- * set the world of the gameObject to this world
- * | if(object_set.size()<100)
- * | then object_set.add(gameObject);
- * | if(gameObject.getWorld()!=null)
- * | then gameObject.setWorld(this);
- */
- public void addGameObject(Entity gameObject,World world) throws Exception {
- if (gameObject instanceof Mazub&&(mazub == null ||gameObject==null)) {mazub = (Mazub)gameObject;}
- if(object_set.size()<100) {object_set.add(gameObject);gameObject.setWorld(world);}
- else {throw new Exception("Already 100 gameObjects");}
- }
- // public void DeathGameObject() {
- // if(mazub.getPixelPosition()[0]>getSizeInPixels()[0] || mazub.getPixelPosition()[1]>getSizeInPixels()[1]) {
- // mazub.setHitPoint(0);}}
- /**
- * returns if the game is over
- *
- * @return
- * if the mazub is dead return true
- * |if(mazub.isDeadGameObject())
- * |then result = true;
- * @return
- * if the mazub reached the target variable
- * |if(didPlayerReachedTarget)
- * |then result = true;
- * @return
- * else the game isn't over
- * |result = false
- *
- */
- public boolean isGameOver() {
- if(mazub.isDeadGameObject()) {return true;}
- if(didPlayerReachedTarget) {return true;}
- return false;
- }
- /**
- * returns that the object is inside this world
- *
- * @param object
- * a given object
- * @return
- * if the object is inside the set of all game object
- * |if(object_set.contains(object)) then result = true
- * @return
- * if the object is not inside of the set of all game object
- * |if(!object_set.contains(object)) then result = false
- */
- public boolean hashAsGameObject(Object object) {if(object_set.contains(object)) {return true;}else {return false;}}
- /*
- *********************
- * Terminate*
- *********************
- */
- /**
- * Start a game in the given world.
- */
- public void startGame() throws ModelException{}
- /*
- *********************
- * Mazub*
- *********************
- */
- /**
- * Variable storing the Mazzub of this world
- */
- private Mazub mazub;
- private boolean didPlayerReachedTarget=false;
- /**
- * returns this mazub
- *
- * @return
- * returns this mazub
- * |result = this.mazub
- */
- public Mazub getMazub() {
- return mazub;
- }
- /**
- * set this mazub of the world to the given newmazub
- *
- * @param newMazub
- * @effect
- * set this mazub of the world to the given newmazub
- * |mazub=newmazub
- */
- public void setMazub(Mazub newMazub) {
- mazub=newMazub;
- }
- /**
- *returns if the player has won
- *
- * @return
- * if the played reached the target return true
- * |if(didPlayerReachedTarget) then result = true
- * @return
- * if the mazubb is dead return false
- * |else if(mazub.isDeadGameObject()==true) then result= false
- * @return
- * otherwise false
- * |else result= false
- *
- */
- public boolean DidPlayerWin() {
- if(didPlayerReachedTarget) {return true;}
- else if(mazub.isDeadGameObject()==true) {return false;}
- else {return false;}
- }
- /*
- *********************
- * School*
- *********************
- */
- /**
- * results either false or true weither the gameobject is removed or not
- *
- * @param gameObject
- * a given object
- * @return
- * if given object is terminated and it doesn't contain in the world
- * | if(((Entity)gameObject).isTerminatedGameObject()==true||hashAsGameObject(gameObject)) then result= false
- * @return
- * if the given object is terminated or it does contain in the world
- * |if(((Entity)gameObject).isTerminatedGameObject()==false||!hashAsGameObject(gameObject)){return false
- */
- public boolean isTerminatedGameObject(Object gameObject) {
- if(((Entity)gameObject).isTerminatedGameObject()==true||hashAsGameObject(gameObject)){return false;}
- else {return true;}}
- /**
- * terminate the game object
- *
- * @param gameObject
- *
- * @effect
- * remove the
- * |removeGameObject(gameObject);
- * @effect
- *
- * |
- */
- public void terminatedGameObject(Entity gameObject) {
- if(gameObject instanceof Mazub) {if(gameObject.getWorld()!=null) {((Mazub) gameObject).terminateGameObject();}
- mazub = null;removeGameObject(gameObject);}
- else {if(gameObject.getWorld()!=null) {gameObject.terminateGameObject();}removeGameObject(gameObject);}
- // removeGameObject(gameObject);
- }
- /**
- * @param newpos
- * @return
- */
- public boolean calculateInsideOfWorld(int[] newpos) {
- if(newpos[0]<0||newpos[0]>getSizeInPixels()[0]||newpos[1]<0||newpos[1]>getSizeInPixels()[1]) {return true;}
- else {return false;}
- }
- /**
- * @post...
- * |new.getSchool().contains(school)==true
- */
- public Set<School> SchoolSet=new HashSet<School>();
- public void setSchools(Set<School> school) {
- SchoolSet=school;
- }
- public void addSchool(School newSchool)throws Exception {
- if(SchoolSet.size()<10) {
- SchoolSet.add(newSchool);}
- else {throw new Exception("To many School in world.");}
- }
- /**
- *
- * @return...
- * |result ==SchoolSet
- */
- public Set<School> getAllSchools() {
- return SchoolSet;
- }
- public boolean OutsideWorldBorder(double[] newPosition){
- if(newPosition[0] < 0 || newPosition[0] > calculateToActual(getSizeInPixels()[0]) || newPosition[1] <0 || newPosition[1] > calculateToActual(getSizeInPixels()[1]) ) {return true;}
- else {return false;}}
- public void terminateWorld() {
- Iterator<Object> itr = object_set.iterator();
- while(itr.hasNext()){
- Entity currentEntity=(Entity)itr.next();
- (currentEntity).terminateGameObject();}
- object_set.clear();
- }
- public int calculateToPixel (double actual) {return ((int)(actual*100));}
- public double calculateToActual (int pixel) {return ((double)(pixel/100));}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement