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.cpp

354 lines
9.3 KiB

#include "guecs.hpp"
#include "shaders.hpp"
#include "sound.hpp"
#include "textures.hpp"
#include <typeinfo>
namespace guecs {
using std::make_shared;
void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
dbc::check(font_ptr != nullptr, "you failed to initialize this WideText");
if(font == nullptr) font = font_ptr;
if(text == nullptr) text = make_shared<sf::Text>(*font, content, size);
text->setFillColor(color);
if(centered) {
auto bounds = text->getLocalBounds();
auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell);
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
text->setPosition({float(text_cell.x), float(text_cell.y) - text_cell.h / 2});
} else {
text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)});
}
text->setCharacterSize(size);
}
void Textual::update(const wstring& new_content) {
content = new_content;
text->setString(content);
}
void Sprite::update(const string& new_name) {
if(new_name != name) {
name = new_name;
auto sprite_texture = textures::get(name);
sprite->setTexture(*sprite_texture.texture);
sprite->setTextureRect(sprite_texture.sprite->getTextureRect());
}
}
void Sprite::init(lel::Cell &cell) {
auto sprite_texture = textures::get(name);
sprite = make_shared<sf::Sprite>(
*sprite_texture.texture,
sprite_texture.sprite->getTextureRect());
sprite->setPosition({
float(cell.x + padding),
float(cell.y + padding)});
auto bounds = sprite->getLocalBounds();
sprite->setScale({
float(cell.w - padding * 2) / bounds.size.x,
float(cell.h - padding * 2) / bounds.size.y});
}
void Rectangle::init(lel::Cell& cell) {
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2};
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
shape->setPosition({float(cell.x + padding), float(cell.y + padding)});
shape->setFillColor(color);
shape->setOutlineColor(border_color);
shape->setOutlineThickness(border_px);
}
void Meter::init(lel::Cell& cell) {
bar.init(cell);
}
void Meter::render(lel::Cell& cell) {
float level = std::clamp(percent, 0.0f, 1.0f) * float(cell.w);
// ZED: this 6 is a border width, make it a thing
bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)});
}
void Sound::play(bool hover) {
if(!hover) {
sound::play(on_click);
}
}
void Sound::stop(bool hover) {
if(!hover) {
sound::stop(on_click);
}
}
void Background::init() {
sf::Vector2f size{float(w), float(h)};
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
shape->setPosition({float(x), float(y)});
shape->setFillColor(color);
}
void Effect::init(lel::Cell &cell) {
$shader_version = shaders::version();
$shader = shaders::get(name);
$shader->setUniform("u_resolution", sf::Vector2f({float(cell.w), float(cell.h)}));
$clock = std::make_shared<sf::Clock>();
}
void Effect::step() {
sf::Time cur_time = $clock->getElapsedTime();
float u_time = cur_time.asSeconds();
if(u_time < $u_time_end) {
$shader->setUniform("u_duration", duration);
$shader->setUniform("u_time_end", $u_time_end);
$shader->setUniform("u_time", u_time);
} else {
$active = false;
}
}
void Effect::run() {
$active = true;
sf::Time u_time = $clock->getElapsedTime();
$u_time_end = u_time.asSeconds() + duration;
}
shared_ptr<sf::Shader> Effect::checkout_ptr() {
if(shaders::updated($shader_version)) {
$shader = shaders::get(name);
$shader_version = shaders::version();
}
return $shader;
}
void Effect::stop() {
$active = false;
}
UI::UI() {
$font = make_shared<sf::Font>(FONT_FILE_NAME);
}
void UI::position(int x, int y, int width, int height) {
$parser.position(x, y, width, height);
}
sf::Vector2f UI::get_position() {
return {float($parser.grid_x), float($parser.grid_y)};
}
sf::Vector2f UI::get_size() {
return {float($parser.grid_w), float($parser.grid_h)};
}
void UI::layout(const string& grid) {
$grid = grid;
bool good = $parser.parse($grid);
dbc::check(good, "LEL parsing failed.");
for(auto& [name, cell] : $parser.cells) {
auto ent = init_entity(name);
set<lel::Cell>(ent, cell);
}
}
Entity UI::init_entity(const string& name) {
auto ent = entity();
// this lets you look up an entity by name
$name_ents.insert_or_assign(name, ent);
// this makes it easier to get the name during querying
set<CellName>(ent, {name});
return ent;
}
Entity UI::entity(const string& name) {
dbc::check($name_ents.contains(name),
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name));
return $name_ents.at(name);
}
Entity UI::entity(const string& name, int id) {
return entity(fmt::format("{}{}", name, id));
}
void UI::init() {
query<Background>([](auto, auto& bg) {
bg.init();
});
query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
rect.init(cell);
});
query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
shader.init(cell);
});
query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
bg.shape->setFillColor(meter.color);
});
query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
meter.init(cell);
});
query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
text.init(cell, $font);
});
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
text.init(cell, $font);
});
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
sprite.init(cell);
});
}
void UI::debug_layout(sf::RenderWindow& window) {
query<lel::Cell>([&](const auto, auto &cell) {
sf::RectangleShape rect{{float(cell.w), float(cell.h)}};
rect.setPosition({float(cell.x), float(cell.y)});
rect.setFillColor(sf::Color::Transparent);
rect.setOutlineColor(sf::Color::Red);
rect.setOutlineThickness(2.0f);
window.draw(rect);
});
}
void UI::render(sf::RenderWindow& window) {
if(auto bg = get_if<Background>(MAIN)) {
window.draw(*(bg->shape));
}
query<Effect>([&](auto, auto& shader) {
if(shader.$active) shader.step();
});
query<Rectangle>([&](auto ent, auto& rect) {
render_helper(window, ent, true, rect.shape);
});
query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
meter.render(cell);
render_helper(window, ent, true, meter.bar.shape);
});
query<Sprite>([&](auto ent, auto& sprite) {
render_helper(window, ent, false, sprite.sprite);
});
query<Label>([&](auto ent, auto& text) {
render_helper(window, ent, false, text.text);
});
query<Textual>([&](auto ent, auto& text) {
render_helper(window, ent, true, text.text);
});
}
bool UI::mouse(float x, float y, bool hover) {
int action_count = 0;
query<lel::Cell>([&](auto ent, auto& cell) {
if((x >= cell.x && x <= cell.x + cell.w) &&
(y >= cell.y && y <= cell.y + cell.h))
{
do_if<Effect>(ent, [hover](auto& effect) {
effect.$shader->setUniform("hover", hover);
effect.run();
});
do_if<Sound>(ent, [hover](auto& sound) {
// here set that it played then only play once
sound.play(hover);
});
if(hover) return;
click_on(ent);
action_count++;
} else {
do_if<Effect>(ent, [hover](auto& effect) {
effect.stop();
});
do_if<Sound>(ent, [hover](auto& sound) {
// here set that it played then only play once
sound.stop(hover);
});
}
});
return action_count > 0;
}
void UI::show_sprite(const string& region, const string& sprite_name) {
auto ent = entity(region);
if(auto sprite = get_if<Sprite>(ent)) {
sprite->update(sprite_name);
} else {
set_init<Sprite>(ent, {sprite_name});
}
}
void UI::show_text(const string& region, const wstring& content) {
auto ent = entity(region);
if(auto tc = get_if<Textual>(ent)) {
tc->text->setString(content);
} else {
auto &cell = cell_for(ent);
Textual to_set{content, TEXT_SIZE};
to_set.init(cell, $font);
set<Textual>(ent, to_set);
}
}
void UI::click_on(const string& name, bool required) {
auto ent = entity(name);
if(required) {
dbc::check(has<Clickable>(ent),
fmt::format("click_on required '{}' to exist but it doesn't", name));
}
click_on(ent);
}
void UI::click_on(Entity ent) {
if(auto clicked = get_if<Clickable>(ent)) {
if(auto action_data = get_if<ActionData>(ent)) {
clicked->action(ent, action_data->data);
} else {
clicked->action(ent, {});
}
}
}
void UI::show_label(const string& region, const wstring& content) {
auto ent = entity(region);
if(auto tc = get_if<Label>(ent)) {
tc->text->setString(content);
} else {
auto &cell = cell_for(ent);
Label to_set{content, LABEL_SIZE};
to_set.init(cell, $font);
to_set.text->setFillColor(TEXT_COLOR);
set<Label>(ent, to_set);
}
}
}