add entity events: on_spawn, on_despawn
This commit is contained in:
parent
e1eb652ef4
commit
eb2be5e8b6
7
res/content/base/scripts/drop.lua
Normal file
7
res/content/base/scripts/drop.lua
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
function on_spawn(eid)
|
||||||
|
print("spawn", eid)
|
||||||
|
end
|
||||||
|
|
||||||
|
function on_despawn(eid)
|
||||||
|
print("despawn", eid)
|
||||||
|
end
|
||||||
@ -321,6 +321,11 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const st
|
|||||||
auto folder = pack->folder;
|
auto folder = pack->folder;
|
||||||
auto configFile = folder/fs::path("entities/"+name+".json");
|
auto configFile = folder/fs::path("entities/"+name+".json");
|
||||||
if (fs::exists(configFile)) loadEntity(def, full, configFile);
|
if (fs::exists(configFile)) loadEntity(def, full, configFile);
|
||||||
|
|
||||||
|
auto scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua");
|
||||||
|
if (fs::is_regular_file(scriptfile)) {
|
||||||
|
scripting::load_entity_script(env, full, scriptfile, def.rt.funcsset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContentLoader::loadBlock(Block& def, const std::string& full, const std::string& name) {
|
void ContentLoader::loadBlock(Block& def, const std::string& full, const std::string& name) {
|
||||||
|
|||||||
@ -13,6 +13,8 @@
|
|||||||
#include "../../logic/BlocksController.hpp"
|
#include "../../logic/BlocksController.hpp"
|
||||||
#include "../../logic/LevelController.hpp"
|
#include "../../logic/LevelController.hpp"
|
||||||
#include "../../objects/Player.hpp"
|
#include "../../objects/Player.hpp"
|
||||||
|
#include "../../objects/EntityDef.hpp"
|
||||||
|
#include "../../objects/Entities.hpp"
|
||||||
#include "../../util/stringutil.hpp"
|
#include "../../util/stringutil.hpp"
|
||||||
#include "../../util/timeutil.hpp"
|
#include "../../util/timeutil.hpp"
|
||||||
#include "../../util/timeutil.hpp"
|
#include "../../util/timeutil.hpp"
|
||||||
@ -225,6 +227,22 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool scripting::on_entity_spawn(const EntityDef& def, entityid_t eid) {
|
||||||
|
std::string name = def.name + ".spawn";
|
||||||
|
return lua::emit_event(lua::get_main_thread(), name, [eid] (auto L) {
|
||||||
|
lua::pushinteger(L, eid);
|
||||||
|
return 1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool scripting::on_entity_despawn(const EntityDef& def, entityid_t eid) {
|
||||||
|
std::string name = def.name + ".despawn";
|
||||||
|
return lua::emit_event(lua::get_main_thread(), name, [eid] (auto L) {
|
||||||
|
lua::pushinteger(L, eid);
|
||||||
|
return 1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_ui_open(
|
void scripting::on_ui_open(
|
||||||
UiDocument* layout,
|
UiDocument* layout,
|
||||||
std::vector<dynamic::Value> args
|
std::vector<dynamic::Value> args
|
||||||
@ -308,6 +326,14 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi
|
|||||||
funcsset.on_block_break_by = register_event(env, "on_block_break_by", prefix+".blockbreakby");
|
funcsset.on_block_break_by = register_event(env, "on_block_break_by", prefix+".blockbreakby");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void scripting::load_entity_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, entity_funcs_set& funcsset) {
|
||||||
|
int env = *senv;
|
||||||
|
load_script(env, "entity", file);
|
||||||
|
funcsset.init = register_event(env, "init", prefix+".init");
|
||||||
|
funcsset.on_spawn = register_event(env, "on_spawn", prefix+".spawn");
|
||||||
|
funcsset.on_despawn = register_event(env, "on_despawn", prefix+".despawn");
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {
|
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
load_script(env, "world", file);
|
load_script(env, "world", file);
|
||||||
|
|||||||
@ -27,9 +27,12 @@ class Inventory;
|
|||||||
class UiDocument;
|
class UiDocument;
|
||||||
struct block_funcs_set;
|
struct block_funcs_set;
|
||||||
struct item_funcs_set;
|
struct item_funcs_set;
|
||||||
|
struct entity_funcs_set;
|
||||||
struct uidocscript;
|
struct uidocscript;
|
||||||
class BlocksController;
|
class BlocksController;
|
||||||
class LevelController;
|
class LevelController;
|
||||||
|
class Entity;
|
||||||
|
struct EntityDef;
|
||||||
|
|
||||||
namespace scripting {
|
namespace scripting {
|
||||||
extern Engine* engine;
|
extern Engine* engine;
|
||||||
@ -73,6 +76,10 @@ namespace scripting {
|
|||||||
/// @return true if prevents default action
|
/// @return true if prevents default action
|
||||||
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||||
|
|
||||||
|
bool on_entity_spawn(const EntityDef& def, entityid_t eid);
|
||||||
|
|
||||||
|
bool on_entity_despawn(const EntityDef& def, entityid_t eid);
|
||||||
|
|
||||||
/// @brief Called on UI view show
|
/// @brief Called on UI view show
|
||||||
void on_ui_open(
|
void on_ui_open(
|
||||||
UiDocument* layout,
|
UiDocument* layout,
|
||||||
@ -89,14 +96,28 @@ namespace scripting {
|
|||||||
/// @param prefix pack id
|
/// @param prefix pack id
|
||||||
/// @param file item script file
|
/// @param file item script file
|
||||||
/// @param funcsset block callbacks set
|
/// @param funcsset block callbacks set
|
||||||
void load_block_script(const scriptenv& env, const std::string& prefix, const fs::path& file, block_funcs_set& funcsset);
|
void load_block_script(
|
||||||
|
const scriptenv& env,
|
||||||
|
const std::string& prefix,
|
||||||
|
const fs::path& file,
|
||||||
|
block_funcs_set& funcsset);
|
||||||
|
|
||||||
/// @brief Load script associated with an Item
|
/// @brief Load script associated with an Item
|
||||||
/// @param env environment
|
/// @param env environment
|
||||||
/// @param prefix pack id
|
/// @param prefix pack id
|
||||||
/// @param file item script file
|
/// @param file item script file
|
||||||
/// @param funcsset item callbacks set
|
/// @param funcsset item callbacks set
|
||||||
void load_item_script(const scriptenv& env, const std::string& prefix, const fs::path& file, item_funcs_set& funcsset);
|
void load_item_script(
|
||||||
|
const scriptenv& env,
|
||||||
|
const std::string& prefix,
|
||||||
|
const fs::path& file,
|
||||||
|
item_funcs_set& funcsset);
|
||||||
|
|
||||||
|
void load_entity_script(
|
||||||
|
const scriptenv& env,
|
||||||
|
const std::string& prefix,
|
||||||
|
const fs::path& file,
|
||||||
|
entity_funcs_set& funcsset);
|
||||||
|
|
||||||
/// @brief Load package-specific world script
|
/// @brief Load package-specific world script
|
||||||
/// @param env environment
|
/// @param env environment
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
#include "../graphics/core/Model.hpp"
|
#include "../graphics/core/Model.hpp"
|
||||||
#include "../maths/FrustumCulling.hpp"
|
#include "../maths/FrustumCulling.hpp"
|
||||||
#include "../objects/EntityDef.hpp"
|
#include "../objects/EntityDef.hpp"
|
||||||
|
#include "../logic/scripting/scripting.hpp"
|
||||||
|
|
||||||
#include <glm/ext/matrix_transform.hpp>
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
|
|
||||||
@ -19,6 +20,12 @@ void Transform::refresh() {
|
|||||||
combined = combined * glm::mat4(rot);
|
combined = combined * glm::mat4(rot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entity::destroy() {
|
||||||
|
if (isValid()){
|
||||||
|
entities.despawn(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Entities::Entities(Level* level) : level(level) {
|
Entities::Entities(Level* level) : level(level) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,13 +33,23 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
|
|||||||
auto entity = registry.create();
|
auto entity = registry.create();
|
||||||
glm::vec3 size(1);
|
glm::vec3 size(1);
|
||||||
auto id = nextID++;
|
auto id = nextID++;
|
||||||
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id));
|
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id), def);
|
||||||
registry.emplace<Transform>(entity, pos, size/4.0f, glm::mat3(1.0f));
|
registry.emplace<Transform>(entity, pos, size/4.0f, glm::mat3(1.0f));
|
||||||
registry.emplace<Rigidbody>(entity, true, Hitbox {pos, def.hitbox});
|
registry.emplace<Rigidbody>(entity, true, Hitbox {pos, def.hitbox});
|
||||||
entities[id] = entity;
|
entities[id] = entity;
|
||||||
|
if (def.rt.funcsset.on_spawn) {
|
||||||
|
scripting::on_entity_spawn(def, id);
|
||||||
|
}
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entities::despawn(entityid_t id) {
|
||||||
|
if (auto entity = get(id)) {
|
||||||
|
scripting::on_entity_despawn(entity->getDef(), id);
|
||||||
|
registry.destroy(get(id)->getHandler());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Entities::clean() {
|
void Entities::clean() {
|
||||||
for (auto it = entities.begin(); it != entities.end();) {
|
for (auto it = entities.begin(); it != entities.end();) {
|
||||||
if (registry.valid(it->second)) {
|
if (registry.valid(it->second)) {
|
||||||
@ -60,7 +77,7 @@ void Entities::updatePhysics(float delta){
|
|||||||
1.0f,
|
1.0f,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
hitbox.linear_damping = hitbox.grounded * 12;
|
hitbox.linearDamping = hitbox.grounded * 12;
|
||||||
transform.pos = hitbox.position;
|
transform.pos = hitbox.position;
|
||||||
//transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0));
|
//transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0));
|
||||||
if (hitbox.grounded) {
|
if (hitbox.grounded) {
|
||||||
|
|||||||
@ -9,8 +9,11 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <entt/entity/registry.hpp>
|
#include <entt/entity/registry.hpp>
|
||||||
|
|
||||||
|
struct EntityDef;
|
||||||
|
|
||||||
struct EntityId {
|
struct EntityId {
|
||||||
entityid_t uid;
|
entityid_t uid;
|
||||||
|
const EntityDef& def;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Transform {
|
struct Transform {
|
||||||
@ -33,15 +36,16 @@ class LineBatch;
|
|||||||
class ModelBatch;
|
class ModelBatch;
|
||||||
class Frustum;
|
class Frustum;
|
||||||
class Rig;
|
class Rig;
|
||||||
struct EntityDef;
|
class Entities;
|
||||||
|
|
||||||
class Entity {
|
class Entity {
|
||||||
|
Entities& entities;
|
||||||
entityid_t id;
|
entityid_t id;
|
||||||
entt::registry& registry;
|
entt::registry& registry;
|
||||||
const entt::entity entity;
|
const entt::entity entity;
|
||||||
public:
|
public:
|
||||||
Entity(entityid_t id, entt::registry& registry, const entt::entity entity)
|
Entity(Entities& entities, entityid_t id, entt::registry& registry, const entt::entity entity)
|
||||||
: id(id), registry(registry), entity(entity) {}
|
: entities(entities), id(id), registry(registry), entity(entity) {}
|
||||||
|
|
||||||
entityid_t getID() const {
|
entityid_t getID() const {
|
||||||
return id;
|
return id;
|
||||||
@ -51,6 +55,10 @@ public:
|
|||||||
return registry.valid(entity);
|
return registry.valid(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const EntityDef& getDef() const {
|
||||||
|
return registry.get<EntityId>(entity).def;
|
||||||
|
}
|
||||||
|
|
||||||
Transform& getTransform() const {
|
Transform& getTransform() const {
|
||||||
return registry.get<Transform>(entity);
|
return registry.get<Transform>(entity);
|
||||||
}
|
}
|
||||||
@ -63,9 +71,11 @@ public:
|
|||||||
return registry.get<EntityId>(entity).uid;
|
return registry.get<EntityId>(entity).uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy() {
|
entt::entity getHandler() const {
|
||||||
registry.destroy(entity);
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void destroy();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Entities {
|
class Entities {
|
||||||
@ -86,11 +96,13 @@ public:
|
|||||||
std::optional<Entity> get(entityid_t id) {
|
std::optional<Entity> get(entityid_t id) {
|
||||||
const auto& found = entities.find(id);
|
const auto& found = entities.find(id);
|
||||||
if (found != entities.end() && registry.valid(found->second)) {
|
if (found != entities.end() && registry.valid(found->second)) {
|
||||||
return Entity(id, registry, found->second);
|
return Entity(*this, id, registry, found->second);
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void despawn(entityid_t id);
|
||||||
|
|
||||||
inline size_t size() const {
|
inline size_t size() const {
|
||||||
return entities.size();
|
return entities.size();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,12 @@
|
|||||||
|
|
||||||
#include "../typedefs.hpp"
|
#include "../typedefs.hpp"
|
||||||
|
|
||||||
|
struct entity_funcs_set {
|
||||||
|
bool init : 1;
|
||||||
|
bool on_spawn : 1;
|
||||||
|
bool on_despawn : 1;
|
||||||
|
};
|
||||||
|
|
||||||
struct EntityDef {
|
struct EntityDef {
|
||||||
/// @brief Entity string id (with prefix included)
|
/// @brief Entity string id (with prefix included)
|
||||||
std::string const name;
|
std::string const name;
|
||||||
@ -15,6 +21,7 @@ struct EntityDef {
|
|||||||
|
|
||||||
struct {
|
struct {
|
||||||
entityid_t id;
|
entityid_t id;
|
||||||
|
entity_funcs_set funcsset;
|
||||||
} rt;
|
} rt;
|
||||||
|
|
||||||
EntityDef(const std::string& name) : name(name) {}
|
EntityDef(const std::string& name) : name(name) {}
|
||||||
|
|||||||
@ -110,9 +110,9 @@ void Player::updateInput(
|
|||||||
noclip = !noclip;
|
noclip = !noclip;
|
||||||
}
|
}
|
||||||
|
|
||||||
hitbox->linear_damping = PLAYER_GROUND_DAMPING;
|
hitbox->linearDamping = PLAYER_GROUND_DAMPING;
|
||||||
if (flight){
|
if (flight){
|
||||||
hitbox->linear_damping = PLAYER_AIR_DAMPING;
|
hitbox->linearDamping = PLAYER_AIR_DAMPING;
|
||||||
hitbox->velocity.y *= 1.0f - delta * 9;
|
hitbox->velocity.y *= 1.0f - delta * 9;
|
||||||
if (input.jump){
|
if (input.jump){
|
||||||
hitbox->velocity.y += speed * delta * 9;
|
hitbox->velocity.y += speed * delta * 9;
|
||||||
@ -122,7 +122,7 @@ void Player::updateInput(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hitbox->grounded) {
|
if (!hitbox->grounded) {
|
||||||
hitbox->linear_damping = PLAYER_AIR_DAMPING;
|
hitbox->linearDamping = PLAYER_AIR_DAMPING;
|
||||||
}
|
}
|
||||||
|
|
||||||
input.noclip = false;
|
input.noclip = false;
|
||||||
|
|||||||
@ -11,10 +11,10 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Camera;
|
class Camera;
|
||||||
class Hitbox;
|
|
||||||
class Inventory;
|
class Inventory;
|
||||||
class ContentLUT;
|
class ContentLUT;
|
||||||
class Level;
|
class Level;
|
||||||
|
struct Hitbox;
|
||||||
struct EngineSettings;
|
struct EngineSettings;
|
||||||
|
|
||||||
struct PlayerInput {
|
struct PlayerInput {
|
||||||
|
|||||||
@ -4,5 +4,5 @@ Hitbox::Hitbox(glm::vec3 position, glm::vec3 halfsize)
|
|||||||
: position(position),
|
: position(position),
|
||||||
halfsize(halfsize),
|
halfsize(halfsize),
|
||||||
velocity(0.0f,0.0f,0.0f),
|
velocity(0.0f,0.0f,0.0f),
|
||||||
linear_damping(0.1f)
|
linearDamping(0.1f)
|
||||||
{}
|
{}
|
||||||
|
|||||||
@ -3,12 +3,11 @@
|
|||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Hitbox {
|
struct Hitbox {
|
||||||
public:
|
|
||||||
glm::vec3 position;
|
glm::vec3 position;
|
||||||
glm::vec3 halfsize;
|
glm::vec3 halfsize;
|
||||||
glm::vec3 velocity;
|
glm::vec3 velocity;
|
||||||
float linear_damping;
|
float linearDamping;
|
||||||
bool grounded = false;
|
bool grounded = false;
|
||||||
|
|
||||||
Hitbox(glm::vec3 position, glm::vec3 halfsize);
|
Hitbox(glm::vec3 position, glm::vec3 halfsize);
|
||||||
|
|||||||
@ -22,7 +22,7 @@ void PhysicsSolver::step(
|
|||||||
bool collisions
|
bool collisions
|
||||||
) {
|
) {
|
||||||
float dt = delta / static_cast<float>(substeps);
|
float dt = delta / static_cast<float>(substeps);
|
||||||
float linear_damping = hitbox->linear_damping;
|
float linearDamping = hitbox->linearDamping;
|
||||||
float s = 2.0f/BLOCK_AABB_GRID;
|
float s = 2.0f/BLOCK_AABB_GRID;
|
||||||
|
|
||||||
const glm::vec3& half = hitbox->halfsize;
|
const glm::vec3& half = hitbox->halfsize;
|
||||||
@ -41,8 +41,8 @@ void PhysicsSolver::step(
|
|||||||
colisionCalc(chunks, hitbox, vel, pos, half,
|
colisionCalc(chunks, hitbox, vel, pos, half,
|
||||||
(prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f);
|
(prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f);
|
||||||
}
|
}
|
||||||
vel.x *= glm::max(0.0f, 1.0f - dt * linear_damping);
|
vel.x *= glm::max(0.0f, 1.0f - dt * linearDamping);
|
||||||
vel.z *= glm::max(0.0f, 1.0f - dt * linear_damping);
|
vel.z *= glm::max(0.0f, 1.0f - dt * linearDamping);
|
||||||
|
|
||||||
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
|
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
|
||||||
if (hitbox->grounded && pos.y < py) {
|
if (hitbox->grounded && pos.y < py) {
|
||||||
|
|||||||
@ -7,8 +7,8 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Block;
|
class Block;
|
||||||
class Hitbox;
|
|
||||||
class Chunks;
|
class Chunks;
|
||||||
|
struct Hitbox;
|
||||||
|
|
||||||
class PhysicsSolver {
|
class PhysicsSolver {
|
||||||
glm::vec3 gravity;
|
glm::vec3 gravity;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user