refactor lua usertypes
This commit is contained in:
parent
48b8c3e9b4
commit
81d21d6e8b
@ -1,7 +1,6 @@
|
||||
#include "coders/binary_json.hpp"
|
||||
#include "api_lua.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
#include "../lua_custom_types.hpp"
|
||||
|
||||
static int l_tobytes(lua::State* L) {
|
||||
auto value = lua::tovalue(L, 1);
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
#include "content/Content.hpp"
|
||||
#include "content/ContentControl.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "../lua_custom_types.hpp"
|
||||
#include "../usertypes/lua_type_voxelfragment.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@
|
||||
#include "items/Inventories.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "../usertypes/lua_type_canvas.hpp"
|
||||
|
||||
using namespace gui;
|
||||
using namespace scripting;
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
#include <vector>
|
||||
#include <cwctype>
|
||||
|
||||
#include "../lua_custom_types.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
static int l_tobytes(lua::State* L) {
|
||||
|
||||
@ -48,4 +48,9 @@ namespace lua {
|
||||
|
||||
void log_error(const std::string& text);
|
||||
|
||||
class Userdata {
|
||||
public:
|
||||
virtual ~Userdata() {};
|
||||
virtual const std::string& getTypeName() const = 0;
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,134 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <random>
|
||||
|
||||
#include "lua_commons.hpp"
|
||||
|
||||
struct fnl_state;
|
||||
class Heightmap;
|
||||
class VoxelFragment;
|
||||
class Texture;
|
||||
class ImageData;
|
||||
|
||||
namespace lua {
|
||||
class Userdata {
|
||||
public:
|
||||
virtual ~Userdata() {};
|
||||
virtual const std::string& getTypeName() const = 0;
|
||||
};
|
||||
|
||||
class LuaHeightmap : public Userdata {
|
||||
std::shared_ptr<Heightmap> map;
|
||||
std::unique_ptr<fnl_state> noise;
|
||||
public:
|
||||
LuaHeightmap(const std::shared_ptr<Heightmap>& map);
|
||||
LuaHeightmap(uint width, uint height);
|
||||
|
||||
virtual ~LuaHeightmap();
|
||||
|
||||
uint getWidth() const;
|
||||
|
||||
uint getHeight() const;
|
||||
|
||||
float* getValues();
|
||||
|
||||
const float* getValues() const;
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
const std::shared_ptr<Heightmap>& getHeightmap() const {
|
||||
return map;
|
||||
}
|
||||
|
||||
fnl_state* getNoise() {
|
||||
return noise.get();
|
||||
}
|
||||
|
||||
void setSeed(int64_t seed);
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "Heightmap";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaHeightmap>());
|
||||
|
||||
class LuaVoxelFragment : public Userdata {
|
||||
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants;
|
||||
public:
|
||||
LuaVoxelFragment(
|
||||
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants
|
||||
);
|
||||
|
||||
virtual ~LuaVoxelFragment();
|
||||
|
||||
std::shared_ptr<VoxelFragment> getFragment(size_t rotation) const {
|
||||
return fragmentVariants.at(rotation & 0b11);
|
||||
}
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "VoxelFragment";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaVoxelFragment>());
|
||||
|
||||
class LuaCanvas : public Userdata {
|
||||
public:
|
||||
explicit LuaCanvas(
|
||||
std::shared_ptr<Texture> inTexture,
|
||||
std::shared_ptr<ImageData> inData
|
||||
);
|
||||
~LuaCanvas() override = default;
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto& texture() const {
|
||||
return *mTexture;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto& data() const {
|
||||
return *mData;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool hasTexture() const {
|
||||
return mTexture != nullptr;
|
||||
}
|
||||
|
||||
auto shareTexture() const {
|
||||
return mTexture;
|
||||
}
|
||||
|
||||
void createTexture();
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "Canvas";
|
||||
private:
|
||||
std::shared_ptr<Texture> mTexture; // nullable
|
||||
std::shared_ptr<ImageData> mData;
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaCanvas>());
|
||||
|
||||
class LuaRandom : public Userdata {
|
||||
public:
|
||||
std::mt19937 rng;
|
||||
|
||||
explicit LuaRandom(uint64_t seed) : rng(seed) {}
|
||||
virtual ~LuaRandom() override = default;
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "__vc_Random";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaRandom>());
|
||||
}
|
||||
@ -8,7 +8,10 @@
|
||||
#include "debug/Logger.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "libs/api_lua.hpp"
|
||||
#include "lua_custom_types.hpp"
|
||||
#include "usertypes/lua_type_heightmap.hpp"
|
||||
#include "usertypes/lua_type_voxelfragment.hpp"
|
||||
#include "usertypes/lua_type_canvas.hpp"
|
||||
#include "usertypes/lua_type_random.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
|
||||
static debug::Logger logger("lua-state");
|
||||
|
||||
@ -6,7 +6,6 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "data/dv.hpp"
|
||||
#include "lua_custom_types.hpp"
|
||||
#include "lua_wrapper.hpp"
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
#include <unordered_map>
|
||||
#include "lua_type_canvas.hpp"
|
||||
|
||||
#include "graphics/core/ImageData.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "logic/scripting/lua/lua_custom_types.hpp"
|
||||
#include "logic/scripting/lua/lua_util.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace lua;
|
||||
|
||||
LuaCanvas::LuaCanvas(
|
||||
|
||||
46
src/logic/scripting/lua/usertypes/lua_type_canvas.hpp
Normal file
46
src/logic/scripting/lua/usertypes/lua_type_canvas.hpp
Normal file
@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "../lua_commons.hpp"
|
||||
|
||||
class Texture;
|
||||
class ImageData;
|
||||
|
||||
namespace lua {
|
||||
class LuaCanvas : public Userdata {
|
||||
public:
|
||||
explicit LuaCanvas(
|
||||
std::shared_ptr<Texture> inTexture,
|
||||
std::shared_ptr<ImageData> inData
|
||||
);
|
||||
~LuaCanvas() override = default;
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto& texture() const {
|
||||
return *mTexture;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto& data() const {
|
||||
return *mData;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool hasTexture() const {
|
||||
return mTexture != nullptr;
|
||||
}
|
||||
|
||||
auto shareTexture() const {
|
||||
return mTexture;
|
||||
}
|
||||
|
||||
void createTexture();
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "Canvas";
|
||||
private:
|
||||
std::shared_ptr<Texture> mTexture; // nullable
|
||||
std::shared_ptr<ImageData> mData;
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaCanvas>());
|
||||
}
|
||||
@ -1,9 +1,4 @@
|
||||
#include "../lua_custom_types.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <filesystem>
|
||||
#include "lua_type_heightmap.hpp"
|
||||
|
||||
#include "util/functional_util.hpp"
|
||||
#define FNL_IMPL
|
||||
@ -14,6 +9,12 @@
|
||||
#include "maths/Heightmap.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "../lua_util.hpp"
|
||||
#include "lua_type_heightmap.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <filesystem>
|
||||
|
||||
using namespace lua;
|
||||
|
||||
|
||||
44
src/logic/scripting/lua/usertypes/lua_type_heightmap.hpp
Normal file
44
src/logic/scripting/lua/usertypes/lua_type_heightmap.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "../lua_commons.hpp"
|
||||
|
||||
struct fnl_state;
|
||||
class Heightmap;
|
||||
|
||||
namespace lua {
|
||||
class LuaHeightmap : public Userdata {
|
||||
std::shared_ptr<Heightmap> map;
|
||||
std::unique_ptr<fnl_state> noise;
|
||||
public:
|
||||
LuaHeightmap(const std::shared_ptr<Heightmap>& map);
|
||||
LuaHeightmap(uint width, uint height);
|
||||
|
||||
virtual ~LuaHeightmap();
|
||||
|
||||
uint getWidth() const;
|
||||
|
||||
uint getHeight() const;
|
||||
|
||||
float* getValues();
|
||||
|
||||
const float* getValues() const;
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
const std::shared_ptr<Heightmap>& getHeightmap() const {
|
||||
return map;
|
||||
}
|
||||
|
||||
fnl_state* getNoise() {
|
||||
return noise.get();
|
||||
}
|
||||
|
||||
void setSeed(int64_t seed);
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "Heightmap";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaHeightmap>());
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
#include "../lua_custom_types.hpp"
|
||||
#include "../lua_util.hpp"
|
||||
#include "lua_type_random.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
|
||||
23
src/logic/scripting/lua/usertypes/lua_type_random.hpp
Normal file
23
src/logic/scripting/lua/usertypes/lua_type_random.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "../lua_commons.hpp"
|
||||
|
||||
#include <random>
|
||||
|
||||
namespace lua {
|
||||
class LuaRandom : public Userdata {
|
||||
public:
|
||||
std::mt19937 rng;
|
||||
|
||||
explicit LuaRandom(uint64_t seed) : rng(seed) {}
|
||||
virtual ~LuaRandom() override = default;
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "__vc_Random";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaRandom>());
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
#include "../lua_custom_types.hpp"
|
||||
#include "lua_type_voxelfragment.hpp"
|
||||
|
||||
#include "../lua_util.hpp"
|
||||
|
||||
#include "world/generator/VoxelFragment.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "world/Level.hpp"
|
||||
|
||||
29
src/logic/scripting/lua/usertypes/lua_type_voxelfragment.hpp
Normal file
29
src/logic/scripting/lua/usertypes/lua_type_voxelfragment.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "../lua_commons.hpp"
|
||||
|
||||
class VoxelFragment;
|
||||
|
||||
namespace lua {
|
||||
class LuaVoxelFragment : public Userdata {
|
||||
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants;
|
||||
public:
|
||||
LuaVoxelFragment(
|
||||
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants
|
||||
);
|
||||
|
||||
virtual ~LuaVoxelFragment();
|
||||
|
||||
std::shared_ptr<VoxelFragment> getFragment(size_t rotation) const {
|
||||
return fragmentVariants.at(rotation & 0b11);
|
||||
}
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
return TYPENAME;
|
||||
}
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "VoxelFragment";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaVoxelFragment>());
|
||||
}
|
||||
@ -17,7 +17,6 @@
|
||||
#include "logic/BlocksController.hpp"
|
||||
#include "logic/LevelController.hpp"
|
||||
#include "lua/lua_engine.hpp"
|
||||
#include "lua/lua_custom_types.hpp"
|
||||
#include "maths/Heightmap.hpp"
|
||||
#include "objects/Player.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#include "scripting_commons.hpp"
|
||||
#include "typedefs.hpp"
|
||||
#include "lua/lua_engine.hpp"
|
||||
#include "lua/lua_custom_types.hpp"
|
||||
#include "lua/usertypes/lua_type_heightmap.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "voxels/Block.hpp"
|
||||
#include "voxels/Chunk.hpp"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user