Commit 6b2095cd authored by Jonas Johan Solsvik's avatar Jonas Johan Solsvik 🎮
Browse files

add animationUpdate and physicsUpdate

parent 2bf71a52
## 02-jumping-boxes
Introducing rudimentary physics.
\ No newline at end of file
......@@ -3,14 +3,28 @@
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
using Thing = sf::RectangleShape;
using ThingVector = std::vector<sf::RectangleShape>;
using ThingIDVector = std::vector<unsigned>;
auto eventsUpdate(
sf::RenderWindow &window,
sf::View &camera) -> void;
auto physicsUpdate(
ThingVector &things,
const ThingIDVector &dynamicThings,
const ThingIDVector &staticThings) -> void;
auto physicsTestCollision(
Thing &thing,
Thing &otherThing) -> void;
auto animationUpdate(ThingVector &things) -> void;
auto renderUpdate(
sf::RenderWindow &window,
const std::vector<sf::RectangleShape> &players,
const sf::RectangleShape &ground) -> void;
const ThingVector &things) -> void;
auto main() -> int
{
......@@ -26,33 +40,43 @@ auto main() -> int
window.setView(camera);
// # Create ground
sf::RectangleShape ground{sf::Vector2f{4000, 20}};
Thing ground{sf::Vector2f{4000, 20}};
ground.setPosition(-2000, 0);
ground.setFillColor(sf::Color::White);
// # Create players
std::vector<sf::Vector2f> playerPositions{
sf::Vector2f{-100, -50},
sf::Vector2f{100, -50},
};
std::vector<sf::Color> playerColors{
sf::Color::Red,
sf::Color::Blue,
const auto PLAYER_SIZE = sf::Vector2f{50.0f, 50.0f};
const auto PLAYER_ORIGIN = sf::Vector2f{PLAYER_SIZE.x / 2.0f, PLAYER_SIZE.y / 2.0f};
Thing player1;
player1.setFillColor(sf::Color::Red);
player1.setSize(PLAYER_SIZE);
player1.setOrigin(PLAYER_ORIGIN);
player1.setPosition(sf::Vector2f{-100, -25});
Thing player2;
player2.setFillColor(sf::Color::Blue);
player2.setSize(PLAYER_SIZE);
player2.setOrigin(PLAYER_ORIGIN);
player2.setPosition(sf::Vector2f{100, -25});
ThingVector things = {
player1,
player2,
ground,
};
std::vector<sf::RectangleShape> players(2);
for (auto i = 0; i < players.size(); ++i)
{
players[i].setFillColor(playerColors[i]);
players[i].setSize(sf::Vector2f{50, 50});
players[i].setPosition(playerPositions[i]);
}
const ThingIDVector dynamicThings = {0, 1};
const ThingIDVector staticThings = {2};
while (window.isOpen())
{
eventsUpdate(window, camera);
renderUpdate(window, players, ground);
physicsUpdate(things, dynamicThings, staticThings);
animationUpdate(things);
renderUpdate(things, window);
}
return 0;
}
......@@ -80,17 +104,47 @@ auto eventsUpdate(
}
}
auto physicsUpdate(
ThingVector &things,
const ThingIDVector &dynamicThings,
const ThingIDVector &staticThings) -> void
{
for (const auto dynamicThingID : dynamicThings)
{
auto &dynamicThing = things[dynamicThingID];
for (const auto otherDynamicThingID : dynamicThings)
{
if (dynamicThingID == otherDynamicThingID)
{
continue;
}
physicsTestCollision(dynamicThing, things[otherDynamicThingID]);
}
for (const auto staticThing : staticThings)
{
physicsTestCollision(dynamicThing, things[staticThing]);
}
}
}
auto physicsTestCollision(
Thing &thing,
Thing &otherThing) -> void
{
}
auto animationUpdate(ThingVector &things) -> void
{
}
auto renderUpdate(
sf::RenderWindow &window,
const std::vector<sf::RectangleShape> &players,
const sf::RectangleShape &ground) -> void
const ThingVector &things) -> void
{
// # Render video
window.clear();
for (auto player : players)
for (auto &thing : things)
{
window.draw(player);
window.draw(thing);
}
window.draw(ground);
window.display();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment