add gfx.blockwraps library & add on_player_tick world event
This commit is contained in:
parent
2ba90625ce
commit
61b1aa8e3d
@ -99,6 +99,7 @@ struct world_funcs_set {
|
||||
bool onblockplaced : 1;
|
||||
bool onblockbroken : 1;
|
||||
bool onblockinteract : 1;
|
||||
bool onplayertick : 1;
|
||||
};
|
||||
|
||||
class ContentPackRuntime {
|
||||
|
||||
@ -79,8 +79,9 @@ void BlockWrapsRenderer::draw(const BlockWrapper& wrapper) {
|
||||
|
||||
void BlockWrapsRenderer::draw(const DrawContext& pctx, const Player& player) {
|
||||
auto ctx = pctx.sub();
|
||||
|
||||
|
||||
for (const auto& [_, wrapper] : wrappers) {
|
||||
draw(*wrapper);
|
||||
}
|
||||
batch->flush();
|
||||
}
|
||||
|
||||
@ -101,3 +102,7 @@ BlockWrapper* BlockWrapsRenderer::get(u64id_t id) const {
|
||||
}
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void BlockWrapsRenderer::remove(u64id_t id) {
|
||||
wrappers.erase(id);
|
||||
}
|
||||
|
||||
@ -35,4 +35,6 @@ public:
|
||||
u64id_t add(const glm::ivec3& position, const std::string& texture);
|
||||
|
||||
BlockWrapper* get(u64id_t id) const;
|
||||
|
||||
void remove(u64id_t id);
|
||||
};
|
||||
|
||||
@ -40,7 +40,6 @@ class WorldRenderer {
|
||||
std::unique_ptr<LineBatch> lineBatch;
|
||||
std::unique_ptr<Batch3D> batch3d;
|
||||
std::unique_ptr<ChunksRenderer> chunks;
|
||||
std::unique_ptr<BlockWrapsRenderer> blockWraps;
|
||||
std::unique_ptr<GuidesRenderer> guides;
|
||||
std::unique_ptr<Skybox> skybox;
|
||||
std::unique_ptr<ModelBatch> modelBatch;
|
||||
@ -70,6 +69,7 @@ class WorldRenderer {
|
||||
public:
|
||||
std::unique_ptr<TextsRenderer> texts;
|
||||
std::unique_ptr<ParticlesRenderer> particles;
|
||||
std::unique_ptr<BlockWrapsRenderer> blockWraps;
|
||||
|
||||
static bool showChunkBorders;
|
||||
static bool showEntitiesDebug;
|
||||
|
||||
@ -195,7 +195,8 @@ PlayerController::PlayerController(
|
||||
: settings(settings), level(level),
|
||||
player(level->getObject<Player>(0)),
|
||||
camControl(player, settings.camera),
|
||||
blocksController(blocksController) {
|
||||
blocksController(blocksController),
|
||||
playerTickClock(20, 3) {
|
||||
}
|
||||
|
||||
void PlayerController::onFootstep(const Hitbox& hitbox) {
|
||||
@ -249,6 +250,13 @@ void PlayerController::update(float delta, bool input, bool pause) {
|
||||
resetKeyboard();
|
||||
}
|
||||
updatePlayer(delta);
|
||||
|
||||
if (playerTickClock.update(delta)) {
|
||||
if (player->getId() % playerTickClock.getParts() ==
|
||||
playerTickClock.getPart()) {
|
||||
scripting::on_player_tick(player.get(), playerTickClock.getTickRate());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -302,7 +310,7 @@ void PlayerController::updatePlayer(float delta) {
|
||||
}
|
||||
|
||||
static int determine_rotation(
|
||||
const Block* def, const glm::ivec3& norm, glm::vec3& camDir
|
||||
const Block* def, const glm::ivec3& norm, const glm::vec3& camDir
|
||||
) {
|
||||
if (def && def->rotatable) {
|
||||
const std::string& name = def->rotations.name;
|
||||
@ -532,10 +540,12 @@ void PlayerController::updateInteraction(float delta) {
|
||||
}
|
||||
}
|
||||
auto& target = indices->blocks.require(vox->id);
|
||||
if (lclick && target.breakable) {
|
||||
blocksController->breakBlock(
|
||||
player.get(), target, iend.x, iend.y, iend.z
|
||||
);
|
||||
if (lclick) {
|
||||
if (player->isInstantDestruction() && target.breakable) {
|
||||
blocksController->breakBlock(
|
||||
player.get(), target, iend.x, iend.y, iend.z
|
||||
);
|
||||
}
|
||||
}
|
||||
if (rclick && !input.shift) {
|
||||
bool preventDefault = false;
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "objects/Player.hpp"
|
||||
#include "util/Clock.hpp"
|
||||
|
||||
class Engine;
|
||||
class Camera;
|
||||
@ -53,6 +54,7 @@ class PlayerController {
|
||||
PlayerInput input {};
|
||||
CameraControl camControl;
|
||||
BlocksController* blocksController;
|
||||
util::Clock playerTickClock;
|
||||
|
||||
float interactionTimer = 0.0f;
|
||||
void updateKeyboard();
|
||||
|
||||
@ -18,6 +18,7 @@ extern const luaL_Reg audiolib[];
|
||||
extern const luaL_Reg base64lib[];
|
||||
extern const luaL_Reg bjsonlib[];
|
||||
extern const luaL_Reg blocklib[];
|
||||
extern const luaL_Reg blockwrapslib[]; // gfx.blockwraps
|
||||
extern const luaL_Reg cameralib[];
|
||||
extern const luaL_Reg consolelib[];
|
||||
extern const luaL_Reg corelib[];
|
||||
@ -32,10 +33,10 @@ extern const luaL_Reg itemlib[];
|
||||
extern const luaL_Reg jsonlib[];
|
||||
extern const luaL_Reg mat4lib[];
|
||||
extern const luaL_Reg packlib[];
|
||||
extern const luaL_Reg particleslib[];
|
||||
extern const luaL_Reg particleslib[]; // gfx.particles
|
||||
extern const luaL_Reg playerlib[];
|
||||
extern const luaL_Reg quatlib[];
|
||||
extern const luaL_Reg text3dlib[];
|
||||
extern const luaL_Reg text3dlib[]; // gfx.text3d
|
||||
extern const luaL_Reg timelib[];
|
||||
extern const luaL_Reg tomllib[];
|
||||
extern const luaL_Reg utf8lib[];
|
||||
|
||||
43
src/logic/scripting/lua/libs/libblockwraps.cpp
Normal file
43
src/logic/scripting/lua/libs/libblockwraps.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "api_lua.hpp"
|
||||
|
||||
#include "logic/scripting/scripting_hud.hpp"
|
||||
#include "graphics/render/WorldRenderer.hpp"
|
||||
#include "graphics/render/BlockWrapsRenderer.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_wrap(lua::State* L) {
|
||||
auto position = lua::tovec3(L, 1);
|
||||
std::string texture = lua::require_string(L, 2);
|
||||
|
||||
return lua::pushinteger(
|
||||
L, renderer->blockWraps->add(position, std::move(texture))
|
||||
);
|
||||
}
|
||||
|
||||
static int l_unwrap(lua::State* L) {
|
||||
renderer->blockWraps->remove(lua::tointeger(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_pos(lua::State* L) {
|
||||
if (auto wrapper = renderer->blockWraps->get(lua::tointeger(L, 1))) {
|
||||
wrapper->position = lua::tovec3(L, 2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_texture(lua::State* L) {
|
||||
if (auto wrapper = renderer->blockWraps->get(lua::tointeger(L, 1))) {
|
||||
wrapper->texture = lua::require_string(L, 2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg blockwrapslib[] = {
|
||||
{"wrap", lua::wrap<l_wrap>},
|
||||
{"unwrap", lua::wrap<l_unwrap>},
|
||||
{"set_pos", lua::wrap<l_set_pos>},
|
||||
{"set_texture", lua::wrap<l_set_texture>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
@ -323,6 +323,21 @@ bool scripting::on_block_interact(
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_player_tick(Player* player, int tps) {
|
||||
auto args = [=](lua::State* L) {
|
||||
lua::pushinteger(L, player ? player->getId() : -1);
|
||||
lua::pushinteger(L, tps);
|
||||
return 2;
|
||||
};
|
||||
for (auto& [packid, pack] : content->getPacks()) {
|
||||
if (pack->worldfuncsset.onplayertick) {
|
||||
lua::emit_event(
|
||||
lua::get_main_state(), packid + ":.playertick", args
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool scripting::on_item_use(Player* player, const ItemDef& item) {
|
||||
std::string name = item.name + ".use";
|
||||
return lua::emit_event(
|
||||
@ -724,6 +739,8 @@ void scripting::load_world_script(
|
||||
register_event(env, "on_block_broken", prefix + ":.blockbroken");
|
||||
funcsset.onblockinteract =
|
||||
register_event(env, "on_block_interact", prefix + ":.blockinteract");
|
||||
funcsset.onplayertick =
|
||||
register_event(env, "on_player_tick", prefix + ":.playertick");
|
||||
}
|
||||
|
||||
void scripting::load_layout_script(
|
||||
|
||||
@ -73,6 +73,7 @@ namespace scripting {
|
||||
Player* player, const Block& block, const glm::ivec3& pos
|
||||
);
|
||||
bool on_block_interact(Player* player, const Block& block, const glm::ivec3& pos);
|
||||
void on_player_tick(Player* player, int tps);
|
||||
|
||||
/// @brief Called on RMB click with the item selected
|
||||
/// @return true if prevents default action
|
||||
|
||||
@ -32,6 +32,7 @@ void scripting::on_frontend_init(Hud* hud, WorldRenderer* renderer) {
|
||||
auto L = lua::get_main_state();
|
||||
|
||||
lua::openlib(L, "hud", hudlib);
|
||||
lua::openlib(L, "gfx", "blockwraps", blockwrapslib);
|
||||
lua::openlib(L, "gfx", "particles", particleslib);
|
||||
lua::openlib(L, "gfx", "text3d", text3dlib);
|
||||
|
||||
|
||||
@ -438,7 +438,7 @@ voxel* Chunks::rayCast(
|
||||
glm::ivec3& norm,
|
||||
glm::ivec3& iend,
|
||||
std::set<blockid_t> filter
|
||||
) {
|
||||
) const {
|
||||
float px = start.x;
|
||||
float py = start.y;
|
||||
float pz = start.z;
|
||||
@ -571,7 +571,7 @@ voxel* Chunks::rayCast(
|
||||
|
||||
glm::vec3 Chunks::rayCastToObstacle(
|
||||
const glm::vec3& start, const glm::vec3& dir, float maxDist
|
||||
) {
|
||||
) const {
|
||||
const float px = start.x;
|
||||
const float py = start.y;
|
||||
const float pz = start.z;
|
||||
|
||||
@ -102,11 +102,11 @@ public:
|
||||
glm::ivec3& norm,
|
||||
glm::ivec3& iend,
|
||||
std::set<blockid_t> filter = {}
|
||||
);
|
||||
) const;
|
||||
|
||||
glm::vec3 rayCastToObstacle(
|
||||
const glm::vec3& start, const glm::vec3& dir, float maxDist
|
||||
);
|
||||
) const;
|
||||
|
||||
const AABB* isObstacleAt(float x, float y, float z) const;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user