|
|
|
#include <chrono> // for operator""s, chrono_literals
|
|
|
|
#include <iostream> // for cout, ostream
|
|
|
|
#include <fstream>
|
|
|
|
#include <memory> // for allocator, shared_ptr
|
|
|
|
#include <string> // for string, operator<<
|
|
|
|
#include <thread> // for sleep_for
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
#include <ftxui/dom/elements.hpp> // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element
|
|
|
|
#include <ftxui/dom/node.hpp> // for Render
|
|
|
|
#include <ftxui/screen/box.hpp> // for ftxui
|
|
|
|
#include <ftxui/component/loop.hpp>
|
|
|
|
#include <ftxui/screen/color.hpp>
|
|
|
|
|
|
|
|
#include <SFML/Window.hpp>
|
|
|
|
#include <SFML/System.hpp>
|
|
|
|
|
|
|
|
#include <fmt/core.h>
|
|
|
|
#include "dbc.hpp"
|
|
|
|
#include "gui.hpp"
|
|
|
|
#include "rand.hpp"
|
|
|
|
#include "components.hpp"
|
|
|
|
#include "systems.hpp"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using namespace fmt;
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
using namespace ftxui;
|
|
|
|
|
|
|
|
std::array<sf::Color, 10> VALUES{
|
|
|
|
sf::Color{1, 4, 2}, // black
|
|
|
|
sf::Color{9, 29, 16}, // dark dark
|
|
|
|
sf::Color{14, 50, 26}, // dark mid
|
|
|
|
sf::Color{0, 109, 44}, // dark light
|
|
|
|
sf::Color{63, 171, 92}, // mid
|
|
|
|
sf::Color{161, 217, 155}, // light dark
|
|
|
|
sf::Color{199, 233, 192}, // light mid
|
|
|
|
sf::Color{229, 245, 224}, // light light
|
|
|
|
sf::Color{255, 255, 255}, // white
|
|
|
|
sf::Color::Transparent, // white
|
|
|
|
};
|
|
|
|
|
|
|
|
sf::Color GUI::color(int val) {
|
|
|
|
return VALUES[size_t(val)];
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Color GUI::color(Value val) {
|
|
|
|
return VALUES[size_t(val)];
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI::GUI() : $game_map(GAME_MAP_X, GAME_MAP_Y),
|
|
|
|
$canvas(GAME_MAP_X * 2, GAME_MAP_Y * 4),
|
|
|
|
$window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"),
|
|
|
|
$screen(SCREEN_X, SCREEN_Y),
|
|
|
|
$map_screen(GAME_MAP_X, GAME_MAP_Y)
|
|
|
|
{
|
|
|
|
int res = $hit_buf.loadFromFile("./assets/hit.wav");
|
|
|
|
dbc::check(res, "failed to load hit.wav");
|
|
|
|
$hit_sound.setBuffer($hit_buf);
|
|
|
|
|
|
|
|
$font.loadFromFile("./assets/text.otf");
|
|
|
|
|
|
|
|
$ui_text.setFont($font);
|
|
|
|
$ui_text.setPosition(0,0);
|
|
|
|
$ui_text.setCharacterSize(UI_FONT_SIZE);
|
|
|
|
$ui_text.setFillColor(color(Value::LIGHT_LIGHT));
|
|
|
|
|
|
|
|
$map_text.setFont($font);
|
|
|
|
$map_text.setPosition(GAME_MAP_POS,0);
|
|
|
|
$map_text.setCharacterSize(MAP_FONT_SIZE);
|
|
|
|
$map_text.setFillColor(color(Value::MID));
|
|
|
|
|
|
|
|
$game_map.generate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::create_renderer() {
|
|
|
|
auto player = $world.get<Player>();
|
|
|
|
|
|
|
|
$map_view = Renderer([&, player] {
|
|
|
|
const auto& player_position = $world.component<Position>(player.entity);
|
|
|
|
Matrix &walls = $game_map.walls();
|
|
|
|
$game_map.set_target(player_position.location);
|
|
|
|
$game_map.make_paths();
|
|
|
|
Matrix &paths = $game_map.paths();
|
|
|
|
|
|
|
|
for(size_t x = 0; x < walls[0].size(); ++x) {
|
|
|
|
for(size_t y = 0; y < walls.size(); ++y) {
|
|
|
|
string tile = walls[y][x] == 1 ? WALL_TILE : format("{}", paths[y][x]);
|
|
|
|
if(tile == WALL_TILE) {
|
|
|
|
$canvas.DrawText(x*2, y*4, tile);
|
|
|
|
} else if($show_paths) {
|
|
|
|
//int pnum = paths[y][x];
|
|
|
|
$canvas.DrawText(x*2, y*4, tile);
|
|
|
|
} else {
|
|
|
|
$canvas.DrawText(x*2, y*4, FLOOR_TILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System::draw_entities($world, $canvas);
|
|
|
|
return canvas($canvas);
|
|
|
|
});
|
|
|
|
|
|
|
|
$document = Renderer([&, player]{
|
|
|
|
const auto& player_combat = $world.component<Combat>(player.entity);
|
|
|
|
return hbox({
|
|
|
|
hflow(
|
|
|
|
vbox(
|
|
|
|
text(format("HP: {}", player_combat.hp)) | border,
|
|
|
|
text($status_text) | border
|
|
|
|
) | xflex_grow
|
|
|
|
),
|
|
|
|
separator(),
|
|
|
|
hbox(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::handle_events() {
|
|
|
|
sf::Event event;
|
|
|
|
auto player = $world.get<Player>();
|
|
|
|
auto& player_motion = $world.component<Motion>(player.entity);
|
|
|
|
|
|
|
|
while($window.pollEvent(event)) {
|
|
|
|
if(event.type == sf::Event::Closed) {
|
|
|
|
$window.close();
|
|
|
|
} else if(event.type == sf::Event::KeyPressed) {
|
|
|
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
|
|
|
player_motion.dx = -1;
|
|
|
|
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
|
|
|
player_motion.dx = 1;
|
|
|
|
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
|
|
|
player_motion.dy = -1;
|
|
|
|
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
|
|
|
player_motion.dy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMPOSE system? You create a bunch of callbacks and then combine them into
|
|
|
|
// a single run over the data?
|
|
|
|
System::enemy_pathing($world, $game_map, player);
|
|
|
|
System::motion($world, $game_map);
|
|
|
|
System::combat($world, player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::burn() {
|
|
|
|
for(int i = 0; i < 20; ++i) {
|
|
|
|
$map_text.setFillColor(color(i % VALUES.size()));
|
|
|
|
int x = Random::uniform<int>(-10,10);
|
|
|
|
int y = Random::uniform<int>(-10,10);
|
|
|
|
draw_screen(false, x, y);
|
|
|
|
std::this_thread::sleep_for(2ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
$map_text.setFillColor(color(Value::MID));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
|
|
|
|
if(clear) $window.clear();
|
|
|
|
$window.draw($ui_text);
|
|
|
|
$map_text.setPosition(GAME_MAP_POS+map_off_x, map_off_y);
|
|
|
|
$window.draw($map_text);
|
|
|
|
$window.display();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::shake() {
|
|
|
|
$hit_sound.play();
|
|
|
|
for(int i = 0; i < 10; ++i) {
|
|
|
|
int x = Random::uniform<int>(-10,10);
|
|
|
|
int y = Random::uniform<int>(-10,10);
|
|
|
|
// add x/y back to draw screen
|
|
|
|
draw_screen(true, x, y);
|
|
|
|
std::this_thread::sleep_for(1ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::configure_world() {
|
|
|
|
dbc::check($game_map.room_count() > 1, "not enough rooms in map.");
|
|
|
|
// configure a player as a fact of the world
|
|
|
|
Player player{$world.entity()};
|
|
|
|
$world.set<Player>(player);
|
|
|
|
|
|
|
|
$world.assign<Position>(player.entity, {$game_map.place_entity(0)});
|
|
|
|
$world.assign<Motion>(player.entity, {0, 0});
|
|
|
|
$world.assign<Combat>(player.entity, {100, 10});
|
|
|
|
$world.assign<Tile>(player.entity, {PLAYER_TILE});
|
|
|
|
|
|
|
|
auto enemy = $world.entity();
|
|
|
|
$world.assign<Position>(enemy, {$game_map.place_entity(1)});
|
|
|
|
$world.assign<Motion>(enemy, {0,0});
|
|
|
|
$world.assign<Combat>(enemy, {20, 10});
|
|
|
|
$world.assign<Tile>(enemy, {ENEMY_TILE});
|
|
|
|
|
|
|
|
auto enemy2 = $world.entity();
|
|
|
|
$world.assign<Position>(enemy2, {$game_map.place_entity(2)});
|
|
|
|
$world.assign<Motion>(enemy2, {0,0});
|
|
|
|
$world.assign<Combat>(enemy2, {20, 10});
|
|
|
|
$world.assign<Tile>(enemy2, {"*"});
|
|
|
|
|
|
|
|
auto gold = $world.entity();
|
|
|
|
$world.assign<Position>(gold, {$game_map.place_entity($game_map.room_count() - 1)});
|
|
|
|
$world.assign<Treasure>(gold, {100});
|
|
|
|
$world.assign<Tile>(gold, {"$"});
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI::render_scene() {
|
|
|
|
Render($map_screen, $map_view->Render());
|
|
|
|
Render($screen, $document->Render());
|
|
|
|
|
|
|
|
std::string $screenout = $screen.ToString();
|
|
|
|
std::wstring main_screen_utf8 = $converter.from_bytes($screenout);
|
|
|
|
$ui_text.setString(main_screen_utf8);
|
|
|
|
|
|
|
|
std::string $map_screenout = $map_screen.ToString();
|
|
|
|
std::wstring map_screen_utf8 = $converter.from_bytes($map_screenout);
|
|
|
|
$map_text.setString(map_screen_utf8);
|
|
|
|
|
|
|
|
draw_screen();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GUI::main() {
|
|
|
|
configure_world();
|
|
|
|
create_renderer();
|
|
|
|
|
|
|
|
while($window.isOpen()) {
|
|
|
|
render_scene();
|
|
|
|
handle_events();
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|