<commit_before>#include "game.hpp"

#include <fstream>
#include <sstream>

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

#include "../../messageBus/messageBus.hpp"

//Global Function
void initGui(aw::GuiController &gui);

namespace aw
{
	Game::Game(StateMachine &statemachine, MessageBus &messageBus):
		m_active(false),
		State(statemachine), 
		m_messageBus(messageBus),
		m_gameState(GameState::STOPPED)
	{
		m_messageBus.addReceiver(this);
		m_gui.loadFont("data/fonts/visitor1.ttf");

		initGui(m_gui); //Global Function
	}

	void Game::update(const sf::Time &frameTime)
	{
		m_active = true;

		if (m_gameState == GameState::RUNNING)
		{
			//check for collision or finish
			auto result = m_collisionSystem.checkCollision(m_player);
			if (result == CollisionType::WALL)
			{
				m_gameState = GameState::CRASHED;
				m_gui.setActiveLayer(1);
				return;
			}
			else if (result == CollisionType::FINISH)
			{
				m_gameState = GameState::FINISHED;
				m_gui.setActiveLayer(2);
				return;
			}

			//Check for Scriptactions
			m_scriptManager.update(m_player, m_camera);

			//Update the position later, so the user see the crash
			//Update player position
			m_player.upate(frameTime);

			//update the camera position
			m_camera.update(m_player.getPosition());
		}
	}

	void Game::render(sf::RenderWindow &window)
	{
		//Set game view
		window.setView(m_camera.getGameView());
		//Render the map
		m_mapRenderer.render(window);
		//Player
		m_player.render(window);
		//Set gui view
		window.setView(m_camera.getDefaultView());

		//Draw different screens
		if (m_gameState != GameState::RUNNING)
		{
			sf::RectangleShape overlay(sf::Vector2f(800, 450));
			overlay.setFillColor(sf::Color(0, 0, 0, 180));
			window.draw(overlay);
			m_gui.render(window);
		}
	}

	void Game::receiveMessage(const Message &msg)
	{
		//Loading the map
		if (msg.ID == std::hash<std::string>()("start game"))
		{
			//Set the name of the level
			m_levelName = *msg.getValue<std::string>(0);
			//Set the gametype
			if (*msg.getValue<std::string>(1) == "official arcade")
			{
				m_gameType = GameType::OFFICIAL_ARCADE;
			}
			//Load the level
			loadLevel();
		}
		//Starting the game
		if (msg.ID == std::hash<std::string>()("event") && m_active)
		{
			sf::Event event = *msg.getValue<sf::Event>(0);

			//Give the event to gui
			m_gui.handleEvent(event);

			//Check for respawn etc.
			if (event.type == sf::Event::KeyReleased)
			{
				if (event.key.code == sf::Keyboard::Return)
				{
					if (m_gameState == GameState::STOPPED || m_gameState == GameState::CRASHED)
					{
						m_gameState = GameState::RUNNING;
						resetToLastCheckpoint();
					}
					else if (m_gameState == GameState::FINISHED)
					{
						if (m_gui.getSelectedElement()->getID() == "next")
						{
							//To inform the settings... unlock new/save progress + start next level
							sendInformationLevelFinished(true);
						}
						else if (m_gui.getSelectedElement()->getID() == "replay")
						{
							resetToStart();
							m_gameState = GameState::STOPPED;
							m_gui.setActiveLayer(0);
							//To inform the settings... unlock new/save progress
							sendInformationLevelFinished(false);
						}
						else if (m_gui.getSelectedElement()->getID() == "back")
						{
							changeActiveState("menu");
							m_active = false;
							//To inform the settings... unlock new/save progress
							sendInformationLevelFinished(false);
						}
					}
				}
				else if (event.key.code == sf::Keyboard::BackSpace)
				{
					if (m_gameState == GameState::CRASHED)
					{
						m_gameState = GameState::RUNNING;
						resetToStart();
					}
				}
			}
		}
	}


	////////// PRIVATE FUNCS ////////////

	void Game::loadLevel()
	{
		//Reset all members first
		m_gameState = GameState::STOPPED;
		m_mapRenderer = MapRenderer();
		m_collisionSystem = CollisionSystem();
		m_player = Player();
		m_camera = Camera();<commit_msg>Escape to return to menu<commit_after>#include "game.hpp"

#include <fstream>
#include <sstream>

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

#include "../../messageBus/messageBus.hpp"

//Global Function
void initGui(aw::GuiController &gui);

namespace aw
{
	Game::Game(StateMachine &statemachine, MessageBus &messageBus):
		m_active(false),
		State(statemachine), 
		m_messageBus(messageBus),
		m_gameState(GameState::STOPPED)
	{
		m_messageBus.addReceiver(this);
		m_gui.loadFont("data/fonts/visitor1.ttf");

		initGui(m_gui); //Global Function
	}

	void Game::update(const sf::Time &frameTime)
	{
		m_active = true;

		if (m_gameState == GameState::RUNNING)
		{
			//check for collision or finish
			auto result = m_collisionSystem.checkCollision(m_player);
			if (result == CollisionType::WALL)
			{
				m_gameState = GameState::CRASHED;
				m_gui.setActiveLayer(1);
				return;
			}
			else if (result == CollisionType::FINISH)
			{
				m_gameState = GameState::FINISHED;
				m_gui.setActiveLayer(2);
				return;
			}

			//Check for Scriptactions
			m_scriptManager.update(m_player, m_camera);

			//Update the position later, so the user see the crash
			//Update player position
			m_player.upate(frameTime);

			//update the camera position
			m_camera.update(m_player.getPosition());
		}
	}

	void Game::render(sf::RenderWindow &window)
	{
		//Set game view
		window.setView(m_camera.getGameView());
		//Render the map
		m_mapRenderer.render(window);
		//Player
		m_player.render(window);
		//Set gui view
		window.setView(m_camera.getDefaultView());

		//Draw different screens
		if (m_gameState != GameState::RUNNING)
		{
			sf::RectangleShape overlay(sf::Vector2f(800, 450));
			overlay.setFillColor(sf::Color(0, 0, 0, 180));
			window.draw(overlay);
			m_gui.render(window);
		}
	}

	void Game::receiveMessage(const Message &msg)
	{
		//Loading the map
		if (msg.ID == std::hash<std::string>()("start game"))
		{
			//Set the name of the level
			m_levelName = *msg.getValue<std::string>(0);
			//Set the gametype
			if (*msg.getValue<std::string>(1) == "official arcade")
			{
				m_gameType = GameType::OFFICIAL_ARCADE;
			}
			//Load the level
			loadLevel();
		}
		//Starting the game
		if (msg.ID == std::hash<std::string>()("event") && m_active)
		{
			sf::Event event = *msg.getValue<sf::Event>(0);

			//Give the event to gui
			m_gui.handleEvent(event);

			//Check for respawn etc.
			if (event.type == sf::Event::KeyReleased)
			{
				if (event.key.code == sf::Keyboard::Return)
				{
					if (m_gameState == GameState::STOPPED || m_gameState == GameState::CRASHED)
					{
						m_gameState = GameState::RUNNING;
						resetToLastCheckpoint();
					}
					else if (m_gameState == GameState::FINISHED)
					{
						if (m_gui.getSelectedElement()->getID() == "next")
						{
							//To inform the settings... unlock new/save progress + start next level
							sendInformationLevelFinished(true);
						}
						else if (m_gui.getSelectedElement()->getID() == "replay")
						{
							resetToStart();
							m_gameState = GameState::STOPPED;
							m_gui.setActiveLayer(0);
							//To inform the settings... unlock new/save progress
							sendInformationLevelFinished(false);
						}
						else if (m_gui.getSelectedElement()->getID() == "back")
						{
							changeActiveState("menu");
							m_active = false;
							//To inform the settings... unlock new/save progress
							sendInformationLevelFinished(false);
						}
					}
				}
				else if (event.key.code == sf::Keyboard::BackSpace)
				{
					if (m_gameState == GameState::CRASHED)
					{
						m_gameState = GameState::RUNNING;
						resetToStart();
					}
				}
				else if (event.key.code == sf::Keyboard::Escape)
				{
					changeActiveState("menu");
					m_active = false;
				}
			}
		}
	}


	////////// PRIVATE FUNCS ////////////

	void Game::loadLevel()
	{
		//Reset all members first
		m_gameState = GameState::STOPPED;
		m_mapRenderer = MapRenderer();
		m_collisionSystem = CollisionSystem();
		m_player = Player();
		m_camera = Camera();
{'fraction_non_alphanumeric': 0.1471066116735408, 'fraction_numerical': 0.004999375078115236, 'mean_word_length': 3.167708333333333, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1760790', 'n_tokens_mistral': 3393, 'n_tokens_neox': 2690, 'n_words': 536}