add on_chunk_present and on_chunk_remove events
This commit is contained in:
@@ -109,6 +109,8 @@ struct WorldFuncsSet {
|
|||||||
bool onblockbroken;
|
bool onblockbroken;
|
||||||
bool onblockinteract;
|
bool onblockinteract;
|
||||||
bool onplayertick;
|
bool onplayertick;
|
||||||
|
bool onchunkpresent;
|
||||||
|
bool onchunkremove;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ContentPackRuntime {
|
class ContentPackRuntime {
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ WorldRenderer::WorldRenderer(
|
|||||||
) {
|
) {
|
||||||
auto& settings = engine.getSettings();
|
auto& settings = engine.getSettings();
|
||||||
level.events->listen(
|
level.events->listen(
|
||||||
EVT_CHUNK_HIDDEN,
|
LevelEventType::CHUNK_HIDDEN,
|
||||||
[this](LevelEventType, Chunk* chunk) { chunks->unload(chunk); }
|
[this](LevelEventType, Chunk* chunk) { chunks->unload(chunk); }
|
||||||
);
|
);
|
||||||
auto assets = engine.getAssets();
|
auto assets = engine.getAssets();
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include "scripting/scripting.hpp"
|
#include "scripting/scripting.hpp"
|
||||||
#include "lighting/Lighting.hpp"
|
#include "lighting/Lighting.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
|
#include "world/LevelEvents.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "world/World.hpp"
|
#include "world/World.hpp"
|
||||||
|
|
||||||
@@ -26,6 +27,14 @@ LevelController::LevelController(
|
|||||||
level(std::move(levelPtr)),
|
level(std::move(levelPtr)),
|
||||||
chunks(std::make_unique<ChunksController>(*level)),
|
chunks(std::make_unique<ChunksController>(*level)),
|
||||||
playerTickClock(20, 3) {
|
playerTickClock(20, 3) {
|
||||||
|
|
||||||
|
level->events->listen(LevelEventType::CHUNK_PRESENT, [](auto, Chunk* chunk) {
|
||||||
|
scripting::on_chunk_present(*chunk, chunk->flags.loaded);
|
||||||
|
});
|
||||||
|
level->events->listen(LevelEventType::CHUNK_UNLOAD, [](auto, Chunk* chunk) {
|
||||||
|
scripting::on_chunk_remove(*chunk);
|
||||||
|
});
|
||||||
|
|
||||||
if (clientPlayer) {
|
if (clientPlayer) {
|
||||||
chunks->lighting = std::make_unique<Lighting>(
|
chunks->lighting = std::make_unique<Lighting>(
|
||||||
level->content, *clientPlayer->chunks
|
level->content, *clientPlayer->chunks
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "util/timeutil.hpp"
|
#include "util/timeutil.hpp"
|
||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
|
#include "voxels/Chunk.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "interfaces/Process.hpp"
|
#include "interfaces/Process.hpp"
|
||||||
|
|
||||||
@@ -411,6 +412,35 @@ bool scripting::on_block_interact(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void scripting::on_chunk_present(const Chunk& chunk, bool loaded) {
|
||||||
|
auto args = [&chunk, loaded](lua::State* L) {
|
||||||
|
lua::pushvec_stack<2>(L, {chunk.x, chunk.z});
|
||||||
|
lua::pushboolean(L, loaded);
|
||||||
|
return 3;
|
||||||
|
};
|
||||||
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
|
if (pack->worldfuncsset.onchunkpresent) {
|
||||||
|
lua::emit_event(
|
||||||
|
lua::get_main_state(), packid + ":.chunkpresent", args
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void scripting::on_chunk_remove(const Chunk& chunk) {
|
||||||
|
auto args = [&chunk](lua::State* L) {
|
||||||
|
lua::pushvec_stack<2>(L, {chunk.x, chunk.z});
|
||||||
|
return 2;
|
||||||
|
};
|
||||||
|
for (auto& [packid, pack] : content->getPacks()) {
|
||||||
|
if (pack->worldfuncsset.onchunkremove) {
|
||||||
|
lua::emit_event(
|
||||||
|
lua::get_main_state(), packid + ":.chunkremove", args
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_player_tick(Player* player, int tps) {
|
void scripting::on_player_tick(Player* player, int tps) {
|
||||||
auto args = [=](lua::State* L) {
|
auto args = [=](lua::State* L) {
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
@@ -837,6 +867,10 @@ void scripting::load_world_script(
|
|||||||
register_event(env, "on_block_interact", prefix + ":.blockinteract");
|
register_event(env, "on_block_interact", prefix + ":.blockinteract");
|
||||||
funcsset.onplayertick =
|
funcsset.onplayertick =
|
||||||
register_event(env, "on_player_tick", prefix + ":.playertick");
|
register_event(env, "on_player_tick", prefix + ":.playertick");
|
||||||
|
funcsset.onchunkpresent =
|
||||||
|
register_event(env, "on_chunk_present", prefix + ":.chunkpresent");
|
||||||
|
funcsset.onchunkremove =
|
||||||
|
register_event(env, "on_chunk_remove", prefix + ":.chunkremove");
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_layout_script(
|
void scripting::load_layout_script(
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ struct ContentPack;
|
|||||||
class ContentIndices;
|
class ContentIndices;
|
||||||
class Level;
|
class Level;
|
||||||
class Block;
|
class Block;
|
||||||
|
class Chunk;
|
||||||
class Player;
|
class Player;
|
||||||
struct ItemDef;
|
struct ItemDef;
|
||||||
class Inventory;
|
class Inventory;
|
||||||
@@ -84,6 +85,10 @@ namespace scripting {
|
|||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
);
|
);
|
||||||
bool on_block_interact(Player* player, const Block& block, const glm::ivec3& pos);
|
bool on_block_interact(Player* player, const Block& block, const glm::ivec3& pos);
|
||||||
|
|
||||||
|
void on_chunk_present(const Chunk& chunk, bool loaded);
|
||||||
|
void on_chunk_remove(const Chunk& chunk);
|
||||||
|
|
||||||
void on_player_tick(Player* player, int tps);
|
void on_player_tick(Player* player, int tps);
|
||||||
|
|
||||||
/// @brief Called on RMB click with the item selected
|
/// @brief Called on RMB click with the item selected
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ Chunks::Chunks(
|
|||||||
areaMap(w, d) {
|
areaMap(w, d) {
|
||||||
areaMap.setCenter(ox-w/2, oz-d/2);
|
areaMap.setCenter(ox-w/2, oz-d/2);
|
||||||
areaMap.setOutCallback([this](int, int, const auto& chunk) {
|
areaMap.setOutCallback([this](int, int, const auto& chunk) {
|
||||||
this->events->trigger(EVT_CHUNK_HIDDEN, chunk.get());
|
this->events->trigger(LevelEventType::CHUNK_HIDDEN, chunk.get());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,7 +323,7 @@ void Chunks::resize(uint32_t newW, uint32_t newD) {
|
|||||||
bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
|
bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
|
||||||
if (areaMap.set(chunk->x, chunk->z, chunk)) {
|
if (areaMap.set(chunk->x, chunk->z, chunk)) {
|
||||||
if (events) {
|
if (events) {
|
||||||
events->trigger(LevelEventType::EVT_CHUNK_SHOWN, chunk.get());
|
events->trigger(LevelEventType::CHUNK_SHOWN, chunk.get());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include "objects/Entities.hpp"
|
#include "objects/Entities.hpp"
|
||||||
#include "voxels/blocks_agent.hpp"
|
#include "voxels/blocks_agent.hpp"
|
||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
|
#include "world/LevelEvents.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "world/World.hpp"
|
#include "world/World.hpp"
|
||||||
#include "Block.hpp"
|
#include "Block.hpp"
|
||||||
@@ -125,6 +126,8 @@ std::shared_ptr<Chunk> GlobalChunks::create(int x, int z) {
|
|||||||
chunk->flags.loadedLights = true;
|
chunk->flags.loadedLights = true;
|
||||||
}
|
}
|
||||||
chunk->blocksMetadata = regions.getBlocksData(chunk->x, chunk->z);
|
chunk->blocksMetadata = regions.getBlocksData(chunk->x, chunk->z);
|
||||||
|
|
||||||
|
level.events->trigger(LevelEventType::CHUNK_PRESENT, chunk.get());
|
||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -52,13 +52,14 @@ Level::Level(
|
|||||||
entities->setNextID(worldInfo.nextEntityId);
|
entities->setNextID(worldInfo.nextEntityId);
|
||||||
}
|
}
|
||||||
|
|
||||||
events->listen(LevelEventType::EVT_CHUNK_SHOWN, [this](LevelEventType, Chunk* chunk) {
|
events->listen(LevelEventType::CHUNK_SHOWN, [this](LevelEventType, Chunk* chunk) {
|
||||||
chunks->incref(chunk);
|
chunks->incref(chunk);
|
||||||
});
|
});
|
||||||
events->listen(LevelEventType::EVT_CHUNK_HIDDEN, [this](LevelEventType, Chunk* chunk) {
|
events->listen(LevelEventType::CHUNK_HIDDEN, [this](LevelEventType, Chunk* chunk) {
|
||||||
chunks->decref(chunk);
|
chunks->decref(chunk);
|
||||||
});
|
});
|
||||||
chunks->setOnUnload([this](const Chunk& chunk) {
|
chunks->setOnUnload([this](Chunk& chunk) {
|
||||||
|
events->trigger(LevelEventType::CHUNK_UNLOAD, &chunk);
|
||||||
AABB aabb = chunk.getAABB();
|
AABB aabb = chunk.getAABB();
|
||||||
entities->despawn(entities->getAllInside(aabb));
|
entities->despawn(entities->getAllInside(aabb));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,9 +6,11 @@
|
|||||||
|
|
||||||
class Chunk;
|
class Chunk;
|
||||||
|
|
||||||
enum LevelEventType {
|
enum class LevelEventType {
|
||||||
EVT_CHUNK_SHOWN,
|
CHUNK_SHOWN,
|
||||||
EVT_CHUNK_HIDDEN,
|
CHUNK_HIDDEN,
|
||||||
|
CHUNK_PRESENT,
|
||||||
|
CHUNK_UNLOAD,
|
||||||
};
|
};
|
||||||
|
|
||||||
using ChunkEventFunc = std::function<void(LevelEventType, Chunk*)>;
|
using ChunkEventFunc = std::function<void(LevelEventType, Chunk*)>;
|
||||||
|
|||||||
Reference in New Issue
Block a user