Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void playerCollisions(Fixed2D* player, Fixed theta, const char** level, int* collision, Rect* collisionPosition, Fixed scrolling)
- {
- *collision = 0b00000000;
- if (fixtoi(player->y) >= GROUND_HEIGHT)
- *collision |= COLLISION_GROUND;
- Rect playerCenter = {fixtoi(player->x) + TILE_WIDTH / 2, fixtoi(player->y) + TILE_WIDTH / 2};
- int inTileSet_playerX = fixtoi(player->x) / TILE_WIDTH;
- int inTileSet_playerY = fixtoi(player->y) / TILE_WIDTH;
- Rect reachableTiles[] = {{inTileSet_playerX + 0, inTileSet_playerY + 1},
- {inTileSet_playerX + 0, inTileSet_playerY - 1},
- {inTileSet_playerX + 1, inTileSet_playerY + 0},
- {inTileSet_playerX - 1, inTileSet_playerY + 0},
- {inTileSet_playerX + 1, inTileSet_playerY + 1},
- {inTileSet_playerX + 1, inTileSet_playerY - 1},
- {inTileSet_playerX - 1, inTileSet_playerY + 1},
- {inTileSet_playerX - 1, inTileSet_playerY - 1}};
- int i, itemI;
- for (i = 0; i < 8; ++i)
- {
- int tileType = level[reachableTiles[i].x][reachableTiles[i].y] - '0';
- if (tileType == ITEM_EMPTY) break;
- Rect item[4];
- if (tileType == ITEM_BLOCK)
- {
- item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH};
- item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH};
- item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
- item[3] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
- for (itemI = 0; itemI < 4; itemI++)
- {
- Rect A = item[itemI];
- Rect B = item[itemI + 1 % 4];
- if (collisionSegmentCircle(A, B, playerCenter, TILE_WIDTH / 2))
- {
- *collision |= COLLISION_BLOCK;
- *collisionPosition = reachableTiles[i];
- break;
- }
- }
- }
- else if (tileType == ITEM_LINE)
- {
- Rect A = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + 14};
- Rect B = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH + 14};
- if (collisionSegmentCircle(A, B, playerCenter, TILE_WIDTH / 2))
- {
- *collision |= COLLISION_DIE;
- return;
- }
- }
- else
- {
- switch (tileType)
- {
- case ITEM_T_SPIKE:
- item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
- item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
- item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH/2, reachableTiles[i].y * TILE_WIDTH};
- break;
- case ITEM_B_SPIKE:
- item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH};
- item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH};
- item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH/2, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
- break;
- case ITEM_L_SPIKE:
- item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH};
- item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH/2};
- item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
- break;
- case ITEM_R_SPIKE:
- item[0] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH};
- item[1] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling) + TILE_WIDTH, reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH/2};
- item[2] = (Rect){reachableTiles[i].x * TILE_WIDTH - fixtoi(scrolling), reachableTiles[i].y * TILE_WIDTH + TILE_WIDTH};
- break;
- }
- for (itemI = 0; itemI < 3; itemI++)
- {
- Rect A = item[itemI];
- Rect B = item[itemI + 1 % 3];
- if (collisionSegmentCircle(A, B, playerCenter, TILE_WIDTH / 2))
- {
- *collision |= COLLISION_DIE;
- return;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement