[CollisionController] Now server-side.

This commit is contained in:
Quentin Bazin 2020-04-05 23:27:18 +02:00
parent 38b3c2aa48
commit d8a3b0a7a4
10 changed files with 27 additions and 27 deletions

View File

@ -27,12 +27,10 @@
#include "AnimationController.hpp"
#include "ClientPlayer.hpp"
#include "ClientScene.hpp"
#include "CollisionController.hpp"
#include "RenderingController.hpp"
ClientScene::ClientScene(ClientPlayer &player) {
m_controllers.emplace_back(new AnimationController);
m_controllers.emplace_back(new CollisionController(player));
m_controllers.emplace_back(new RenderingController);
}

View File

@ -72,7 +72,7 @@ void ServerApplication::init() {
m_serverCommandHandler.setupCallbacks();
m_worldController.setServer(m_serverCommandHandler);
m_worldController.init();
m_worldController.init(m_players);
m_scriptEngine.luaCore().setRegistry(&m_registry);

View File

@ -24,18 +24,21 @@
*
* =====================================================================================
*/
#include "ClientPlayer.hpp"
#include <gk/gl/Transformable.hpp>
#include "CollisionController.hpp"
#include "InventoryCube.hpp"
#include "ItemStack.hpp"
#include "PlayerList.hpp"
void CollisionController::update(entt::DefaultRegistry &registry) {
registry.view<gk::Transformable, gk::DoubleBox, ItemStack>().each([&](auto entity, auto &transformable, auto &box, auto &itemStack) {
gk::DoubleBox hitbox = box + transformable.getPosition();
gk::DoubleBox playerHitbox = m_player.hitbox() + gk::Vector3d{m_player.x(), m_player.y(), m_player.z()};
if (hitbox.intersects(playerHitbox)) {
m_player.inventory().addStack(itemStack.item().stringID(), itemStack.amount());
registry.destroy(entity);
for (auto &it : m_players) {
gk::DoubleBox hitbox = box + transformable.getPosition();
gk::DoubleBox playerHitbox = it.second.hitbox() + gk::Vector3d{it.second.x(), it.second.y(), it.second.z()};
if (hitbox.intersects(playerHitbox)) {
it.second.inventory().addStack(itemStack.item().stringID(), itemStack.amount());
registry.destroy(entity);
}
}
});
}

View File

@ -29,16 +29,16 @@
#include "AbstractController.hpp"
class ClientPlayer;
class PlayerList;
class CollisionController : public AbstractController {
public:
CollisionController(ClientPlayer &player) : m_player(player) {}
CollisionController(PlayerList &players) : m_players(players) {}
void update(entt::DefaultRegistry &registry);
private:
ClientPlayer &m_player;
PlayerList &m_players;
};
#endif // COLLISIONCONTROLLER_HPP_

View File

@ -47,8 +47,7 @@ void ItemDropFactory::create(entt::DefaultRegistry &registry, double x, double y
animationComponent.addRotation(0.f, 0.f, 1.f, 0.5f);
animationComponent.addTranslation(0.f, 0.f, -0.0005f, -0.2f, 0.f, true);
// FIXME
// registry.assign<gk::DoubleBox>(entity, 0., 0., 0., cube.size, cube.size, cube.size);
registry.assign<gk::DoubleBox>(entity, 0., 0., 0., cube.size, cube.size, cube.size);
registry.assign<ItemStack>(entity, itemID, amount);
}

View File

@ -27,11 +27,10 @@
#include <gk/core/Debug.hpp>
#include <gk/core/GameClock.hpp>
#include "CollisionController.hpp"
#include "ServerScene.hpp"
ServerScene::ServerScene() {
}
void ServerScene::update() {
ServerScene::ServerScene(PlayerList &players) {
m_controllers.emplace_back(new CollisionController(players));
}

View File

@ -29,11 +29,11 @@
#include "Scene.hpp"
class PlayerList;
class ServerScene : public Scene {
public:
ServerScene();
void update() override;
ServerScene(PlayerList &players);
};
#endif // SERVERSCENE_HPP_

View File

@ -40,6 +40,7 @@ namespace gk {
class ClientInfo;
class Dimension;
class PlayerList;
class ServerCommandHandler;
class ServerPlayer;
@ -47,8 +48,8 @@ class ServerWorld : public World {
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ServerChunk>>;
public:
ServerWorld(const Dimension &dimension, gk::GameClock &clock)
: m_dimension(dimension), m_terrainGenerator(dimension), m_clock(clock) {}
ServerWorld(PlayerList &players, const Dimension &dimension, gk::GameClock &clock)
: m_dimension(dimension), m_terrainGenerator(dimension), m_clock(clock), m_scene(players) {}
void update();

View File

@ -29,9 +29,9 @@
#include "Registry.hpp"
#include "WorldController.hpp"
void WorldController::init() {
void WorldController::init(PlayerList &players) {
for (const Dimension &dimension : m_registry.dimensions()) {
m_worldList.emplace_back(dimension, m_clock);
m_worldList.emplace_back(players, dimension, m_clock);
m_worldList.back().setServer(m_server);
}
}

View File

@ -41,7 +41,7 @@ class WorldController {
public:
WorldController(Registry &registry, gk::GameClock &clock) : m_registry(registry), m_clock(clock) {}
void init();
void init(PlayerList &players);
void update();