Advertisement
NovaYoshi

ApplyVelocity()

Jul 12th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1.   function ApplyVelocity(VelocityX, VelocityY) {
  2.     OldStandingOnSolid = StandingOnSolid;
  3.     StandingOnSolid = false;
  4.     SideBlocked = false;
  5.  
  6.     local OldPosX = PosX, OldPosY = PosY;
  7.  
  8.     // check against touching stuff
  9.     if(VelocityX > 0) {
  10.       if(!lib.ChkBlockFlagXY(((PosX+VelocityX)>>8)+Width-1, (PosY>>8) + (Height/2), BlockFlags.SolidMiddle)
  11.       && !lib.ChkBlockFlagXY(((PosX+VelocityX)>>8)+Width-1, (PosY>>8) + (Height-1), BlockFlags.SolidMiddle)
  12.       && !lib.ChkBlockFlagXY(((PosX+VelocityX)>>8)+Width-1, (PosY>>8), BlockFlags.SolidMiddle))
  13.         PosX += VelocityX;
  14.       else {
  15.         PosX = (((PosX+VelocityX)+(Width-1<<8)) & ~0xfff) - (16<<8);
  16.         SideBlocked = true;
  17.       }
  18.     } else {
  19.       if(!lib.ChkBlockFlagXY((PosX+VelocityX)>>8, (PosY>>8) + (Height/2), BlockFlags.SolidMiddle)
  20.       && !lib.ChkBlockFlagXY((PosX+VelocityX)>>8, (PosY>>8) + (Height-1), BlockFlags.SolidMiddle)
  21.       && !lib.ChkBlockFlagXY((PosX+VelocityX)>>8, (PosY>>8), BlockFlags.SolidMiddle))
  22.         PosX += VelocityX;
  23.       else {
  24.         PosX = ((PosX-VelocityX) & ~0xfff);
  25.         SideBlocked = true;
  26.       }
  27.     }
  28.  
  29.     // check for solids above
  30.     if(lib.ChkBlockFlagXY((PosX>>8), (PosY>>8), BlockFlags.SolidMiddle)
  31.     || lib.ChkBlockFlagXY((PosX>>8)+(Width-1), (PosY>>8), BlockFlags.SolidMiddle)) {
  32.       VelY = 0x0f0;
  33.     } else {
  34.     }
  35.     PosY += VelocityY;
  36.  
  37.     // check for flat floor (best kind)
  38.     if(lib.ChkBlockFlagXY((PosX>>8), (PosY>>8) + Height, BlockFlags.SolidTop)
  39.     || lib.ChkBlockFlagXY((PosX>>8)+(Width-1), (PosY>>8) + Height, BlockFlags.SolidTop)) {
  40.       local CanFallLeft = lib.ChkBlockFlagXY((PosX>>8), (PosY>>8) + Height, BlockFlags.FallThrough);
  41.       local CanFallRight = lib.ChkBlockFlagXY((PosX>>8) + Width - 1, (PosY>>8) + Height, BlockFlags.FallThrough);
  42.       if((VelocityY >= 0 && ((((PosY>>8) + Height) >>4) != (((OldPosY>>8) + Height - 1)>>4)))
  43.       && !(WillFallThrough && CanFallLeft && CanFallRight)) {
  44.         PosY = ((PosY+(Height<<8)) & 0xf000)-(Height<<8);
  45.         VelY = 0;
  46.         StandingOnSolid = true;
  47.       }
  48.     }
  49.  
  50.     // slopes mostly fixed but still a bit buggy
  51.     if(lib.ChkBlockFlagXY((PosX>>8)+(Width-1), (PosY>>8)+Height-1, BlockFlags.SlopeL) && VelocityY >= 0) {
  52.       local PartX = ((PosX>>8)+(Width-1))&15, PartY = ((PosY>>8)+(Height-1))&15, MaxY = 15-PartX;
  53.       if(PartY>MaxY) PosY = (((((PosY>>8)+Height-1) & ~15)) - Height + MaxY) <<8;
  54.       StandingOnSolid = true;
  55.     }
  56.     if(lib.ChkBlockFlagXY((PosX>>8), (PosY>>8)+Height-1, BlockFlags.SlopeR) && VelocityY >= 0) {
  57.       local PartX = (PosX>>8)&15, PartY = ((PosY>>8)+(Height-1))&15, MaxY = PartX;
  58.       if(PartY>MaxY) PosY = (((((PosY>>8)+Height-1) & ~15)) - Height + MaxY) <<8;
  59.       StandingOnSolid = true;
  60.     }
  61.  
  62.     if(lib.ChkBlockType((PosX>>8), (PosY>>8) + Height - 1, 23)
  63.     || lib.ChkBlockType((PosX>>8)+(Width-1), (PosY>>8) + Height - 1, 23)) {
  64.        VelY = -0x0500;
  65.     }
  66.  
  67.   }
  68. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement