add test entities prototype
This commit is contained in:
parent
6f618ae3ff
commit
ba458be334
@ -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();
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 blockid_t = uint16_t;
|
||||
|
||||
using entityid_t = uint64_t;
|
||||
using itemcount_t = uint32_t;
|
||||
using blockstate_t = uint16_t;
|
||||
using light_t = uint16_t;
|
||||
|
||||
@ -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<ChunksStorage>(this)),
|
||||
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
|
||||
events(std::make_unique<LevelEvents>()),
|
||||
entities(std::make_unique<Entities>(this)),
|
||||
settings(settings)
|
||||
{
|
||||
auto inv = std::make_shared<Inventory>(
|
||||
|
||||
@ -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<PhysicsSolver> physics;
|
||||
std::unique_ptr<Lighting> lighting;
|
||||
std::unique_ptr<LevelEvents> events;
|
||||
std::unique_ptr<Entities> entities;
|
||||
|
||||
const EngineSettings& settings;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user