Skip to content

Commit

Permalink
Working on entity serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Apr 5, 2020
1 parent 0f610ea commit 357daba
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ This list is non exhaustive.
- Player model display (currently without rotation nor animation)
- Dimensions (like the Nether or the Ender in Minecraft) ([#80](https://github.com/Unarelith/OpenMiner/pull/80))
- World loading/saving (using `/save <name>` and `/load <name>` commands, see [#26](https://github.com/Unarelith/OpenMiner/issues/26))
- Texture pack system (partially implemented, see [#34](https://github.com/Unarelith/OpenMiner/issues/34))
- Entities (block drops, mobs, etc...) ([#90](https://github.com/Unarelith/OpenMiner/pull/90))

### Missing features

- Texture pack system ([#34](https://github.com/Unarelith/OpenMiner/issues/34))
- Fluid propagation ([#62](https://github.com/Unarelith/OpenMiner/issues/62))
- Day/night cycle with sun/moon display ([#73](https://github.com/Unarelith/OpenMiner/issues/73))
- Real worldgen (seed-based, cave tunnels) ([#79](https://github.com/Unarelith/OpenMiner/issues/79))
- Clouds ([#52](https://github.com/Unarelith/OpenMiner/pull/52))
- Particle system
- Entities (block drops, mobs, etc...)

## Screenshots

Expand Down
4 changes: 2 additions & 2 deletions source/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ endif ()

target_link_libraries(${PROJECT_NAME}
${CMAKE_PROJECT_NAME}_server_lib
${CMAKE_PROJECT_NAME}_common
${GAMEKIT_LIBRARIES}
${OPENGL_LIBRARIES}
${SDL2_LIBRARIES}
Expand All @@ -60,6 +61,5 @@ target_link_libraries(${PROJECT_NAME}
${LUA_LIBRARIES}
sfml-system
sfml-network
${UNIX_LIBS}
${CMAKE_PROJECT_NAME}_common)
${UNIX_LIBS})

7 changes: 3 additions & 4 deletions source/client/scene/AnimationController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
#include "InventoryCube.hpp"

void AnimationController::update(entt::DefaultRegistry &registry) {
// FIXME: This shouldn't use InventoryCube but a more generic class
registry.view<InventoryCube, AnimationComponent>().each([](auto, auto &cube, auto &animation) {
registry.view<gk::Transformable, AnimationComponent>().each([](auto, auto &transformable, auto &animation) {
for (auto &it : animation.list) {
if (it.type == AnimationType::Rotation)
cube.rotate(it.rotation.angle, {it.rotation.axisX, it.rotation.axisY, it.rotation.axisZ});
transformable.rotate(it.rotation.angle, {it.rotation.axisX, it.rotation.axisY, it.rotation.axisZ});
else if (it.type == AnimationType::Translation) {
float dx = it.translation.dx;
float dy = it.translation.dy;
Expand All @@ -51,7 +50,7 @@ void AnimationController::update(entt::DefaultRegistry &registry) {
|| it.translation.cz + it.translation.dz < it.translation.min)
dz = (it.translation.loop) ? -dz : 0;

cube.move(dx, dy, dz);
transformable.move(dx, dy, dz);

it.translation.cx += dx;
it.translation.cy += dy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,34 @@
*/
#include "AnimationController.hpp"
#include "ClientPlayer.hpp"
#include "ClientScene.hpp"
#include "CollisionController.hpp"
#include "Scene.hpp"
#include "RenderingController.hpp"

Scene::Scene(ClientPlayer &player) : m_player(player) {
ClientScene::ClientScene(ClientPlayer &player) : m_player(player) {
m_controllers.emplace_back(new AnimationController);
m_controllers.emplace_back(new CollisionController(player));
m_controllers.emplace_back(new RenderingController);
}

void Scene::update() {
void ClientScene::update() {
for (auto &controller : m_controllers)
controller->update(m_registry);

static bool test = false;
if (!test && m_registry.alive() > 2) {
gkDebug() << "serializing...";
sf::Packet packet;
serialize(packet);
gkDebug() << "deserializing...";
deserialize(packet);
gkDebug() << "serializing...";
serialize(packet);
test = true;
}
}

void Scene::draw(gk::RenderTarget &target, gk::RenderStates states) const {
void ClientScene::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (!m_camera) return;

// Subtract the camera position - see comment in ClientWorld::draw()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*
* =====================================================================================
*/
#ifndef SCENE_HPP_
#define SCENE_HPP_
#ifndef CLIENTSCENE_HPP_
#define CLIENTSCENE_HPP_

#include <deque>
#include <memory>
Expand All @@ -36,29 +36,26 @@
#include <entt/entt.hpp>

#include "AbstractController.hpp"
#include "Scene.hpp"

class ClientPlayer;

class Scene : public gk::Drawable {
class ClientScene : public Scene, public gk::Drawable {
public:
Scene(ClientPlayer &player);
ClientScene(ClientPlayer &player);

void update();

void setCamera(gk::Camera &camera) { m_camera = &camera; }

entt::DefaultRegistry &registry() { return m_registry; }

private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

ClientPlayer &m_player;

gk::Camera *m_camera = nullptr;

mutable entt::DefaultRegistry m_registry;

std::deque<std::unique_ptr<AbstractController>> m_controllers;
};

#endif // SCENE_HPP_
#endif // CLIENTSCENE_HPP_
4 changes: 2 additions & 2 deletions source/client/scene/CollisionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

void CollisionController::update(entt::DefaultRegistry &registry) {
// FIXME: This shouldn't use InventoryCube, but instead a callback stored in a CollisionComponent
registry.view<InventoryCube, gk::DoubleBox, ItemStack>().each([&](auto entity, auto &cube, auto &box, auto &itemStack) {
gk::DoubleBox hitbox = box + cube.getPosition();
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());
Expand Down
8 changes: 6 additions & 2 deletions source/client/scene/ItemDropFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* =====================================================================================
*/
#include "AnimationComponent.hpp"
#include "DrawableComponent.hpp"
#include "InventoryCube.hpp"
#include "ItemDropFactory.hpp"
#include "ItemStack.hpp"
Expand All @@ -33,11 +34,14 @@
void ItemDropFactory::create(entt::DefaultRegistry &registry, double x, double y, double z, const std::string &itemID, u16 amount) {
auto entity = registry.create();

InventoryCube &cube = registry.assign<InventoryCube>(entity, 0.25f, true);
auto &drawableComponent = registry.assign<DrawableComponent>(entity);
auto &cube = drawableComponent.setDrawable<InventoryCube>(0.25f, true);
cube.setOrigin(cube.size() / 2.f, cube.size() / 2.f, cube.size() / 2.f);
cube.setPosition(x + 0.5, y + 0.5, z + 0.5);
cube.updateVertexBuffer(Registry::getInstance().getBlockFromStringID(itemID));

auto &transformable = registry.assign<gk::Transformable>(entity);
transformable.setPosition(x + 0.5, y + 0.5, z + 0.5);

auto &animationComponent = registry.assign<AnimationComponent>(entity);
animationComponent.addRotation(0.f, 0.f, 1.f, 0.5f);
animationComponent.addTranslation(0.f, 0.f, -0.0005f, -0.2f, 0.f, true);
Expand Down
14 changes: 11 additions & 3 deletions source/client/scene/RenderingController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@
*
* =====================================================================================
*/
#include "DrawableComponent.hpp"
#include "InventoryCube.hpp"
#include "RenderingController.hpp"

#include <gk/core/Debug.hpp>
#include <gk/core/GameClock.hpp>

void RenderingController::draw(entt::DefaultRegistry &registry, gk::RenderTarget &target, gk::RenderStates states) {
// FIXME: There's probably another way to do this
registry.view<InventoryCube>().each([&](auto, auto &cube) {
target.draw(cube, states);
registry.view<DrawableComponent, gk::Transformable>().each([&](auto entity, auto &drawable, auto &transformable) {
if (gk::GameClock::getInstance().getTicks() % 100 < 12)
gkDebug() << "Drawing entity" << entity << "at" << transformable.getPosition();

gk::RenderStates drawStates = states;
drawStates.transform *= transformable.getTransform();
drawable.draw(target, drawStates);
});
}

6 changes: 3 additions & 3 deletions source/client/world/ClientWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#include <gk/core/Vector4.hpp>

#include "ClientChunk.hpp"
#include "ClientScene.hpp"
#include "Network.hpp"
#include "Scene.hpp"
#include "World.hpp"

class ClientCommandHandler;
Expand Down Expand Up @@ -62,7 +62,7 @@ class ClientWorld : public World, public gk::Drawable {

Chunk *getChunk(int cx, int cy, int cz) const override;

Scene &scene() { return m_scene; }
ClientScene &scene() { return m_scene; }

void setClient(ClientCommandHandler &client) { m_client = &client; }
void setCamera(gk::Camera &camera) { m_camera = &camera; m_scene.setCamera(camera); }
Expand All @@ -74,7 +74,7 @@ class ClientWorld : public World, public gk::Drawable {

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Scene m_scene;
ClientScene m_scene;

ChunkMap m_chunks;

Expand Down
20 changes: 20 additions & 0 deletions source/common/network/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ sf::Packet &operator>>(sf::Packet &packet, gk::Color &color) {
return packet;
}

sf::Packet &operator<<(sf::Packet &packet, const gk::Transformable &transformable) {
packet << transformable.getPosition() << transformable.getOrigin() << transformable.getScale()
<< transformable.getRotation() << transformable.getRotationTransform().getMatrix();
return packet;
}

sf::Packet &operator>>(sf::Packet &packet, gk::Transformable &transformable) {
gk::Vector3f position, origin, scale;
float rotation;
packet >> position >> origin >> scale >> rotation
>> transformable.getRotationTransform().getMatrix();

transformable.setPosition(position);
transformable.setOrigin(origin);
transformable.setScale(scale);
transformable.setRotation(rotation);

return packet;
}

31 changes: 31 additions & 0 deletions source/common/network/NetworkUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ sf::Packet &operator>>(sf::Packet &packet, std::unordered_map<T, U> &map) {
return packet;
}

//======================================================================================
// glm::mat4
//======================================================================================
#include <glm/gtc/type_ptr.hpp>
#include <glm/mat4x4.hpp>

template<typename T>
sf::Packet &operator<<(sf::Packet &packet, const glm::tmat4x4<T> &matrix) {
for (int i = 0 ; i < 4 * 4 ; ++i) {
packet << matrix[i % 4][i / 4];
}
return packet;
}

template<typename T>
sf::Packet &operator>>(sf::Packet &packet, glm::tmat4x4<T> &matrix) {
for (int i = 0 ; i < 4 * 4 ; ++i) {
packet >> matrix[i % 4][i / 4];
}
return packet;
}


//======================================================================================
// gk::Rect
//======================================================================================
Expand Down Expand Up @@ -143,4 +166,12 @@ sf::Packet &operator>>(sf::Packet &packet, gk::Vector3<T> &vec) {
sf::Packet &operator<<(sf::Packet &packet, const gk::Color &color);
sf::Packet &operator>>(sf::Packet &packet, gk::Color &color);

//======================================================================================
// gk::Transformable
//======================================================================================
#include <gk/gl/Transformable.hpp>

sf::Packet &operator<<(sf::Packet &packet, const gk::Transformable &transformable);
sf::Packet &operator>>(sf::Packet &packet, gk::Transformable &transformable);

#endif // NETWORKUTILS_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@

#include <vector>

#include <gk/core/IntTypes.hpp>

#include "ISerializable.hpp"
#include "NetworkUtils.hpp"

enum class AnimationType {
Rotation,
Translation,
};

struct AnimationData {
struct AnimationData : public ISerializable {
AnimationType type;

union {
Expand All @@ -53,9 +58,36 @@ struct AnimationData {
bool loop;
} translation;
};

void serialize(sf::Packet &packet) const override {
packet << u8(type);
if (type == AnimationType::Rotation) {
packet << rotation.axisX << rotation.axisY << rotation.axisZ << rotation.angle;
}
else if (type == AnimationType::Translation) {
packet << translation.dx << translation.dy << translation.dz
<< translation.cx << translation.cy << translation.cz
<< translation.min << translation.max << translation.loop;
}
}

void deserialize(sf::Packet &packet) override {
u8 type;
packet >> type;
if (type == u8(AnimationType::Rotation)) {
packet >> rotation.axisX >> rotation.axisY >> rotation.axisZ >> rotation.angle;
}
else if (type == u8(AnimationType::Translation)) {
packet >> translation.dx >> translation.dy >> translation.dz
>> translation.cx >> translation.cy >> translation.cz
>> translation.min >> translation.max >> translation.loop;
}

this->type = AnimationType(type);
}
};

struct AnimationComponent {
struct AnimationComponent : public ISerializable {
void addRotation(float axisX, float axisY, float axisZ, float angle) {
list.emplace_back();
AnimationData &data = list.back();
Expand Down Expand Up @@ -89,6 +121,9 @@ struct AnimationComponent {
data.translation.loop = loop;
}

void serialize(sf::Packet &packet) const override { packet << list; }
void deserialize(sf::Packet &packet) override { packet >> list; }

std::vector<AnimationData> list;
};

Expand Down
Loading

0 comments on commit 357daba

Please sign in to comment.