@@ -68,6 +68,7 @@ pub enum RunState {
6868 KeyBindingsMenu ,
6969 SaveGame ,
7070 NextLevel ,
71+ GameOver ,
7172}
7273
7374pub struct State {
@@ -230,15 +231,25 @@ impl GameState for State {
230231 * runstate
231232 } ;
232233
234+ // FIXME: appears runstate is being possibly set to AwaitingInput before
235+ // going into `tick`
236+ /*
237+ tick runstate: PlayerTurn
238+ tick runstate: MonsterTurn
239+ tick runstate: AwaitingInput
240+ new runstate: GameOver
241+ tick runstate: AwaitingInput
242+ tick runstate: AwaitingInput
243+ */
233244 ctx. cls ( ) ;
234245
235- match newrunstate {
236- RunState :: MainMenu { .. } => { }
246+ newrunstate = match newrunstate {
247+ RunState :: MainMenu { .. } => newrunstate ,
237248 _ => {
238249 draw_map ( & self . ecs , ctx) ;
239250 draw_ui ( & self . ecs , ctx, & self . display ) ;
240251
241- delete_the_dead ( & mut self . ecs ) ;
252+ let game_over_opt = delete_the_dead ( & mut self . ecs ) ;
242253
243254 {
244255 // draw renderables
@@ -254,9 +265,9 @@ impl GameState for State {
254265 ctx. set ( pos. xx , pos. yy , render. fg , render. bg , render. glyph ) ;
255266 } ) ;
256267 }
268+ game_over_opt. unwrap_or ( newrunstate)
257269 }
258- }
259-
270+ } ;
260271 match newrunstate {
261272 RunState :: PreRun => {
262273 self . run_systems ( ) ;
@@ -362,6 +373,7 @@ impl GameState for State {
362373 }
363374 gui:: MainMenuStatus :: Selected => match result. highlighted {
364375 gui:: MainMenuSelection :: NewGame => {
376+ delete_state ( & mut self . ecs ) ;
365377 ( * self , _) = init_state ( false , Some ( ctx) ) ;
366378 newrunstate = RunState :: PreRun
367379 }
@@ -404,12 +416,33 @@ impl GameState for State {
404416 self . goto_next_level ( ) ;
405417 newrunstate = RunState :: PreRun ;
406418 }
419+ RunState :: GameOver => {
420+ let result = gui:: game_over ( ctx) ;
421+ match result {
422+ gui:: GameOverResult :: NoSelection => { }
423+ gui:: GameOverResult :: QuitToMenu => {
424+ delete_state ( & mut self . ecs ) ;
425+ newrunstate = RunState :: MainMenu {
426+ menu_selection : gui:: MainMenuSelection :: NewGame ,
427+ }
428+ }
429+ }
430+ }
407431 }
408432 let mut runstate = self . ecs . fetch_mut :: < RunState > ( ) ;
409433 * runstate = newrunstate;
410434 }
411435}
412436
437+ pub fn delete_state ( ecs : & mut World ) {
438+ // Delete everything
439+ let to_delete: Vec < Entity > = ecs. entities ( ) . join ( ) . collect ( ) ;
440+ to_delete. iter ( ) . for_each ( |entity| {
441+ ecs. delete_entity ( * entity)
442+ . unwrap_or_else ( |er| panic ! ( "Unable to delete entity with id {}: {}" , entity. id( ) , er) )
443+ } ) ;
444+ }
445+
413446pub fn init_state ( test_ecs : bool , ctxt_opt : Option < & BTerm > ) -> ( State , Option < BTerm > ) {
414447 let ( mut gs, opt_ctxt) = if test_ecs {
415448 (
0 commit comments