1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
window.setFramerateLimit(60);
float dt;
sf::Clock dtClock;
const float gridSize = 50.0f;
//Player
const float movementSpeed = 100.f;
sf::Vector2f velocity;
sf::RectangleShape player;
player.setFillColor(sf::Color::Green);
player.setSize(sf::Vector2f(gridSize, gridSize));
player.setPosition(sf::Vector2f(0, 0));
//Walls
std::vector<sf::RectangleShape> walls;
sf::RectangleShape wall;
wall.setFillColor(sf::Color::Red);
wall.setSize(sf::Vector2f(gridSize, gridSize));
wall.setPosition(gridSize * 5, gridSize * 2);
walls.push_back(wall);
//Collision
sf::FloatRect nextPos;
while (window.isOpen())
{
dt = dtClock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
//Prevents contents of window from being stretched
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
}
//Player Movement
velocity.x = 0.f;
velocity.y = 0.f;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
velocity.y += -movementSpeed * dt;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
velocity.x += -movementSpeed * dt;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
velocity.y += movementSpeed * dt;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
velocity.x += movementSpeed * dt;
}
//General Collision
for (auto& wall : walls)
{
sf::FloatRect playerBounds = player.getGlobalBounds();
sf::FloatRect wallBounds = wall.getGlobalBounds();
nextPos = playerBounds;
nextPos.left += velocity.x;
nextPos.top += velocity.y;
if (wallBounds.intersects(nextPos))
{
//Bottom Collision
if (playerBounds.top < wallBounds.top
&& playerBounds.top + playerBounds.height < wallBounds.top + wallBounds.height
&& playerBounds.left < wallBounds.left + wallBounds.width
&& playerBounds.left + playerBounds.width > wallBounds.left)
{
velocity.y = 0;
player.setPosition(playerBounds.left, wallBounds.top - playerBounds.height);
}
//Top Collision
if (playerBounds.top > wallBounds.top
&& playerBounds.top + playerBounds.height > wallBounds.top + wallBounds.height
&& playerBounds.left < wallBounds.left + wallBounds.width
&& playerBounds.left + playerBounds.width > wallBounds.left)
{
velocity.y = 0;
player.setPosition(playerBounds.left, wallBounds.top + wallBounds.height);
}
//Right Collision
if (playerBounds.left < wallBounds.left
&& playerBounds.left + playerBounds.width < wallBounds.left + wallBounds.width
&& playerBounds.top < wallBounds.top + wallBounds.height
&& playerBounds.top + playerBounds.height > wallBounds.top)
{
velocity.x = 0;
player.setPosition(wallBounds.left - playerBounds.width, playerBounds.top);
}
//Left Collision
if (playerBounds.left > wallBounds.left
&& playerBounds.left + playerBounds.width > wallBounds.left + wallBounds.width
&& playerBounds.top < wallBounds.top + wallBounds.height
&& playerBounds.top + playerBounds.height > wallBounds.top)
{
velocity.x = 0;
player.setPosition(wallBounds.left + wallBounds.width, playerBounds.top);
}
}
}
player.move(velocity);
//Screen Collision
//Left Collision
if (player.getPosition().x < 0.0)
{
player.setPosition(0, player.getPosition().y);
}
//Top Collision
if (player.getPosition().y < 0)
{
player.setPosition(player.getPosition().x, 0);
}
//Right Collision
if (player.getPosition().x + player.getGlobalBounds().width > window.getSize().x)
{
player.setPosition(window.getSize().x - player.getGlobalBounds().width, player.getPosition().y);
}
//Bottom Collision
//Right Collision
if (player.getPosition().y + player.getGlobalBounds().height > window.getSize().y)
{
player.setPosition(player.getPosition().x, window.getSize().y - player.getGlobalBounds().height);
}
window.clear();
window.draw(player);
for (auto& i : walls)
{
window.draw(i);
}
window.display();
}
return 0;
}
|