Example #1
0
		void MainMenuState::cleanup(GameBase &game)
		{
			delete _menu;
			_menu = nullptr;
			delete _advertismentList;
			_advertismentList = nullptr;

			game.getEngine()->getDrawEngine()->unloadAll();
			game.getEngine()->getInputEngine()->clearBindings();
		}
Example #2
0
		void GameOverState::redraw(GameBase &game) {
			game.getEngine()->getDrawEngine()->prepareForDraw();
			game.getEngine()->getDrawEngine()->draw("gameoverbackground");
			game.getEngine()->getDrawEngine()->draw("winner", 190, 190);

			for (int i = 1; i <= _characterCount; i++) {
				game.getEngine()->getDrawEngine()->drawText("rank" + std::to_string(i), 717, 280 + (i * 75));
			}

			if (_isLoaded)
				_menu->draw(&game);

			game.getEngine()->getDrawEngine()->render();
		}
Example #3
0
		void MainMenuState::draw(GameBase &game, GameTime &gameTime)
		{
			game.getEngine()->getDrawEngine()->prepareForDraw();
			game.getEngine()->getDrawEngine()->draw("mainmenu_background");

			game.getEngine()->getDrawEngine()->drawText("maintitle", 50, 70);

			//game.getEngine()->getDrawEngine()->draw("menu_play", 585, 243);
			game.getEngine()->getDrawEngine()->draw("advertisement", _advertisementX, _advertisementY);

			if (_advertisementIndex >= 0)
				//game.getEngine()->getDrawEngine()->draw("advertisement", _advertisementX, _advertisementY);

			_menu->draw(&game);
			game.getEngine()->getDrawEngine()->render();
		}
Example #4
0
		void MainMenuState::handleEvents(GameBase &game, GameTime &gameTime)
		{
			SDL_Event event;

			while (SDL_PollEvent(&event))
			{
				game.getEngine()->getInputEngine()->handleEvent(event);

				switch (event.type) {
				case SDLK_ESCAPE:
					game.stop();
					break;
				case SDL_CONTROLLERBUTTONDOWN:
					switch (event.cbutton.button)
					{
					case SDL_CONTROLLER_BUTTON_A:
						_menu->doAction();
						break;
					case SDL_CONTROLLER_BUTTON_B:
						game.stop();
					case SDL_CONTROLLER_BUTTON_DPAD_UP:
						_menu->selectPrevious();
						break;
					case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
						_menu->selectNext();
						break;
					}
					break;
				case SDL_KEYDOWN:
					switch (event.key.keysym.sym)
					{
					case SDLK_DOWN:
					case 1:
						if (_menu)
							_menu->selectNext();
						//game.getEngine()->getAudioEngine()->play("menu_switch_effect", 0);
						break;
					case SDLK_UP:
					case 0:
						if (_menu)
							_menu->selectPrevious();
						//game.getEngine()->getAudioEngine()->play("menu_switch_effect", 0);
						break;
					case SDLK_KP_ENTER:
					case SDLK_RETURN:
					case 10:
						if (_menu)
							_menu->doAction();
						break;
					}
				}
			}
		}
Example #5
0
		void CreateLevelState::draw(GameBase &game, GameTime &gameTime) {

			game.getEngine()->getDrawEngine()->prepareForDraw();
			game.getEngine()->getDrawEngine()->draw("create_background");
			game.getEngine()->getDrawEngine()->drawText("createtitle", 50, 75);

			int x = (game.getEngine()->getDrawEngine()->getWindowWidth() - 600) / 2;
			game.getEngine()->getDrawEngine()->drawRectangle(Rectangle(x, 250, 600, 80), 102, 175, 207);
			game.getEngine()->getDrawEngine()->drawDynamicText("input", *_name, x + 20, 260);

			if (!_error->empty()) {
				auto size = game.getEngine()->getDrawEngine()->getTextSize(*_error);
				int errX = (game.getEngine()->getDrawEngine()->getWindowWidth() - size[0]) / 2;

				game.getEngine()->getDrawEngine()->drawText(*_error, errX, 210);
			}

			_menu->draw(&game);

			game.getEngine()->getDrawEngine()->render();

		}
Example #6
0
		void GameOverState::handleEvents(GameBase &game, GameTime &gameTime)
		{
			SDL_Event event;

			while (SDL_PollEvent(&event))
			{
				game.getEngine()->getInputEngine()->getMouse().handleMouseEvent(event);

				if (event.type == SDL_KEYDOWN)
				{
					switch (event.key.keysym.sym)
					{
					case SDLK_DOWN:
						if (_isLoaded)
							_menu->selectNext();
						break;
					case SDLK_UP:
						if (_isLoaded)
							_menu->selectPrevious();
						break;
					case SDLK_KP_ENTER:
					case SDLK_RETURN:
						if (_isLoaded)
							_menu->doAction();
						break;
					}
				}
				else if (event.type == SDL_CONTROLLERBUTTONDOWN)
				{
					switch (event.cbutton.button)
					{
					case SDL_CONTROLLER_BUTTON_A:
						if (_isLoaded)
							_menu->doAction();
						break;
					case SDL_CONTROLLER_BUTTON_DPAD_UP:
						if (_isLoaded)
							_menu->selectPrevious();
						break;
					case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
						if (_isLoaded)
							_menu->selectNext();
						break;
					}
				}
			}
		}
Example #7
0
		void MainMenuState::update(GameBase &game, GameTime &gameTime)
		{
			game.getEngine()->getInputEngine()->update(game);

			if (_advertisementIndex >= 0.0f && _shouldRefreshAdvertisement)
			{
				int timeSinceLastUpdate = game.getGameTime()->getElapsedSinceLastUpdate() / 1000.0f;
				if (timeSinceLastUpdate >= 0.0f)
				{
					if ((_lastTimeSinceAdvertisementChange += timeSinceLastUpdate) >= _advertisementRefreshRate)
					{
						loadAdvertisement();
						_lastTimeSinceAdvertisementChange = 0.0f;
					}
				}
			}
		}
Example #8
0
		void CreateLevelState::handleEvents(GameBase &game, GameTime &gameTime) {

			SDL_Event event;

			while (SDL_PollEvent(&event))
			{
				game.getEngine()->getInputEngine()->handleEvent(event);

				if (event.type == SDL_KEYDOWN)
				{
					switch (event.key.keysym.sym) {
					case SDLK_BACKSPACE:
						if (_name->size() > 0)
							_name->pop_back();
						break;
					case SDLK_SPACE:
						*_name += " ";
						break;
					case SDLK_DOWN:
						_menu->selectNext();
						break;
					case SDLK_UP:
						_menu->selectPrevious();
						break;
					case SDLK_RETURN:
						_menu->doAction();
						break;
					default:
						if (_name->size() <= 18) {
							
							std::string input = SDL_GetKeyName(event.key.keysym.sym);
							if (input.size() == 1 && input[0] >= 'A' && input[0] <= 'Z') {
								*_name += _name->size() == 0 ? input[0] : tolower(input[0]);
							}

						}
						break;
					}
				}
			}

		}
Example #9
0
		void CreateLevelState::init(GameBase &game) {
			_name = new std::string();
			_game = &game;
			_error = new std::string();

			game.getEngine()->getDrawEngine()->loadText("createtitle", "Enter a name for your new level", { 255, 255, 255 }, "trebucbd", 48);
			game.getEngine()->getDrawEngine()->loadText("existserr", "There already exists a level with this name.", { 217, 13, 13 }, "trebucbd", 20);
			game.getEngine()->getDrawEngine()->loadText("invaliderr", "Enter a name for your new level.", { 217, 13, 13 }, "trebucbd", 20);
			game.getEngine()->getDrawEngine()->loadDynamicText("input", { 255, 255, 255 }, "trebucbd", 48);
			game.getEngine()->getDrawEngine()->load("create_background", "assets/screens/mainmenu");

			//std::function<void(MenuItem *item)> callBack = &MainMenuState::menuAction;
			_menu = new Menu(453, 360, game);

			_menu->addMenuTextItem("Create", (std::function<void()>)std::bind(&CreateLevelState::create, this));
			_menu->addMenuTextItem("Cancel", (std::function<void()>)[&] { 
				_game->getStateManager()->pushState(LevelSelectionState::getInstance()); 
			});

			game.getEngine()->getInputEngine()->setMouseEnabled();
		}
Example #10
0
		void MainMenuState::init(GameBase &game)
		{
			_game = &game;
			
			_game->getEngine()->getDrawEngine()->loadText("maintitle", "Main menu", { 255, 255, 255 }, "trebucbd", 48);

			//std::function<void(MenuItem *item)> callBack = &MainMenuState::menuAction;
			_menu = new Menu(50, 250, game);

			_menu->addMenuTextItem("Play", (std::function<void()>)[&] { changeState(*_game, GameModeState::getInstance()); });
			_menu->addMenuTextItem("Level editor", (std::function<void()>)[&] { 
				_game->setGameMode(GameBase::GameMode::Edit);
				_game->getStateManager()->changeState(LevelSelectionState::getInstance()); 
			});
			_menu->addMenuTextItem("Options", (std::function<void()>)[&] { _game->getStateManager()->pushState(OptionsState::getInstance()); });
			_menu->addMenuTextItem("Credits", (std::function<void()>)[&] { _game->getStateManager()->pushState(CreditsState::getInstance()); });
			
			auto files = util::FileManager::getInstance().getFiles("assets\\playbacks\\");
			auto file = std::find(files.begin(), files.end(), "recording");
			if (file != files.end())
			{
				_menu->addMenuTextItem("Playback", (std::function<void()>)[&] {
					_game->setGameMode(GameBase::GameMode::Playback);
					LoadingPlayBackState::getInstance().setPlaybackFileName("recording");
					_game->getStateManager()->changeState(LoadingPlayBackState::getInstance());
				});
			}

			_menu->addMenuTextItem("Quit", (std::function<void()>)[&] {
				_game->stop();
			});

			game.getEngine()->getAudioEngine()->load("main_menu_bgm", "assets/sounds/mainmenu/bgm.mp3", AUDIOTYPE::MUSIC);
			//game.getEngine()->getAudioEngine()->load("menu_switch_effect", R"(assets/sounds/effects/menu_sound3.ogg)", AUDIOTYPE::SOUND_EFFECT);
			game.getEngine()->getAudioEngine()->play("main_menu_bgm", 0);
			game.getEngine()->getInputEngine()->setMouseEnabled();

			game.getEngine()->getDrawEngine()->load("mainmenu_background", "assets/screens/mainmenu");
			//game.getEngine()->getDrawEngine()->load("menu_play", "assets/screens/main/play");
			//game.getEngine()->getDrawEngine()->load("menu_options", "assets/screens/main/options");
			//game.getEngine()->getDrawEngine()->load("menu_credits", "assets/screens/mainm/credits");
			//game.getEngine()->getDrawEngine()->load("menu_quit", "assets/screens/main/quit");


			_advertisementIndex = -1;
			_advertisementRefreshRate = 15 * 10000;
			_lastTimeSinceAdvertisementChange = 0;

			if (_advertismentList != nullptr) {
				delete _advertismentList;
				_advertismentList = nullptr;
			}

			_advertismentList = new std::vector<std::string>(util::FileManager::getInstance().getFiles("assets/advertisements/"));

			if (_advertismentList->size() > 0)
			{
				loadAdvertisement();
				_shouldRefreshAdvertisement = _advertismentList->size() > 1;
			}

			// Load game
			if (ProgressManager::getInstance().currentSavegame < 0)
				_game->getStateManager()->pushState(ProgressLoadState::getInstance());
		}
Example #11
0
		void CreateLevelState::cleanup(GameBase &game) {
			delete _name;
			delete _error;
			
			game.getEngine()->getDrawEngine()->unloadAll();
		}
Example #12
0
		void GameOverState::init(GameBase &game)
		{
			_game = &game;

			_game->getEngine()->getPhysicsEngine()->pause();
			
			_isLoaded = false;
			_savedReplay = false;
			_enteredGameOverState = game.getGameTime()->getTotalSecondsRunning();

			game.getEngine()->getAudioEngine()->stopMusic();
			
			_menu = new Menu(game.getEngine()->getDrawEngine()->getWindowWidth() - (187.5f * 3), 50.0f, game);

			const std::vector<GameObject*> &deadList = game.getWorld()->getDeadList();
			bool unlocked = false;

			model::Character *chas = nullptr;

			if (game.getGameMode() == GameBase::GameMode::SinglePlayer)
			{
				static_cast<Character*>(game.getWorld()->getPlayers()[1])->getAI()->pause();

				if (!LoadingSinglePlayerState::getInstance().hasFinished())
				{
					if (static_cast<Character*>(deadList[1])->getKey() == LoadingSinglePlayerState::getInstance().getPlayerName())
					{
						game.getEngine()->getDrawEngine()->load("gameoverbackground", "assets/screens/winner");
						_menu->addMenuTextItem("Next", (std::function<void()>)std::bind(&GameOverState::next, this));
						game.getEngine()->getAudioEngine()->load("winner", "assets/sounds/effects/win.ogg", AUDIOTYPE::SOUND_EFFECT);
						game.getEngine()->getAudioEngine()->play("winner", 0);
					}
					else
					{
						chas = static_cast<model::Character*>(deadList[0]);
						game.getEngine()->getDrawEngine()->load("gameoverbackground", "assets/screens/loser");

						game.getEngine()->getAudioEngine()->load("loser", "assets/sounds/effects/lose.ogg", AUDIOTYPE::SOUND_EFFECT);
						game.getEngine()->getAudioEngine()->play("loser", 0);
					}
				}
				else if (static_cast<Character*>(deadList[1])->getKey() == LoadingSinglePlayerState::getInstance().getPlayerName())
				{
					game.getEngine()->getDrawEngine()->load("gameoverbackground", "assets/screens/winner");

					ProgressManager &manager = ProgressManager::getInstance();

					if (!manager.isUnlockedCharacter(static_cast<Character*>(deadList[0])->getName()))
					{
						std::string asd = static_cast<Character*>(deadList[0])->getKey();
						std::string has = LoadingSinglePlayerState::getInstance().getLevelName();
						UnlockedState::getInstance().setPlayerName(static_cast<Character*>(deadList[0])->getKey());
						UnlockedState::getInstance().setLevelName(LoadingSinglePlayerState::getInstance().getLevelName());
						manager.setIsUnlockedCharacter(static_cast<Character*>(deadList[0])->getName(), true);
						manager.setIsUnlockedLevel(LoadingSinglePlayerState::getInstance().getLevelName(), true);
						//  UnlockedState::getInstance().cleanup(game);
						unlocked = true;
					}

					game.getEngine()->getAudioEngine()->load("winner", "assets/sounds/effects/win.ogg", AUDIOTYPE::SOUND_EFFECT);
					game.getEngine()->getAudioEngine()->play("winner", 0);
				}
				_menu->addMenuTextItem("Achievements", (std::function<void()>)[&] { _game->getStateManager()->pushState(StatisticsState::getInstance()); });
			}
			else if (game.getGameMode() == GameBase::GameMode::Versus)
			{
				game.getEngine()->getAudioEngine()->load("winner", "assets/sounds/effects/win.ogg", AUDIOTYPE::SOUND_EFFECT);
				game.getEngine()->getAudioEngine()->play("winner", 0);
				game.getEngine()->getDrawEngine()->load("gameoverbackground", "assets/screens/winner");
				_menu->addMenuTextItem("Play again", (std::function<void()>)std::bind(&GameOverState::replay, this));
				_menu->addMenuTextItem("Save replay", (std::function<void()>)std::bind(&GameOverState::saveReplay, this));
				_menu->addMenuTextItem("Statistics", (std::function<void()>)[&] { _game->getStateManager()->pushState(StatisticsState::getInstance()); });
			}
			else if (game.getGameMode() == GameBase::GameMode::Playback)
			{
				game.getEngine()->getAudioEngine()->load("winner", "assets/sounds/effects/win.ogg", AUDIOTYPE::SOUND_EFFECT);
				game.getEngine()->getAudioEngine()->play("winner", 0);
				game.getEngine()->getDrawEngine()->load("gameoverbackground", "assets/screens/winner");
			}

			_menu->addMenuTextItem("Main menu", (std::function<void()>)[&] {
				if (game.getGameMode() == GameBase::GameMode::SinglePlayer)
					LoadingSinglePlayerState::getInstance().unloadAll();
				changeState(*_game, MainMenuState::getInstance());
			});
		
			Uint8 color = 255;
			for (int i = deadList.size() - 1; i >= 0; i--) {
				int rank = (deadList.size() - i);
				game.getEngine()->getDrawEngine()->loadText("rank" + std::to_string(rank), std::to_string(rank) + ". " + deadList[i]->getName(), { color, color, color }, "arial", 54);
				std::string asd = deadList[i]->getName();
				color = 64;
			}

			_characterCount = deadList.size();
			if (!chas)
				chas = static_cast<model::Character*>(deadList[_characterCount - 1]);

			game.getEngine()->getDrawEngine()->load("winner", "assets/characters/" + chas->getKey() + "/win");

			// Update statistics
			JSON::JSONArray &statistics = ProgressManager::getInstance().getStatistics();
			
			for (size_t rank = 0, ranklen = deadList.size(); rank < ranklen; rank++) {

				for (auto i = 0; i < statistics.size(); i++) {
					JSON::JSONObject &characterObj = statistics.getObject(i);

					if (characterObj.getString("name") == deadList.at(rank)->getName()) {
						if (rank == (deadList.size() - 1))
							characterObj.getVariable("wins").setValue(std::to_string(1 + characterObj.getInt("wins")));
						else
							characterObj.getVariable("losses").setValue(std::to_string(1 + characterObj.getInt("losses")));
						break;
					}
				}
			}
			
			// Save progress if autosave is enabled
			if (ProgressManager::getInstance().autosaveEnabled())
				ProgressManager::getInstance().save();

			if (unlocked)
			{
				if (ProgressManager::getInstance().autosaveEnabled())
					ProgressManager::getInstance().save();
				unlocked = false;
				game.getStateManager()->pushState(UnlockedState::getInstance());
			}
		}
Example #13
0
		void GameOverState::cleanup(GameBase &game)
		{
			delete _menu;
			_menu = nullptr;

			game.getRecorder().reset();

			if (_replay)
			{
				DrawEngine *de = game.getEngine()->getDrawEngine();
				de->unload("winner");
				de->unload("gameoverbackground");
				de->unloadText("replay");
				de->unloadText("statistics");
				de->unloadText("main menu");

				game.getEngine()->getAudioEngine()->unload("winner");

				for (int i = 1; i <= _characterCount; i++) {
					std::string asd = "rank" + std::to_string(i);
					de->unloadText("rank" + std::to_string(i));
				}

				PlayState::getInstance().resume(game);
				_replay = false;
			}
			else
			{
				delete PlayState::getInstance()._level;
				PlayState::getInstance()._level = nullptr;

				game.getWorld()->destroyShootBodies();
				game.getEngine()->getAudioEngine()->unloadAll();
				game.getEngine()->getDrawEngine()->unloadAll();
				game.getEngine()->getPhysicsEngine()->cleanUp();
				game.getEngine()->getParticleEngine()->unloadAll();
				game.getEngine()->getInputEngine()->clearBindings();

				game.getWorld()->clearWorld();

				std::vector<HUD*> *huds = game.getGameMode() == GameBase::GameMode::Playback ? PlayBackState::getInstance()._huds : PlayState::getInstance()._huds;

				if (huds) {
					for (auto it : *huds) {
						delete it;
					}
					huds->clear();
				}

				delete huds;
				huds = nullptr;
				

				if (game.getGameMode() != GameBase::GameMode::Edit) {
					delete PlayState::getInstance()._slotKeyInput;
					PlayState::getInstance()._slotKeyInput = nullptr;
					delete PlayState::getInstance()._keys;
					PlayState::getInstance()._keys = nullptr;
				}
			}

			game.getEngine()->getInputEngine()->getMouse().clear();
		}