add test entities prototype
This commit is contained in:
parent
6f618ae3ff
commit
ba458be334
@ -15,6 +15,7 @@
|
|||||||
#include "../../maths/FrustumCulling.hpp"
|
#include "../../maths/FrustumCulling.hpp"
|
||||||
#include "../../maths/voxmaths.hpp"
|
#include "../../maths/voxmaths.hpp"
|
||||||
#include "../../objects/Player.hpp"
|
#include "../../objects/Player.hpp"
|
||||||
|
#include "../../objects/Entities.hpp"
|
||||||
#include "../../settings.hpp"
|
#include "../../settings.hpp"
|
||||||
#include "../../voxels/Block.hpp"
|
#include "../../voxels/Block.hpp"
|
||||||
#include "../../voxels/Chunk.hpp"
|
#include "../../voxels/Chunk.hpp"
|
||||||
@ -195,7 +196,7 @@ void WorldRenderer::renderLevel(
|
|||||||
drawChunks(level->chunks.get(), camera, shader);
|
drawChunks(level->chunks.get(), camera, shader);
|
||||||
|
|
||||||
shader->uniformMatrix("u_model", glm::mat4(1.0f));
|
shader->uniformMatrix("u_model", glm::mat4(1.0f));
|
||||||
// draw entities here
|
level->entities->render(assets, *modelBatch);
|
||||||
modelBatch->render();
|
modelBatch->render();
|
||||||
|
|
||||||
skybox->unbind();
|
skybox->unbind();
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include "../world/Level.hpp"
|
#include "../world/Level.hpp"
|
||||||
#include "../world/World.hpp"
|
#include "../world/World.hpp"
|
||||||
#include "../physics/Hitbox.hpp"
|
#include "../physics/Hitbox.hpp"
|
||||||
|
#include "../objects/Entities.hpp"
|
||||||
|
|
||||||
#include "scripting/scripting.hpp"
|
#include "scripting/scripting.hpp"
|
||||||
#include "../interfaces/Object.hpp"
|
#include "../interfaces/Object.hpp"
|
||||||
@ -47,6 +48,7 @@ void LevelController::update(float delta, bool input, bool pause) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
blocks->update(delta);
|
blocks->update(delta);
|
||||||
|
level->entities->updatePhysics(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
55
src/objects/Entities.cpp
Normal file
55
src/objects/Entities.cpp
Normal file
@ -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 <glm/ext/matrix_transform.hpp>
|
||||||
|
|
||||||
|
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<EntityId>(entity, 1);
|
||||||
|
registry.emplace<Transform>(entity, pos, size, glm::mat3(1.0f));
|
||||||
|
registry.emplace<Hitbox>(entity, pos, size/20.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entities::updatePhysics(float delta){
|
||||||
|
auto view = registry.view<Transform, Hitbox>();
|
||||||
|
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<Transform>();
|
||||||
|
auto model = assets->get<model::Model>("dingus");
|
||||||
|
for (auto [entity, transform] : view.each()) {
|
||||||
|
transform.refresh();
|
||||||
|
batch.pushMatrix(transform.combined);
|
||||||
|
batch.draw(model);
|
||||||
|
batch.popMatrix();
|
||||||
|
}
|
||||||
|
}
|
||||||
57
src/objects/Entities.hpp
Normal file
57
src/objects/Entities.hpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#ifndef OBJECTS_ENTITIES_HPP_
|
||||||
|
#define OBJECTS_ENTITIES_HPP_
|
||||||
|
|
||||||
|
#include "../typedefs.hpp"
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <entt/entity/registry.hpp>
|
||||||
|
|
||||||
|
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<Transform>(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
entityid_t getUID() const {
|
||||||
|
return registry.get<EntityId>(entity).uid;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Entities {
|
||||||
|
entt::registry registry;
|
||||||
|
Level* level;
|
||||||
|
std::unordered_map<entityid_t, entt::entity> entities;
|
||||||
|
public:
|
||||||
|
Entities(Level* level);
|
||||||
|
void updatePhysics(float delta);
|
||||||
|
void render(Assets* assets, ModelBatch& batch);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // OBJECTS_ENTITIES_HPP_
|
||||||
@ -21,6 +21,7 @@ using ubyte = uint8_t;
|
|||||||
using itemid_t = uint32_t;
|
using itemid_t = uint32_t;
|
||||||
using blockid_t = uint16_t;
|
using blockid_t = uint16_t;
|
||||||
|
|
||||||
|
using entityid_t = uint64_t;
|
||||||
using itemcount_t = uint32_t;
|
using itemcount_t = uint32_t;
|
||||||
using blockstate_t = uint16_t;
|
using blockstate_t = uint16_t;
|
||||||
using light_t = uint16_t;
|
using light_t = uint16_t;
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
#include "../physics/Hitbox.hpp"
|
#include "../physics/Hitbox.hpp"
|
||||||
#include "../physics/PhysicsSolver.hpp"
|
#include "../physics/PhysicsSolver.hpp"
|
||||||
#include "../objects/Player.hpp"
|
#include "../objects/Player.hpp"
|
||||||
|
#include "../objects/Entities.hpp"
|
||||||
#include "../items/Inventory.hpp"
|
#include "../items/Inventory.hpp"
|
||||||
#include "../items/Inventories.hpp"
|
#include "../items/Inventories.hpp"
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ Level::Level(
|
|||||||
chunksStorage(std::make_unique<ChunksStorage>(this)),
|
chunksStorage(std::make_unique<ChunksStorage>(this)),
|
||||||
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
|
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
|
||||||
events(std::make_unique<LevelEvents>()),
|
events(std::make_unique<LevelEvents>()),
|
||||||
|
entities(std::make_unique<Entities>(this)),
|
||||||
settings(settings)
|
settings(settings)
|
||||||
{
|
{
|
||||||
auto inv = std::make_shared<Inventory>(
|
auto inv = std::make_shared<Inventory>(
|
||||||
|
|||||||
@ -14,6 +14,7 @@ class Content;
|
|||||||
class World;
|
class World;
|
||||||
class Player;
|
class Player;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
|
class Entities;
|
||||||
class Inventory;
|
class Inventory;
|
||||||
class Inventories;
|
class Inventories;
|
||||||
class LevelEvents;
|
class LevelEvents;
|
||||||
@ -35,6 +36,7 @@ public:
|
|||||||
std::unique_ptr<PhysicsSolver> physics;
|
std::unique_ptr<PhysicsSolver> physics;
|
||||||
std::unique_ptr<Lighting> lighting;
|
std::unique_ptr<Lighting> lighting;
|
||||||
std::unique_ptr<LevelEvents> events;
|
std::unique_ptr<LevelEvents> events;
|
||||||
|
std::unique_ptr<Entities> entities;
|
||||||
|
|
||||||
const EngineSettings& settings;
|
const EngineSettings& settings;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user