Player now colliding with map properly and other small adjustments

This commit is contained in:
Maximilian Giller 2023-06-10 15:24:03 +02:00
parent 916050153a
commit 37ede84344
19 changed files with 44 additions and 44 deletions

View file

@ -84,7 +84,6 @@ set(SOURCES
src/game/input/gamepad_buttons.hpp src/game/input/gamepad_buttons.hpp
src/game/physics/map_simulation.cpp src/game/physics/map_simulation.cpp
src/game/physics/map_simulation.hpp src/game/physics/map_simulation.hpp
src/game/physics/map_player.cpp
src/game/physics/map_player.hpp src/game/physics/map_player.hpp
src/game/level/level_config.hpp src/game/level/level_config.hpp
src/game/level/level_loader.cpp src/game/level/level_loader.cpp

View file

@ -28,7 +28,7 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY)
} }
} }
void GridDebugLayer::draw(sf::RenderWindow *window) const void GridDebugLayer::draw(sf::RenderWindow *window)
{ {
for (auto &gameObject: getChildren()) for (auto &gameObject: getChildren())
{ {

View file

@ -9,7 +9,7 @@ class GridDebugLayer : public GameObject
public: public:
GridDebugLayer(int minX, int maxX, int minY, int maxY); GridDebugLayer(int minX, int maxX, int minY, int maxY);
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) override;
}; };

View file

@ -132,7 +132,7 @@ void TrackingView::moveCenter(sf::Vector2<float> delta)
hasViewChanged = true; hasViewChanged = true;
} }
void TrackingView::draw(sf::RenderWindow *window) const void TrackingView::draw(sf::RenderWindow *window)
{ {
if (!DEVELOPER_MODE) if (!DEVELOPER_MODE)
{ {

View file

@ -18,7 +18,7 @@ public:
~TrackingView(); ~TrackingView();
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) override;
void lateUpdate() override; void lateUpdate() override;

View file

@ -5,7 +5,7 @@ EnvironmentCollectable::EnvironmentCollectable(GridCoordinates position)
coordinates->set(position); coordinates->set(position);
} }
void EnvironmentCollectable::draw(sf::RenderWindow *window) const void EnvironmentCollectable::draw(sf::RenderWindow *window)
{ {
} }

View file

@ -9,7 +9,7 @@ class EnvironmentCollectable : public GameObject
public: public:
EnvironmentCollectable(GridCoordinates position); EnvironmentCollectable(GridCoordinates position);
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) override;
}; };

View file

@ -17,7 +17,7 @@ void GameObject::setActive(bool active)
} }
} }
void GameObject::draw(sf::RenderWindow *window) const void GameObject::draw(sf::RenderWindow *window)
{ {
for (auto &child: children) for (auto &child: children)
{ {

View file

@ -12,7 +12,7 @@ class GameObject
public: public:
GameObject(); GameObject();
virtual void draw(sf::RenderWindow *window) const; virtual void draw(sf::RenderWindow *window);
virtual void update(); virtual void update();

View file

@ -1 +0,0 @@
#include "map_player.hpp"

View file

@ -4,13 +4,33 @@
#include <box2d/box2d.h> #include <box2d/box2d.h>
#include "../player/player.hpp" #include "../player/player.hpp"
#include "../../config.h"
struct MapPlayer struct MapPlayer
{ {
Player *player; Player *player;
b2Body *body; b2Body *body;
MapPlayer(Player *player, b2Body *body) : player(player), body(body) {} MapPlayer(Player *player, b2Body *body) : player(player), body(body)
{}
void updateSimulationPosition() const
{
auto coordinates = player->coordinates->world();
b2Vec2 playerPosition = b2Vec2(coordinates.x, coordinates.y);
// Calculate velocity that theoretically needs to be applied to the body, to get to the same position
b2Vec2 delta = playerPosition - body->GetPosition();
b2Vec2 velocity = {delta.x * FRAME_RATE, delta.y * FRAME_RATE};
body->SetLinearVelocity(velocity);
}
void updatePlayerPosition() const
{
b2Vec2 playerPosition = body->GetPosition();
player->coordinates->set(sf::Vector2f(playerPosition.x, playerPosition.y));
}
}; };

View file

@ -17,15 +17,10 @@ std::shared_ptr<MapSimulation> MapSimulation::getInstance()
void MapSimulation::updateSimulation() void MapSimulation::updateSimulation()
{ {
// Get player positions // Update simulation positions
for (auto &mapPlayer: mapPlayersById) for (auto &mapPlayer: mapPlayersById)
{ {
auto player = mapPlayer.second->player; mapPlayer.second->updateSimulationPosition();
auto body = mapPlayer.second->body;
auto coordinates = player->coordinates->world();
b2Vec2 playerPosition = b2Vec2(coordinates.x, coordinates.y);
body->SetTransform(playerPosition, 0);
} }
world->Step(FRAME_TIME.asSeconds(), MAPSIM_VELOCITY_ITERATIONS, MAPSIM_POSITION_ITERATIONS); world->Step(FRAME_TIME.asSeconds(), MAPSIM_VELOCITY_ITERATIONS, MAPSIM_POSITION_ITERATIONS);
@ -33,11 +28,7 @@ void MapSimulation::updateSimulation()
// Update player positions // Update player positions
for (auto &mapPlayer: mapPlayersById) for (auto &mapPlayer: mapPlayersById)
{ {
auto player = mapPlayer.second->player; mapPlayer.second->updatePlayerPosition();
auto body = mapPlayer.second->body;
b2Vec2 playerPosition = body->GetPosition();
player->coordinates->set(sf::Vector2f(playerPosition.x, playerPosition.y));
} }
} }
@ -74,8 +65,8 @@ void MapSimulation::constructSquareObstacle(float minX, float minY, float maxX,
void MapSimulation::addPlayer(Player *player) void MapSimulation::addPlayer(Player *player)
{ {
b2BodyDef bodyDef; b2BodyDef bodyDef;
bodyDef.type = b2_kinematicBody; bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(player->getTrackablePosition().x, player->getTrackablePosition().y); bodyDef.position.Set(player->coordinates->world().x, player->coordinates->world().y);
b2Body *body = world->CreateBody(&bodyDef); b2Body *body = world->CreateBody(&bodyDef);
b2CircleShape shape; b2CircleShape shape;

View file

@ -1,6 +1,7 @@
#include "player_spawner.hpp" #include "player_spawner.hpp"
#include "player.hpp" #include "player.hpp"
#include "../../texture_config.h" #include "../../texture_config.h"
#include "../physics/map_simulation.hpp"
PlayerSpawner::PlayerSpawner(const std::vector<sf::Vector2f> &spawnPoints) PlayerSpawner::PlayerSpawner(const std::vector<sf::Vector2f> &spawnPoints)
{ {
@ -30,7 +31,9 @@ void PlayerSpawner::spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdent
auto player = new Player(inputIdentity, PLAYER_SKIN, spawn); auto player = new Player(inputIdentity, PLAYER_SKIN, spawn);
MapSimulation::getInstance()->addPlayer(player);
Game::getInstance()->addGameObject(player); Game::getInstance()->addGameObject(player);
// TODO: Better view handling // TODO: Better view handling
Game::getInstance()->views[0]->addTrackable(player); Game::getInstance()->views[0]->addTrackable(player);
} }

View file

@ -6,7 +6,7 @@ CircleObject::CircleObject(int radius, sf::Color color) : radius(radius), color(
} }
void CircleObject::draw(sf::RenderWindow *window) const void CircleObject::draw(sf::RenderWindow *window)
{ {
sf::CircleShape circle(radius); sf::CircleShape circle(radius);
circle.setFillColor(color); circle.setFillColor(color);
@ -15,11 +15,6 @@ void CircleObject::draw(sf::RenderWindow *window) const
window->draw(circle); window->draw(circle);
} }
void CircleObject::update()
{
}
int CircleObject::getRadius() const int CircleObject::getRadius() const
{ {
return radius; return radius;

View file

@ -10,8 +10,7 @@ class CircleObject : public GameObject {
public: public:
CircleObject(int radius, sf::Color color); CircleObject(int radius, sf::Color color);
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) override;
void update() override;
int getRadius() const; int getRadius() const;

View file

@ -22,7 +22,7 @@ void AnimatedSprite::update()
} }
} }
void AnimatedSprite::draw(sf::RenderWindow *window) const void AnimatedSprite::draw(sf::RenderWindow *window)
{ {
auto currentSprite = sprites[currentFrame]; auto currentSprite = sprites[currentFrame];
currentSprite.setPosition(coordinates->isometric().toScreen()); currentSprite.setPosition(coordinates->isometric().toScreen());

View file

@ -13,7 +13,7 @@ public:
void update() override; void update() override;
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) override;
sf::Time frameDuration = sf::seconds(0.1f); sf::Time frameDuration = sf::seconds(0.1f);

View file

@ -15,14 +15,10 @@ SingleSprite::SingleSprite(const std::shared_ptr<sf::Texture> &texture, const sf
setSize(size); setSize(size);
} }
void SingleSprite::draw(sf::RenderWindow *window) const void SingleSprite::draw(sf::RenderWindow *window)
{
window->draw(sprite);
}
void SingleSprite::lateUpdate()
{ {
sprite.setPosition(coordinates->isometric().toScreen()); sprite.setPosition(coordinates->isometric().toScreen());
window->draw(sprite);
} }
void SingleSprite::setSize(const sf::Vector2f &size) void SingleSprite::setSize(const sf::Vector2f &size)

View file

@ -13,9 +13,7 @@ public:
SingleSprite(const std::shared_ptr<sf::Texture> &texture, const sf::Vector2f &size = sf::Vector2f(0, 0)); SingleSprite(const std::shared_ptr<sf::Texture> &texture, const sf::Vector2f &size = sf::Vector2f(0, 0));
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) override;
void lateUpdate() override;
void setSize(const sf::Vector2f &size) override; void setSize(const sf::Vector2f &size) override;