Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void collisionDetect(GameState *game) {
- //Check for collision with any ledges (brick blocks)
- for (int tile = 0; tile < 300; tile++) {
- float playerWidth = 15 * 2, playerHeight = 32 * 2;
- float playerX = game->player.x + (9 * 2) + 1, playerY = game->player.y;
- float blockX = game->blocks[tile].x, blockY = game->blocks[tile].y, blockWidth = game->blocks[tile].w, blockHeight = game->blocks[tile].h;
- if (playerX + playerWidth / 2 > blockX && playerX + playerWidth / 2 < blockX + blockWidth) {
- //are we bumping our head?
- if (playerY < blockY + blockHeight && playerY > blockY && game->player.dy < 0) {
- //correct y
- game->player.y = blockY + blockHeight;
- playerY = blockY + blockHeight;
- game->player.dy = 0;
- game->player.onLedge = 1;
- }
- }
- if (playerX + playerWidth > blockX && playerX < blockX + blockWidth) {
- //are we landing on the ledge
- if (playerY + playerHeight > blockY && playerY < blockY && game->player.dy > 0) {
- //correct y
- game->player.y = blockY - playerHeight;
- playerY = blockY - playerHeight;
- game->player.dy = 0;
- game->player.onLedge = 1;
- }
- }
- if (playerY + playerHeight > blockY && playerY < blockY + blockHeight) {
- //rubbing against right edge
- if (playerX < blockX + blockWidth && playerX + playerWidth > blockX + blockWidth && game->player.dx < 0) {
- //correct x
- game->player.x = blockX + blockWidth;
- playerX = blockX + blockWidth;
- game->player.dx = 0;
- }//rubbing against left edge
- else if (playerX + playerWidth > blockX && playerX < blockX && game->player.dx > 0) {
- //correct x
- game->player.x = blockX - playerWidth;
- playerX = blockX - playerWidth;
- game->player.dx = 0;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment