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/physics/map_simulation.cpp
src/game/physics/map_simulation.hpp
src/game/physics/map_player.cpp
src/game/physics/map_player.hpp
src/game/level/level_config.hpp
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())
{

View file

@ -9,7 +9,7 @@ class GridDebugLayer : public GameObject
public:
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;
}
void TrackingView::draw(sf::RenderWindow *window) const
void TrackingView::draw(sf::RenderWindow *window)
{
if (!DEVELOPER_MODE)
{

View file

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

View file

@ -5,7 +5,7 @@ EnvironmentCollectable::EnvironmentCollectable(GridCoordinates 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:
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)
{

View file

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

View file

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

View file

@ -4,13 +4,33 @@
#include <box2d/box2d.h>
#include "../player/player.hpp"
#include "../../config.h"
struct MapPlayer
{
Player *player;
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()
{
// Get player positions
// Update simulation positions
for (auto &mapPlayer: mapPlayersById)
{
auto player = mapPlayer.second->player;
auto body = mapPlayer.second->body;
auto coordinates = player->coordinates->world();
b2Vec2 playerPosition = b2Vec2(coordinates.x, coordinates.y);
body->SetTransform(playerPosition, 0);
mapPlayer.second->updateSimulationPosition();
}
world->Step(FRAME_TIME.asSeconds(), MAPSIM_VELOCITY_ITERATIONS, MAPSIM_POSITION_ITERATIONS);
@ -33,11 +28,7 @@ void MapSimulation::updateSimulation()
// Update player positions
for (auto &mapPlayer: mapPlayersById)
{
auto player = mapPlayer.second->player;
auto body = mapPlayer.second->body;
b2Vec2 playerPosition = body->GetPosition();
player->coordinates->set(sf::Vector2f(playerPosition.x, playerPosition.y));
mapPlayer.second->updatePlayerPosition();
}
}
@ -74,8 +65,8 @@ void MapSimulation::constructSquareObstacle(float minX, float minY, float maxX,
void MapSimulation::addPlayer(Player *player)
{
b2BodyDef bodyDef;
bodyDef.type = b2_kinematicBody;
bodyDef.position.Set(player->getTrackablePosition().x, player->getTrackablePosition().y);
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(player->coordinates->world().x, player->coordinates->world().y);
b2Body *body = world->CreateBody(&bodyDef);
b2CircleShape shape;

View file

@ -1,6 +1,7 @@
#include "player_spawner.hpp"
#include "player.hpp"
#include "../../texture_config.h"
#include "../physics/map_simulation.hpp"
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);
MapSimulation::getInstance()->addPlayer(player);
Game::getInstance()->addGameObject(player);
// TODO: Better view handling
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);
circle.setFillColor(color);
@ -15,11 +15,6 @@ void CircleObject::draw(sf::RenderWindow *window) const
window->draw(circle);
}
void CircleObject::update()
{
}
int CircleObject::getRadius() const
{
return radius;

View file

@ -10,8 +10,7 @@ class CircleObject : public GameObject {
public:
CircleObject(int radius, sf::Color color);
void draw(sf::RenderWindow *window) const override;
void update() override;
void draw(sf::RenderWindow *window) override;
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];
currentSprite.setPosition(coordinates->isometric().toScreen());

View file

@ -13,7 +13,7 @@ public:
void update() override;
void draw(sf::RenderWindow *window) const override;
void draw(sf::RenderWindow *window) override;
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);
}
void SingleSprite::draw(sf::RenderWindow *window) const
{
window->draw(sprite);
}
void SingleSprite::lateUpdate()
void SingleSprite::draw(sf::RenderWindow *window)
{
sprite.setPosition(coordinates->isometric().toScreen());
window->draw(sprite);
}
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));
void draw(sf::RenderWindow *window) const override;
void lateUpdate() override;
void draw(sf::RenderWindow *window) override;
void setSize(const sf::Vector2f &size) override;