Exploring raycasters and possibly make a little "doom like" game based on it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
raycaster/guecs.hpp

293 lines
7.2 KiB

#pragma once
#include "color.hpp"
#include "dinkyecs.hpp"
#include "lel.hpp"
#include <string>
#include <memory>
#include <SFML/Graphics.hpp>
#include "textures.hpp"
#include <functional>
#include "events.hpp"
#include "constants.hpp"
#include "components.hpp"
#include <any>
#include "shaders.hpp"
namespace guecs {
using std::shared_ptr, std::make_shared, std::wstring, std::string;
using Entity = DinkyECS::Entity;
using EntityMap = std::unordered_map<Entity, size_t>;
template <typename T>
struct ComponentStorage {
std::vector<T> data;
std::queue<size_t> free_indices;
};
struct Textual {
std::wstring content;
unsigned int size = GUECS_FONT_SIZE;
sf::Color color = GUECS_TEXT_COLOR;
int padding = GUECS_PADDING;
bool centered = false;
shared_ptr<sf::Font> font = nullptr;
shared_ptr<sf::Text> text = nullptr;
void init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr);
void update(const std::wstring& new_content);
};
struct Label : public Textual {
template<typename... Args>
Label(Args... args) : Textual(args...)
{
centered = true;
}
Label() {
centered = true;
};
};
struct Clickable {
/* This is actually called by UI::mouse and passed the entity ID of the
* button pressed so you can interact with it in the event handler.
*/
std::function<void(Entity ent, std::any data)> action;
};
struct Sprite {
string name;
int padding = GUECS_PADDING;
std::shared_ptr<sf::Sprite> sprite = nullptr;
void init(lel::Cell &cell);
void update(const string& new_name);
};
struct Rectangle {
int padding = GUECS_PADDING;
sf::Color color = GUECS_FILL_COLOR;
sf::Color border_color = GUECS_BORDER_COLOR;
int border_px = GUECS_BORDER_PX;
shared_ptr<sf::RectangleShape> shape = nullptr;
void init(lel::Cell& cell);
};
struct Meter {
float percent = 1.0f;
sf::Color color = ColorValue::BLACK;
Rectangle bar;
void init(lel::Cell& cell);
void render(lel::Cell& cell);
};
struct ActionData {
std::any data;
};
struct CellName {
string name;
};
struct Effect {
float duration = 0.1f;
string name{"ui_shader"};
float $u_time_end = 0.0;
bool $active = false;
std::shared_ptr<sf::Clock> $clock = nullptr;
std::shared_ptr<sf::Shader> $shader = nullptr;
int $shader_version = 0;
void init(lel::Cell &cell);
void run();
void stop();
void step();
shared_ptr<sf::Shader> checkout_ptr();
};
struct Sound {
string on_click{"ui_click"};
void play(bool hover);
void stop(bool hover);
};
struct Background {
float x = 0.0f;
float y = 0.0f;
float w = 0.0f;
float h = 0.0f;
sf::Color color = GUECS_BG_COLOR;
shared_ptr<sf::RectangleShape> shape = nullptr;
Background(lel::Parser& parser, sf::Color bg_color=GUECS_BG_COLOR) :
x(parser.grid_x),
y(parser.grid_y),
w(parser.grid_w),
h(parser.grid_h),
color(bg_color)
{}
Background() {}
void init();
};
class UI {
public:
DinkyECS::World $world;
unsigned long entity_count = 0;
std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $facts;
std::unordered_map<std::type_index, std::any> $component_storages;
std::unordered_map<string, Entity> $name_ents;
shared_ptr<sf::Font> $font = nullptr;
lel::Parser $parser;
string $grid = "";
UI();
void position(int x, int y, int width, int height);
sf::Vector2f get_position();
sf::Vector2f get_size();
void layout(const string& grid);
Entity init_entity(const string& name);
Entity entity(const string& name);
Entity entity(const string& name, int id);
inline lel::CellMap& cells() {
return $parser.cells;
}
inline DinkyECS::World& world() {
return $world;
}
void init();
void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover);
void click_on(const string& name, bool required=false);
void click_on(Entity slot_id);
void debug_layout(sf::RenderWindow& window);
Entity entity() { return $world.entity(); }
template <typename Comp>
bool has_the() {
return $world.has_the<Comp>();
}
template <typename Comp>
void set_the(Comp val) {
return $world.set_the<Comp>(val);
}
template <typename Comp>
Comp &get_the() {
return $world.get_the<Comp>();
}
template <typename Comp>
void set(Entity ent, Comp val) {
$world.set<Comp>(ent, val);
}
template <typename Comp>
Comp& get(Entity entity) {
return $world.get<Comp>(entity);
}
template <typename Comp>
Comp* get_if(Entity entity) {
return $world.get_if<Comp>(entity);
}
template <typename Comp>
bool has(Entity entity) {
return $world.has<Comp>(entity);
}
template <typename Comp>
void remove(Entity ent) {
$world.remove<Comp>(ent);
}
template <typename Comp>
void query(std::function<void(Entity, Comp &)> cb) {
$world.query<Comp>(cb);
}
template <typename CompA, typename CompB>
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
$world.query<CompA, CompB>(cb);
}
template <typename Comp>
void set_init(Entity ent, Comp val) {
dbc::check(has<lel::Cell>(ent),"WRONG! slot is missing its cell?!");
auto& cell = get<lel::Cell>(ent);
val.init(cell);
$world.set<Comp>(ent, val);
}
template <typename Comp>
void do_if(Entity ent, std::function<void(Comp &)> cb) {
if($world.has<Comp>(ent)) {
cb($world.get<Comp>(ent));
}
}
lel::Cell& cell_for(Entity ent) {
return $world.get<lel::Cell>(ent);
}
lel::Cell& cell_for(const string& name) {
Entity ent = entity(name);
return $world.get<lel::Cell>(ent);
}
// BUG: close could just be remove with overload
template <typename Comp>
void close(string region) {
auto ent = entity(region);
if(has<Comp>(ent)) {
remove<Comp>(ent);
}
}
template<typename T>
void render_helper(sf::RenderWindow& window, Entity ent, bool is_shape, T& target) {
sf::Shader *shader_ptr = nullptr;
if(auto shader = $world.get_if<Effect>(ent)) {
if(shader->$active && !is_shape) {
auto ptr = shader->checkout_ptr();
ptr->setUniform("is_shape", is_shape);
// NOTE: this is needed because SFML doesn't handle shared_ptr
shader_ptr = ptr.get();
}
}
window.draw(*target, shader_ptr);
}
void show_sprite(const string& region, const string& sprite_name);
void show_text(const string& region, const wstring& content);
void show_label(const string& region, const wstring& content);
};
Clickable make_action(DinkyECS::World& target, Events::GUI event);
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data);
}