Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef SFML_STATIC
- #pragma comment(lib, "glew.lib")
- #pragma comment(lib, "freetype.lib")
- #pragma comment(lib, "jpeg.lib")
- #pragma comment(lib, "opengl32.lib")
- #pragma comment(lib, "winmm.lib")
- #pragma comment(lib, "gdi32.lib")
- #endif // SFML_STATIC
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <cmath>
- using namespace std;
- class GameObject
- {
- public:
- enum Object_type
- {
- player,
- tile
- };
- ~GameObject()
- {
- }
- virtual void Update()
- {
- GetLastPosition();
- }
- void Draw(sf::RenderWindow& Window)
- {
- Window.draw(shape);
- }
- sf::RectangleShape shape;
- Object_type object_type;
- sf::Vector2f velocity;
- sf::Vector2f lastPosition;
- sf::Vector2f lastvelocity;
- int loopcounter;
- bool canjump;
- float speed;
- protected:
- void GetLastPosition()
- {
- loopcounter++;
- if (loopcounter == 2)
- {
- lastPosition = shape.getPosition();
- lastvelocity = velocity;
- loopcounter = 0;
- }
- }
- };
- class Player : public GameObject
- {
- public:
- Player()
- {
- object_type = player;
- speed = .1;
- loopcounter = 0;
- canjump = false;
- shape.setSize(sf::Vector2f(75, 75));
- shape.setFillColor(sf::Color::Blue);
- }
- ~Player()
- {
- }
- void Update()
- {
- GetLastPosition();
- shape.move(velocity.x, velocity.y);
- }
- };
- class Tile : public GameObject
- {
- public:
- enum Tile_type
- {
- ground
- };
- Tile(sf::Vector2f new_pos, Tile_type new_type)
- {
- tile_type = new_type;
- object_type = tile;
- shape.setSize(sf::Vector2f(125, 125));
- shape.setPosition(new_pos);
- if (tile_type == ground)
- {
- shape.setFillColor(sf::Color::Green);
- }
- }
- ~Tile()
- {
- };
- Tile_type tile_type;
- };
- class Camera
- {
- public:
- Camera(Player* player)
- {
- view.setSize(sf::Vector2f(1024, 768));
- view.setCenter(player->shape.getPosition());
- }
- ~Camera()
- {
- }
- void Update(Player* player)
- {
- view.setCenter(player->shape.getPosition());
- }
- void Draw(sf::RenderWindow& Window)
- {
- Window.setView(view);
- }
- sf::View view;
- };
- void MovePlayer(Player* player)
- {
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
- player->shape.move(-player->speed, 0);
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
- player->shape.move(player->speed, 0);
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && player->canjump)
- {
- player->velocity.y -= .2;
- player->canjump = false;
- }
- }
- void CheckCollision(std::vector<GameObject*>& objects)
- {
- for (auto it = objects.begin(); it != objects.end(); it++)
- {
- if ((*it)->object_type != GameObject::tile)
- {
- if ((*it)->lastPosition != (*it)->shape.getPosition())
- {
- int iteratorindex1 = std::distance(objects.begin(), it);
- for (auto it2 = objects.begin(); it2 != objects.end(); it2++)
- {
- int iteratorindex2 = std::distance(objects.begin(), it2);
- if ((*it)->shape.getGlobalBounds().intersects((*it2)->shape.getGlobalBounds()) && iteratorindex1 != iteratorindex2)
- {
- //checks left
- if ((*it)->lastPosition.x > (*it)->shape.getPosition().x)
- (*it)->shape.move((*it)->speed, 0);
- //checks right
- if ((*it)->lastPosition.x < (*it)->shape.getPosition().x)
- (*it)->shape.move(-(*it)->speed, 0);
- //checks bottom
- if ((*it)->lastPosition.y < (*it)->shape.getPosition().y)
- {
- if((*it)->shape.getPosition().x > (*it2)->shape.getPosition().x &&
- (*it)->shape.getPosition().x < (*it2)->shape.getPosition().x + (*it2)->shape.getGlobalBounds().width)
- {
- (*it)->shape.move(0, -(*it)->velocity.y);
- (*it)->velocity.y = 0;
- (*it)->canjump = true;
- }
- if ((*it)->shape.getPosition().x + (*it)->shape.getGlobalBounds().width >(*it2)->shape.getPosition().x &&
- (*it)->shape.getPosition().x + (*it)->shape.getGlobalBounds().width < (*it2)->shape.getPosition().x + (*it2)->shape.getGlobalBounds().width)
- {
- (*it)->shape.move(0, -(*it)->velocity.y);
- (*it)->velocity.y = 0;
- (*it)->canjump = true;
- }
- }
- //checks top
- }
- }
- }
- }
- }
- }
- const int map_w = 20;
- const int map_h = 10;
- const sf::Vector2f startingPoint = sf::Vector2f(1, 1);
- int tile_map[map_h][map_w] =
- {
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
- { 1, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
- { 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0 },
- { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
- { 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
- };
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(1024, 768), "");
- sf::Vector2f gravity(0, .0001);
- std::vector<GameObject*> objects;
- Player * player = new Player;
- Camera camera(player);
- for (int i = 0; i < map_h; i++)
- {
- for (int x = 0; x < map_w; x++)
- {
- if (tile_map[i][x] == 1)
- {
- Tile * tile = new Tile(sf::Vector2f(startingPoint.x * x * 125, startingPoint.y * i * 125), Tile::ground);
- objects.push_back(tile);
- }
- if (tile_map[i][x] == 2)
- {
- player->shape.setPosition(sf::Vector2f(startingPoint.x * x * 125, startingPoint.y * i * 125));
- objects.push_back(player);
- }
- }
- }
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- window.clear();
- MovePlayer(player);
- player->velocity.y += gravity.y;
- CheckCollision(objects);
- camera.Update(player);
- for (auto it = objects.begin(); it != objects.end(); it++)
- {
- (*it)->Update();
- (*it)->Draw(window);
- }
- camera.Draw(window);
- window.display();
- }
- delete player;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment