コード例 #1
0
ファイル: Game.cpp プロジェクト: anneomcl/MyGame
void Game::handleCamera()
{
	PlayerCharacter * player = (PlayerCharacter *)_gameObjectManager.get("PlayerCharacter");

	float pos_x = player->getPosition().x + 10 - (SCREEN_WIDTH / 2);
	float pos_y = player->getPosition().y + 10 - (SCREEN_HEIGHT / 2);

	if (pos_x < 0)
	{
		pos_x = 0;
	}

	if (pos_y < 0)
	{
		pos_y = 0;
	}

	_view.reset(sf::FloatRect(pos_x, pos_y, SCREEN_WIDTH, SCREEN_HEIGHT));
}
コード例 #2
0
ファイル: Game.cpp プロジェクト: anneomcl/MyGame
void Game::gameLoop()
{
	sf::Event currentEvent;
	_mainWindow.pollEvent(currentEvent);

	switch (_gameState)
	{
		case Game::ShowingMenu:
		{
			showMenu();
			break;
		}

		case Game::ShowingSplash:
		{
			showSplashScreen("C:/Users/Anne/Documents/Visual Studio 2013/Projects/MyGame/Graphics/MyGameScreen.png");
			break;
		}

		case Game::ShowingSplashVictory:
		{
			showSplashScreen("C:/Users/Anne/Documents/Visual Studio 2013/Projects/MyGame/Graphics/Victory.png");
			break;
		}

		case Game::ShowingSplashLoss:
		{
			showSplashScreen("C:/Users/Anne/Documents/Visual Studio 2013/Projects/MyGame/Graphics/Loss.png");
			break;
		}

		case Game::Playing:
		{
			rigidBodyCoords = findRigidBodies();
			_gameObjectManager.updateAll();
			//_mainWindow.draw(*coinstring);
			_gameObjectManager.drawAll(_mainWindow);


			PlayerCharacter * player = (PlayerCharacter *)_gameObjectManager.get("PlayerCharacter");

			if (player->getPosition().y > 2000)
			{
				if (player->lives > 0)
				{
					player->lives--;
					start_again();
				}

				else
				{
					_gameState = Loss;
					start_loss();
					break;
				}
			}

			handleSurfaces();

			handleCamera();

			_mainWindow.setView(_view);

			_mainWindow.display();


			if (currentEvent.type == sf::Event::Closed) _gameState = Game::Exiting;

			if ((currentEvent.type == sf::Event::KeyPressed) && (currentEvent.key.code == sf::Keyboard::Escape))
				showMenu();

			if(game_victory != 0)
			{
				_gameState = Game::Victory;
			}

			break;
		}

		case Game::Victory:
		{
			_gameState = Game::Uninitialized;
			start_victory();
			break;
		}

		case Game::Loss:
		{
			_gameState = Game::Uninitialized;
			start_loss();
			break;
		}
	}
}
コード例 #3
0
ファイル: Game.cpp プロジェクト: anneomcl/MyGame
void Game::handleSurfaces()
{
	PlayerCharacter * player = (PlayerCharacter *)_gameObjectManager.get("PlayerCharacter");
	RigidSurface_Brick * v_block = (RigidSurface_Brick *)_gameObjectManager.get("block_victory");

	if (player->getSprite().getGlobalBounds().intersects(v_block->getSprite().getGlobalBounds()))
	{
		game_victory = 1;
	}

	if (display_coin)
	{
		coin_animation_frames--;
		if (coin_animation_frames <= 0)
		{
			display_coin = false;
			Coin * coin = (Coin *) _gameObjectManager.getByTypeSingle("Coin");
			coin->isVisible = false;
			coin_animation_frames = 150;
		}
	}

	for (int i = 0; i < rigidBodyCoords.size(); ++i)
	{
		if (player->getSprite().getGlobalBounds().intersects(rigidBodyCoords[i]->getSprite().getGlobalBounds()))
		{
			if (rigidBodyCoords[i]->getType() == "RigidSurface_Mystery")
			{
				RigidSurface_Mystery * block = (RigidSurface_Mystery *) rigidBodyCoords[i];
				if (block->times_hit < 3)
				{
					display_coin = true;
					Coin * new_coin = new Coin();
					initObject(new_coin,
						sf::Vector2f(rigidBodyCoords[i]->getSprite().getGlobalBounds().left, rigidBodyCoords[i]->getSprite().getGlobalBounds().top - 100)
						, "coin" + std::to_string(player->coins));
					player->coins++;
					block->times_hit++;
					std::cout << "Coins: "  << player->coins << std::endl;
					std::cout << "Lives: " << player->lives << std::endl;
					if (player->coins % 10 == 0)
					{
						player->lives++;
					}
				}
				else
				{
					//block is dead
				}

				player->velocity.x = -player->velocity.x;
				if (player->velocity.y < 0)
				{
					player->velocity.y = -2* player->velocity.y;
				}

				if (player->velocity.y > 0)
				{
					player->setPosition(player->getPosition().x, player->getPosition().y - 1);
					player->grounded = true;
				}
			}

			else
			{
				if (player->velocity.y > 0)
				{
					player->setPosition(player->getPosition().x, player->getPosition().y - 1);
					//player->velocity.x = -player->velocity.x;
					//TO-DO: Distinguish between bumping horizontally and landing vertically on blocks.
					//TO-DO: Make kitty stop floating
					player->grounded = true;
				}
				else
				{
					player->velocity.x = -player->velocity.x;
				}
				
			}
		}
	}
}