on_blocks_tick event
This commit is contained in:
parent
0c2b58c8ac
commit
5188ee7bd8
@ -40,11 +40,16 @@ int Clock::getPart() const {
|
||||
return tickParts-tickPartsUndone-1;
|
||||
}
|
||||
|
||||
int Clock::getTickRate() const {
|
||||
return tickRate;
|
||||
}
|
||||
|
||||
BlocksController::BlocksController(Level* level, uint padding)
|
||||
: level(level),
|
||||
chunks(level->chunks),
|
||||
lighting(level->lighting),
|
||||
randTickClock(20, 3),
|
||||
blocksTickClock(20, 1),
|
||||
padding(padding) {
|
||||
}
|
||||
|
||||
@ -84,10 +89,26 @@ void BlocksController::update(float delta) {
|
||||
if (randTickClock.update(delta)) {
|
||||
randomTick(randTickClock.getPart(), randTickClock.getParts());
|
||||
}
|
||||
if (blocksTickClock.update(delta)) {
|
||||
onBlocksTick(blocksTickClock.getPart(), blocksTickClock.getParts());
|
||||
}
|
||||
}
|
||||
|
||||
void BlocksController::onBlocksTick(int tickid, int parts) {
|
||||
auto content = level->content;
|
||||
auto indices = content->getIndices();
|
||||
int tickRate = blocksTickClock.getTickRate();
|
||||
for (size_t id = 0; id < indices->countBlockDefs(); id++) {
|
||||
if ((id + tickid) % parts != 0)
|
||||
continue;
|
||||
auto def = indices->getBlockDef(id);
|
||||
if (def->rt.funcsset.onblockstick) {
|
||||
scripting::on_blocks_tick(def, tickRate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BlocksController::randomTick(int tickid, int parts) {
|
||||
// timeutil::ScopeLogTimer timer(5000+tickid);
|
||||
const int w = chunks->w;
|
||||
const int d = chunks->d;
|
||||
int segments = 4;
|
||||
@ -99,7 +120,7 @@ void BlocksController::randomTick(int tickid, int parts) {
|
||||
int index = z * w + x;
|
||||
if ((index + tickid) % parts != 0)
|
||||
continue;
|
||||
std::shared_ptr<Chunk> chunk = chunks->chunks[index];
|
||||
auto chunk = chunks->chunks[index];
|
||||
if (chunk == nullptr || !chunk->isLighted())
|
||||
continue;
|
||||
for (int s = 0; s < segments; s++) {
|
||||
|
||||
@ -23,6 +23,7 @@ public:
|
||||
|
||||
int getParts() const;
|
||||
int getPart() const;
|
||||
int getTickRate() const;
|
||||
};
|
||||
|
||||
class BlocksController {
|
||||
@ -30,6 +31,7 @@ class BlocksController {
|
||||
Chunks* chunks;
|
||||
Lighting* lighting;
|
||||
Clock randTickClock;
|
||||
Clock blocksTickClock;
|
||||
uint padding;
|
||||
public:
|
||||
BlocksController(Level* level, uint padding);
|
||||
@ -41,6 +43,7 @@ public:
|
||||
|
||||
void update(float delta);
|
||||
void randomTick(int tickid, int parts);
|
||||
void onBlocksTick(int tickid, int parts);
|
||||
};
|
||||
|
||||
#endif // LOGIC_BLOCKS_CONTROLLER_H_
|
||||
|
||||
@ -112,6 +112,13 @@ void scripting::on_world_quit() {
|
||||
scripting::content = nullptr;
|
||||
}
|
||||
|
||||
void scripting::on_blocks_tick(const Block* block, int tps) {
|
||||
std::string name = block->name+".blockstick";
|
||||
lua_getglobal(L, name.c_str());
|
||||
lua_pushinteger(L, tps);
|
||||
call_func(L, 1, name);
|
||||
}
|
||||
|
||||
void scripting::update_block(const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name+".update";
|
||||
lua_getglobal(L, name.c_str());
|
||||
@ -188,6 +195,7 @@ void scripting::load_block_script(std::string prefix, fs::path file, block_funcs
|
||||
funcsset->onbroken=rename_global(L, "on_broken", (prefix+".broken").c_str());
|
||||
funcsset->onplaced=rename_global(L, "on_placed", (prefix+".placed").c_str());
|
||||
funcsset->oninteract=rename_global(L, "on_interact", (prefix+".oninteract").c_str());
|
||||
funcsset->onblockstick=rename_global(L, "on_blocks_tick", (prefix+".blockstick").c_str());
|
||||
}
|
||||
|
||||
void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset) {
|
||||
|
||||
@ -22,6 +22,7 @@ namespace scripting {
|
||||
void initialize(Engine* engine);
|
||||
void on_world_load(Level* level, BlocksController* blocks);
|
||||
void on_world_quit();
|
||||
void on_blocks_tick(const Block* block, int tps);
|
||||
void update_block(const Block* block, int x, int y, int z);
|
||||
void random_update_block(const Block* block, int x, int y, int z);
|
||||
void on_block_placed(Player* player, const Block* block, int x, int y, int z);
|
||||
|
||||
@ -27,6 +27,7 @@ struct block_funcs_set {
|
||||
bool onbroken: 1;
|
||||
bool oninteract: 1;
|
||||
bool randupdate: 1;
|
||||
bool onblockstick: 1;
|
||||
};
|
||||
|
||||
struct CoordSystem {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user