add: lua func

This commit is contained in:
Cogi Asd 2024-07-07 23:53:27 +03:00
parent 2f2d6a69e8
commit 7a1c035f01
7 changed files with 114 additions and 4 deletions

View File

@ -11,6 +11,7 @@
#include "../../../window/Events.hpp"
#include "../../../window/Window.hpp"
#include "../../../world/WorldGenerators.hpp"
#include "../../../constants.hpp"
#include <vector>
#include <memory>
@ -158,6 +159,35 @@ static int l_get_generators(lua::State* L) {
return 1;
}
static int l_get_constants(lua::State* L) {
lua::createtable(L, 0, 20);
const std::pair<const char*, int> numberConstants[] = {
{"ENGINE_VERSION_MAJOR", ENGINE_VERSION_MAJOR},
{"ENGINE_VERSION_MINOR", ENGINE_VERSION_MINOR},
{"BLOCK_AIR", BLOCK_AIR},
{"ITEM_EMPTY", ITEM_EMPTY},
{"CHUNK_W", CHUNK_W},
{"CHUNK_H", CHUNK_H},
{"CHUNK_D", CHUNK_D},
{"VOXEL_USER_BITS", VOXEL_USER_BITS},
{"VOXEL_USER_BITS_OFFSET", VOXEL_USER_BITS_OFFSET},
{"ITEM_ICON_SIZE", ITEM_ICON_SIZE},
{"CHUNK_VOL", CHUNK_VOL},
{"BLOCK_VOID", BLOCK_VOID},
{"ITEM_VOID", ITEM_VOID},
{"MAX_BLOCKS", MAX_BLOCKS}
};
for (const auto& constant : numberConstants) {
lua::pushnumber(L, constant.second);
lua::setfield(L, constant.first);
}
return 1;
}
const luaL_Reg corelib [] = {
{"new_world", lua::wrap<l_new_world>},
{"open_world", lua::wrap<l_open_world>},
@ -172,5 +202,6 @@ const luaL_Reg corelib [] = {
{"quit", lua::wrap<l_quit>},
{"get_default_generator", lua::wrap<l_get_default_generator>},
{"get_generators", lua::wrap<l_get_generators>},
{"get_constants", lua::wrap<l_get_constants>},
{NULL, NULL}
};

View File

@ -126,6 +126,46 @@ static int l_player_get_selected_block(lua::State* L) {
return 0;
}
static int l_player_get_spawnpoint(lua::State* L) {
if (auto player = get_player(L, 1)) {
return lua::pushvec3(L, player->getSpawnPoint());
}
return 0;
}
static int l_player_set_spawnpoint(lua::State* L) {
auto player = get_player(L, 1);
if (player) {
auto x = lua::tonumber(L, 2);
auto y = lua::tonumber(L, 3);
auto z = lua::tonumber(L, 4);
player->setSpawnPoint(glm::vec3(x, y, z));
}
return 0;
}
static int l_player_set_jump_force(lua::State* L) {
if (auto player = get_player(L, 1)) {
player->setJumpForce(std::abs(lua::tonumber(L, 2)));
}
return 0;
}
static int l_player_get_jump_force(lua::State* L) {
if (auto player = get_player(L, 1)) {
return lua::pushnumber(L, player->getJumpForce());
}
return 0;
}
const luaL_Reg playerlib [] = {
{"get_pos", lua::wrap<l_player_get_pos>},
{"set_pos", lua::wrap<l_player_set_pos>},
@ -139,5 +179,9 @@ const luaL_Reg playerlib [] = {
{"is_noclip", lua::wrap<l_player_is_noclip>},
{"set_noclip", lua::wrap<l_player_set_noclip>},
{"get_selected_block", lua::wrap<l_player_get_selected_block>},
{"set_spawnpoint", lua::wrap<l_player_set_spawnpoint>},
{"get_spawnpoint", lua::wrap<l_player_get_spawnpoint>},
{"get_jump_force", lua::wrap<l_player_get_jump_force>},
{"set_jump_force", lua::wrap<l_player_set_jump_force>},
{NULL, NULL}
};

View File

@ -54,6 +54,12 @@ static int l_world_set_day_time(lua::State* L) {
return 0;
}
static int l_wolrd_set_speed_time(lua::State* L) {
auto value = lua::tonumber(L, 1);
level->getWorld()->factorSpeedTime = std::abs(value);
return 0;
}
static int l_world_get_seed(lua::State* L) {
return lua::pushinteger(L, level->getWorld()->getSeed());
}
@ -64,12 +70,25 @@ static int l_world_exists(lua::State* L) {
return lua::pushboolean(L, fs::is_directory(worldsDir));
}
static int l_world_is_day(lua::State* L) {
auto daytime = level->getWorld()->daytime;
return lua::pushboolean(L, daytime >= 0.2 && daytime <= 0.8);
}
static int l_world_is_night(lua::State* L) {
auto daytime = level->getWorld()->daytime;
return lua::pushboolean(L, daytime < 0.2 || daytime > 0.8);
}
const luaL_Reg worldlib [] = {
{"get_list", lua::wrap<l_world_get_list>},
{"get_total_time", lua::wrap<l_world_get_total_time>},
{"get_day_time", lua::wrap<l_world_get_day_time>},
{"set_day_time", lua::wrap<l_world_set_day_time>},
{"set_speed_time", lua::wrap<l_wolrd_set_speed_time>},
{"get_seed", lua::wrap<l_world_get_seed>},
{"is_day", lua::wrap<l_world_is_day>},
{"is_night", lua::wrap<l_world_is_night>},
{"exists", lua::wrap<l_world_exists>},
{NULL, NULL}
};

View File

@ -18,7 +18,7 @@ const float PLAYER_GROUND_DAMPING = 10.0f;
const float PLAYER_AIR_DAMPING = 7.0f;
const float FLIGHT_SPEED_MUL = 4.0f;
const float CHEAT_SPEED_MUL = 5.0f;
const float JUMP_FORCE = 8.0f;
const float JUMP_FACTOR = 8.0f;
Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv) :
speed(speed),
@ -96,7 +96,7 @@ void Player::updateInput(
}
if (input.jump && hitbox->grounded){
hitbox->velocity.y = JUMP_FORCE;
hitbox->velocity.y = JUMP_FACTOR * jumpForce;
}
if ((input.flight && !noclip) ||
@ -106,6 +106,7 @@ void Player::updateInput(
hitbox->grounded = false;
}
}
if (input.noclip) {
noclip = !noclip;
}
@ -184,6 +185,14 @@ void Player::setNoclip(bool flag) {
this->noclip = flag;
}
float Player::getJumpForce() const {
return jumpForce;
}
void Player::setJumpForce(float value) {
jumpForce = value;
}
std::shared_ptr<Inventory> Player::getInventory() const {
return inventory;
}

View File

@ -45,6 +45,7 @@ class Player : public Object, public Serializable {
int chosenSlot;
glm::vec3 spawnpoint {};
std::shared_ptr<Inventory> inventory;
float jumpForce = 1.0f;
bool flight = false;
bool noclip = false;
public:
@ -73,6 +74,9 @@ public:
bool isNoclip() const;
void setNoclip(bool flag);
float getJumpForce() const;
void setJumpForce(float value);
std::shared_ptr<Inventory> getInventory() const;

View File

@ -44,7 +44,7 @@ World::~World(){
}
void World::updateTimers(float delta) {
daytime += delta * daytimeSpeed;
daytime += delta * daytimeSpeed * factorSpeedTime;
daytime = fmod(daytime, 1.0f);
totalTime += delta;
}

View File

@ -42,8 +42,11 @@ public:
/// 0.5 - is noon
float daytime = timeutil::time_value(10, 00, 00);
// factor speed time
float factorSpeedTime = 1.0f;
// looking bad
float daytimeSpeed = 1.0f/60.0f/24.0f;
float daytimeSpeed = 0.000694444444444f; //1.0f/60.0f/24.0f;
/// @brief total time passed in the world (not depending on daytimeSpeed)
double totalTime = 0.0;