@ -23,6 +23,7 @@
# include "components.hpp"
# include "components.hpp"
# include "systems.hpp"
# include "systems.hpp"
# include "collider.hpp"
# include "collider.hpp"
# include "events.hpp"
using std : : string ;
using std : : string ;
using namespace fmt ;
using namespace fmt ;
@ -109,12 +110,45 @@ void GUI::create_renderer() {
bool GUI : : handle_events ( ) {
bool GUI : : handle_events ( ) {
sf : : Event event ;
sf : : Event event ;
bool event_happened = false ;
bool event_happened = false ;
auto & log = $ world . get_the < ActionLog > ( ) ;
auto player = $ world . get_the < Player > ( ) ;
auto sounds = $ world . get_the < SoundManager > ( ) ;
while ( $ world . has_event < GUIEvent > ( ) ) {
auto [ evt , entity ] = $ world . recv < GUIEvent > ( ) ;
switch ( evt ) {
case GUIEvent : : HIT : {
auto combat = $ world . get < Combat > ( entity ) ;
if ( entity = = player . entity ) {
log . log ( format ( " Enemy HIT YOU, you have {} HP! " , combat . hp ) ) ;
sounds . play ( " hit " ) ;
shake ( ) ;
} else {
log . log ( format ( " You HIT enemy, they have {} HP! " , combat . hp ) ) ;
sounds . play ( " hit " ) ;
shake ( ) ;
}
} break ;
case GUIEvent : : MISS :
if ( entity = = player . entity ) {
log . log ( " You MISSED the enemy. " ) ;
} else {
log . log ( " Enemy MISSED YOU. " ) ;
}
break ;
case GUIEvent : : DEAD :
log . log ( " --- ENEMY DEAD! " ) ;
break ;
default :
log . log ( format ( " INVALID EVENT! {},{} " , evt , entity ) ) ;
}
}
while ( $ window . pollEvent ( event ) ) {
while ( $ window . pollEvent ( event ) ) {
if ( event . type = = sf : : Event : : Closed ) {
if ( event . type = = sf : : Event : : Closed ) {
$ window . close ( ) ;
$ window . close ( ) ;
} else if ( event . type = = sf : : Event : : KeyPressed ) {
} else if ( event . type = = sf : : Event : : KeyPressed ) {
auto player = $ world . get_the < Player > ( ) ;
auto & player_motion = $ world . get < Motion > ( player . entity ) ;
auto & player_motion = $ world . get < Motion > ( player . entity ) ;
if ( sf : : Keyboard : : isKeyPressed ( sf : : Keyboard : : Left ) ) {
if ( sf : : Keyboard : : isKeyPressed ( sf : : Keyboard : : Left ) ) {
@ -211,7 +245,7 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
continue ; // skip these, just windows junk
continue ; // skip these, just windows junk
} else {
} else {
// it's a visual cell
// it's a visual cell
bg_sprite . setPosition ( { x , y } ) ;
bg_sprite . setPosition ( { x + map_off_x , y + map_off_ y} ) ;
sf : : Sprite & sprite = get_text_sprite ( tile ) ;
sf : : Sprite & sprite = get_text_sprite ( tile ) ;
// should look into caching all this instead of calcing it each time
// should look into caching all this instead of calcing it each time
@ -222,7 +256,7 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
auto height_delta = bg_bounds . height > sp_bounds . width ? ( bg_bounds . height - sp_bounds . height ) / 2 : 0 ;
auto height_delta = bg_bounds . height > sp_bounds . width ? ( bg_bounds . height - sp_bounds . height ) / 2 : 0 ;
// TODO: need to center it inside the bg_sprite
// TODO: need to center it inside the bg_sprite
sprite . setPosition ( { x + width_delta , y + height_delta } ) ;
sprite . setPosition ( { x + width_delta + map_off_x , y + height_delta + map_off_y } ) ;
// get the entity combat and make them light gray if dead
// get the entity combat and make them light gray if dead
if ( tile = = L ' █ ' ) {
if ( tile = = L ' █ ' ) {
@ -259,8 +293,8 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
void GUI : : shake ( ) {
void GUI : : shake ( ) {
for ( int i = 0 ; i < 10 ; + + i ) {
for ( int i = 0 ; i < 10 ; + + i ) {
int x = Random : : uniform < int > ( - 10 , 1 0) ;
int x = Random : : uniform < int > ( - 20 , 2 0) ;
int y = Random : : uniform < int > ( - 10 , 1 0) ;
int y = Random : : uniform < int > ( - 20 , 2 0) ;
// add x/y back to draw screen
// add x/y back to draw screen
draw_screen ( true , x , y ) ;
draw_screen ( true , x , y ) ;
std : : this_thread : : sleep_for ( 1 ms ) ;
std : : this_thread : : sleep_for ( 1 ms ) ;
@ -271,6 +305,7 @@ void GUI::configure_world() {
SoundManager sounds ( " ./assets " ) ;
SoundManager sounds ( " ./assets " ) ;
sounds . load ( " hit " , " hit.wav " ) ;
sounds . load ( " hit " , " hit.wav " ) ;
$ world . set_the < SoundManager > ( sounds ) ;
$ world . set_the < SoundManager > ( sounds ) ;
$ world . set_the < GUIEvent > ( GUIEvent : : START ) ;
dbc : : check ( $ game_map . room_count ( ) > 1 , " not enough rooms in map. " ) ;
dbc : : check ( $ game_map . room_count ( ) > 1 , " not enough rooms in map. " ) ;
// configure a player as a fact of the world
// configure a player as a fact of the world