Guest User

Untitled

a guest
Dec 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. void collisionDetect(GameState *game) {
  2. //Check for collision with any ledges (brick blocks)
  3.  
  4. for (int tile = 0; tile < 300; tile++) {
  5. float playerWidth = 15 * 2, playerHeight = 32 * 2;
  6. float playerX = game->player.x + (9 * 2) + 1, playerY = game->player.y;
  7. float blockX = game->blocks[tile].x, blockY = game->blocks[tile].y, blockWidth = game->blocks[tile].w, blockHeight = game->blocks[tile].h;
  8.  
  9. if (playerX + playerWidth / 2 > blockX && playerX + playerWidth / 2 < blockX + blockWidth) {
  10. //are we bumping our head?
  11. if (playerY < blockY + blockHeight && playerY > blockY && game->player.dy < 0) {
  12. //correct y
  13. game->player.y = blockY + blockHeight;
  14. playerY = blockY + blockHeight;
  15.  
  16. game->player.dy = 0;
  17. game->player.onLedge = 1;
  18. }
  19. }
  20. if (playerX + playerWidth > blockX && playerX < blockX + blockWidth) {
  21. //are we landing on the ledge
  22. if (playerY + playerHeight > blockY && playerY < blockY && game->player.dy > 0) {
  23. //correct y
  24. game->player.y = blockY - playerHeight;
  25. playerY = blockY - playerHeight;
  26.  
  27. game->player.dy = 0;
  28. game->player.onLedge = 1;
  29. }
  30. }
  31.  
  32. if (playerY + playerHeight > blockY && playerY < blockY + blockHeight) {
  33. //rubbing against right edge
  34. if (playerX < blockX + blockWidth && playerX + playerWidth > blockX + blockWidth && game->player.dx < 0) {
  35. //correct x
  36. game->player.x = blockX + blockWidth;
  37. playerX = blockX + blockWidth;
  38. game->player.dx = 0;
  39.  
  40.  
  41. }//rubbing against left edge
  42. else if (playerX + playerWidth > blockX && playerX < blockX && game->player.dx > 0) {
  43. //correct x
  44. game->player.x = blockX - playerWidth;
  45. playerX = blockX - playerWidth;
  46. game->player.dx = 0;
  47.  
  48. }
  49. }
  50. }
  51. }
Add Comment
Please, Sign In to add comment