add test entities prototype

This commit is contained in:
MihailRis
2024-06-23 22:09:04 +03:00
parent 6f618ae3ff
commit ba458be334
7 changed files with 121 additions and 1 deletions
+55
View 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
View 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_