学习链接:中文--------英语原文
给游戏添加音乐!
参考上文链接导入相应的头文件和lib、DLL即可。 Game.cpp:
#include "ResourceManager.h"
#include "Game.h"
#include "SpriteRenderer.h"
#include "BallObject.h"
#include "ParticleGenerator.h"
#include "PostProcessor.h"
#include <irrklang/irrKlang.h>
using namespace irrklang;
SpriteRenderer* renderer;
GameObject* player;
BallObject* ball;
ParticleGenerator* particles;
PostProcessor* effects;
ISoundEngine* soundEngine = createIrrKlangDevice();
GLfloat shakeTime = 0.0f;
Game::Game(GLuint w, GLuint h) :state(GAME_ACTIVE), keys(), width(w), height(h) {}
Game::~Game()
{
delete renderer;
delete player;
delete ball;
delete particles;
delete effects;
soundEngine->drop();
}
void Game::Init()
{
ResourceManager::LoadShader("Breakout/Shaders/Sprite.vs", "Breakout/Shaders/Sprite.fs", nullptr, "sprite");
ResourceManager::LoadShader("Breakout/Shaders/Particle.vs", "Breakout/Shaders/Particle.fs", nullptr, "particle");
ResourceManager::LoadShader("Breakout/Shaders/postprocessor.vs", "Breakout/Shaders/postprocessor.fs", nullptr, "postprocessor");
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(this->width), static_cast<GLfloat>(this->height), 0.0f, -1.0f, 1.0f);
ResourceManager::GetShader("sprite").Use().SetInteger("image", 0);
ResourceManager::GetShader("sprite").SetMatrix4("projection", projection);
ResourceManager::GetShader("particle").Use().SetInteger("sprite", 0);
ResourceManager::GetShader("particle").SetMatrix4("projection", projection);
ResourceManager::LoadTexture("Breakout/Images/background.jpg", GL_FALSE, "background");
ResourceManager::LoadTexture("Breakout/Images/block.png", GL_FALSE, "block");
ResourceManager::LoadTexture("Breakout/Images/block_solid.png", GL_FALSE, "block_solid");
ResourceManager::LoadTexture("Breakout/paddle.png", GL_TRUE, "paddle");
ResourceManager::LoadTexture("Breakout/face.png", GL_TRUE, "ball");
ResourceManager::LoadTexture("Breakout/particle.png", GL_TRUE, "particle");
ResourceManager::LoadTexture("Breakout/tex_speed.png", GL_TRUE, "tex_speed");
ResourceManager::LoadTexture("Breakout/tex_sticky.png", GL_TRUE, "tex_sticky");
ResourceManager::LoadTexture("Breakout/tex_pass.png", GL_TRUE, "tex_pass");
ResourceManager::LoadTexture("Breakout/tex_size.png", GL_TRUE, "tex_size");
ResourceManager::LoadTexture("Breakout/tex_confuse.png", GL_TRUE, "tex_confuse");
ResourceManager::LoadTexture("Breakout/tex_chaos.png", GL_TRUE, "tex_chaos");
renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
particles = new ParticleGenerator(ResourceManager::GetShader("particle"), ResourceManager::GetTexture("particle"), 500);
effects = new PostProcessor(ResourceManager::GetShader("postprocessor"), this->width, this->height);
GameLevel one; one.Load("Breakout/Levels/1.lvl", this->width, this->height * 0.5);
GameLevel two; two.Load("Breakout/Levels/2.lvl", this->width, this->height * 0.5);
GameLevel three; three.Load("Breakout/Levels/3.lvl", this->width, this->height * 0.5);
GameLevel four; four.Load("Breakout/Levels/4.lvl", this->width, this->height * 0.5);
this->levels.push_back(one);
this->levels.push_back(two);
this->levels.push_back(three);
this->levels.push_back(four);
this->currentLevel = 0;
glm::vec2 playerPos = glm::vec2(this->width / 2 - PLAYER_SIZE.x / 2, this->height - PLAYER_SIZE.y);
player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle"));
glm::vec2 ballPos = playerPos + glm::vec2(PLAYER_SIZE.x / 2 - BALL_RADIUS, -BALL_RADIUS * 2);
ball = new BallObject(ballPos, BALL_RADIUS, INITIAL_BALL_VELOCITY, ResourceManager::GetTexture("ball"));
soundEngine->play2D("Breakout/Audios/breakout.mp3", true);
}
void Game::Update(GLfloat dt)
{
ball->Move(dt, this->width);
this->DoCollisions();
particles->Update(dt, *ball, 2, glm::vec2(ball->radius / 2));
this->UpdatePowerUps(dt);
if (shakeTime > 0.0f)
{
shakeTime -= dt;
if (shakeTime <= 0.0f)
effects->shake = GL_FALSE;
}
if (ball->position.y >= this->height)
{
this->ResetLevel();
this->ResetPlayer();
}
}
void Game::ProcessInput(GLfloat dt)
{
if (this->state == GAME_ACTIVE)
{
GLfloat velocity = PLAYER_VELOCITY * dt;
if (this->keys[GLFW_KEY_A])
{
if (player->position.x >= 0)
{
player->position.x -= velocity;
if (ball->stuck)
ball->position.x -= velocity;
}
}
if (this->keys[GLFW_KEY_D])
{
if (player->position.x <= this->width - player->size.x)
{
player->position.x += velocity;
if (ball->stuck)
ball->position.x += velocity;
}
}
if (this->keys[GLFW_KEY_SPACE])
ball->stuck = GL_FALSE;
}
}
void Game::Render()
{
if (this->state == GAME_ACTIVE)
{
effects->BeginRender();
renderer->DrawSprite(ResourceManager::GetTexture("background"), glm::vec2(0, 0), glm::vec2(this->width, this->height), 0.0f);
this->levels[this->currentLevel].Draw(*renderer);
player->Draw(*renderer);
for (PowerUp& powerup : this->powerups)
{
if (!powerup.destroyed)
powerup.Draw(*renderer);
}
particles->Draw();
ball->Draw(*renderer);
effects->EndRender();
effects->Render(glfwGetTime());
}
}
Direction VectorDirection(glm::vec2 target)
{
glm::vec2 compass[] =
{
glm::vec2(0.0f,1.0f),
glm::vec2(1.0f,0.0f),
glm::vec2(0.0f,-1.0f),
glm::vec2(-1.0f,0.0f),
};
GLfloat max = 0.0f;
GLuint bestMatch = -1;
for (GLuint i = 0; i < 4; i++)
{
GLfloat dot = glm::dot(glm::normalize(target), compass[i]);
if (dot > max)
{
max = dot;
bestMatch = i;
}
}
return (Direction)bestMatch;
}
GLboolean CheckCollision(GameObject& one, GameObject& two)
{
bool collisionX = one.position.x + one.size.x >= two.position.x &&
two.position.x + two.size.x >= one.position.x;
bool collisionY = one.position.y + one.size.y >= two.position.y &&
two.position.y + two.size.y >= one.position.y;
return collisionX && collisionY;
}
Collision CheckCollision(BallObject& one, GameObject& two)
{
glm::vec2 center(one.position + one.radius);
glm::vec2 aabb_half_extents(two.size.x / 2, two.size.y / 2);
glm::vec2 aabb_center(two.position.x + aabb_half_extents.x, two.position.y + aabb_half_extents.y);
glm::vec2 difference = center - aabb_center;
glm::vec2 clamped = glm::clamp(difference, -aabb_half_extents, aabb_half_extents);
glm::vec2 closest = aabb_center + clamped;
difference = closest - center;
if (glm::length(difference) < one.radius)
return std::make_tuple(GL_TRUE, VectorDirection(difference), difference);
else
return std::make_tuple(GL_FALSE, UP, glm::vec2(0, 0));
}
void ActivatePowerUp(PowerUp& powerup)
{
if (powerup.type == "speed")
{
ball->velocity *= 1.2;
}
else if (powerup.type == "sticky")
{
ball->sticky = GL_TRUE;
player->color = glm::vec3(1.0f, 0.5f, 1.0f);
}
else if (powerup.type == "pass-through")
{
ball->passThrough = GL_TRUE;
ball->color = glm::vec3(1.0f, 0.5f, 0.5f);
}
else if (powerup.type == "pad-size-increase")
{
player->size.x += 50;
}
else if (powerup.type == "confuse")
{
if (!effects->chaos)
effects->confuse = GL_TRUE;
}
else if (powerup.type == "chaos")
{
if (!effects->confuse)
effects->chaos = GL_TRUE;
}
}
void Game::DoCollisions()
{
for (GameObject& box : this->levels[this->currentLevel].bricks)
{
if (!box.destroyed)
{
Collision collision = CheckCollision(*ball, box);
if (std::get<0>(collision))
{
if (!box.isSoild)
{
box.destroyed = GL_TRUE;
this->SpawnPowerUps(box);
soundEngine->play2D("Breakout/Audios/bleep.mp3", GL_FALSE);
}
else
{
shakeTime = 0.05f;
effects->shake = true;
soundEngine->play2D("Breakout/Audios/solid.wav", GL_FALSE);
}
Direction dir = std::get<1>(collision);
glm::vec2 diff_vector = std::get<2>(collision);
if (!(ball->passThrough && !box.isSoild))
{
if (dir == LEFT || dir == RIGHT)
{
ball->velocity.x = -ball->velocity.x;
GLfloat penetration = ball->radius - std::abs(diff_vector.x);
if (dir == LEFT)
ball->position.x += penetration;
else
ball->position.x -= penetration;
}
else
{
ball->velocity.y = -ball->velocity.y;
GLfloat penetration = ball->radius - std::abs(diff_vector.y);
if (dir == UP)
ball->position.y -= penetration;
else
ball->position.y += penetration;
}
}
}
for (PowerUp& powerup : this->powerups)
{
if (!powerup.destroyed)
{
if (powerup.position.y >= this->height)
{
powerup.destroyed = GL_TRUE;
}
if (CheckCollision(*player, powerup))
{
ActivatePowerUp(powerup);
powerup.destroyed = GL_TRUE;
powerup.activated = GL_TRUE;
soundEngine->play2D("Breakout/Audios/powerup.wav", GL_FALSE);
}
}
}
Collision result = CheckCollision(*ball, *player);
if (!ball->stuck && std::get<0>(result))
{
GLfloat centerBoard = player->position.x + player->size.x / 2;
GLfloat distance = (ball->position.x + ball->radius) - centerBoard;
GLfloat percentage = distance / (player->size.x / 2);
GLfloat strength = 2.0f;
glm::vec2 oldVelocity = ball->velocity;
ball->velocity.x = INITIAL_BALL_VELOCITY.x * percentage * strength;
ball->velocity.y = -1 * abs(ball->velocity.y);
ball->velocity = glm::normalize(ball->velocity) * glm::length(oldVelocity);
ball->stuck = ball->sticky;
soundEngine->play2D("Breakout/Audios/bleep.wav", GL_FALSE);
}
}
}
}
void Game::ResetLevel()
{
if (this->currentLevel == 0)
this->levels[0].Load("Breakout/Levels/1.lvl", this->width, this->height * 0.5f);
else if (this->currentLevel == 1)
this->levels[1].Load("Breakout/Levels/2.lvl", this->width, this->height * 0.5f);
else if (this->currentLevel == 2)
this->levels[1].Load("Breakout/Levels/3.lvl", this->width, this->height * 0.5f);
else if (this->currentLevel == 3)
this->levels[1].Load("Breakout/Levels/4.lvl", this->width, this->height * 0.5f);
}
void Game::ResetPlayer()
{
player->size = PLAYER_SIZE;
player->position = glm::vec2(this->width / 2 - PLAYER_SIZE.x / 2, this->height - PLAYER_SIZE.y);
ball->Reset(player->position + glm::vec2(PLAYER_SIZE.x / 2 - ball->radius, -(BALL_RADIUS * 2)), INITIAL_BALL_VELOCITY);
}
GLboolean ShouldSpawn(GLuint chance)
{
GLuint random = rand() % chance;
return random == 0;
}
void Game::SpawnPowerUps(GameObject& block)
{
if (ShouldSpawn(10))
this->powerups.push_back(PowerUp("speed", glm::vec3(0.5f, 0.5f, 1.0f), 0.0f, block.position, ResourceManager::GetTexture("tex_speed")));
if (ShouldSpawn(10))
this->powerups.push_back(PowerUp("sticky", glm::vec3(1.0f, 0.5f, 1.0f), 20.0f, block.position, ResourceManager::GetTexture("tex_sticky")));
if (ShouldSpawn(10))
this->powerups.push_back(PowerUp("pass-through", glm::vec3(0.5f, 1.0f, 0.5f), 10.0f, block.position, ResourceManager::GetTexture("tex_pass")));
if (ShouldSpawn(10))
this->powerups.push_back(PowerUp("pad-size-increase", glm::vec3(1.0f, 0.6f, 0.4), 0.0f, block.position, ResourceManager::GetTexture("tex_size")));
}
GLboolean IsOtherPowerUpActive(std::vector<PowerUp>& powerUps, std::string type)
{
for (const PowerUp& powerUp : powerUps)
{
if (powerUp.activated)
if (powerUp.type == type)
return GL_TRUE;
}
return GL_FALSE;
}
void Game::UpdatePowerUps(GLfloat dt)
{
for (PowerUp& powerUp : this->powerups)
{
powerUp.position += powerUp.velocity * dt;
if (powerUp.activated)
{
powerUp.duration -= dt;
if (powerUp.duration <= 0.0f)
{
powerUp.activated = GL_FALSE;
if (powerUp.type == "sticky")
{
if (!IsOtherPowerUpActive(this->powerups, "sticky"))
{
ball->sticky = GL_FALSE;
player->color = glm::vec3(1.0f);
}
}
else if (powerUp.type == "pass-through")
{
if (!IsOtherPowerUpActive(this->powerups, "pass-through"))
{
ball->passThrough = GL_FALSE;
player->color = glm::vec3(1.0f);
}
}
else if (powerUp.type == "pass-through")
{
if (!IsOtherPowerUpActive(this->powerups, "pass-through"))
{
ball->passThrough = GL_FALSE;
player->color = glm::vec3(1.0f);
}
}
}
}
}
this->powerups.erase(std::remove_if(this->powerups.begin(), this->powerups.end(),
[](const PowerUp& powerUp) { return powerUp.destroyed && !powerUp.activated; }
), this->powerups.end());
}
|