Skip to content

Commit

Permalink
[Scene] Test item drop entity almost working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Apr 29, 2020
1 parent 5815549 commit 4a9007f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 18 deletions.
22 changes: 13 additions & 9 deletions source/client/gui/InventoryCube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@
#include "TextureAtlas.hpp"
#include "Vertex.hpp"

InventoryCube::InventoryCube(float size) : m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks")) {
InventoryCube::InventoryCube(float size, bool isEntity)
: m_textureAtlas(&gk::ResourceHandler::getInstance().get<TextureAtlas>("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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions source/client/gui/InventoryCube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 41 additions & 1 deletion source/client/scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*
* =====================================================================================
*/
#include "ClientPlayer.hpp"
#include "Scene.hpp"

struct RotationAnimation {
Expand All @@ -34,20 +35,55 @@ 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<gk::BoxShape>(testEntity, 0.25f, 0.25f, 0.25f);
shape.setOrigin(shape.getSize().x / 2.f, shape.getSize().y / 2.f, shape.getSize().z / 2.f);
shape.setPosition(13 + shape.getOrigin().x, 13 + shape.getOrigin().y, 16 + shape.getOrigin().z);

m_registry.assign<RotationAnimation>(testEntity, 0.f, 0.f, 1.f, 1.f);
m_registry.assign<gk::DoubleBox>(testEntity, 0., 0., 0., shape.getSize().x, shape.getSize().y, shape.getSize().z);
m_registry.assign<ItemStack>(testEntity, "default:stick", 1);

auto testEntity2 = m_registry.create();

InventoryCube &cube = m_registry.assign<InventoryCube>(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<RotationAnimation>(testEntity2, 0.f, 0.f, 1.f, 1.f);

m_isInitialized = true;
}

void Scene::update() {
if (!m_isInitialized) init();

m_registry.view<gk::BoxShape, RotationAnimation>().each([](auto, auto &boxShape, auto &rotation) {
boxShape.rotate(rotation.angle, {rotation.axisX, rotation.axisY, rotation.axisZ});
});

m_registry.view<InventoryCube, RotationAnimation>().each([](auto, auto &cube, auto &rotation) {
cube.rotate(rotation.angle, {rotation.axisX, rotation.axisY, rotation.axisZ});
});

m_registry.view<gk::BoxShape, gk::DoubleBox, ItemStack>().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 {
Expand All @@ -60,5 +96,9 @@ void Scene::draw(gk::RenderTarget &target, gk::RenderStates states) const {
m_registry.view<gk::BoxShape>().each([&](auto, auto &boxShape) {
target.draw(boxShape, states);
});

m_registry.view<InventoryCube>().each([&](auto, auto &cube) {
target.draw(cube, states);
});
}

10 changes: 9 additions & 1 deletion source/client/scene/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@

#include <entt/entt.hpp>

class ClientPlayer;

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

void init();

void update();

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion source/client/states/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class GameState : public gk::ApplicationState {

Client m_client;

ClientWorld m_world;
ClientWorld m_world{m_player};

std::unordered_map<u16, PlayerBox> m_playerBoxes;

Expand Down
2 changes: 1 addition & 1 deletion source/client/world/ClientWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextureAtlas>("atlas-blocks"))
{
}
Expand Down
6 changes: 3 additions & 3 deletions source/client/world/ClientWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ClientWorld : public World, public gk::Drawable {
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ClientChunk>>;

public:
ClientWorld();
ClientWorld(ClientPlayer &player);

void update();
void sendChunkRequests();
Expand All @@ -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;
Expand All @@ -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_

0 comments on commit 4a9007f

Please sign in to comment.