From 9e6f29eabd646eaf16b47ec3da735748b86c01e7 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sun, 4 Sep 2022 00:21:59 +0100
Subject: [PATCH] add victory condition

---
 demo/demo/grid.cpp           | 22 ++++++++++++++++++----
 demo/include/grid.hpp        |  1 +
 include/fggl/scenes/game.hpp |  5 ++++-
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/demo/demo/grid.cpp b/demo/demo/grid.cpp
index d3e659f..cd23691 100644
--- a/demo/demo/grid.cpp
+++ b/demo/demo/grid.cpp
@@ -28,10 +28,9 @@ namespace demo {
 	using namespace fggl::entity::grid;
 
 	static void build_tileset(TileSet& tiles) {
-		fggl::entity::grid::FloorTile empty{fggl::entity::grid::FloorTile::IMPOSSIBLE, BLACK};
-		fggl::entity::grid::FloorTile ground{1, GREEN};
-		tiles.m_floors.push_back(empty);
-		tiles.m_floors.push_back(ground);
+		tiles.m_floors.push_back(FloorTile{FloorTile::IMPOSSIBLE, BLACK});
+		tiles.m_floors.push_back(FloorTile{1, GREEN});
+		tiles.m_floors.push_back(FloorTile{1, YELLOW_GREEN});
 
 		fggl::entity::grid::WallTile noWall{};
 		tiles.m_walls.push_back(noWall);
@@ -82,6 +81,9 @@ namespace demo {
 		build_doorway(area, {10, 5}, false, 1);
 		build_doorway(area, {13, 5}, false, 1);
 
+		// set goal
+		area->setFloorAt(17, 5, 2);
+
 		build_room(area, {25, 5}, {3,3});
 
 		// player
@@ -273,6 +275,18 @@ namespace demo {
 			m_program.playing = false;
 			m_program.m_currInstruction = 0;
 			m_program.m_instructions.clear();
+			checkVictory();
+		}
+	}
+
+	void GridScene::checkVictory() {
+		if ( !m_program.playing ) {
+			auto& botPos = m_grid->entities().get<CellPos>(m_player).pos;
+			auto gridCell = m_grid->floorAt(botPos.x, botPos.y);
+			if ( gridCell.colour == YELLOW_GREEN ) {
+				// a winner is you!
+				returnToMenu();
+			}
 		}
 	}
 
diff --git a/demo/include/grid.hpp b/demo/include/grid.hpp
index 99d6b11..91ffab3 100644
--- a/demo/include/grid.hpp
+++ b/demo/include/grid.hpp
@@ -68,6 +68,7 @@ namespace demo {
 			Program m_program;
 
 			void tickPlayer();
+			void checkVictory();
 
 			inline void forward() {
 				auto& cell = m_grid->entities().get<CellPos>(m_player);
diff --git a/include/fggl/scenes/game.hpp b/include/fggl/scenes/game.hpp
index 71a129f..1a451d3 100644
--- a/include/fggl/scenes/game.hpp
+++ b/include/fggl/scenes/game.hpp
@@ -34,11 +34,14 @@ namespace fggl::scenes {
 			void render(fggl::gfx::Graphics &gfx) override = 0;
 
 		protected:
-
 			inline auto input() -> input::Input & {
 				return *m_input;
 			}
 
+			inline void returnToMenu() {
+				m_owner.change_state(m_previous);
+			}
+
 		private:
 			input::Input *m_input;
 			std::string m_previous = "menu";
-- 
GitLab