From f7d0ec438fa0493ba21c9fa3dcda0a800e12a0dc Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 9 Jul 2024 18:41:03 +0300 Subject: [PATCH] add falling_block entity --- res/content/base/content.json | 11 +++-- res/content/base/entities/drop.json | 4 +- res/content/base/entities/falling_block.json | 7 +++ res/content/base/models/block.obj | 48 +++++++++++++++++++ res/content/base/preload.json | 1 + res/content/base/rigs/block.json | 5 ++ .../base/scripts/components/falling_block.lua | 31 ++++++++++++ res/content/base/scripts/sand.lua | 14 ++++++ src/content/ContentLoader.cpp | 2 + 9 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 res/content/base/entities/falling_block.json create mode 100644 res/content/base/models/block.obj create mode 100644 res/content/base/rigs/block.json create mode 100644 res/content/base/scripts/components/falling_block.lua create mode 100644 res/content/base/scripts/sand.lua diff --git a/res/content/base/content.json b/res/content/base/content.json index 9d1f016b..9b696dfd 100644 --- a/res/content/base/content.json +++ b/res/content/base/content.json @@ -1,6 +1,8 @@ { - "items": [ - "bazalt_breaker" + "entities": [ + "drop", + "player", + "falling_block" ], "blocks": [ "dirt", @@ -28,8 +30,7 @@ "torch", "wooden_door" ], - "entities": [ - "drop", - "player" + "items": [ + "bazalt_breaker" ] } \ No newline at end of file diff --git a/res/content/base/entities/drop.json b/res/content/base/entities/drop.json index 348bffd2..47050d78 100644 --- a/res/content/base/entities/drop.json +++ b/res/content/base/entities/drop.json @@ -6,7 +6,5 @@ "sensors": [ ["aabb", -0.2, -0.2, -0.2, 0.2, 0.2, 0.2], ["radius", 1.6] - ], - "save-rig-textures": true, - "save-rig-pose": true + ] } diff --git a/res/content/base/entities/falling_block.json b/res/content/base/entities/falling_block.json new file mode 100644 index 00000000..9cabe1d7 --- /dev/null +++ b/res/content/base/entities/falling_block.json @@ -0,0 +1,7 @@ +{ + "components": [ + "falling_block" + ], + "rig-name": "base:block", + "hitbox": [0.4, 0.4, 0.4] +} diff --git a/res/content/base/models/block.obj b/res/content/base/models/block.obj new file mode 100644 index 00000000..2e307e52 --- /dev/null +++ b/res/content/base/models/block.obj @@ -0,0 +1,48 @@ +o Cube +v 0.5 -0.5 -0.5 +v 0.5 -0.5 0.5 +v -0.5 -0.5 0.5 +v -0.5 -0.5 -0.5 +v 0.5 0.5 -0.5 +v 0.5 0.5 0.5 +v -0.5 0.5 0.5 +v -0.5 0.5 -0.5 +vt 0.0 0.0 +vt 1.0 0.0 +vt 1.0 1.0 +vt 0.0 1.0 +vt 0.0 0.0 +vt 1.0 0.0 +vt 1.0 1.0 +vt 0.0 1.0 +vt 1.0 0.0 +vt 1.0 1.0 +vt 0.0 0.0 +vt 1.0 0.0 +vt 0.0 1.0 +vt 0.0 0.0 +vt 0.0 1.0 +vt 1.0 0.0 +vt 1.0 1.0 +vt 1.0 1.0 +vt 0.0 1.0 +vt 0.0 0.0 +vn 0.0 -1.0 0.0 +vn 0.0 1.0 0.0 +vn 1.0 -0.0 0.0 +vn -1.0 -0.0 -0.0 +vn 0.0 0.0 -1.0 +vn -0.0 -0.0 1.0 +usemtl $2 +s off +f 1/1/1 2/2/1 3/3/1 4/4/1 +usemtl $3 +f 5/5/2 8/6/2 7/7/2 6/8/2 +usemtl $0 +f 1/9/3 5/10/3 6/8/3 2/11/3 +usemtl $1 +f 3/12/4 7/7/4 8/13/4 4/14/4 +usemtl $4 +f 5/15/5 1/1/5 4/16/5 8/17/5 +usemtl $5 +f 2/2/6 6/18/6 7/19/6 3/20/6 diff --git a/res/content/base/preload.json b/res/content/base/preload.json index 2f4e8150..693fe7a9 100644 --- a/res/content/base/preload.json +++ b/res/content/base/preload.json @@ -5,6 +5,7 @@ "events/pickup" ], "models": [ + "block", "drop-block", "drop-item" ] diff --git a/res/content/base/rigs/block.json b/res/content/base/rigs/block.json new file mode 100644 index 00000000..77444f6e --- /dev/null +++ b/res/content/base/rigs/block.json @@ -0,0 +1,5 @@ +{ + "root": { + "model": "block" + } +} diff --git a/res/content/base/scripts/components/falling_block.lua b/res/content/base/scripts/components/falling_block.lua new file mode 100644 index 00000000..959ceaec --- /dev/null +++ b/res/content/base/scripts/components/falling_block.lua @@ -0,0 +1,31 @@ +local tsf = entity.transform +local body = entity.rigidbody +local rig = entity.modeltree + +ARGS = ARGS or {} +local blockid = ARGS.block +if SAVED_DATA.block then + blockid = SAVED_DATA.block +else + SAVED_DATA.block = blockid +end + +do -- setup visuals + local textures = block.get_textures(block.index(blockid)) + for i,t in ipairs(textures) do + rig:set_texture("$"..tostring(i-1), "blocks:"..textures[i]) + end +end + +function on_grounded() + local pos = tsf:get_pos() + local ix = math.floor(pos[1]) + local iy = math.floor(pos[2]) + local iz = math.floor(pos[3]) + if block.is_replaceable_at(ix, iy, iz) then + block.set(ix, iy, iz, block.index(blockid)) + else + entities.spawn("base:drop", pos, {item={id=item.index(blockid..".item"), count=1}}) + end + entity:despawn() +end diff --git a/res/content/base/scripts/sand.lua b/res/content/base/scripts/sand.lua new file mode 100644 index 00000000..e699c61a --- /dev/null +++ b/res/content/base/scripts/sand.lua @@ -0,0 +1,14 @@ +local function update(x, y, z) + if block.is_replaceable_at(x, y-1, z) then + entities.spawn("base:falling_block", {x+0.5, y+0.5, z+0.5}, {block='base:sand'}) + block.set(x, y, z, 0) + end +end + +function on_update(x, y, z) + update(x, y, z) +end + +function on_placed(x, y, z) + update(x, y, z) +end diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 1bcebf4d..efc5b361 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -344,6 +344,8 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs if (auto bodyType = BodyType_from(bodyTypeName)) { def.bodyType = *bodyType; } + + root->str("rig-name", def.rigName); } void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) {