Advertisement
pierrotdu18

Untitled

Dec 2nd, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. void playerCollisions(Fixed2D* player, Fixed theta, const char** level, int* collision, Rect* collisionPosition, Fixed scrolling)
  2. {
  3.     *collision = 0b00000000;
  4.    
  5.     if (fixtoi(player->y) >= GROUND_HEIGHT)
  6.         *collision |= COLLISION_GROUND;
  7.    
  8.     Rect playerCenter = {fixtoi(player->x) + TILE_WIDTH / 2, fixtoi(player->y) + TILE_WIDTH / 2};
  9.  
  10.     int inTileSet_playerX = fixtoi(player->x) / TILE_WIDTH;
  11.     int inTileSet_playerY = fixtoi(player->y) / TILE_WIDTH;
  12.  
  13.     Rect reachableTiles[] = {{inTileSet_playerX + 0, inTileSet_playerY + 1},
  14.                              {inTileSet_playerX + 0, inTileSet_playerY - 1},
  15.                              {inTileSet_playerX + 1, inTileSet_playerY + 0},
  16.                              {inTileSet_playerX - 1, inTileSet_playerY + 0},
  17.                              {inTileSet_playerX + 1, inTileSet_playerY + 1},
  18.                              {inTileSet_playerX + 1, inTileSet_playerY - 1},
  19.                              {inTileSet_playerX - 1, inTileSet_playerY + 1},
  20.                              {inTileSet_playerX - 1, inTileSet_playerY - 1}};
  21.     int i, itemI;
  22.     for (i = 0; i < 8; ++i)
  23.     {
  24.         int tileType = level[reachableTiles[i].x][reachableTiles[i].y] - '0';
  25.         if (tileType == ITEM_EMPTY) break;
  26.        
  27.         Rect item[4];
  28.        
  29.         if (tileType == ITEM_BLOCK)
  30.         {
  31.             item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH};
  32.             item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH};
  33.             item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
  34.             item[3] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
  35.             for (itemI = 0; itemI < 4; itemI++)
  36.             {
  37.                 Rect A = item[itemI];
  38.                 Rect B = item[itemI + 1 % 4];
  39.                 if (collisionSegmentCircle(A, B, playerCenter, TILE_WIDTH / 2))
  40.                 {
  41.                     *collision |= COLLISION_BLOCK;
  42.                     *collisionPosition = reachableTiles[i];
  43.                     break;
  44.                 }
  45.             }
  46.         }
  47.         else if (tileType == ITEM_LINE)
  48.         {
  49.             Rect A = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + 14};
  50.             Rect B = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH + 14};
  51.             if (collisionSegmentCircle(A, B, playerCenter, TILE_WIDTH / 2))
  52.             {
  53.                 *collision |= COLLISION_DIE;
  54.                 return;
  55.             }
  56.         }
  57.         else
  58.         {
  59.             switch (tileType)
  60.             {
  61.                 case ITEM_T_SPIKE:
  62.                     item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
  63.                     item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
  64.                     item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH/2, reachableTiles[i].y * TILE_WIDTH};
  65.                     break;
  66.                 case ITEM_B_SPIKE:
  67.                     item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH};
  68.                     item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH};
  69.                     item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH/2, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
  70.                     break;
  71.                 case ITEM_L_SPIKE:
  72.                     item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH};
  73.                     item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH/2};
  74.                     item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
  75.                     break;
  76.                 case ITEM_R_SPIKE:
  77.                     item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH};
  78.                     item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH/2};
  79.                     item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
  80.                     break;
  81.             }
  82.             for (itemI = 0; itemI < 3; itemI++)
  83.             {
  84.                 Rect A = item[itemI];
  85.                 Rect B = item[itemI + 1 % 3];
  86.                 if (collisionSegmentCircle(A, B, playerCenter, TILE_WIDTH / 2))
  87.                 {
  88.                     *collision |= COLLISION_DIE;
  89.                     return;
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement