diff --git a/camera.cpp b/camera.cpp index 91680db..d616351 100644 --- a/camera.cpp +++ b/camera.cpp @@ -43,7 +43,7 @@ bool CameraLOL::play_move(Raycaster &rayview) { return t >= 1.0; } -void CameraLOL::abort_plan(Raycaster &rayview) { +void CameraLOL::abort_plan(Raycaster& rayview) { target_x = rayview.$pos_x; target_y = rayview.$pos_y; } diff --git a/camera.hpp b/camera.hpp index 79f9460..445324b 100644 --- a/camera.hpp +++ b/camera.hpp @@ -18,5 +18,5 @@ struct CameraLOL { bool play_rotate(Raycaster &rayview); bool play_move(Raycaster &rayview); - void abort_plan(Raycaster &rayview); + void abort_plan(Raycaster& rayview); }; diff --git a/gui_fsm.cpp b/gui_fsm.cpp index c3bcdbf..64cb45e 100644 --- a/gui_fsm.cpp +++ b/gui_fsm.cpp @@ -19,8 +19,7 @@ namespace gui { $map_ui($level), $combat_ui($level), $status_ui($level), - $font{FONT_FILE_NAME}, - $rayview($textures, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT) + $font{FONT_FILE_NAME} { $textures.load_tiles(); $textures.load_sprites(); @@ -41,13 +40,11 @@ namespace gui { } void FSM::START(Event ) { - generate_map(); + $main_ui.generate_map(); $level.world->set_the({}); - $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); $status_ui.render($textures); $status_ui.log("Welcome to the game!"); @@ -79,8 +76,9 @@ namespace gui { } void FSM::MOVING(Event ) { - if($camera.play_move($rayview)) { - System::plan_motion(*$level.world, {size_t($camera.target_x), size_t($camera.target_y)}); + // this should be an optional that returns a point + 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(); state(State::IDLE); } @@ -107,13 +105,13 @@ namespace gui { } void FSM::ROTATING(Event ) { - if($camera.play_rotate($rayview)) { + if($main_ui.play_rotate()) { state(State::IDLE); } } void FSM::COMBAT_ROTATE(Event ) { - if($camera.play_rotate($rayview)) { + if($main_ui.play_rotate()) { state(State::IN_COMBAT); } } @@ -139,11 +137,11 @@ namespace gui { try_move(-1, true); break; case ROTATE_LEFT: - $camera.plan_rotate($rayview, 1); + $main_ui.plan_rotate(1); state(State::ROTATING); break; case ROTATE_RIGHT: - $camera.plan_rotate($rayview, -1); + $main_ui.plan_rotate(-1); state(State::ROTATING); break; case MAP_OPEN: @@ -179,11 +177,11 @@ namespace gui { state(State::ATTACKING); break; case ROTATE_LEFT: - $camera.plan_rotate($rayview, 1); + $main_ui.plan_rotate(1); state(State::COMBAT_ROTATE); break; case ROTATE_RIGHT: - $camera.plan_rotate($rayview, -1); + $main_ui.plan_rotate(-1); state(State::COMBAT_ROTATE); break; case STOP_COMBAT: @@ -202,13 +200,13 @@ namespace gui { void FSM::try_move(int dir, bool strafe) { using enum State; // 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)) { state(MOVING); } else { state(IDLE); - $camera.abort_plan($rayview); + $main_ui.abort_plan(); } } @@ -262,7 +260,6 @@ namespace gui { } } - void FSM::draw_gui() { $status_ui.draw($window); $combat_ui.draw($window); @@ -275,14 +272,7 @@ namespace gui { $map_ui.render(); $renderer.draw($map_ui); } 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(end - start); - $main_ui.$stats.sample(1/elapsed.count()); - draw_gui(); - $main_ui.draw_blood(); } $window.display(); @@ -293,17 +283,10 @@ namespace gui { sf::Vector2f pos = $window.mapPixelToCoords(sf::Mouse::getPosition($window)); $combat_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(); - auto& player_position = $level.world->get(player.entity); - $player = player_position.location; - $rayview.set_level($level); - } - void FSM::run_systems() { System::enemy_pathing($level); System::collision($level); @@ -361,8 +344,7 @@ namespace gui { break; case eGUI::DEATH: { if(entity != player.entity) { - auto &sprite = $level.world->get(entity); - $rayview.update_sprite(entity, sprite); + $main_ui.dead_entity(entity); } } break; case eGUI::NOOP: diff --git a/gui_fsm.hpp b/gui_fsm.hpp index 2f92394..051d2bf 100644 --- a/gui_fsm.hpp +++ b/gui_fsm.hpp @@ -1,9 +1,7 @@ #pragma once -#include "raycaster.hpp" #include "constants.hpp" #include "stats.hpp" #include "levelmanager.hpp" -#include "camera.hpp" #include "fsm.hpp" #include "render.hpp" #include "map_view.hpp" @@ -46,7 +44,6 @@ namespace gui { public: sf::RenderWindow $window; bool $draw_stats = false; - Point $player{0,0}; LevelManager $levels; MainUI $main_ui; SFMLRender $renderer; @@ -54,10 +51,8 @@ namespace gui { MapViewUI $map_ui; CombatUI $combat_ui; StatusUI $status_ui; - CameraLOL $camera; sf::Font $font; TexturePack $textures; - Raycaster $rayview; FSM(); diff --git a/main_ui.cpp b/main_ui.cpp index 5a8ceef..c247248 100644 --- a/main_ui.cpp +++ b/main_ui.cpp @@ -8,7 +8,8 @@ namespace gui { $window(window), $level(level), $textures(textures), - $overlay_ui($level, $textures) + $overlay_ui($level, $textures), + $rayview($textures, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT) { $window.setVerticalSyncEnabled(VSYNC); $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(); } 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(end - start); + $stats.sample(1/elapsed.count()); + $overlay_ui.draw($window); auto debug = $level.world->get_the(); 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(); + auto& player_position = $level.world->get(player.entity); + $player = player_position.location; + $rayview.set_level($level); + } + + void MainUI::dead_entity(DinkyECS::Entity entity) { + auto &sprite = $level.world->get(entity); + $rayview.update_sprite(entity, sprite); + } + + void MainUI::mouse(int x, int y) { + $overlay_ui.$gui.mouse(x, y); } } diff --git a/main_ui.hpp b/main_ui.hpp index 970973d..347c94c 100644 --- a/main_ui.hpp +++ b/main_ui.hpp @@ -3,22 +3,39 @@ #include #include "stats.hpp" #include "overlay_ui.hpp" +#include "raycaster.hpp" +#include "camera.hpp" namespace gui { class MainUI { public: + Point $player{0,0}; Stats $stats; sf::RenderWindow& $window; GameLevel $level; TexturePack& $textures; OverlayUI $overlay_ui; + Raycaster $rayview; + CameraLOL $camera; MainUI(sf::RenderWindow& window, GameLevel level, TexturePack &textures); + + void mouse(int x, int y); void debug(); void draw_stats(); 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 generate_map(); + void dead_entity(DinkyECS::Entity entity); }; }