Advertisement
Guest User

Untitled

a guest
Apr 19th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include "textures.h"
  4. #include "tile.h"
  5. #include "collidables.h"
  6.  
  7. int main()
  8. {
  9. const int tileSize = 32;
  10. const int leftTileSection = 4;
  11. const int middleTileSection = 12;
  12. const int rightTileSection = 4;
  13. const int numTilesWide = (leftTileSection+middleTileSection+rightTileSection);
  14. const int numTilesHigh = numTilesWide;
  15. const int numPixelsWide = numTilesWide*tileSize;
  16. const int numPixelsHigh = numTilesHigh*tileSize;
  17. const int bottomTileIndex = numTilesHigh-3;
  18.  
  19. sf::RenderWindow window(sf::VideoMode(numPixelsWide, numPixelsHigh), "Water Fail");
  20. Textures textures;
  21. window.setFramerateLimit(60);
  22.  
  23. //add stationary collidables
  24. Collidables collidables(tileSize);
  25. for(int i = 0; i < numPixelsHigh; i++)
  26. {
  27. for (int j = 0; j < leftTileSection; j++)
  28. {
  29. sf::Vector2i tempPosition;
  30. tempPosition.x = j*tileSize;
  31. tempPosition.y = i*tileSize;
  32. if (i == bottomTileIndex || i == bottomTileIndex+1)
  33. {
  34. collidables.addCollidable(tempPosition, "ledge");
  35. std::cout << tempPosition.x << ", " << tempPosition.y << std::endl;
  36. }
  37. }
  38. }
  39.  
  40. //test player
  41. sf::Vector2i playerPosition(tileSize*1, 544-tileSize);
  42. int playerVelocity = 50;
  43. int gravity = 5;
  44. bool falling = false;
  45. bool jumping = false;
  46.  
  47. //main loop
  48. while (window.isOpen())
  49. {
  50. sf::Event evnt;
  51. while (window.pollEvent(evnt))
  52. {
  53. switch (evnt.type)
  54. {
  55. case sf::Event::Closed:
  56. window.close();
  57. break;
  58. case sf::Event::KeyPressed:
  59. if (evnt.key.code == sf::Keyboard::W)
  60. jumping = true;
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. window.clear(sf::Color(255, 255, 255));
  67.  
  68. //draw left section
  69. for(int i = 0; i < numTilesHigh; i++)
  70. {
  71. for (int j = 0; j < numTilesWide; j++)
  72. {
  73. if (j < leftTileSection)
  74. {
  75. Tile leftTiles(textures.getTexture(0), tileSize);
  76. leftTiles.setRect(0, tileSize);
  77. if (i == bottomTileIndex)
  78. leftTiles.setRect(0, tileSize*2);
  79. if (i == bottomTileIndex+1)
  80. leftTiles.setRect(0, tileSize*3);
  81. if (j == leftTileSection-1 && i == bottomTileIndex)
  82. leftTiles.setRect(tileSize, tileSize*2);
  83. if (j == leftTileSection-1 && i == bottomTileIndex+1)
  84. leftTiles.setRect(tileSize, tileSize*3);
  85. leftTiles.drawOnGrid(j, i, window);
  86. }
  87.  
  88. }
  89. }
  90.  
  91. //draw middle section
  92. for(int i = 0; i < numTilesHigh; i++)
  93. {
  94. for (int j = 0; j < numTilesWide; j++)
  95. {
  96. if (j >= leftTileSection && j < leftTileSection+middleTileSection)
  97. {
  98. Tile centerTiles(textures.getTexture(0), tileSize);
  99. centerTiles.setRect(0, tileSize*4);
  100. centerTiles.drawOnGrid(j, i, window);
  101. }
  102. }
  103. }
  104.  
  105. Tile playerTile(textures.getTexture(0), tileSize);
  106. playerTile.setRect(0, 0);
  107.  
  108. playerTile.draw(playerPosition.x, playerPosition.y, window);
  109.  
  110. if (jumping == true && playerVelocity <= 0)
  111. falling = true;
  112.  
  113. if (falling == true)
  114. {
  115. sf::Vector2i tempPlayerPosition = playerPosition;
  116. tempPlayerPosition.y = tempPlayerPosition.y-playerVelocity;
  117. if (collidables.checkForCollision(tempPlayerPosition) == true)
  118. {
  119. playerPosition.y = 544-tileSize;
  120. playerVelocity = 50;
  121. falling = false;
  122. jumping = false;
  123. }
  124. }
  125.  
  126. if (jumping == true)
  127. {
  128.  
  129. playerVelocity = playerVelocity-gravity;
  130. playerPosition.y = playerPosition.y-playerVelocity;
  131. std::cout << "jumping " << jumping << " falling " << falling << " velocity " << playerVelocity << " y " << playerPosition.y << std::endl;
  132. }
  133.  
  134. window.display();
  135. }
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement