From 055781eeaf4ad8b6c0a013e880090964653af024 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 15 Jul 2024 08:56:27 +0300 Subject: [PATCH] add skeleton:index(...) and player test model --- doc/en/scripting/ecs.md | 3 + doc/ru/scripting/ecs.md | 3 + res/content/base/models/player.obj | 79 ++++++++++++++++++ res/content/base/preload.json | 3 +- res/content/base/skeletons/player.json | 8 +- res/content/base/textures/entities/player.png | Bin 0 -> 579 bytes res/modules/internal/stdcomp.lua | 1 + src/logic/scripting/lua/lib__skeleton.cpp | 11 +++ src/objects/Player.cpp | 41 +++++---- src/objects/rigging.cpp | 16 +++- src/objects/rigging.hpp | 6 ++ 11 files changed, 148 insertions(+), 23 deletions(-) create mode 100644 res/content/base/models/player.obj create mode 100644 res/content/base/textures/entities/player.png diff --git a/doc/en/scripting/ecs.md b/doc/en/scripting/ecs.md index 852f756e..6c294f9b 100644 --- a/doc/en/scripting/ecs.md +++ b/doc/en/scripting/ecs.md @@ -128,4 +128,7 @@ rig:get_texture(key: str) -> str -- Assigns texture by key rig:set_texture(key: str, value: str) + +-- Returns the bone index by name or nil +rig:index(name: str) -> int ``` diff --git a/doc/ru/scripting/ecs.md b/doc/ru/scripting/ecs.md index 8970304a..3d83eb21 100644 --- a/doc/ru/scripting/ecs.md +++ b/doc/ru/scripting/ecs.md @@ -129,4 +129,7 @@ rig:get_texture(key: str) -> str -- Назначает текстуру по ключу rig:set_texture(key: str, value: str) + +-- Возвращает индекс кости по имени или nil +rig:index(name: str) -> int ``` diff --git a/res/content/base/models/player.obj b/res/content/base/models/player.obj new file mode 100644 index 00000000..b038797f --- /dev/null +++ b/res/content/base/models/player.obj @@ -0,0 +1,79 @@ +o Cube +v 0.125000 -0.900000 -0.125000 +v 0.125000 -0.900000 0.125000 +v -0.125000 -0.900000 0.125000 +v -0.125000 -0.900000 -0.125000 +v 0.125000 0.700000 -0.125000 +v 0.125000 0.700000 0.125000 +v -0.125000 0.700000 0.125000 +v -0.125000 0.700000 -0.125000 +v -0.238204 0.898288 -0.238204 +v -0.238204 0.421881 -0.238204 +v -0.238204 0.898288 0.238204 +v -0.238204 0.421881 0.238204 +v 0.238204 0.421881 0.238204 +v 0.238204 0.898288 0.238204 +v 0.238204 0.421881 -0.238204 +v 0.238204 0.898288 -0.238204 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.209065 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.209065 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.209065 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.783122 0.209065 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.209065 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.209065 +vt 0.735873 0.213345 +vt 0.735873 0.739780 +vt 0.209439 0.739780 +vt 0.209439 0.213345 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.209065 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.209065 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.982503 0.209065 +vt 0.783122 0.009685 +vt 0.982503 0.009685 +vt 0.783122 0.209065 +vn 0.0000 -1.0000 -0.0000 +vn 0.0000 1.0000 0.0000 +vn 1.0000 0.0000 0.0000 +vn -0.0000 -0.0000 1.0000 +vn -1.0000 -0.0000 -0.0000 +vn 0.0000 0.0000 -1.0000 +usemtl entities/player +s off +f 1/1/1 2/2/1 3/3/1 4/4/1 +f 5/5/2 8/6/2 7/7/2 6/8/2 +f 1/1/3 5/9/3 6/10/3 2/11/3 +f 2/12/4 6/13/4 7/7/4 3/14/4 +f 3/15/5 7/16/5 8/17/5 4/4/5 +f 5/5/6 1/18/6 4/19/6 8/20/6 +f 12/21/5 11/22/5 9/23/5 10/24/5 +f 10/25/6 9/26/6 16/27/6 15/28/6 +f 15/29/3 16/30/3 14/31/3 13/32/3 +f 13/33/4 14/34/4 11/35/4 12/36/4 +f 10/37/1 15/38/1 13/39/1 12/36/1 +f 16/40/2 9/41/2 11/35/2 14/42/2 diff --git a/res/content/base/preload.json b/res/content/base/preload.json index 693fe7a9..caf81b1c 100644 --- a/res/content/base/preload.json +++ b/res/content/base/preload.json @@ -7,6 +7,7 @@ "models": [ "block", "drop-block", - "drop-item" + "drop-item", + "player" ] } diff --git a/res/content/base/skeletons/player.json b/res/content/base/skeletons/player.json index 9065044f..a2a41980 100644 --- a/res/content/base/skeletons/player.json +++ b/res/content/base/skeletons/player.json @@ -1,3 +1,9 @@ { - "root": {} + "root": { + "nodes": [ + { + "model": "player" + } + ] + } } diff --git a/res/content/base/textures/entities/player.png b/res/content/base/textures/entities/player.png new file mode 100644 index 0000000000000000000000000000000000000000..3e92a4307f70d3db70d734a70cfa2e7f96bf6be0 GIT binary patch literal 579 zcmV-J0=)f+P)EX>4Tx04R}tkv&MmKpe$iQ%glE9n2u&kfAzR5G&%SRVYG*P%E_RU~=gfG_fQp zE{=k0!NHHks)LKOt`4q(Aou~|?BJy6A|?JWDYS_3;J6>}?mh0_0YbCNbgO3^&<)$n zq>@rDzaoWS5k?dtBp@oxs?ExB8ouM}9s$1I#d((hxj#q0RIHu_aJ}{ee5``6Cn5uTp1nzMiZF+B)!qm zVn;x98@RacXzCttxdRM7)f7{4r65hASOnhB=$rDuz%9_X>h;#%$LRx*rLNL9z`-Ff zTB7VVpLh3m_xA6Zc7H!_CX>@2HM@dakSAh-}0000k zNkla3|NlP&DS(NAL@=s>k&%&+kr5XcDoAQPuwV+oO~q;!<7iA10RRow3?oa& R59getSkeleton(); + if (auto bone = skeleton.config->find(lua::require_string(L, 2))) { + return lua::tointeger(L, bone->getIndex()); + } + } + return 0; +} + const luaL_Reg skeletonlib [] = { {"get_model", lua::wrap}, {"get_matrix", lua::wrap}, {"set_matrix", lua::wrap}, {"get_texture", lua::wrap}, {"set_texture", lua::wrap}, + {"index", lua::wrap}, {NULL, NULL} }; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index ac01964e..80f99dd4 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -49,7 +49,7 @@ void Player::updateEntity() { } else if (auto entity = level->entities->get(eid)) { position = entity->getTransform().pos; } else { - // check if chunk loaded + // TODO: check if chunk loaded } } @@ -61,11 +61,16 @@ Hitbox* Player::getHitbox() { } void Player::updateInput(PlayerInput& input, float delta) { - auto hitbox = getHitbox(); - if (hitbox == nullptr) { + auto entity = level->entities->get(eid); + if (!entity.has_value()) { return; } - bool crouch = input.shift && hitbox->grounded && !input.sprint; + auto& hitbox = entity->getRigidbody().hitbox; + auto& transform = entity->getTransform(); + transform.setRot( + glm::rotate(glm::mat4(1.0f), glm::radians(cam.x), glm::vec3(0, 1, 0))); + + bool crouch = input.shift && hitbox.grounded && !input.sprint; float speed = this->speed; if (flight){ speed *= FLIGHT_SPEED_MUL; @@ -74,7 +79,7 @@ void Player::updateInput(PlayerInput& input, float delta) { speed *= CHEAT_SPEED_MUL; } - hitbox->crouching = crouch; + hitbox.crouching = crouch; if (crouch) { speed *= CROUCH_SPEED_MUL; } else if (input.sprint) { @@ -96,37 +101,37 @@ void Player::updateInput(PlayerInput& input, float delta) { } if (glm::length(dir) > 0.0f){ dir = glm::normalize(dir); - hitbox->velocity += dir * speed * delta * 9.0f; + hitbox.velocity += dir * speed * delta * 9.0f; } - hitbox->linearDamping = PLAYER_GROUND_DAMPING; - hitbox->verticalDamping = flight; - hitbox->gravityScale = flight ? 0.0f : 1.0f; + hitbox.linearDamping = PLAYER_GROUND_DAMPING; + hitbox.verticalDamping = flight; + hitbox.gravityScale = flight ? 0.0f : 1.0f; if (flight){ - hitbox->linearDamping = PLAYER_AIR_DAMPING; + hitbox.linearDamping = PLAYER_AIR_DAMPING; if (input.jump){ - hitbox->velocity.y += speed * delta * 9; + hitbox.velocity.y += speed * delta * 9; } if (input.shift){ - hitbox->velocity.y -= speed * delta * 9; + hitbox.velocity.y -= speed * delta * 9; } } - if (!hitbox->grounded) { - hitbox->linearDamping = PLAYER_AIR_DAMPING; + if (!hitbox.grounded) { + hitbox.linearDamping = PLAYER_AIR_DAMPING; } - if (input.jump && hitbox->grounded){ - hitbox->velocity.y = JUMP_FORCE; + if (input.jump && hitbox.grounded){ + hitbox.velocity.y = JUMP_FORCE; } if ((input.flight && !noclip) || (input.noclip && flight == noclip)){ flight = !flight; if (flight){ - hitbox->velocity.y += 1.0f; + hitbox.velocity.y += 1.0f; } } - hitbox->type = noclip ? BodyType::KINEMATIC : BodyType::DYNAMIC; + hitbox.type = noclip ? BodyType::KINEMATIC : BodyType::DYNAMIC; if (input.noclip) { noclip = !noclip; } diff --git a/src/objects/rigging.cpp b/src/objects/rigging.cpp index 909120c0..2eed0fcf 100644 --- a/src/objects/rigging.cpp +++ b/src/objects/rigging.cpp @@ -81,6 +81,16 @@ void SkeletonConfig::render( } } +Bone* SkeletonConfig::find(std::string_view str) const { + for (size_t i = 0; i < nodes.size(); i++) { + auto* node = nodes[i]; + if (node->getName() == str) { + return node; + } + } + return nullptr; +} + static std::tuple> read_node( dynamic::Map* root, size_t index ) { @@ -88,19 +98,19 @@ static std::tuple> read_node( std::string model; root->str("name", name); root->str("model", model); - + std::vector> bones; size_t count = 1; if (auto nodesList = root->list("nodes")) { for (size_t i = 0; i < nodesList->size(); i++) { if (const auto& map = nodesList->map(i)) { auto [subcount, subNode] = read_node(map.get(), index+count); - subcount += count; + count += subcount; bones.push_back(std::move(subNode)); } } } - return {index + count, std::make_unique(index, name, model, std::move(bones))}; + return {count, std::make_unique(index, name, model, std::move(bones))}; } std::unique_ptr SkeletonConfig::parse( diff --git a/src/objects/rigging.hpp b/src/objects/rigging.hpp index aa084d8f..f0d833e0 100644 --- a/src/objects/rigging.hpp +++ b/src/objects/rigging.hpp @@ -46,6 +46,10 @@ namespace rigging { void refreshModel(const Assets* assets); + const std::string& getName() const { + return name; + } + const std::string& getModelName() const { return modelName; } @@ -105,6 +109,8 @@ namespace rigging { }; } + Bone* find(std::string_view str) const; + static std::unique_ptr parse( std::string_view src, std::string_view file,