Fixed an issue with lua context manager, tested to make sure it worked, started a document for the algorithm lua structure, updated engine loop
This commit is contained in:
14
documents/algorithms.txt
Normal file
14
documents/algorithms.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
Dungeon generation algorithms are written in lua and dropped into scripting->algorithms. They are automatically parsed by the visualizer and assigned keys in the program to run them.
|
||||
Algorithms are made up of a single lua file (possibly more with requires) with the following functions:
|
||||
|
||||
initialize()
|
||||
Initializes the algorithm and returns a structure/table with the following fields:
|
||||
{
|
||||
updates_per_second: int // Number of times the algorithm's update function should be called per second. Minimum of 1, converted into frames per update with a minimum of 1.
|
||||
free_data: table // Free table that the developer can store necessary state data in
|
||||
}
|
||||
|
||||
update(state)
|
||||
|
||||
The following functions are available to algorithms in either their update or initialize functions:
|
||||
|
||||
@@ -33,10 +33,11 @@ void Engine::start_loop() {
|
||||
}
|
||||
game->render(renderer.get());
|
||||
auto endtime = std::chrono::high_resolution_clock::now();
|
||||
auto update_duration = std::chrono::duration_cast<std::chrono::milliseconds>(endtime - starttime);
|
||||
last_frame = render_fps(endtime, last_frame);
|
||||
renderer->flush(); // not technically correct to do this here, but is done so we can put the fps over the display
|
||||
|
||||
std::this_thread::sleep_for(duration_per_frame);
|
||||
std::this_thread::sleep_for(duration_per_frame - update_duration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,3 +4,12 @@
|
||||
|
||||
#include "luacontextmanager.hpp"
|
||||
|
||||
std::string LuaContextManager::get_key(std::string args) {
|
||||
return args;
|
||||
}
|
||||
|
||||
std::shared_ptr<sol::state> LuaContextManager::load_resource(std::string args) {
|
||||
auto lua = std::make_shared<sol::state>();
|
||||
lua->open_libraries(sol::lib::base, sol::lib::package);
|
||||
return lua;
|
||||
}
|
||||
|
||||
@@ -5,11 +5,22 @@
|
||||
#ifndef RLA_IIPP_LUACONTEXTMANAGER_HPP
|
||||
#define RLA_IIPP_LUACONTEXTMANAGER_HPP
|
||||
#include <sol/sol.hpp>
|
||||
#include "../resources/resourcemanager.hpp"
|
||||
#define DEFAULT_CONTEXT_KEY "default"
|
||||
|
||||
|
||||
class LuaContextManager {
|
||||
class LuaContextManager : public ResourceManager<std::shared_ptr<sol::state>, std::string, std::string> {
|
||||
public:
|
||||
std::shared_ptr<sol::state> get_default_context() {
|
||||
return fetch_resource(DEFAULT_CONTEXT_KEY);
|
||||
}
|
||||
void clear_default_context() {
|
||||
clear_resource(DEFAULT_CONTEXT_KEY);
|
||||
}
|
||||
protected:
|
||||
std::string get_key(std::string args) override;
|
||||
|
||||
std::shared_ptr<sol::state> load_resource(std::string args) override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
struct DungeonAlgorithm {
|
||||
std::string path;
|
||||
std::optional<sol::table> algorithm_data;
|
||||
const int frames_per_update;
|
||||
int frames_until_update=0;
|
||||
};
|
||||
|
||||
#endif //RLA_IIPP_DUNGEONALGORITHM_HPP
|
||||
|
||||
@@ -20,6 +20,14 @@ bool Visualizer::update(InputResult input) {
|
||||
this->y = std::clamp(this->y, 0, (int)this->tile_map.get_height()-1);
|
||||
this->camera.move_camera(Point{x,y});
|
||||
|
||||
if(current_algorithm.has_value()) {
|
||||
current_algorithm->frames_until_update -= 1;
|
||||
if(current_algorithm->frames_until_update <= 0) {
|
||||
update_algorithm(current_algorithm.value());
|
||||
current_algorithm->frames_until_update = current_algorithm->frames_per_update;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -53,6 +61,8 @@ void Visualizer::initialize(GameInitArgs args) {
|
||||
int tilesy = (window_height / TILE_HEIGHT)+1;
|
||||
initialize_map(MAP_SIZE_W, MAP_SIZE_H);
|
||||
this->camera = Camera(Point{x,y}, tilesx, tilesy, tile_map.get_width(), tile_map.get_height());
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Visualizer::initialize_map(int width, int height) {
|
||||
@@ -64,4 +74,8 @@ void Visualizer::initialize_map(int width, int height) {
|
||||
}
|
||||
}
|
||||
|
||||
void Visualizer::update_algorithm(DungeonAlgorithm algorithm) {
|
||||
|
||||
}
|
||||
|
||||
Visualizer::~Visualizer() = default;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "../engine/utility/camera.hpp"
|
||||
#include "../engine/utility/grid2d.hpp"
|
||||
#include "../engine/scripting/luacontextmanager.hpp"
|
||||
#include "dungeonalgorithm.hpp"
|
||||
#include <vector>
|
||||
#include <range/v3/range.hpp>
|
||||
#include <range/v3/view.hpp>
|
||||
@@ -36,12 +37,14 @@ private:
|
||||
Grid2D<char> tile_map = Grid2D<char>(0, 0);
|
||||
Camera camera = Camera();
|
||||
LuaContextManager lua_context_manager = LuaContextManager();
|
||||
std::optional<DungeonAlgorithm> current_algorithm;
|
||||
|
||||
int x = 0;
|
||||
int y=0;
|
||||
int sprite_index = 1;
|
||||
|
||||
void initialize_map(int width, int height);
|
||||
void update_algorithm(DungeonAlgorithm algorithm);
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user