Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include "textures.h"
- #include "tile.h"
- #include "collidables.h"
- int main()
- {
- const int tileSize = 32;
- const int leftTileSection = 4;
- const int middleTileSection = 12;
- const int rightTileSection = 4;
- const int numTilesWide = (leftTileSection+middleTileSection+rightTileSection);
- const int numTilesHigh = numTilesWide;
- const int numPixelsWide = numTilesWide*tileSize;
- const int numPixelsHigh = numTilesHigh*tileSize;
- const int bottomTileIndex = numTilesHigh-3;
- sf::RenderWindow window(sf::VideoMode(numPixelsWide, numPixelsHigh), "Water Fail");
- Textures textures;
- window.setFramerateLimit(60);
- //add stationary collidables
- Collidables collidables(tileSize);
- for(int i = 0; i < numPixelsHigh; i++)
- {
- for (int j = 0; j < leftTileSection; j++)
- {
- sf::Vector2i tempPosition;
- tempPosition.x = j*tileSize;
- tempPosition.y = i*tileSize;
- if (i == bottomTileIndex || i == bottomTileIndex+1)
- {
- collidables.addCollidable(tempPosition, "ledge");
- std::cout << tempPosition.x << ", " << tempPosition.y << std::endl;
- }
- }
- }
- //test player
- sf::Vector2i playerPosition(tileSize*1, 544-tileSize);
- int playerVelocity = 50;
- int gravity = 5;
- bool falling = false;
- bool jumping = false;
- //main loop
- while (window.isOpen())
- {
- sf::Event evnt;
- while (window.pollEvent(evnt))
- {
- switch (evnt.type)
- {
- case sf::Event::Closed:
- window.close();
- break;
- case sf::Event::KeyPressed:
- if (evnt.key.code == sf::Keyboard::W)
- jumping = true;
- break;
- default:
- break;
- }
- }
- window.clear(sf::Color(255, 255, 255));
- //draw left section
- for(int i = 0; i < numTilesHigh; i++)
- {
- for (int j = 0; j < numTilesWide; j++)
- {
- if (j < leftTileSection)
- {
- Tile leftTiles(textures.getTexture(0), tileSize);
- leftTiles.setRect(0, tileSize);
- if (i == bottomTileIndex)
- leftTiles.setRect(0, tileSize*2);
- if (i == bottomTileIndex+1)
- leftTiles.setRect(0, tileSize*3);
- if (j == leftTileSection-1 && i == bottomTileIndex)
- leftTiles.setRect(tileSize, tileSize*2);
- if (j == leftTileSection-1 && i == bottomTileIndex+1)
- leftTiles.setRect(tileSize, tileSize*3);
- leftTiles.drawOnGrid(j, i, window);
- }
- }
- }
- //draw middle section
- for(int i = 0; i < numTilesHigh; i++)
- {
- for (int j = 0; j < numTilesWide; j++)
- {
- if (j >= leftTileSection && j < leftTileSection+middleTileSection)
- {
- Tile centerTiles(textures.getTexture(0), tileSize);
- centerTiles.setRect(0, tileSize*4);
- centerTiles.drawOnGrid(j, i, window);
- }
- }
- }
- Tile playerTile(textures.getTexture(0), tileSize);
- playerTile.setRect(0, 0);
- playerTile.draw(playerPosition.x, playerPosition.y, window);
- if (jumping == true && playerVelocity <= 0)
- falling = true;
- if (falling == true)
- {
- sf::Vector2i tempPlayerPosition = playerPosition;
- tempPlayerPosition.y = tempPlayerPosition.y-playerVelocity;
- if (collidables.checkForCollision(tempPlayerPosition) == true)
- {
- playerPosition.y = 544-tileSize;
- playerVelocity = 50;
- falling = false;
- jumping = false;
- }
- }
- if (jumping == true)
- {
- playerVelocity = playerVelocity-gravity;
- playerPosition.y = playerPosition.y-playerVelocity;
- std::cout << "jumping " << jumping << " falling " << falling << " velocity " << playerVelocity << " y " << playerPosition.y << std::endl;
- }
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement