diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index f94939de..e6e89b96 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -15,6 +15,7 @@ #include "../../maths/FrustumCulling.hpp" #include "../../maths/voxmaths.hpp" #include "../../objects/Player.hpp" +#include "../../objects/Entities.hpp" #include "../../settings.hpp" #include "../../voxels/Block.hpp" #include "../../voxels/Chunk.hpp" @@ -195,7 +196,7 @@ void WorldRenderer::renderLevel( drawChunks(level->chunks.get(), camera, shader); shader->uniformMatrix("u_model", glm::mat4(1.0f)); - // draw entities here + level->entities->render(assets, *modelBatch); modelBatch->render(); skybox->unbind(); diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index d8afbc57..551beb71 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -6,6 +6,7 @@ #include "../world/Level.hpp" #include "../world/World.hpp" #include "../physics/Hitbox.hpp" +#include "../objects/Entities.hpp" #include "scripting/scripting.hpp" #include "../interfaces/Object.hpp" @@ -47,6 +48,7 @@ void LevelController::update(float delta, bool input, bool pause) { } } blocks->update(delta); + level->entities->updatePhysics(delta); } } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp new file mode 100644 index 00000000..674ae1f1 --- /dev/null +++ b/src/objects/Entities.cpp @@ -0,0 +1,55 @@ +#include "Entities.hpp" + +#include "../assets/Assets.hpp" +#include "../world/Level.hpp" +#include "../physics/Hitbox.hpp" +#include "../physics/PhysicsSolver.hpp" +#include "../graphics/render/ModelBatch.hpp" +#include "../graphics/core/Model.hpp" + +#include + +void Transform::refresh() { + combined = glm::mat4(1.0f); + combined = glm::translate(combined, pos); +} + +Entities::Entities(Level* level) : level(level) { + auto entity = registry.create(); + glm::vec3 pos(0.5f, 170, 0.5f); + glm::vec3 size(1); + registry.emplace(entity, 1); + registry.emplace(entity, pos, size, glm::mat3(1.0f)); + registry.emplace(entity, pos, size/20.0f); +} + +void Entities::updatePhysics(float delta){ + auto view = registry.view(); + auto physics = level->physics.get(); + for (auto [entity, transform, hitbox] : view.each()) { + physics->step( + level->chunks.get(), + &hitbox, + delta, + 10, + false, + 1.0f, + true + ); + transform.pos = hitbox.position; + if (hitbox.grounded) { + hitbox.velocity.y = 10; + } + } +} + +void Entities::render(Assets* assets, ModelBatch& batch) { + auto view = registry.view(); + auto model = assets->get("dingus"); + for (auto [entity, transform] : view.each()) { + transform.refresh(); + batch.pushMatrix(transform.combined); + batch.draw(model); + batch.popMatrix(); + } +} diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp new file mode 100644 index 00000000..0c725f89 --- /dev/null +++ b/src/objects/Entities.hpp @@ -0,0 +1,57 @@ +#ifndef OBJECTS_ENTITIES_HPP_ +#define OBJECTS_ENTITIES_HPP_ + +#include "../typedefs.hpp" + +#include +#include +#include + +struct EntityId { + entityid_t uid; +}; + +struct Transform { + glm::vec3 pos; + glm::vec3 size; + glm::mat3 rot; + glm::mat4 combined; + + void refresh(); +}; + +class Level; +class Assets; +class ModelBatch; + +class Entity { + entt::registry& registry; + entt::entity entity; +public: + Entity(entt::registry& registry, entt::entity entity) + : registry(registry), entity(entity) {} + + bool isValid() const { + return registry.valid(entity); + } + + Transform& getTransform() const { + return registry.get(entity); + } + + entityid_t getUID() const { + return registry.get(entity).uid; + } +}; + +class Entities { + entt::registry registry; + Level* level; + std::unordered_map entities; +public: + Entities(Level* level); + void updatePhysics(float delta); + void render(Assets* assets, ModelBatch& batch); +}; + +#endif // OBJECTS_ENTITIES_HPP_ diff --git a/src/typedefs.hpp b/src/typedefs.hpp index 7b065aa3..0d8b9b6e 100644 --- a/src/typedefs.hpp +++ b/src/typedefs.hpp @@ -21,6 +21,7 @@ using ubyte = uint8_t; using itemid_t = uint32_t; using blockid_t = uint16_t; +using entityid_t = uint64_t; using itemcount_t = uint32_t; using blockstate_t = uint16_t; using light_t = uint16_t; diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 1df81c74..84b10742 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -10,6 +10,7 @@ #include "../physics/Hitbox.hpp" #include "../physics/PhysicsSolver.hpp" #include "../objects/Player.hpp" +#include "../objects/Entities.hpp" #include "../items/Inventory.hpp" #include "../items/Inventories.hpp" @@ -22,6 +23,7 @@ Level::Level( chunksStorage(std::make_unique(this)), physics(std::make_unique(glm::vec3(0, -22.6f, 0))), events(std::make_unique()), + entities(std::make_unique(this)), settings(settings) { auto inv = std::make_shared( diff --git a/src/world/Level.hpp b/src/world/Level.hpp index 7b18f06f..3f57ac10 100644 --- a/src/world/Level.hpp +++ b/src/world/Level.hpp @@ -14,6 +14,7 @@ class Content; class World; class Player; class Chunks; +class Entities; class Inventory; class Inventories; class LevelEvents; @@ -35,6 +36,7 @@ public: std::unique_ptr physics; std::unique_ptr lighting; std::unique_ptr events; + std::unique_ptr entities; const EngineSettings& settings;