mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-09 10:13:21 +08:00
Update breakout code to match revised changes 'Levels' chapter.
This commit is contained in:
@@ -179,7 +179,7 @@ void Game::ProcessInput(float dt)
|
||||
// move playerboard
|
||||
if (this->Keys[GLFW_KEY_A])
|
||||
{
|
||||
if (Player->Position.x >= 0)
|
||||
if (Player->Position.x >= 0.0f)
|
||||
{
|
||||
Player->Position.x -= velocity;
|
||||
if (Ball->Stuck)
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
|
||||
GameObject::GameObject()
|
||||
: Position(0, 0), Size(1, 1), Velocity(0.0f), Color(1.0f), Rotation(0.0f), Sprite(), IsSolid(false), Destroyed(false) { }
|
||||
: Position(0.0f, 0.0f), Size(1.0f, 1.0f), Velocity(0.0f), Color(1.0f), Rotation(0.0f), Sprite(), IsSolid(false), Destroyed(false) { }
|
||||
|
||||
GameObject::GameObject(glm::vec2 pos, glm::vec2 size, Texture2D sprite, glm::vec3 color, glm::vec2 velocity)
|
||||
: Position(pos), Size(size), Velocity(velocity), Color(color), Rotation(0.0f), Sprite(sprite), IsSolid(false), Destroyed(false) { }
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*******************************************************************
|
||||
** This code is part of Breakout.
|
||||
**
|
||||
** Breakout is free software: you can redistribute it and/or modify
|
||||
** it under the terms of the CC BY 4.0 license as published by
|
||||
** Creative Commons, either version 4 of the License, or (at your
|
||||
** option) any later version.
|
||||
******************************************************************/
|
||||
#include "game.h"
|
||||
#include "resource_manager.h"
|
||||
#include "sprite_renderer.h"
|
||||
|
||||
|
||||
// Game-related State data
|
||||
SpriteRenderer *Renderer;
|
||||
GameObject *Player;
|
||||
|
||||
Game::Game(unsigned int width, unsigned int height)
|
||||
: State(GAME_MENU), Keys(), KeysProcessed(), Width(width), Height(height)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Game::Init()
|
||||
{
|
||||
// load shaders
|
||||
ResourceManager::LoadShader("shaders/sprite.vs", "shaders/sprite.frag", nullptr, "sprite");
|
||||
// configure shaders
|
||||
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(this->Width),
|
||||
static_cast<float>(this->Height), 0.0f, -1.0f, 1.0f);
|
||||
ResourceManager::GetShader("sprite").Use().SetInteger("image", 0);
|
||||
ResourceManager::GetShader("sprite").SetMatrix4("projection", projection);
|
||||
// set render-specific controls
|
||||
Renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
|
||||
// load textures
|
||||
ResourceManager::LoadTexture("textures/background.jpg", false, "background");
|
||||
ResourceManager::LoadTexture("textures/awesomeface.png", true, "face");
|
||||
ResourceManager::LoadTexture("textures/block.png", false, "block");
|
||||
ResourceManager::LoadTexture("textures/block_solid.png", false, "block_solid");
|
||||
ResourceManager::LoadTexture("textures/paddle.png", true, "paddle");
|
||||
// load levels
|
||||
GameLevel one; one.Load("levels/one.lvl", this->Width, this->Height / 2);
|
||||
GameLevel two; two.Load("levels/two.lvl", this->Width, this->Height / 2);
|
||||
GameLevel three; three.Load("levels/three.lvl", this->Width, this->Height / 2);
|
||||
GameLevel four; four.Load("levels/four.lvl", this->Width, this->Height / 2);
|
||||
this->Levels.push_back(one);
|
||||
this->Levels.push_back(two);
|
||||
this->Levels.push_back(three);
|
||||
this->Levels.push_back(four);
|
||||
this->Level = 0;
|
||||
// configure game objects
|
||||
glm::vec2 playerPos = glm::vec2(this->Width / 2.0f - PLAYER_SIZE.x / 2.0f, this->Height - PLAYER_SIZE.y);
|
||||
Player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle"));
|
||||
}
|
||||
|
||||
void Game::Update(float dt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Game::ProcessInput(float dt)
|
||||
{
|
||||
if (this->State == GAME_ACTIVE)
|
||||
{
|
||||
float velocity = PLAYER_VELOCITY * dt;
|
||||
// move playerboard
|
||||
if (this->Keys[GLFW_KEY_A])
|
||||
{
|
||||
if (Player->Position.x >= 0.0f)
|
||||
Player->Position.x -= velocity;
|
||||
}
|
||||
if (this->Keys[GLFW_KEY_D])
|
||||
{
|
||||
if (Player->Position.x <= this->Width - Player->Size.x)
|
||||
Player->Position.x += velocity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::Render()
|
||||
{
|
||||
if(this->State == GAME_ACTIVE)
|
||||
{
|
||||
// draw background
|
||||
Renderer->DrawSprite(ResourceManager::GetTexture("background"), glm::vec2(0.0f, 0.0f), glm::vec2(this->Width, this->Height), 0.0f);
|
||||
// draw level
|
||||
this->Levels[this->Level].Draw(*Renderer);
|
||||
// draw player
|
||||
Player->Draw(*Renderer);
|
||||
}
|
||||
}
|
||||
49
src/7.in_practice/3.2d_game/0.full_source/progress/4.game.h
Normal file
49
src/7.in_practice/3.2d_game/0.full_source/progress/4.game.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*******************************************************************
|
||||
** This code is part of Breakout.
|
||||
**
|
||||
** Breakout is free software: you can redistribute it and/or modify
|
||||
** it under the terms of the CC BY 4.0 license as published by
|
||||
** Creative Commons, either version 4 of the License, or (at your
|
||||
** option) any later version.
|
||||
******************************************************************/
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
// Represents the current state of the game
|
||||
enum GameState {
|
||||
GAME_ACTIVE,
|
||||
GAME_MENU,
|
||||
GAME_WIN
|
||||
};
|
||||
|
||||
// Initial size of the player paddle
|
||||
const glm::vec2 PLAYER_SIZE(100.0f, 20.0f);
|
||||
// Initial velocity of the player paddle
|
||||
const float PLAYER_VELOCITY(500.0f);
|
||||
|
||||
// Game holds all game-related state and functionality.
|
||||
// Combines all game-related data into a single class for
|
||||
// easy access to each of the components and manageability.
|
||||
class Game
|
||||
{
|
||||
public:
|
||||
// game state
|
||||
GameState State;
|
||||
bool Keys[1024];
|
||||
bool KeysProcessed[1024];
|
||||
unsigned int Width, Height;
|
||||
// constructor/destructor
|
||||
Game(unsigned int width, unsigned int height);
|
||||
~Game();
|
||||
// initialize game state (load all shaders/textures/levels)
|
||||
void Init();
|
||||
// game loop
|
||||
void ProcessInput(float dt);
|
||||
void Update(float dt);
|
||||
void Render();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user