implement player entity

This commit is contained in:
MihailRis 2024-07-08 18:45:35 +03:00
parent 49aa64a033
commit 487ba84517
8 changed files with 24 additions and 12 deletions

View File

@ -1,6 +1,6 @@
{ {
"entities": [ "items": [
"drop" "bazalt_breaker"
], ],
"blocks": [ "blocks": [
"dirt", "dirt",
@ -28,7 +28,8 @@
"torch", "torch",
"wooden_door" "wooden_door"
], ],
"items": [ "entities": [
"bazalt_breaker" "drop",
"player"
] ]
} }

View File

@ -0,0 +1,3 @@
{
"hitbox": [0.4, 0.9, 0.4]
}

View File

@ -1,3 +0,0 @@
{
"hitbox": [0.6, 1.8, 0.6]
}

View File

@ -439,6 +439,7 @@ void ContentLoader::load() {
stats->totalItems++; stats->totalItems++;
} }
} }
if (auto entitiesarr = root->list("entities")) { if (auto entitiesarr = root->list("entities")) {
for (size_t i = 0; i < entitiesarr->size(); i++) { for (size_t i = 0; i < entitiesarr->size(); i++) {
std::string name = entitiesarr->str(i); std::string name = entitiesarr->str(i);

View File

@ -273,7 +273,7 @@ dynamic::Value scripting::get_component_value(const scriptenv& env, const std::s
} }
void scripting::on_entity_spawn( void scripting::on_entity_spawn(
const EntityDef& def, const EntityDef&,
entityid_t eid, entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components, const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Value args, dynamic::Value args,
@ -285,9 +285,12 @@ void scripting::on_entity_spawn(
lua::pushinteger(L, eid); lua::pushinteger(L, eid);
lua::call(L, 1); lua::call(L, 1);
} }
for (size_t i = 0; i < components.size()-1; i++) { if (components.size() > 1) {
lua::pushvalue(L, -1); for (int i = 0; i < components.size()-1; i++) {
lua::pushvalue(L, -1);
}
} }
std::cout << "entity duplicated " << (components.size()-1) << " times" << std::endl;
for (auto& component : components) { for (auto& component : components) {
auto compenv = create_component_environment(get_root_environment(), -1, auto compenv = create_component_environment(get_root_environment(), -1,
component->name); component->name);

View File

@ -66,6 +66,7 @@ entityid_t Entities::spawn(
dynamic::Map_sptr saved, dynamic::Map_sptr saved,
entityid_t uid) entityid_t uid)
{ {
std::cout << "spawn entity " << def.name << std::endl;
auto rig = level->content->getRig(def.rigName); auto rig = level->content->getRig(def.rigName);
if (rig == nullptr) { if (rig == nullptr) {
throw std::runtime_error("rig "+def.rigName+" not found"); throw std::runtime_error("rig "+def.rigName+" not found");
@ -108,8 +109,10 @@ entityid_t Entities::spawn(
componentName, entity_funcs_set {}, nullptr); componentName, entity_funcs_set {}, nullptr);
scripting.components.emplace_back(std::move(component)); scripting.components.emplace_back(std::move(component));
} }
std::cout << "on_entity_spawn" << std::endl;
scripting::on_entity_spawn( scripting::on_entity_spawn(
def, id, scripting.components, std::move(args), std::move(saved)); def, id, scripting.components, std::move(args), std::move(saved));
std::cout << "done" << std::endl;
return id; return id;
} }

View File

@ -41,7 +41,10 @@ Player::~Player() {
void Player::updateEntity() { void Player::updateEntity() {
if (eid == 0) { if (eid == 0) {
// spawn entity auto& def = level->content->entities.require("base:player");
eid = level->entities->spawn(def, Transform {
getPosition(), glm::vec3(1.0f), glm::mat3(1.0f), {}, true
});
} else if (auto entity = level->entities->get(eid)) { } else if (auto entity = level->entities->get(eid)) {
position = entity->getTransform().pos; position = entity->getTransform().pos;
} else { } else {
@ -264,11 +267,12 @@ void Player::deserialize(dynamic::Map *src) {
src->flag("flight", flight); src->flag("flight", flight);
src->flag("noclip", noclip); src->flag("noclip", noclip);
setChosenSlot(src->get("chosen-slot", getChosenSlot())); setChosenSlot(src->get("chosen-slot", getChosenSlot()));
src->num("enitity", eid); src->num("entity", eid);
if (auto invmap = src->map("inventory")) { if (auto invmap = src->map("inventory")) {
getInventory()->deserialize(invmap.get()); getInventory()->deserialize(invmap.get());
} }
std::cout << "Player::DESERIALIZE " << eid << std::endl;
} }
void Player::convert(dynamic::Map* data, const ContentLUT* lut) { void Player::convert(dynamic::Map* data, const ContentLUT* lut) {