More refactoring of the gui. Now most things are out of the FSM and MainUI is responsible for the rayvew and its overlay.
This commit is contained in:
parent
23ed1594f2
commit
dd4f77a106
6 changed files with 89 additions and 45 deletions
52
gui_fsm.cpp
52
gui_fsm.cpp
|
@ -19,8 +19,7 @@ namespace gui {
|
||||||
$map_ui($level),
|
$map_ui($level),
|
||||||
$combat_ui($level),
|
$combat_ui($level),
|
||||||
$status_ui($level),
|
$status_ui($level),
|
||||||
$font{FONT_FILE_NAME},
|
$font{FONT_FILE_NAME}
|
||||||
$rayview($textures, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)
|
|
||||||
{
|
{
|
||||||
$textures.load_tiles();
|
$textures.load_tiles();
|
||||||
$textures.load_sprites();
|
$textures.load_sprites();
|
||||||
|
@ -41,13 +40,11 @@ namespace gui {
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::START(Event ) {
|
void FSM::START(Event ) {
|
||||||
generate_map();
|
$main_ui.generate_map();
|
||||||
$level.world->set_the<Debug>({});
|
$level.world->set_the<Debug>({});
|
||||||
$rayview.init_shaders();
|
|
||||||
$rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
|
|
||||||
$rayview.position_camera($player.x + 0.5, $player.y + 0.5);
|
|
||||||
|
|
||||||
$main_ui.render();
|
$main_ui.init();
|
||||||
|
|
||||||
$combat_ui.render($textures);
|
$combat_ui.render($textures);
|
||||||
$status_ui.render($textures);
|
$status_ui.render($textures);
|
||||||
$status_ui.log("Welcome to the game!");
|
$status_ui.log("Welcome to the game!");
|
||||||
|
@ -79,8 +76,9 @@ namespace gui {
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::MOVING(Event ) {
|
void FSM::MOVING(Event ) {
|
||||||
if($camera.play_move($rayview)) {
|
// this should be an optional that returns a point
|
||||||
System::plan_motion(*$level.world, {size_t($camera.target_x), size_t($camera.target_y)});
|
if($main_ui.play_move()) {
|
||||||
|
System::plan_motion(*$level.world, {size_t($main_ui.$camera.target_x), size_t($main_ui.$camera.target_y)});
|
||||||
run_systems();
|
run_systems();
|
||||||
state(State::IDLE);
|
state(State::IDLE);
|
||||||
}
|
}
|
||||||
|
@ -107,13 +105,13 @@ namespace gui {
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::ROTATING(Event ) {
|
void FSM::ROTATING(Event ) {
|
||||||
if($camera.play_rotate($rayview)) {
|
if($main_ui.play_rotate()) {
|
||||||
state(State::IDLE);
|
state(State::IDLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::COMBAT_ROTATE(Event ) {
|
void FSM::COMBAT_ROTATE(Event ) {
|
||||||
if($camera.play_rotate($rayview)) {
|
if($main_ui.play_rotate()) {
|
||||||
state(State::IN_COMBAT);
|
state(State::IN_COMBAT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,11 +137,11 @@ namespace gui {
|
||||||
try_move(-1, true);
|
try_move(-1, true);
|
||||||
break;
|
break;
|
||||||
case ROTATE_LEFT:
|
case ROTATE_LEFT:
|
||||||
$camera.plan_rotate($rayview, 1);
|
$main_ui.plan_rotate(1);
|
||||||
state(State::ROTATING);
|
state(State::ROTATING);
|
||||||
break;
|
break;
|
||||||
case ROTATE_RIGHT:
|
case ROTATE_RIGHT:
|
||||||
$camera.plan_rotate($rayview, -1);
|
$main_ui.plan_rotate(-1);
|
||||||
state(State::ROTATING);
|
state(State::ROTATING);
|
||||||
break;
|
break;
|
||||||
case MAP_OPEN:
|
case MAP_OPEN:
|
||||||
|
@ -179,11 +177,11 @@ namespace gui {
|
||||||
state(State::ATTACKING);
|
state(State::ATTACKING);
|
||||||
break;
|
break;
|
||||||
case ROTATE_LEFT:
|
case ROTATE_LEFT:
|
||||||
$camera.plan_rotate($rayview, 1);
|
$main_ui.plan_rotate(1);
|
||||||
state(State::COMBAT_ROTATE);
|
state(State::COMBAT_ROTATE);
|
||||||
break;
|
break;
|
||||||
case ROTATE_RIGHT:
|
case ROTATE_RIGHT:
|
||||||
$camera.plan_rotate($rayview, -1);
|
$main_ui.plan_rotate(-1);
|
||||||
state(State::COMBAT_ROTATE);
|
state(State::COMBAT_ROTATE);
|
||||||
break;
|
break;
|
||||||
case STOP_COMBAT:
|
case STOP_COMBAT:
|
||||||
|
@ -202,13 +200,13 @@ namespace gui {
|
||||||
void FSM::try_move(int dir, bool strafe) {
|
void FSM::try_move(int dir, bool strafe) {
|
||||||
using enum State;
|
using enum State;
|
||||||
// prevent moving into occupied space
|
// prevent moving into occupied space
|
||||||
Point move_to = $camera.plan_move($rayview, dir, strafe);
|
Point move_to = $main_ui.plan_move(dir, strafe);
|
||||||
|
|
||||||
if($level.map->can_move(move_to) && !$level.collision->occupied(move_to)) {
|
if($level.map->can_move(move_to) && !$level.collision->occupied(move_to)) {
|
||||||
state(MOVING);
|
state(MOVING);
|
||||||
} else {
|
} else {
|
||||||
state(IDLE);
|
state(IDLE);
|
||||||
$camera.abort_plan($rayview);
|
$main_ui.abort_plan();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,7 +260,6 @@ namespace gui {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void FSM::draw_gui() {
|
void FSM::draw_gui() {
|
||||||
$status_ui.draw($window);
|
$status_ui.draw($window);
|
||||||
$combat_ui.draw($window);
|
$combat_ui.draw($window);
|
||||||
|
@ -275,14 +272,7 @@ namespace gui {
|
||||||
$map_ui.render();
|
$map_ui.render();
|
||||||
$renderer.draw($map_ui);
|
$renderer.draw($map_ui);
|
||||||
} else {
|
} else {
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
$rayview.draw($window);
|
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
|
||||||
auto elapsed = std::chrono::duration<double>(end - start);
|
|
||||||
$main_ui.$stats.sample(1/elapsed.count());
|
|
||||||
|
|
||||||
draw_gui();
|
draw_gui();
|
||||||
$main_ui.draw_blood();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$window.display();
|
$window.display();
|
||||||
|
@ -293,17 +283,10 @@ namespace gui {
|
||||||
sf::Vector2f pos = $window.mapPixelToCoords(sf::Mouse::getPosition($window));
|
sf::Vector2f pos = $window.mapPixelToCoords(sf::Mouse::getPosition($window));
|
||||||
$combat_ui.$gui.mouse(pos.x, pos.y);
|
$combat_ui.$gui.mouse(pos.x, pos.y);
|
||||||
$status_ui.$gui.mouse(pos.x, pos.y);
|
$status_ui.$gui.mouse(pos.x, pos.y);
|
||||||
|
$main_ui.mouse(pos.x, pos.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::generate_map() {
|
|
||||||
// ZED: this should eventually go away now that level manager is in play
|
|
||||||
auto& player = $level.world->get_the<Player>();
|
|
||||||
auto& player_position = $level.world->get<Position>(player.entity);
|
|
||||||
$player = player_position.location;
|
|
||||||
$rayview.set_level($level);
|
|
||||||
}
|
|
||||||
|
|
||||||
void FSM::run_systems() {
|
void FSM::run_systems() {
|
||||||
System::enemy_pathing($level);
|
System::enemy_pathing($level);
|
||||||
System::collision($level);
|
System::collision($level);
|
||||||
|
@ -361,8 +344,7 @@ namespace gui {
|
||||||
break;
|
break;
|
||||||
case eGUI::DEATH: {
|
case eGUI::DEATH: {
|
||||||
if(entity != player.entity) {
|
if(entity != player.entity) {
|
||||||
auto &sprite = $level.world->get<Sprite>(entity);
|
$main_ui.dead_entity(entity);
|
||||||
$rayview.update_sprite(entity, sprite);
|
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case eGUI::NOOP:
|
case eGUI::NOOP:
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "raycaster.hpp"
|
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "stats.hpp"
|
#include "stats.hpp"
|
||||||
#include "levelmanager.hpp"
|
#include "levelmanager.hpp"
|
||||||
#include "camera.hpp"
|
|
||||||
#include "fsm.hpp"
|
#include "fsm.hpp"
|
||||||
#include "render.hpp"
|
#include "render.hpp"
|
||||||
#include "map_view.hpp"
|
#include "map_view.hpp"
|
||||||
|
@ -46,7 +44,6 @@ namespace gui {
|
||||||
public:
|
public:
|
||||||
sf::RenderWindow $window;
|
sf::RenderWindow $window;
|
||||||
bool $draw_stats = false;
|
bool $draw_stats = false;
|
||||||
Point $player{0,0};
|
|
||||||
LevelManager $levels;
|
LevelManager $levels;
|
||||||
MainUI $main_ui;
|
MainUI $main_ui;
|
||||||
SFMLRender $renderer;
|
SFMLRender $renderer;
|
||||||
|
@ -54,10 +51,8 @@ namespace gui {
|
||||||
MapViewUI $map_ui;
|
MapViewUI $map_ui;
|
||||||
CombatUI $combat_ui;
|
CombatUI $combat_ui;
|
||||||
StatusUI $status_ui;
|
StatusUI $status_ui;
|
||||||
CameraLOL $camera;
|
|
||||||
sf::Font $font;
|
sf::Font $font;
|
||||||
TexturePack $textures;
|
TexturePack $textures;
|
||||||
Raycaster $rayview;
|
|
||||||
|
|
||||||
FSM();
|
FSM();
|
||||||
|
|
||||||
|
|
54
main_ui.cpp
54
main_ui.cpp
|
@ -8,7 +8,8 @@ namespace gui {
|
||||||
$window(window),
|
$window(window),
|
||||||
$level(level),
|
$level(level),
|
||||||
$textures(textures),
|
$textures(textures),
|
||||||
$overlay_ui($level, $textures)
|
$overlay_ui($level, $textures),
|
||||||
|
$rayview($textures, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)
|
||||||
{
|
{
|
||||||
$window.setVerticalSyncEnabled(VSYNC);
|
$window.setVerticalSyncEnabled(VSYNC);
|
||||||
$window.setFramerateLimit(FRAME_LIMIT);
|
$window.setFramerateLimit(FRAME_LIMIT);
|
||||||
|
@ -61,14 +62,63 @@ namespace gui {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainUI::render() {
|
void MainUI::init() {
|
||||||
|
$rayview.init_shaders();
|
||||||
|
$rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
|
||||||
|
$rayview.position_camera($player.x + 0.5, $player.y + 0.5);
|
||||||
|
|
||||||
$overlay_ui.render();
|
$overlay_ui.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainUI::draw() {
|
void MainUI::draw() {
|
||||||
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
$rayview.draw($window);
|
||||||
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
auto elapsed = std::chrono::duration<double>(end - start);
|
||||||
|
$stats.sample(1/elapsed.count());
|
||||||
|
|
||||||
$overlay_ui.draw($window);
|
$overlay_ui.draw($window);
|
||||||
|
|
||||||
auto debug = $level.world->get_the<Debug>();
|
auto debug = $level.world->get_the<Debug>();
|
||||||
if(debug.FPS) draw_stats();
|
if(debug.FPS) draw_stats();
|
||||||
|
|
||||||
|
draw_blood();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MainUI::play_rotate() {
|
||||||
|
return $camera.play_rotate($rayview);
|
||||||
|
}
|
||||||
|
|
||||||
|
// this could be an optional that returs a Point
|
||||||
|
bool MainUI::play_move() {
|
||||||
|
return $camera.play_move($rayview);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainUI::plan_rotate(int dir) {
|
||||||
|
$camera.plan_rotate($rayview, dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point MainUI::plan_move(int dir, bool strafe) {
|
||||||
|
return $camera.plan_move($rayview, dir, strafe);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainUI::abort_plan() {
|
||||||
|
$camera.abort_plan($rayview);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainUI::generate_map() {
|
||||||
|
auto& player = $level.world->get_the<Player>();
|
||||||
|
auto& player_position = $level.world->get<Position>(player.entity);
|
||||||
|
$player = player_position.location;
|
||||||
|
$rayview.set_level($level);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainUI::dead_entity(DinkyECS::Entity entity) {
|
||||||
|
auto &sprite = $level.world->get<Sprite>(entity);
|
||||||
|
$rayview.update_sprite(entity, sprite);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainUI::mouse(int x, int y) {
|
||||||
|
$overlay_ui.$gui.mouse(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
main_ui.hpp
19
main_ui.hpp
|
@ -3,22 +3,39 @@
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include "stats.hpp"
|
#include "stats.hpp"
|
||||||
#include "overlay_ui.hpp"
|
#include "overlay_ui.hpp"
|
||||||
|
#include "raycaster.hpp"
|
||||||
|
#include "camera.hpp"
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
|
|
||||||
class MainUI {
|
class MainUI {
|
||||||
public:
|
public:
|
||||||
|
Point $player{0,0};
|
||||||
Stats $stats;
|
Stats $stats;
|
||||||
sf::RenderWindow& $window;
|
sf::RenderWindow& $window;
|
||||||
GameLevel $level;
|
GameLevel $level;
|
||||||
TexturePack& $textures;
|
TexturePack& $textures;
|
||||||
OverlayUI $overlay_ui;
|
OverlayUI $overlay_ui;
|
||||||
|
Raycaster $rayview;
|
||||||
|
CameraLOL $camera;
|
||||||
|
|
||||||
MainUI(sf::RenderWindow& window, GameLevel level, TexturePack &textures);
|
MainUI(sf::RenderWindow& window, GameLevel level, TexturePack &textures);
|
||||||
|
|
||||||
|
void mouse(int x, int y);
|
||||||
void debug();
|
void debug();
|
||||||
void draw_stats();
|
void draw_stats();
|
||||||
void draw_blood();
|
void draw_blood();
|
||||||
void render();
|
|
||||||
|
bool play_move();
|
||||||
|
void plan_rotate(int dir);
|
||||||
|
bool play_rotate();
|
||||||
|
Point plan_move(int dir, bool strafe);
|
||||||
|
void abort_plan();
|
||||||
|
|
||||||
|
void init();
|
||||||
void draw();
|
void draw();
|
||||||
|
|
||||||
|
void generate_map();
|
||||||
|
void dead_entity(DinkyECS::Entity entity);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue