add drop.json
This commit is contained in:
parent
8d41f6af11
commit
20ab48ecab
@ -1,6 +1,6 @@
|
||||
{
|
||||
"items": [
|
||||
"bazalt_breaker"
|
||||
"entities": [
|
||||
"drop"
|
||||
],
|
||||
"blocks": [
|
||||
"dirt",
|
||||
@ -27,5 +27,8 @@
|
||||
"lightbulb",
|
||||
"torch",
|
||||
"wooden_door"
|
||||
],
|
||||
"items": [
|
||||
"bazalt_breaker"
|
||||
]
|
||||
}
|
||||
3
res/content/base/entities/drop.json
Normal file
3
res/content/base/entities/drop.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"hitbox": [0.2, 0.2, 0.2]
|
||||
}
|
||||
@ -5,7 +5,7 @@ function on_hud_open()
|
||||
input.add_callback("player.drop", function ()
|
||||
local pid = hud.get_player()
|
||||
local pvel = {player.get_vel(pid)}
|
||||
local eid = entity.test()
|
||||
local eid = entity.spawn("base:drop")
|
||||
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
|
||||
entity.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
|
||||
end)
|
||||
|
||||
@ -301,8 +301,7 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa
|
||||
root->num("stack-size", def.stackSize);
|
||||
|
||||
// item light emission [r, g, b] where r,g,b in range [0..15]
|
||||
auto emissionarr = root->list("emission");
|
||||
if (emissionarr) {
|
||||
if (auto emissionarr = root->list("emission")) {
|
||||
def.emission[0] = emissionarr->num(0);
|
||||
def.emission[1] = emissionarr->num(1);
|
||||
def.emission[2] = emissionarr->num(2);
|
||||
@ -311,7 +310,11 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa
|
||||
|
||||
void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs::path& file) {
|
||||
auto root = files::read_json(file);
|
||||
|
||||
root->str("script-name", def.scriptName);
|
||||
if (auto boxarr = root->list("hitbox")) {
|
||||
def.hitbox = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
|
||||
}
|
||||
std::cout << "loading entity " << name << " from " << file.u8string() << std::endl;
|
||||
}
|
||||
|
||||
void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) {
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include "../../../physics/Hitbox.hpp"
|
||||
#include "../../../window/Camera.hpp"
|
||||
#include "../../../frontend/hud.hpp"
|
||||
#include "../../../content/Content.hpp"
|
||||
|
||||
#include <optional>
|
||||
|
||||
@ -21,10 +22,12 @@ static std::optional<Entity> get_entity(lua::State* L, int idx) {
|
||||
return level->entities->get(id);
|
||||
}
|
||||
|
||||
static int l_test(lua::State* L) {
|
||||
static int l_spawn(lua::State* L) {
|
||||
auto level = controller->getLevel();
|
||||
auto player = hud->getPlayer();
|
||||
auto id = level->entities->drop(player->camera->position);
|
||||
auto defname = lua::tostring(L, 1);
|
||||
auto& def = content->entities.require(defname);
|
||||
auto id = level->entities->spawn(def, player->camera->position);
|
||||
return lua::pushinteger(L, id);
|
||||
}
|
||||
|
||||
@ -43,7 +46,7 @@ static int l_set_vel(lua::State* L) {
|
||||
}
|
||||
|
||||
const luaL_Reg entitylib [] = {
|
||||
{"test", lua::wrap<l_test>},
|
||||
{"spawn", lua::wrap<l_spawn>},
|
||||
{"get_vel", lua::wrap<l_get_vel>},
|
||||
{"set_vel", lua::wrap<l_set_vel>},
|
||||
{NULL, NULL}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "../graphics/core/LineBatch.hpp"
|
||||
#include "../graphics/core/Model.hpp"
|
||||
#include "../maths/FrustumCulling.hpp"
|
||||
#include "../objects/EntityDef.hpp"
|
||||
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
@ -21,14 +22,13 @@ void Transform::refresh() {
|
||||
Entities::Entities(Level* level) : level(level) {
|
||||
}
|
||||
|
||||
entityid_t Entities::drop(glm::vec3 pos) {
|
||||
entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
|
||||
auto entity = registry.create();
|
||||
glm::vec3 size(1);
|
||||
auto id = nextID++;
|
||||
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id));
|
||||
registry.emplace<Transform>(entity, pos, size/4.0f, glm::mat3(1.0f));
|
||||
registry.emplace<Hitbox>(entity, pos,
|
||||
glm::vec3(size.x*0.2f, size.y*0.5f, size.z*0.2f));
|
||||
registry.emplace<Hitbox>(entity, pos, def.hitbox);
|
||||
entities[id] = entity;
|
||||
return id;
|
||||
}
|
||||
|
||||
@ -28,6 +28,7 @@ class LineBatch;
|
||||
class ModelBatch;
|
||||
class Frustum;
|
||||
class Rig;
|
||||
struct EntityDef;
|
||||
|
||||
class Entity {
|
||||
entt::registry& registry;
|
||||
@ -65,7 +66,7 @@ public:
|
||||
void renderDebug(LineBatch& batch);
|
||||
void render(Assets* assets, ModelBatch& batch, Frustum& frustum);
|
||||
|
||||
entityid_t drop(glm::vec3 pos);
|
||||
entityid_t spawn(EntityDef& def, glm::vec3 pos);
|
||||
|
||||
std::optional<Entity> get(entityid_t id) {
|
||||
const auto& found = entities.find(id);
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#define OBJECTS_ENTITY_DEF_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../typedefs.hpp"
|
||||
|
||||
@ -10,6 +11,7 @@ struct EntityDef {
|
||||
std::string const name;
|
||||
|
||||
std::string scriptName = name.substr(name.find(':')+1);
|
||||
glm::vec3 hitbox {0.5f};
|
||||
|
||||
struct {
|
||||
entityid_t id;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user