Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jumpingalien.model;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Set;
- import com.sun.javafx.scene.traversal.WeightedClosestCorner;
- import be.kuleuven.cs.som.annotate.Raw;
- import jumpingalien.facade.Facade;
- import jumpingalien.model.Mazub;
- import jumpingalien.util.ModelException;
- /**
- * 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 {
- 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;
- */
- public World(int pixelSize, int nbTilesX, int nbTilesY, int[] targetTileCoordinate, int visibleWindowWidth, int visibleWindowHeight, int...geologicalFeatures){
- nmbTilesX=nbTilesX/100;
- nmbTilesY=nbTilesY/100;
- TileLength=pixelSize;
- setVisibleWindowDimension(visibleWindowWidth,visibleWindowHeight);
- setTargetTileCoordinate(targetTileCoordinate);
- setGeologicalFeaturesSize(nbTilesX, nbTilesY);
- setSizeInPixels(nbTilesX*pixelSize,nbTilesY*pixelSize);
- for (int y= 0; y < nbTilesY; y++) {
- for (int x = 0; x< nbTilesX; x++) {
- int counter =y*nbTilesX + x;
- setFeature(x, y,geologicalFeatures[counter]);
- }
- }
- }
- /*
- * Terminate the given world.
- */
- public void terminateWorld(World world) throws ModelException{
- }
- /*
- *******************************
- * *Tiles Total programming. *
- * *****************************
- */
- public int [] SizeInPixels ;
- /**
- * 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;}
- public void setSizeInPixels(int Width, int Height) {
- SizeInPixels = new int[2];
- SizeInPixels[0] =Width;
- SizeInPixels[1] =Height;
- }
- public int getTileLength(){
- return TileLength;
- }
- /*
- * ***********************
- * Geological features *
- * ***********************/
- public int[][] GeologicalFeaturesCanvas;
- public void setGeologicalFeaturesSize(int GeoRow,int GeoCol) {this.GeologicalFeaturesCanvas = new int[GeoRow][GeoCol];}
- public void setGeologicalFeatures(int X, int Y, int geoFeature) {
- if (X > ((SizeInPixels[0]/this.getTileLength())-1) || Y > ((SizeInPixels[0]/this.getTileLength())-1) || X<0 || Y<0) {return;} ///Problem
- int GeoX = calculateInFeature(X);
- int GeoY = calculateInFeature(Y);
- this.GeologicalFeaturesCanvas[GeoX][GeoY] = geoFeature;
- }
- 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];
- }
- public void setFeature(int X, int Y, int F) {
- this.GeologicalFeaturesCanvas[X][Y] = F;
- }
- public int calculateInFeature(int aPixel) {
- int resultPixel = (int)(aPixel/getTileLength());
- return resultPixel;
- }
- /*
- **********************************
- *Visible Window Defensive program*
- **********************************
- */
- public int[] VisibleWindowDimension=new int[2];
- public void setVisibleWindowDimension(int weight , int height){
- VisibleWindowDimension[0] = weight;
- VisibleWindowDimension[1] = height;
- }
- /**
- * gets visible window dimensions.
- * @return
- * |result=VisibleWindowDimension
- */
- public int[] getVisibleWindowDimension() {
- return VisibleWindowDimension;
- }
- int LEFT, DOWN;
- public int[] getVisibleWindowsPosition() {
- int[]VisibleWindowPosition=new int[2];
- getSizeInPixels();
- if(mazub.getPixelPosition()[0]!=getSizeInPixels()[0]&& visibleMazubCanMove(0) ){
- this.LEFT=(int)(VisiblePositionAdaptor);}
- if (mazub.getPixelPosition()[1]!=getSizeInPixels()[1] &&visibleMazubCanMove(1)) {
- this.DOWN=(int)(VisiblePositionAdaptor);}
- VisibleWindowPosition[0]=LEFT;VisibleWindowPosition[1]=DOWN;
- return VisibleWindowPosition;
- }
- public int VisiblePositionAdaptor;
- public boolean visibleMazubCanMove(int direction) {
- int newPos = mazub.getPixelPosition()[direction];
- if(newPos<= 400 ||newPos >SizeInPixels[direction]-400) { VisiblePositionAdaptor = 0; return false;}
- else if(newPos <= newPos + (getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) || newPos > newPos +(getVisibleWindowDimension()[direction]-400)) {VisiblePositionAdaptor = newPos -400; return true;}
- else if(newPos <= newPos + (getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) || newPos > newPos +(getVisibleWindowDimension()[direction])) {VisiblePositionAdaptor = 0; return false;}
- else {return false;}
- /*
- if(prevPos >= 0 && prevPos<200) { VisiblePositionAdaptor =prevPos +0;return false;}
- else if(prevPos >= 200 && prevPos >getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) { VisiblePositionAdaptor =prevPos;return true;}
- else if(prevPos >= (getVisibleWindowDimension()[direction]-(getVisibleWindowDimension()[direction]-400)) && prevPos<getVisibleWindowDimension()[direction]-400) {VisiblePositionAdaptor=prevPos-400 ;return false;}
- else if(prevPos >= getVisibleWindowDimension()[direction]-400 && prevPos >(getSizeInPixels()[direction]-200)) {VisiblePositionAdaptor = prevPos ; return true;}
- else if(prevPos >= getSizeInPixels()[direction]-200 && prevPos<getSizeInPixels()[direction]) {VisiblePositionAdaptor= prevPos -0 ;return false;}
- else {return false;
- */
- }
- // public void setVisiblePositionAdaptor(int visiblePositionAdaptor) {
- // VisiblePositionAdaptor = visiblePositionAdaptor;
- // }
- /*
- ************************************
- | Target tiles Nominal programming |
- ************************************
- */
- 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 ModelException{
- targetTileCoordinates = tileCoordinate;}
- /*
- * 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;}
- /*
- * Start a game in the given world.
- */
- public void startGame() throws ModelException{
- }
- /*
- ************************************
- | Advenced time Defensieve program |
- ************************************
- */
- /**
- * In-game-time
- */
- public double a1, a2, v1, v2;
- /**
- * Advances the time for the world and all its objects by the given amount.
- * @param dt
- * The in game time.
- * @throws IllegalArgumentException
- */
- public void advanceWorldTime(double dt) throws IllegalArgumentException { if(dt<0) {throw new IllegalArgumentException();}
- DeathGameObject();
- double time=dt;
- int PosMZx = mazub.getPixelPosition()[0]; int PosMZy=mazub.getPixelPosition()[1];
- int pixelX=mazub.getSpriteDimention()[0]; int pixelY=mazub.getSpriteDimention()[1];
- //All still need to be improved so they conclude all pixels of MAzub and not only the bottom ones.
- /*
- * EARTH
- */
- //BottomPixels
- if(getGeologicalFeatures((PosMZx+(mazub.getSpriteDimention()[0])), PosMZy)==1 && mazub.getVelocity()[1]<=0 ) {
- mazub.endJump();
- // mazub.setPixelPosition(PosMZx,PosMZy );
- // mazub.verticalChanging(dt);
- }
- //TopPixels
- if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==1 && mazub.getVelocity()[1]>0) {
- // mazub.endJump();
- mazub.setVelocity(mazub.getVelocity()[0],0);
- mazub.setAcceleration(mazub.getAcceleration()[0],0);
- // if(mazub.getVelocity()[1]>0) {mazub.setVelocity(mazub.getVelocity()[0], 0); mazub.setAcceleration(mazub.getAcceleration()[0], -10);}
- }
- //RightPixels
- if((getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), (PosMZy)+(mazub.getSpriteDimention()[1]-2))==1) && mazub.getOrientation()==1 && mazub.getVelocity()[1]<=0) {
- mazub.endJump();
- // mazub.setAcceleration(0, mazub.getAcceleration()[1]);
- mazub.setVelocity(0, mazub.getVelocity()[1]);
- if(mazub.getVelocity()[1]>0) {mazub.setVelocity(mazub.getVelocity()[0], 0); mazub.setAcceleration(0, -10);}
- }
- //LeftPixels
- if(getGeologicalFeatures(PosMZx, (PosMZy)+(mazub.getSpriteDimention()[1]-2))==1 && mazub.getOrientation()==-1) {
- mazub.endJump();
- // mazub.setOrientation(0);
- mazub.setAcceleration(0, mazub.getAcceleration()[1]);
- mazub.setVelocity(0, mazub.getVelocity()[1]);
- }
- //After Ducking
- // if(!(mazub.isDucking()) && getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==1) {mazub.Ducking=true;}
- // if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==0 && mazub.isDucking()==true ) {mazub.endDucking();}
- /*
- * WATER
- */
- if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-2), PosMZy)==2
- || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy)==2
- || getGeologicalFeatures((PosMZx+(mazub.getSpriteDimention()[0]-1)), PosMZy+(mazub.getSpriteDimention()[1]-2))==2
- || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==2
- ){
- if(time%0.2!=0) {mazub.setHitPoint(-2);}
- if(time==0.2) {time=0;}
- time=+dt;
- }
- /*
- * MAGMA
- */
- if(getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-2), PosMZy)==3
- || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy)==3
- || getGeologicalFeatures((PosMZx+(mazub.getSpriteDimention()[0]-1)), PosMZy+(mazub.getSpriteDimention()[1]-2))==3
- || getGeologicalFeatures(PosMZx+(mazub.getSpriteDimention()[0]-1), PosMZy+(mazub.getSpriteDimention()[1]-1))==3
- ){
- if(time%0.2!=0) {mazub.setHitPoint(-50);}
- if(time==0.2) {time=0;}
- time=+dt;
- }
- if(mazub.isMoving() || mazub.isJumping()) {CollisionDetect(time);}
- if(Overlap && (mazub.isMoving() || mazub.isJumping())) {mazub.endMove(); mazub.endJump();}
- /*
- * AdvanceTime Objects
- */
- if(time%0.2!=0) {
- mazub.advanceTime(time);
- plant.advanceTime(time);
- if(time==0.2) {time=0;}
- time=+dt;}
- }
- /*
- * *****************
- * Collision Detect*
- * *****************
- */
- public int WidthTiles, HeigthTiles;
- /**
- * Boolean value that is true if Mazub will collide with something, and false if not.
- */
- public boolean Overlap;
- /**
- * Detects if Mazub collides with anything, and determines if Mazub should the be able to make the initial move.
- * @param time
- */
- public void CollisionDetect(double time) {
- //Calculation of time fragments with given acceleration, velocity and time.
- this.v1=mazub.getVelocity()[0]; this.v2=mazub.getVelocity()[1]; this.a1=mazub.getAcceleration()[0]; this.a2=mazub.getAcceleration()[1];
- double dt=0.01/(Math.sqrt(Math.pow(v1,2)+Math.pow(v2,2))+(Math.sqrt(Math.pow(a1,2)+Math.pow(a2,2)))*time);
- WidthTiles=54; HeigthTiles=27;
- //Voor andere voorwerpen getSpriteSize proberen te maken in Entity.
- if(time%dt==0) {
- VirtualMoving(time);
- }
- if((mazub.getPixelPosition()[0]+(mazub.getSpriteDimention()[0]-1)<VirtualPos[0]
- || VirtualPos[0]+(WidthTiles-1)<mazub.getPixelPosition()[0]
- || mazub.getPixelPosition()[1]+(mazub.getSpriteDimention()[1]-1)<VirtualPos[1]
- || VirtualPos[1]+(HeigthTiles-1)<mazub.getPixelPosition()[1]))
- {this.Overlap=false;}
- else {Overlap=true;}
- }
- int[] VirtualPos=new int[2];
- /**
- * Method that calculates the "Virtual" position of Mazub after moving. This to be used in collision detect to see if it collides with something.
- * @param time
- * @return
- * result==VirtualPos
- */
- public int[] VirtualMoving(double T) {
- int posx=(int)(mazub.getActualPosition()[0]+(mazub.getVelocity()[0]*T)+(0.5*mazub.getAcceleration()[0]*T*T));
- int posy=(int)(mazub.getActualPosition()[1]+ (mazub.getVelocity()[1]*T)+(0.5*mazub.getAcceleration()[1]*T*T));
- VirtualPos[0]=posx; VirtualPos[1]=posy;
- return VirtualPos; }
- /*
- * *************
- * Game Objects*
- * *************
- */
- public Set<Object> object_set = new HashSet<Object>();
- public void setGameObjects(Object gameobject){object_set.add(gameobject);}
- public Set<Object> getGameObjects(){return object_set;}
- /*
- * Remove the given object to the given world.
- */
- public void removeGameObject(Object object){
- getGameObjects().remove(object);
- }
- /**
- * Variable storing the Mazub of this World
- */
- private Mazub mazub;
- /**
- * Variable storing the Plant of this world
- */
- private Plant plant;
- /**
- * @param gameobject
- */
- public void addGameObject(Object gameobject) {
- if(gameobject instanceof Plant) {this.plant=(Plant)gameobject; setGameObjects(plant);}
- if(gameobject instanceof Mazub) {this.mazub=(Mazub)gameobject; setGameObjects(mazub);}
- }
- // public double[] getActualPosition(Object gameobject) {
- // double[] position=new double[2];
- // if(gameobject instanceof Plant) {position[0]=plant.getActualPosition()[0];position[1]=plant.getActualPosition()[1];}
- // if(gameobject instanceof Mazub) {position[0]=mazub.getActualPosition()[0]; position[1]=mazub.getActualPosition()[1];}
- // return position;
- // }
- //
- // public int[] getPixelPosition(Object gameobject) {
- // int[] PixelPosition=new int[2];
- // if(gameobject instanceof Plant) {PixelPosition=plant.getPixelPosition();}
- // if(gameobject instanceof Mazub) {PixelPosition[0]=mazub.getPixelPosition()[0];PixelPosition[1]=mazub.getPixelPosition()[1];}
- // return PixelPosition;
- // }
- public void DeathGameObject() {
- if(mazub.getPixelPosition()[0]>getSizeInPixels()[0] || mazub.getPixelPosition()[1]>getSizeInPixels()[1]) {
- mazub.setHitPoint(0);}
- }
- public boolean GoodGame;
- public boolean DidPlayerWin() {
- if(mazub.isDeadGameObject()==false) {GoodGame=true;}
- else if(mazub.isDeadGameObject()==true) {GoodGame=false;}
- return GoodGame;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment