First batch of changes merged back from Under. Fixes the router, adds magic_enum to FSM, and makes it easy to reset the world.
This commit is contained in:
parent
d583a28b9c
commit
110612c33a
11 changed files with 44 additions and 19 deletions
|
|
@ -76,13 +76,18 @@ sfml_system = subproject('sfml').get_variable('sfml_system_dep')
|
||||||
sfml_window = subproject('sfml').get_variable('sfml_window_dep')
|
sfml_window = subproject('sfml').get_variable('sfml_window_dep')
|
||||||
lel_guecs = subproject('lel-guecs').get_variable('lel_guecs_dep')
|
lel_guecs = subproject('lel-guecs').get_variable('lel_guecs_dep')
|
||||||
|
|
||||||
|
# have to disable tests because this dude's using catch2 from the 1890s
|
||||||
|
magic_enum_proj = subproject('magic_enum', default_options: {
|
||||||
|
'test': false})
|
||||||
|
magic_enum = magic_enum_proj.get_variable('magic_enum_dep')
|
||||||
|
|
||||||
inc_dirs = include_directories('src')
|
inc_dirs = include_directories('src')
|
||||||
|
|
||||||
subdir('src')
|
subdir('src')
|
||||||
subdir('tests')
|
subdir('tests')
|
||||||
|
|
||||||
dependencies += [
|
dependencies += [
|
||||||
fmt, json, freetype2,
|
fmt, json, freetype2, magic_enum,
|
||||||
flac, ogg, vorbis, vorbisfile, vorbisenc,
|
flac, ogg, vorbis, vorbisfile, vorbisenc,
|
||||||
sfml_audio, sfml_graphics,
|
sfml_audio, sfml_graphics,
|
||||||
sfml_network, sfml_system,
|
sfml_network, sfml_system,
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
|
||||||
#ifndef FSM_DEBUG
|
#ifndef FSM_DEBUG
|
||||||
#define FSM_STATE(C, S, E, ...) case C::S: S(E, ##__VA_ARGS__); break
|
#define FSM_STATE(C, S, E, ...) case C::S: S(E, ##__VA_ARGS__); break
|
||||||
#else
|
#else
|
||||||
|
#include <magic_enum/magic_enum.hpp>
|
||||||
static int last_event=-1;
|
static int last_event=-1;
|
||||||
#define FSM_STATE(C, S, E, ...) case C::S: if(last_event != int(E)) { last_event = int(E); fmt::println(">> " #C " " #S " event={}, state={}", int(E), int($state));}; S(E, ##__VA_ARGS__); break
|
#define FSM_STATE(C, S, E, ...) case C::S: if(last_event != int(E)) { last_event = int(E); fmt::println(">> [{}:EVENT] " #S " event={}", FSM_DEBUG, magic_enum::enum_name(E));}; S(E, ##__VA_ARGS__); break
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<typename S, typename E>
|
template<typename S, typename E>
|
||||||
|
|
@ -21,7 +21,7 @@ public:
|
||||||
|
|
||||||
void state(S next_state) {
|
void state(S next_state) {
|
||||||
#ifdef FSM_DEBUG
|
#ifdef FSM_DEBUG
|
||||||
fmt::println("<< STATE: {} -> {}", int($state), int(next_state));
|
fmt::println(">> [{}:TRANS]: {} -> {}", FSM_DEBUG, magic_enum::enum_name($state), magic_enum::enum_name(next_state));
|
||||||
#endif
|
#endif
|
||||||
$state = next_state;
|
$state = next_state;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -235,7 +235,7 @@ namespace boss {
|
||||||
dbc::check($window != nullptr, "you didn't set_window");
|
dbc::check($window != nullptr, "you didn't set_window");
|
||||||
|
|
||||||
while(const auto ev = $window->pollEvent()) {
|
while(const auto ev = $window->pollEvent()) {
|
||||||
auto gui_ev = $router.process_event(ev);
|
auto [gui_ev, mods] = $router.process_event(ev);
|
||||||
|
|
||||||
if(gui_ev == game::Event::KEY_PRESS) {
|
if(gui_ev == game::Event::KEY_PRESS) {
|
||||||
using KEY = sf::Keyboard::Scan;
|
using KEY = sf::Keyboard::Scan;
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,7 @@ namespace components {
|
||||||
settings::Config bosses;
|
settings::Config bosses;
|
||||||
settings::Config rituals;
|
settings::Config rituals;
|
||||||
settings::Config stories;
|
settings::Config stories;
|
||||||
|
settings::Config scenes;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Personality {
|
struct Personality {
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,12 @@ namespace GameDB {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
LDB = make_shared<LevelDB>();
|
||||||
|
initialized = true;
|
||||||
|
new_level(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<DinkyECS::World> current_world() {
|
shared_ptr<DinkyECS::World> current_world() {
|
||||||
dbc::check(initialized, "Forgot to call GameDB::init()");
|
dbc::check(initialized, "Forgot to call GameDB::init()");
|
||||||
return current_level().world;
|
return current_level().world;
|
||||||
|
|
@ -132,7 +138,8 @@ namespace GameDB {
|
||||||
settings::get("devices"),
|
settings::get("devices"),
|
||||||
settings::get("bosses"),
|
settings::get("bosses"),
|
||||||
settings::get("rituals"),
|
settings::get("rituals"),
|
||||||
settings::get("stories")
|
settings::get("stories"),
|
||||||
|
settings::get("scenes")
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,9 @@ namespace GameDB {
|
||||||
Level& create_level();
|
Level& create_level();
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
void reset();
|
||||||
Level ¤t_level();
|
Level ¤t_level();
|
||||||
|
void new_level(std::shared_ptr<DinkyECS::World> prev_world);
|
||||||
std::shared_ptr<DinkyECS::World> current_world();
|
std::shared_ptr<DinkyECS::World> current_world();
|
||||||
components::Position& player_position();
|
components::Position& player_position();
|
||||||
DinkyECS::Entity the_player();
|
DinkyECS::Entity the_player();
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
#define FSM_DEBUG "router"
|
||||||
#include "event_router.hpp"
|
#include "event_router.hpp"
|
||||||
#include "dbc.hpp"
|
#include "dbc.hpp"
|
||||||
#include "events.hpp"
|
#include "events.hpp"
|
||||||
|
|
@ -7,17 +8,22 @@ namespace gui {
|
||||||
using enum Event;
|
using enum Event;
|
||||||
using enum State;
|
using enum State;
|
||||||
|
|
||||||
game::Event Router::process_event(std::optional<sf::Event> ev) {
|
std::pair<game::Event, guecs::Modifiers> Router::process_event(std::optional<sf::Event> ev) {
|
||||||
$next_event = game::Event::TICK;
|
$next_event = game::Event::TICK;
|
||||||
|
|
||||||
if(ev->is<sf::Event::Closed>()) {
|
if(ev->is<sf::Event::Closed>()) {
|
||||||
return game::Event::QUIT;
|
return {game::Event::QUIT, guecs::NO_MODS};
|
||||||
}
|
}
|
||||||
|
|
||||||
if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) {
|
if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) {
|
||||||
if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
|
if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
|
||||||
left_button = mouse->button == sf::Mouse::Button::Left;
|
left_button = mouse->button == sf::Mouse::Button::Left;
|
||||||
position = mouse->position;
|
position = mouse->position;
|
||||||
|
if(left_button) {
|
||||||
|
mouse_mods = {1 << guecs::ModBit::left};
|
||||||
|
} else {
|
||||||
|
mouse_mods = {1 << guecs::ModBit::right};
|
||||||
|
}
|
||||||
event(MOUSE_DOWN);
|
event(MOUSE_DOWN);
|
||||||
}
|
}
|
||||||
} else if(const auto* mouse = ev->getIf<sf::Event::MouseButtonReleased>()) {
|
} else if(const auto* mouse = ev->getIf<sf::Event::MouseButtonReleased>()) {
|
||||||
|
|
@ -25,10 +31,12 @@ namespace gui {
|
||||||
if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
|
if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
|
||||||
left_button = mouse->button == sf::Mouse::Button::Left;
|
left_button = mouse->button == sf::Mouse::Button::Left;
|
||||||
position = mouse->position;
|
position = mouse->position;
|
||||||
|
mouse_mods = guecs::NO_MODS;
|
||||||
event(MOUSE_UP);
|
event(MOUSE_UP);
|
||||||
}
|
}
|
||||||
} else if(const auto* mouse = ev->getIf<sf::Event::MouseMoved>()) {
|
} else if(const auto* mouse = ev->getIf<sf::Event::MouseMoved>()) {
|
||||||
position = mouse->position;
|
position = mouse->position;
|
||||||
|
mouse_mods = {1 << guecs::ModBit::hover};
|
||||||
event(MOUSE_MOVE);
|
event(MOUSE_MOVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,7 +45,7 @@ namespace gui {
|
||||||
event(KEY_PRESS);
|
event(KEY_PRESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $next_event;
|
return {$next_event, mouse_mods};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Router::event(Event ev) {
|
void Router::event(Event ev) {
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,16 @@
|
||||||
#include "events.hpp"
|
#include "events.hpp"
|
||||||
#include "algos/simplefsm.hpp"
|
#include "algos/simplefsm.hpp"
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <guecs/ui.hpp>
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
namespace routing {
|
namespace routing {
|
||||||
enum class State {
|
enum class State {
|
||||||
START,
|
START=__LINE__,
|
||||||
IDLE,
|
IDLE=__LINE__,
|
||||||
MOUSE_ACTIVE,
|
MOUSE_ACTIVE=__LINE__,
|
||||||
MOUSE_MOVING,
|
MOUSE_MOVING=__LINE__,
|
||||||
MOUSE_DRAGGING
|
MOUSE_DRAGGING=__LINE__
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Event {
|
enum class Event {
|
||||||
|
|
@ -25,6 +26,7 @@ namespace gui {
|
||||||
class Router : public DeadSimpleFSM<State, Event> {
|
class Router : public DeadSimpleFSM<State, Event> {
|
||||||
public:
|
public:
|
||||||
sf::Vector2i position;
|
sf::Vector2i position;
|
||||||
|
guecs::Modifiers mouse_mods = guecs::NO_MODS;
|
||||||
sf::Keyboard::Scancode scancode;
|
sf::Keyboard::Scancode scancode;
|
||||||
game::Event $next_event = game::Event::TICK;
|
game::Event $next_event = game::Event::TICK;
|
||||||
int move_count = 0;
|
int move_count = 0;
|
||||||
|
|
@ -39,7 +41,7 @@ namespace gui {
|
||||||
void MOUSE_MOVING(Event ev);
|
void MOUSE_MOVING(Event ev);
|
||||||
void MOUSE_DRAGGING(Event ev);
|
void MOUSE_DRAGGING(Event ev);
|
||||||
|
|
||||||
game::Event process_event(std::optional<sf::Event> ev);
|
std::pair<game::Event, guecs::Modifiers> process_event(std::optional<sf::Event> ev);
|
||||||
|
|
||||||
void set_event(game::Event ev) {
|
void set_event(game::Event ev) {
|
||||||
$next_event = ev;
|
$next_event = ev;
|
||||||
|
|
|
||||||
|
|
@ -305,7 +305,7 @@ namespace gui {
|
||||||
|
|
||||||
void FSM::handle_keyboard_mouse() {
|
void FSM::handle_keyboard_mouse() {
|
||||||
while(const auto ev = $window.pollEvent()) {
|
while(const auto ev = $window.pollEvent()) {
|
||||||
auto gui_ev = $router.process_event(ev);
|
auto [gui_ev, mods] = $router.process_event(ev);
|
||||||
|
|
||||||
if(gui_ev == Event::KEY_PRESS) {
|
if(gui_ev == Event::KEY_PRESS) {
|
||||||
using KEY = sf::Keyboard::Scan;
|
using KEY = sf::Keyboard::Scan;
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,7 @@ namespace animator {
|
||||||
while(const auto ev = $window.pollEvent()) {
|
while(const auto ev = $window.pollEvent()) {
|
||||||
using enum game::Event;
|
using enum game::Event;
|
||||||
using KEY = sf::Keyboard::Scan;
|
using KEY = sf::Keyboard::Scan;
|
||||||
auto gui_ev = $router.process_event(ev);
|
auto [gui_ev, mods] = $router.process_event(ev);
|
||||||
auto mouse_pos = $window.mapPixelToCoords($router.position);
|
auto mouse_pos = $window.mapPixelToCoords($router.position);
|
||||||
|
|
||||||
switch(gui_ev) {
|
switch(gui_ev) {
|
||||||
|
|
|
||||||
|
|
@ -31,13 +31,13 @@ int main(int, char*[]) {
|
||||||
main.render(window);
|
main.render(window);
|
||||||
|
|
||||||
while(const auto ev = window.pollEvent()) {
|
while(const auto ev = window.pollEvent()) {
|
||||||
auto gui_ev = router.process_event(ev);
|
auto [gui_ev, mods] = router.process_event(ev);
|
||||||
auto mouse_pos = window.mapPixelToCoords(router.position);
|
auto mouse_pos = window.mapPixelToCoords(router.position);
|
||||||
|
|
||||||
if(gui_ev == game::Event::QUIT) {
|
if(gui_ev == game::Event::QUIT) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if(gui_ev == game::Event::MOUSE_CLICK) {
|
} else if(gui_ev == game::Event::MOUSE_CLICK) {
|
||||||
main.mouse(mouse_pos.x, mouse_pos.y, guecs::NO_MODS);
|
main.mouse(mouse_pos.x, mouse_pos.y, mods);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue