From 4a9007fc49af65f0dd53261b2aa20a0c48a38ddc Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sat, 4 Apr 2020 22:50:43 +0200 Subject: [PATCH] [Scene] Test item drop entity almost working. --- source/client/gui/InventoryCube.cpp | 22 ++++++++------- source/client/gui/InventoryCube.hpp | 6 +++-- source/client/scene/Scene.cpp | 42 ++++++++++++++++++++++++++++- source/client/scene/Scene.hpp | 10 ++++++- source/client/states/GameState.hpp | 2 +- source/client/world/ClientWorld.cpp | 2 +- source/client/world/ClientWorld.hpp | 6 ++--- 7 files changed, 72 insertions(+), 18 deletions(-) diff --git a/source/client/gui/InventoryCube.cpp b/source/client/gui/InventoryCube.cpp index 766b0b3db..f8da8e9c1 100644 --- a/source/client/gui/InventoryCube.cpp +++ b/source/client/gui/InventoryCube.cpp @@ -39,16 +39,20 @@ #include "TextureAtlas.hpp" #include "Vertex.hpp" -InventoryCube::InventoryCube(float size) : m_textureAtlas(gk::ResourceHandler::getInstance().get("atlas-blocks")) { +InventoryCube::InventoryCube(float size, bool isEntity) + : m_textureAtlas(&gk::ResourceHandler::getInstance().get("atlas-blocks")) +{ m_size = size; - m_transform.setOrigin(size * 0.5, size * 0.5, size * 0.5); + if (!isEntity) { + m_transform.setOrigin(size * 0.5, size * 0.5, size * 0.5); - // NOTE: intrinsic rotations! The axis is the local axis of the object. - // Note also that we start looking at the bottom of the cube due to how - // glm::orto is used (see comment below). - m_transform.rotate(120.f, {1, 0, 0}); - m_transform.rotate(-45.f, {0, 0, 1}); + // NOTE: intrinsic rotations! The axis is the local axis of the object. + // Note also that we start looking at the bottom of the cube due to how + // glm::ortho is used (see comment below). + m_transform.rotate(120.f, {1, 0, 0}); + m_transform.rotate(-45.f, {0, 0, 1}); + } } using namespace BlockGeometry; @@ -96,7 +100,7 @@ void InventoryCube::updateVertexBuffer(const Block &block) { V1 = (f <= 3) ? 1.f - boundingBox.z : (f == 4) ? boundingBox.y + boundingBox.sizeY : 1.f - boundingBox.y; } - const gk::FloatRect &blockTexCoords = m_textureAtlas.getTexCoords(block.tiles().getTextureForFace(f)); + const gk::FloatRect &blockTexCoords = m_textureAtlas->getTexCoords(block.tiles().getTextureForFace(f)); for (u8f v = 0; v < nVertsPerFace; ++v) { if (block.drawType() == BlockDrawType::Cactus) { @@ -144,7 +148,7 @@ void InventoryCube::draw(gk::RenderTarget &target, gk::RenderStates states) cons // at start. states.projectionMatrix = glm::ortho(0.0f, (float)Config::screenWidth, (float)Config::screenHeight, 0.0f, DIST_2D_FAR, DIST_2D_NEAR); - states.texture = &m_textureAtlas.texture(); + states.texture = &m_textureAtlas->texture(); states.vertexAttributes = VertexAttribute::Basic; glCheck(glEnable(GL_CULL_FACE)); diff --git a/source/client/gui/InventoryCube.hpp b/source/client/gui/InventoryCube.hpp index 1419bf2d1..c668279ee 100644 --- a/source/client/gui/InventoryCube.hpp +++ b/source/client/gui/InventoryCube.hpp @@ -36,16 +36,18 @@ class TextureAtlas; class InventoryCube : public gk::Drawable, public gk::Transformable { public: - InventoryCube(float size = 1.0f); + InventoryCube(float size = 1.0f, bool isEntity = false); void updateVertexBuffer(const Block &block); + float size() const { return m_size; } + private: void draw(gk::RenderTarget &target, gk::RenderStates states) const override; float m_size = 1.0f; - const TextureAtlas &m_textureAtlas; + const TextureAtlas *m_textureAtlas; gk::VertexBuffer m_vbo; bool m_isVboInitialized = false; diff --git a/source/client/scene/Scene.cpp b/source/client/scene/Scene.cpp index fd0bdbd6c..1ea0165dd 100644 --- a/source/client/scene/Scene.cpp +++ b/source/client/scene/Scene.cpp @@ -24,6 +24,7 @@ * * ===================================================================================== */ +#include "ClientPlayer.hpp" #include "Scene.hpp" struct RotationAnimation { @@ -34,7 +35,14 @@ struct RotationAnimation { float angle; }; -Scene::Scene() { +#include "InventoryCube.hpp" +#include "ItemStack.hpp" +#include "Registry.hpp" + +Scene::Scene(ClientPlayer &player) : m_player(player) { +} + +void Scene::init() { auto testEntity = m_registry.create(); gk::BoxShape &shape = m_registry.assign(testEntity, 0.25f, 0.25f, 0.25f); @@ -42,12 +50,40 @@ Scene::Scene() { shape.setPosition(13 + shape.getOrigin().x, 13 + shape.getOrigin().y, 16 + shape.getOrigin().z); m_registry.assign(testEntity, 0.f, 0.f, 1.f, 1.f); + m_registry.assign(testEntity, 0., 0., 0., shape.getSize().x, shape.getSize().y, shape.getSize().z); + m_registry.assign(testEntity, "default:stick", 1); + + auto testEntity2 = m_registry.create(); + + InventoryCube &cube = m_registry.assign(testEntity2, 0.25f, true); + cube.setOrigin(cube.size() / 2.f, cube.size() / 2.f, cube.size() / 2.f); + cube.setPosition(14 + cube.getOrigin().x, 13 + cube.getOrigin().y, 16 + cube.getOrigin().z); + cube.updateVertexBuffer(Registry::getInstance().getBlockFromStringID("default:cobblestone")); + + m_registry.assign(testEntity2, 0.f, 0.f, 1.f, 1.f); + + m_isInitialized = true; } void Scene::update() { + if (!m_isInitialized) init(); + m_registry.view().each([](auto, auto &boxShape, auto &rotation) { boxShape.rotate(rotation.angle, {rotation.axisX, rotation.axisY, rotation.axisZ}); }); + + m_registry.view().each([](auto, auto &cube, auto &rotation) { + cube.rotate(rotation.angle, {rotation.axisX, rotation.axisY, rotation.axisZ}); + }); + + m_registry.view().each([this](auto entity, auto &boxShape, auto &box, auto &itemStack) { + gk::DoubleBox hitbox = box + boxShape.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()); + m_registry.destroy(entity); + } + }); } void Scene::draw(gk::RenderTarget &target, gk::RenderStates states) const { @@ -60,5 +96,9 @@ void Scene::draw(gk::RenderTarget &target, gk::RenderStates states) const { m_registry.view().each([&](auto, auto &boxShape) { target.draw(boxShape, states); }); + + m_registry.view().each([&](auto, auto &cube) { + target.draw(cube, states); + }); } diff --git a/source/client/scene/Scene.hpp b/source/client/scene/Scene.hpp index 60ebddcaf..170715c18 100644 --- a/source/client/scene/Scene.hpp +++ b/source/client/scene/Scene.hpp @@ -33,9 +33,13 @@ #include +class ClientPlayer; + class Scene : public gk::Drawable { public: - Scene(); + Scene(ClientPlayer &player); + + void init(); void update(); @@ -44,6 +48,10 @@ class Scene : public gk::Drawable { private: void draw(gk::RenderTarget &target, gk::RenderStates states) const override; + bool m_isInitialized = false; + + ClientPlayer &m_player; + gk::Camera *m_camera = nullptr; gk::BoxShape m_testBox; diff --git a/source/client/states/GameState.hpp b/source/client/states/GameState.hpp index 52df5c93c..ad933f630 100644 --- a/source/client/states/GameState.hpp +++ b/source/client/states/GameState.hpp @@ -79,7 +79,7 @@ class GameState : public gk::ApplicationState { Client m_client; - ClientWorld m_world; + ClientWorld m_world{m_player}; std::unordered_map m_playerBoxes; diff --git a/source/client/world/ClientWorld.cpp b/source/client/world/ClientWorld.cpp index 74dcf6f0d..3c191108b 100644 --- a/source/client/world/ClientWorld.cpp +++ b/source/client/world/ClientWorld.cpp @@ -39,7 +39,7 @@ #include "TextureAtlas.hpp" #include "World.hpp" -ClientWorld::ClientWorld() : +ClientWorld::ClientWorld(ClientPlayer &player) : m_scene(player), m_textureAtlas(gk::ResourceHandler::getInstance().get("atlas-blocks")) { } diff --git a/source/client/world/ClientWorld.hpp b/source/client/world/ClientWorld.hpp index c5b6a91c7..63f9c3ba6 100644 --- a/source/client/world/ClientWorld.hpp +++ b/source/client/world/ClientWorld.hpp @@ -45,7 +45,7 @@ class ClientWorld : public World, public gk::Drawable { using ChunkMap = std::unordered_map>; public: - ClientWorld(); + ClientWorld(ClientPlayer &player); void update(); void sendChunkRequests(); @@ -70,6 +70,8 @@ class ClientWorld : public World, public gk::Drawable { void draw(gk::RenderTarget &target, gk::RenderStates states) const override; + Scene m_scene; + ChunkMap m_chunks; TextureAtlas &m_textureAtlas; @@ -80,8 +82,6 @@ class ClientWorld : public World, public gk::Drawable { mutable gk::Vector4f m_closestInitializedChunk{0, 0, 0, 1000000}; const Sky *m_sky = nullptr; - - Scene m_scene; }; #endif // CLIENTWORLD_HPP_