Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Ultroman (posted 2012-07-26 11:16:46, viewed 1305 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509   510   511   512   513   514   515   516   517   518   519   520   521   522   523   524   525   526   527   528   529   530   531   532   533   534   535   536   537   538   539   540   541   542   543   544   545   546   547   548   549   550   551   552   553   554   555   556   557   558   559   560   561   562   563   564   565   566   567   568   569   570   571   572   573   574   575   576   577   578   579   580   581   582   583   584   585   586   587   588   589   590   591   592   593   594   595   596   597   598   599   600   601   602   603   604   605   606   607   608   609   610   611   612   613   614   615   616   617   618   619   620   621   622   623   624   625   626   627   628   629   630   631   632   633   634   635   636   637   638   639   640   641   642   643   644   645   646   647   648   649   650   651   652   653   654   655   656   657   658   659   660   661   662   663   664   665   666   667   668   669   670   671   672   673   674   675   676   677   678   679   680   681   682   683   684   685   686   687   688   689   690   691   692   693   694   695   696   697   698   699   700   701   702   703   704   705   706   707   708   709   710   711   712   713   714   715   716   717   718   719   720   721   722   723   724   725   726   727   728   729   730   731   732   733   734   735   736   737   738   739   740   741   742   743   744   745   746   747   748   749   750   751   752   753   754   755   756   757   758   759   760   761   762   763   764   765   766   767   768   769   770   771   772   773   774   775   776   777   778   779   780   781   782   783   784   785   786   787   788   789   790   791   792   793   794   795   796   797   798   799   800   801   802   803   804   805   806   807   808   809   810   811   812   813   814   815   816   817   818   819   820   821   822   823   824   825   826   827   828   829   830   831   832   833   834   835   836   837   838   839   840   841   842   843   844   845   846   847   848   849   850   851   852   853   854   855   856   857   858   859   860   861   862   863   864   865   866   867   868   869   870   871   872   873   874   875   876   877   878   879   880   881   882   883   884   885   886   887   888   889   890   891   892   893   894   895   896   897   898   899   900   901   902   903   904   905   906   907   908   909   910   911   912   913   914   915   916   917   918   919   920   921   922   923   924   925   926   927   928   929   930   931   932  
package gameObjects;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import states.Game;
import dtos.collisionDetection.CollisionAndType;

public class Player extends Entity {

   private int meleeDelay = 250;
   private final double maxMovementSpeed = 0.20;
   private double movementSpeed;
   private final double maxFallSpeed = 0.45, maxJumpSpeed = 0.40; // was 0.40
   private double fallSpeed, jumpSpeed;
   private double angle;
   private boolean facingRight = true, moving, falling = true, jumping, corrected;
   private int currentAnim, jumpTickCounter, maxJumpTicks=44; // was 44
   private String[] animationIDs;
   private Animation[] animations;
   private boolean[] keys;
   private Game game;
   private CollisionAndType tempCollisionInfo;
   private boolean traversing;
   private BufferedImage arm;
   private int armOffsetX = -5;
   private int armOffsetY = 50;
   private int height = 65, width = 18;
   private final int WALKING = 1, STANDING=0, JUMPING=2, FALLING=2;

   public Player(Game game) {
      super();
      animationIDs = new String[3];
      animationIDs[0] = "standing";
      animationIDs[1] = "walking";
      animationIDs[2] = "jumping";
      keys = game.getKeys();

      this.game = game;
      try {
         arm = ImageIO.read(getClass().getResource("/resources/playerImages/ArmWithGun.png"));
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   
   private void updateColRectPosition(){
      colRect.x = (int)Math.round(posX)-10;
      colRect.y = (int)Math.round(posY)-64;
   }

   public void update(int elapsedTime){
      
      double oldPosX = posX;
      double oldPosY = posY;
      
      if(keys[game.getLeftKey()] || keys[game.getRightKey()]){
         if(keys[game.getLeftKey()] && keys[game.getRightKey()]){

         }
         else if(keys[game.getLeftKey()]){
            moveLeft();
         }
         else if(keys[game.getRightKey()]){
            moveRight();
         }
      }
      else{
         if(!jumping && !falling)currentAnim=0;
         if(movementSpeed>0.00)movementSpeed-=0.015;
         else if (movementSpeed<0.00)movementSpeed+=0.015;
         if(movementSpeed<0.02 && movementSpeed>0)movementSpeed = 0;
         if(movementSpeed>-0.02 && movementSpeed<0)movementSpeed = 0;
      }

      if (keys[game.getDownKey()])traversing = true;
      if (keys[game.getJumpKey()] && !falling){
         if(!jumping){
            startJumping();
         }
      }else if(!keys[game.getJumpKey()]){
         if(jumping){
            stopJumping();
            startFalling();
         }
      }

      // move the player vertically, according to the variable settings
      if(falling){
         // move down the Y-axis
         if(fallSpeed<maxFallSpeed)fallSpeed+=0.030;
         if(fallSpeed>maxFallSpeed)fallSpeed=maxFallSpeed;
         posY+=fallSpeed*elapsedTime;
      }
      else if(jumping){
         // move up the Y-axis
//         System.out.println("Elapsed time: "+elapsedTime);
//         System.out.println("jumpTickCounter: "+jumpTickCounter);
//         System.out.println("Elapsed time: "+elapsedTime);
//         System.out.println("Elapsed time: "+elapsedTime);
         if(jumpTickCounter < maxJumpTicks){
            jumpTickCounter += 1;
            jumpSpeed = maxJumpSpeed-((maxJumpSpeed/maxJumpTicks)*jumpTickCounter);
            posY -= jumpSpeed*elapsedTime;
         }else if(jumpTickCounter >= maxJumpTicks){
            stopJumping();
            startFalling();
         }
      }

      // if the player is at the edges of the map, stop him from moving 
      if(Math.round(posX)+(movementSpeed*elapsedTime) < 10)movementSpeed = 0;
      if(Math.round(posX)+(movementSpeed*elapsedTime) > game.getMapWidthInPixels()-10)movementSpeed = 0;

      // move the player horizontally
      posX += movementSpeed * elapsedTime;

      // move the collision rectangle
      colRect.x = (int)Math.round(posX)-10;
      colRect.y = (int)Math.round(posY)-64;


      // declaring rounded position-variables to do calculations
      int roundPosX = (int)Math.round(posX), roundPosY = (int)Math.round(posY);
      int currentPlayerTileX = (int) Math.round((posX-(posX%40))/40)+1;
      int currentPlayerTileY = (int) Math.round((posY-(posY%40))/40)+1;
      int slopeOffset = 10;

      // start doing collision-detections, using points derived from the collision rectangle
      
      boolean botLeftCollided = false, midLeftCollided = false, topLeftCollided = false, botRightCollided = false, midRightCollided = false, topRightCollided = false;

      CollisionAndType botRight = game.checkTileCollisionFromMapCoordinates((int)Math.round(colRect.x+colRect.getWidth()), colRect.y+colRect.height-slopeOffset);
      if(botRight.isCollided()){
         if(botRight.getTileType()==0){
            botRightCollided = true;
            posX = oldPosX;
            updateColRectPosition();
            movementSpeed=0;
         }
         else if(botRight.getTileType()==1){
            SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(botRight.getPosX(), botRight.getPosY());
            if(tile.isSlopingAtTop()){
               if(!tile.isSlopingUpRight()){
                  // if bottom right has hit below the slope
                  if(39-(botRight.getPosY()%40)+1+(fallSpeed*elapsedTime) < tile.findYOnLineBetweenTwoPoints(botRight.getPosX()%40)){
                     botRightCollided = true;
                     posX = oldPosX;
                     updateColRectPosition();
                     movementSpeed-=0.05;
                  }
               }
               else if(tile.isSlopingUpRight()){
                  // if bottom right has hit below the slope
                  if(39-(botRight.getPosY()%40)+1+(fallSpeed*elapsedTime) < tile.findYOnLineBetweenTwoPoints(botRight.getPosX()%40)){
                     botRightCollided = true;
                     posX = oldPosX;
                     updateColRectPosition();
                     movementSpeed-=0.05;
                  }
                  // playerPosY in the tile is below the lowPoint, meaning the tile starts higher than the player posY
                  else if(39-(roundPosY%40)+tile.getSlopeOffset(colRect.width) < Math.abs(tile.getLowPoint())){
                     botRightCollided = true;
                     posX = oldPosX;
                     updateColRectPosition();
                     movementSpeed-=0.05;
                  }
               }
            }
            else{ // !tile.isSlopingAtTop()
               botRightCollided = true;
               posX = oldPosX;
               updateColRectPosition();
               movementSpeed=0;
            }
         }
      }
      
      CollisionAndType botLeft = game.checkTileCollisionFromMapCoordinates((int)Math.round(colRect.x), colRect.y+colRect.height-slopeOffset);
      if(botLeft.isCollided()){
         if(botLeft.getTileType()==0){
            botLeftCollided = true;
            posX = oldPosX;
            updateColRectPosition();
            movementSpeed=0;
         }
         else if(botLeft.getTileType()==1){
            SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(botLeft.getPosX(), botLeft.getPosY());
            if(tile.isSlopingAtTop()){
               if(!tile.isSlopingUpRight()){
                  // if bottom left has hit below the slope
                  if(39-(botLeft.getPosY()%40)+1+(fallSpeed*elapsedTime) < tile.findYOnLineBetweenTwoPoints(botLeft.getPosX()%40)){
                     botLeftCollided = true;
                     posX = oldPosX;
                     updateColRectPosition();
                     movementSpeed+=0.05;
                  }
                  // playerPosY-1 hits a sloping tile, and the player is below the lowPoint, meaning the tile starts higher than the player posY
                  else if(39-(roundPosY%40)+tile.getSlopeOffset(colRect.width) < Math.abs(tile.getLowPoint())){
                     botLeftCollided = true;
                     posX = oldPosX;
                     updateColRectPosition();
                     movementSpeed+=0.05;
                  }
                  
               }
               else if(tile.isSlopingUpRight()){
                  // if bottom left has hit below the slope
                  if(39-(botLeft.getPosY()%40)+1+(fallSpeed*elapsedTime) < tile.findYOnLineBetweenTwoPoints(botLeft.getPosX()%40)){
                     botLeftCollided = true;
                     posX = oldPosX;
                     updateColRectPosition();
                     movementSpeed+=0.05;
                  }
               }
            }
            else{ // !tile.isSlopingAtTop()
               botLeftCollided = true;
               posX = oldPosX;
               roundPosX = (int)Math.round(posX);
               updateColRectPosition();
               movementSpeed=0;
            }
         }
      }

      CollisionAndType midRight = game.checkTileCollisionFromMapCoordinates((int)Math.round(colRect.x+colRect.getWidth()), colRect.y+(colRect.height/2));
      if(midRight.isCollided()){
         if(midRight.getTileType()==0){
            midRightCollided = true;
         }
         else if(midRight.getTileType()==1){

            SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(midRight.getPosX(), midRight.getPosY());
            if(tile.isSlopingAtTop()){
               if(39-(midRight.getPosY()%40) < tile.findYOnLineBetweenTwoPoints(midRight.getPosX()%40)){
                  midRightCollided = true;
               }
            }
            else{ // !tile.isSlopingAtTop()
               if(midRight.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(midRight.getPosX()%40)){
                  midRightCollided = true;
               }
            }
         }
      }
      
      CollisionAndType midLeft = game.checkTileCollisionFromMapCoordinates((int)Math.round(colRect.x), colRect.y+(colRect.height/2));
      if(midLeft.isCollided()){
         if(midLeft.getTileType()==0){
            midLeftCollided = true;
         }
         else if(midLeft.getTileType()==1){

            SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(midLeft.getPosX(), midLeft.getPosY());
            if(tile.isSlopingAtTop()){
               if(39-(midLeft.getPosY()%40) < tile.findYOnLineBetweenTwoPoints(midLeft.getPosX()%40)){
                  midLeftCollided = true;
               }
            }
            else{ // !tile.isSlopingAtTop()
               if(midLeft.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(midLeft.getPosX()%40)){
                  midLeftCollided = true;
               }
            }
         }
      }

      CollisionAndType topRight = game.checkTileCollisionFromMapCoordinates((int)Math.round(colRect.x+colRect.getWidth()), colRect.y+slopeOffset);
      if(topRight.isCollided()){
         if(topRight.getTileType()==0){
            topRightCollided = true;
            posY = oldPosY;
            updateColRectPosition();
            if(!jumping && !falling){
               movementSpeed=0;
            }else if(jumping){
               stopJumping();
               startFalling();
            }
         }
         else if(topRight.getTileType()==1){
            SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(topRight.getPosX(), topRight.getPosY());
            
            if(tile.isSlopingAtTop()){
               if(39-(topRight.getPosY()%40) < tile.findYOnLineBetweenTwoPoints(topRight.getPosX()%40)){
                  topRightCollided = true;
                  posY = oldPosY;
                  updateColRectPosition();
                  if(!jumping && !falling){
                     movementSpeed=0;
                  }else if(jumping){
                     stopJumping();
                     startFalling();
                  }
               }
            }
            else{ // !tile.isSlopingAtTop()
               if(!tile.isSlopingUpRight()){
                  if(topRight.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(topRight.getPosX()%40)){
                     topRightCollided = true;
                     posY = oldPosY;
                     updateColRectPosition();
                     if(!jumping && !falling){
                        movementSpeed=0;
                     }else if(jumping){
                        stopJumping();
                        startFalling();
                     }
                  }
               }
               else if(tile.isSlopingUpRight()){
                  if(topRight.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(topRight.getPosX()%40)){
                     topRightCollided = true;
                     posY = oldPosY;
                     updateColRectPosition();
                     if(!jumping && !falling){
                        movementSpeed=0;
                     }else if(jumping){
                        stopJumping();
                        startFalling();
                     }
                  }
               }
            }
         }
      }

      CollisionAndType topLeft = game.checkTileCollisionFromMapCoordinates((int)Math.round(colRect.x), colRect.y+slopeOffset);
      if(topLeft.isCollided()){
         if(topLeft.getTileType()==0){
            topLeftCollided = true;
            posY = oldPosY;
            updateColRectPosition();
            if(!jumping && !falling){
               movementSpeed=0;
            }else if(jumping){
               stopJumping();
               startFalling();
            }
         }
         else if(topLeft.getTileType()==1){
            SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(topLeft.getPosX(), topLeft.getPosY());
            if(tile.isSlopingAtTop()){
               if(topLeft.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(topLeft.getPosX()%40)){
                  topLeftCollided = true;
                  posY = oldPosY;
                  updateColRectPosition();
                  if(!jumping && !falling){
                     movementSpeed=0;
                  }else if(jumping){
                     stopJumping();
                     startFalling();
                  }
               }
            }
            else{ // !tile.isSlopingAtTop()
               if(!tile.isSlopingUpRight()){
                  if(topLeft.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(topLeft.getPosX()%40)){
                     topLeftCollided = true;
                     posY = oldPosY;
                     updateColRectPosition();
                     if(!jumping && !falling){
                        movementSpeed=0;
                     }else if(jumping){
                        stopJumping();
                        startFalling();
                     }
                  }
               }
               else if(tile.isSlopingUpRight()){
                  if(topLeft.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(topLeft.getPosX()%40)){
                     topLeftCollided = true;
                     posY = oldPosY;
                     updateColRectPosition();
                     if(!jumping && !falling){
                        movementSpeed=0;
                     }else if(jumping){
                        stopJumping();
                        startFalling();
                     }
                  }
               }
            }
         }
      }
      
      if(topLeftCollided)System.out.println("topLeftCollided");
      if(midLeftCollided)System.out.println("midLeftCollided");
      if(botLeftCollided)System.out.println("botLeftCollided");
      if(topLeftCollided)System.out.println("topLeftCollided");
      if(midRightCollided)System.out.println("midRightCollided");
      if(botRightCollided)System.out.println("botRightCollided");
      
      if(falling || jumping){
         if(topLeftCollided && midLeft.isCollided()){
            posX = oldPosX;
         }
         if(botLeftCollided && midLeft.isCollided()){
            posX = oldPosX;
         }
         if(topRightCollided && midRight.isCollided()){
            posX = oldPosX;
         }
         if(botRightCollided && midRight.isCollided()){
            posX = oldPosX;
         }
         
         if(topLeftCollided && !midLeftCollided){
            if(topLeft.getPosX()%40>32)posX+= 2;
            else posY+=2;
            
         }else if(botLeftCollided && !midLeftCollided){
            posX+= 2;
         }
         if(topRightCollided && !midRightCollided){
            if(topRight.getPosX()%40>32)posX-= 2;
            else posY+=2;   
         }
         else if(botRightCollided && !midRightCollided){
            posX-= 0;
         }
      }
      
      roundPosX = (int)Math.round(posX);
      roundPosY = (int)Math.round(posY);
      posX = roundPosX;
      posY = roundPosY;
      
      // do head collision-detection
      tempCollisionInfo = game.checkTileCollisionFromMapCoordinates(roundPosX, roundPosY-height);
      
      if(!jumping && !falling){
         if(tempCollisionInfo.isCollided()){
            if(tempCollisionInfo.getTileType()==0){
               if(movementSpeed>0)posX = oldPosX-1;
               if(movementSpeed<0)posX = oldPosX+1;
               roundPosX = (int)Math.round(posX);
               updateColRectPosition();
               movementSpeed = 0;
            }
            else if(tempCollisionInfo.getTileType()==1){
               SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(roundPosX, roundPosY-height);
               if(tile.isSlopingAtTop()){
                  if(movementSpeed>0)posX = oldPosX-1;
                  if(movementSpeed<0)posX = oldPosX+1;
                  roundPosX = (int)Math.round(posX);
                  updateColRectPosition();
                  movementSpeed = 0;
               }else{
                  if(tempCollisionInfo.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(tempCollisionInfo.getPosX()%40)){
                     if(movementSpeed>0)posX = oldPosX-1;
                     if(movementSpeed<0)posX = oldPosX+1;
                     roundPosX = (int)Math.round(posX);
                     tempCollisionInfo = game.checkTileCollisionFromMapCoordinates(roundPosX, roundPosY-height);
                     if(tempCollisionInfo.isCollided()){
                        SlopedTile tempTile = (SlopedTile)game.getTileFromCoordinates(roundPosX, roundPosY-height);
                        int leyway = (int)Math.round(posY%40);
//                        System.out.println("Leyway: "+leyway);
                        int yInsideTile = (int)Math.round(tempTile.findYOnLineBetweenTwoPoints(roundPosX%40))-((roundPosY-height)%40);

//                        System.out.println("yInsideTile: "+yInsideTile);
                        if(yInsideTile > 0 && leyway>=yInsideTile)posY-=yInsideTile-leyway;
                        else if(yInsideTile > 0) posY+=leyway;
                        roundPosY = (int)Math.round(posY);
                        
                        while(tempTile!=null && tempTile.findYOnLineBetweenTwoPoints(roundPosX%40)+1 > (roundPosY-height)%40){
                           if(tempTile.isSlopingUpRight())posX+=1;
                           else posX-=1;
                           roundPosX = (int)Math.round(posX);
//                           System.out.println("roundPosX: "+roundPosX);
                           try {
                              tempTile = (SlopedTile)game.getTileFromCoordinates(roundPosX, roundPosY-height);
                           } catch (Exception e) {
                              break;
                           }
                        }
                     }
                     updateColRectPosition();
                     movementSpeed = 0;
                  }
               }
            }
         }
      }
      else{ // if jumping or falling
         if(tempCollisionInfo.isCollided()){
            if(tempCollisionInfo.getTileType()==0){
//               System.out.println("Colliding with fullcoltile"); TODO
               if(jumping)posY = (int)Math.round(posY+(jumpSpeed*elapsedTime));
               else if(falling)posY = (int)Math.round(posY+(fallSpeed*elapsedTime));
               if(movementSpeed!=0){
                  if(movementSpeed>0)posX-=movementSpeed*elapsedTime;
                  else if(movementSpeed<0)posX-=movementSpeed*elapsedTime;
               }
               posX = oldPosX;
               posY = oldPosY;
               roundPosX = (int)Math.round(posX);
               roundPosY = (int)Math.round(posY);
               
               updateColRectPosition();
               if(jumping){
                  stopJumping();
                  startFalling();
               }
            }
            else if(tempCollisionInfo.getTileType()==1){
               SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(roundPosX, roundPosY-height);
               if(tile.isSlopingAtTop()){
                  if(jumping)posY = (int)Math.round(posY+(jumpSpeed*elapsedTime));
                  else if(falling)posY = (int)Math.round(posY+(fallSpeed*elapsedTime));
                  if(movementSpeed!=0){
                     if(movementSpeed>0)posX-=movementSpeed*elapsedTime;
                     else if(movementSpeed<0)posX-=movementSpeed*elapsedTime;
                  }
                  roundPosX = (int)Math.round(posX);
                  
                  updateColRectPosition();
                  if(jumping){
                     stopJumping();
                     startFalling();
                  }
               }else{
                  if(tempCollisionInfo.getPosY()%40 < tile.findYOnLineBetweenTwoPoints(tempCollisionInfo.getPosX()%40)){
                     if(jumping)posY = (int)Math.round(posY+(jumpSpeed*elapsedTime));
                     else if(falling)posY = (int)Math.round(posY+(fallSpeed*elapsedTime));
                     if(movementSpeed!=0){
                        if(movementSpeed>0)posX-=movementSpeed*elapsedTime;
                        else if(movementSpeed<0)posX-=movementSpeed*elapsedTime;
                     }
                     
                     roundPosX = (int)Math.round(posX);
                     roundPosY = (int)Math.round(posY);
                     updateColRectPosition();
                     if(jumping){
                        stopJumping();
                        startFalling();
                     }
                  }
               }
            }
         }
      }

      posX = roundPosX;
      posY = roundPosY;
      
      // do foot collision detection and adjustments
      tempCollisionInfo = game.checkTileCollisionFromMapCoordinates(roundPosX, roundPosY);
      if(tempCollisionInfo.isCollided()){
         if(!jumping && !falling){
            if(tempCollisionInfo.getTileType()==0){
               FullColTile tile = (FullColTile)game.getTileFromCoordinates(roundPosX, roundPosY);
               // if we hit a fullcoltile that isn't a transition-tile, make sure to move the player to the top of it, if he isn't already
               if(!tile.isTransition()){
                  if(roundPosY %20 != 0){
                     int count = 40;
                     while(count!=0 && posY %20 != 0){
//                        System.out.println("While 1 - Count: "+count);
                        posY--;
                        count--;
                     }
                     roundPosY = (int)Math.round(posY);
                     updateColRectPosition();
                  }
               }else{
               // if we hit a transition-tile, move the player up, and if he isn't correctly placed, move him upwards.
                  if(roundPosX%40 < 7 && movementSpeed > 0 || roundPosX%40 > 32 && movementSpeed < 0){
                     if(roundPosY %10 != 9){
                        posY = roundPosY;
                        int count = 40;
                        while(count!=0 && posY %10 != 9){
//                           System.out.println("While 2 - Count: "+count);
                           posY--;
                           count--;
                        }
                     }
                     roundPosY = (int)Math.round(posY);
                     updateColRectPosition();
                  }
               }
            }else if(tempCollisionInfo.getTileType()==1){
               SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(roundPosX, roundPosY);
               if(tile.isSlopingAtTop()){
                  int xPositionInTile = roundPosX%40;
                  int yPositionInTile = roundPosY%40;
                  int yOnLine = (int)Math.round(tile.findYOnLineBetweenTwoPoints(xPositionInTile));
                  posY -= yOnLine-(38-yPositionInTile)-2;
                  roundPosY = (int)Math.round(posY);
               }else{
//                  System.out.println("SPECIOL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                  // special circumstances, where the player lands lower than the tile he's trying to jump to,
                  // because we can't ensure that after he has been allowed to move by the horizontal collision-detectors,
                  // that his player position doesn't fall just below the desired tile. This is a crappy fix, but it works.
                  if(game.checkTileCollisionFromMapCoordinates((int)Math.round(posX), (int)Math.round(posY)).isCollided()){
                     CollisionAndType temp = game.checkTileCollisionFromMapCoordinates((int)Math.round(posX), (int)Math.round(posY)-1);
                     if(temp.getTileType()==1){
                        posY-=1;
                        roundPosY = (int)Math.round(posY);
                     }
                  }

               }
            }
         }
         else if(falling){
               if(tempCollisionInfo.getTileType()==0){
                  FullColTile tile = (FullColTile)game.getTileFromCoordinates(roundPosX, roundPosY);
                  // if we hit a fullcoltile that isn't a transition-tile, make sure to move the player to the top of it, if he isn't already
                  if(!tile.isTransition()){
                     if(roundPosY %40 != 0){
                        int count = 40;
                        posY = roundPosY;
                        while(count!=0 && posY %40 != 0){
//                           System.out.println("While 3 - Count: "+count);
                           posY--;
                           count--;
                        }
                        roundPosY = (int)Math.round(posY);
                     }
                     stopFalling();
                  }else{
                  // if we hit a transition-tile, move the player up, and if he isn't correctly placed, move him upwards.
                     posY = roundPosY-1;
                     int count = 40;
                     while(count!=0 && posY %40 != 39){
//                        System.out.println("While 4 - Count: "+count);
                        posY--;
                        count--;
                     }
                     roundPosY = (int)Math.round(posY);
                     stopFalling();
                  }
               }else if(tempCollisionInfo.getTileType()==1){
                  SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(roundPosX, roundPosY);
                  if(tile.isSlopingAtTop()){
                     if(!tile.isSlopingUpRight()){
                        // if he has hit below the slope
                        if(39-(roundPosY % 40) < tile.findYOnLineBetweenTwoPoints(roundPosX % 40)){
                           int xPositionInTile = roundPosX%40;
                           int yPositionInTile = roundPosY%40;
                           int yOnLine = (int)Math.round(tile.findYOnLineBetweenTwoPoints(xPositionInTile));
                           posY -= yOnLine-(39-yPositionInTile)+1;
                           roundPosY = (int)Math.round(posY);
                           updateColRectPosition();
                           stopFalling();
                        }
                     }
                     else if(tile.isSlopingUpRight()){
                        // if he has hit below the slope
                        if(39-(roundPosY % 40) < tile.findYOnLineBetweenTwoPoints(roundPosX % 40)){
                           int xPositionInTile = roundPosX%40;
                           int yPositionInTile = roundPosY%40;
                           int yOnLine = (int)Math.round(tile.findYOnLineBetweenTwoPoints(xPositionInTile));
                           posY -= yOnLine-(39-yPositionInTile)+1;
                           roundPosY = (int)Math.round(posY);
                           updateColRectPosition();
                           stopFalling();
                        }
                     }
                  }
                  else{ // !tile.isSlopingAtTop()
                     if(roundPosY %40 != 0){
                        posY = roundPosY;
                        int count = 40;
                        while(count!=0 && posY %40 != 0){
//                           System.out.println("While 5 - Count: "+count);
                           posY--;
                           count--;
                        }
                        roundPosY = (int)Math.round(posY);
                     }
                     stopFalling();
                  }
               }
         }else if(jumping){
            if(tempCollisionInfo.getTileType()==0){
               FullColTile tile = (FullColTile)game.getTileFromCoordinates(roundPosX, roundPosY);
               // if we hit a fullcoltile that isn't a transition-tile, make sure to move the player to the top of it, if he isn't already
               if(!tile.isTransition()){
                  if(roundPosY %40 != 0){
                     posY = roundPosY;
                     int count = 40;
                     while(posY %40 != 0){
//                        System.out.println("While 6 - Count: "+count);
                        posY--;
                        count--;
                     }
                     roundPosY = (int)Math.round(posY);
                  }
                  stopJumping();
               }else{
               // if we hit a transition-tile, move the player up, and if he isn't correctly placed, move him upwards.
                  posY = roundPosY-1;
                  int count = 40;
                  while(posY %40 != 39){
//                     System.out.println("While 7 - Count: "+count);
                     posY--;
                     count--;
                  }
                  roundPosY = (int)Math.round(posY);
                  stopJumping();
               }
            }else if(tempCollisionInfo.getTileType()==1){
               SlopedTile tile = (SlopedTile)game.getTileFromCoordinates(roundPosX, roundPosY);
               if(tile.isSlopingAtTop()){
                  if(!tile.isSlopingUpRight()){
                     // if he has hit below the slope
                     if(39-(roundPosY % 40) < tile.findYOnLineBetweenTwoPoints(roundPosX % 40)){
                        int xPositionInTile = roundPosX%40;
                        int yPositionInTile = roundPosY%40;
                        int yOnLine = (int)Math.round(tile.findYOnLineBetweenTwoPoints(xPositionInTile));
                        posY -= yOnLine-(39-yPositionInTile)+1;
                        roundPosY = (int)Math.round(posY);
                        updateColRectPosition();
                        stopJumping();
                     }
                  }
                  else if(tile.isSlopingUpRight()){
                     // if he has hit below the slope
                     if(39-(roundPosY % 40) < tile.findYOnLineBetweenTwoPoints(roundPosX % 40)){
                        int xPositionInTile = roundPosX%40;
                        int yPositionInTile = roundPosY%40;
                        int yOnLine = (int)Math.round(tile.findYOnLineBetweenTwoPoints(xPositionInTile));
                        posY -= yOnLine-(39-yPositionInTile)+1;
                        roundPosY = (int)Math.round(posY);
                        updateColRectPosition();
                        stopJumping();
                     }
                  }
               }
               else{ // !tile.isSlopingAtTop()
                  if(roundPosY %40 != 0){
                     posY = roundPosY;
                     int count = 40;
                     while(count!=0 && posY %40 != 0){
//                        System.out.println("While 8 - Count: "+count);
                        posY--;
                        count--;
                     }
                     roundPosY = (int)Math.round(posY);
                  }
                  stopJumping();
               }
            }
      }
         posY = roundPosY;
      }else{
         // DEBUGGING!
         // We're not colliding with anything!! If we're not jumping or falling, that's weird.
         // Since we're just walking around, that either means a calculation has
         // gone a pixel off, or we're trying to float in mid-air. So first we check for a tile beneath us.
         if(!jumping && !falling){
            tempCollisionInfo = game.checkTileCollisionFromMapCoordinates(roundPosX, roundPosY+1);
            if(tempCollisionInfo.isCollided())posY = roundPosY+1;
            else{
               // We're trying to fly!
               startFalling();
            }
         }
      }

      // after the repositioning of the player, correct the collision.rectangle
      colRect.x = (int)Math.round(posX)-10;
      colRect.y = (int)Math.round(posY)-64;

      // after correct placement has been done, update internal collision rectangles
      if(internalColRects!=null && internalColRects.length>0){
         for (int i = 0; i < internalColRects.length; i++) {
            internalColRects[i].x = (int)Math.round(posX)-(animations[currentAnim].getImage().getWidth()/2);
            internalColRects[i].y = (int)Math.round(posY);
         }
      }
      animations[currentAnim].update(elapsedTime);
   }

   public void createMainCollisionRectangle() {
      this.colRect = new Rectangle((int)Math.round(posX)-(animations[currentAnim].getImage().getWidth()/2), (int)Math.round(posY)-(animations[currentAnim].getImage().getHeight()), 18, 65);
   }

   public void createInternalCollisionRectangles() {
      // TODO Auto-generated method stub

   }

   public void moveLeft(){
      facingRight = false;
      if(movementSpeed>-maxMovementSpeed){
         movementSpeed -= 0.006;
         if(movementSpeed>0)movementSpeed -= 0.006;
      }
      if(currentAnim!=1 && currentAnim!=2)currentAnim=1;
   }

   public void moveRight(){
      facingRight = true;
      if(movementSpeed<maxMovementSpeed){
         movementSpeed += 0.006;
         if(movementSpeed<0)movementSpeed += 0.006;
      }
      if(currentAnim!=1 && currentAnim!=2)currentAnim=1;
   }

   public void moveDown(){
      // go through otherwise one-way tiles
   }

   public int getMeleeDelay() {
      return meleeDelay;
   }

   public double getMaxMovementSpeed() {
      return maxMovementSpeed;
   }

   public double getMovementSpeed() {
      return movementSpeed;
   }

   public double getAngle() {
      return angle;
   }

   public boolean isFacingRight() {
      return facingRight;
   }

   public boolean isMoving() {
      return moving;
   }

   public int getCurrentAnim() {
      return currentAnim;
   }

   public String[] getAnimationIDs() {
      return animationIDs;
   }

   public Animation[] getAnimations() {
      return animations;
   }

   public boolean[] getKeys() {
      return keys;
   }

   public Game getGame() {
      return game;
   }

   public void setGame(Game game) {
      this.game = game;
   }

   public int getWidth(){
      return animations[currentAnim].getImage().getWidth();
   }
   public BufferedImage getImage(){
      return animations[currentAnim].getImage();
   }

   public void setCurrentAnim(int currentAnim) {
      this.currentAnim = currentAnim;
   }

   public void setAnimations(Animation[] animations) {
      this.animations = animations;
   }

   public boolean isFalling() {
      return falling;
   }

   public boolean isJumping() {
      return jumping;
   }

   public void setFalling(boolean falling) {
      this.falling = falling;
   }

   public void setJumping(boolean jumping) {
      this.jumping = jumping;
   }

   public void setJumpTickCounter(int jumpTickCounter) {
      this.jumpTickCounter = jumpTickCounter;
   }
   public void startJumping(){
      jumping = true;
      jumpTickCounter = 0;
      jumpSpeed = 0;
      currentAnim = JUMPING;
   }
   public void startFalling(){
      falling = true;
      fallSpeed = 0;
      currentAnim = FALLING;
   }
   public void stopJumping(){
      jumping = false;
      jumpTickCounter = 0;
   }
   public void stopFalling(){
      falling = false;
      fallSpeed = 0;
      if(movementSpeed != 0)currentAnim = WALKING;
      else currentAnim = STANDING;
   }

   public BufferedImage getArm() {
      return arm;
   }

   public void setArm(BufferedImage arm) {
      this.arm = arm;
   }

   public int getArmOffsetX() {
      return armOffsetX;
   }

   public int getArmOffsetY() {
      return armOffsetY;
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
EgonOlsen (364 views)
2018-06-10 19:43:48

EgonOlsen (364 views)
2018-06-10 19:43:44

EgonOlsen (304 views)
2018-06-10 19:43:20

DesertCoockie (540 views)
2018-05-13 18:23:11

nelsongames (871 views)
2018-04-24 18:15:36

nelsongames (854 views)
2018-04-24 18:14:32

ivj94 (1306 views)
2018-03-24 14:47:39

ivj94 (426 views)
2018-03-24 14:46:31

ivj94 (1089 views)
2018-03-24 14:43:53

Solater (445 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!