add skeleton:index(...) and player test model

This commit is contained in:
MihailRis 2024-07-15 08:56:27 +03:00
parent e8e3626c63
commit 055781eeaf
11 changed files with 148 additions and 23 deletions

View File

@ -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
```

View File

@ -129,4 +129,7 @@ rig:get_texture(key: str) -> str
-- Назначает текстуру по ключу
rig:set_texture(key: str, value: str)
-- Возвращает индекс кости по имени или nil
rig:index(name: str) -> int
```

View File

@ -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

View File

@ -7,6 +7,7 @@
"models": [
"block",
"drop-block",
"drop-item"
"drop-item",
"player"
]
}

View File

@ -1,3 +1,9 @@
{
"root": {}
"root": {
"nodes": [
{
"model": "player"
}
]
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 579 B

View File

@ -43,6 +43,7 @@ local Skeleton = {__index={
set_matrix=function(self, i, m) return __skeleton.set_matrix(self.eid, i, m) end,
get_texture=function(self, s) return __skeleton.get_texture(self.eid, s) end,
set_texture=function(self, s, s2) return __skeleton.set_texture(self.eid, s, s2) end,
index=function(self, s) return __skeleton.index(self.eid, s) end,
}}
local function new_Skeleton(eid)

View File

@ -59,11 +59,22 @@ static int l_set_texture(lua::State* L) {
return 0;
}
static int l_index(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
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<l_get_model>},
{"get_matrix", lua::wrap<l_get_matrix>},
{"set_matrix", lua::wrap<l_set_matrix>},
{"get_texture", lua::wrap<l_get_texture>},
{"set_texture", lua::wrap<l_set_texture>},
{"index", lua::wrap<l_index>},
{NULL, NULL}
};

View File

@ -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;
}

View File

@ -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<size_t, std::unique_ptr<Bone>> read_node(
dynamic::Map* root, size_t index
) {
@ -95,12 +105,12 @@ static std::tuple<size_t, std::unique_ptr<Bone>> read_node(
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<Bone>(index, name, model, std::move(bones))};
return {count, std::make_unique<Bone>(index, name, model, std::move(bones))};
}
std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(

View File

@ -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<SkeletonConfig> parse(
std::string_view src,
std::string_view file,