Levels
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -1 +1,2 @@
|
|||||||
*.png filter=lfs diff=lfs merge=lfs -text
|
*.png filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.jpg filter=lfs diff=lfs merge=lfs -text
|
||||||
|
|||||||
6
levels/four.lvl
Normal file
6
levels/four.lvl
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
1 2 1 2 1 2 1 2 1 2 1 2 1
|
||||||
|
2 2 2 2 2 2 2 2 2 2 2 2 2
|
||||||
|
2 1 3 1 4 1 5 1 4 1 3 1 2
|
||||||
|
2 3 3 4 4 5 5 5 4 4 3 3 2
|
||||||
|
2 1 3 1 4 1 5 1 4 1 3 1 2
|
||||||
|
2 2 3 3 4 4 5 4 4 3 3 2 2
|
||||||
8
levels/one.lvl
Normal file
8
levels/one.lvl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
|
||||||
|
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
|
||||||
|
4 4 4 4 4 0 0 0 0 0 4 4 4 4 4
|
||||||
|
4 1 4 1 4 0 0 1 0 0 4 1 4 1 4
|
||||||
|
3 3 3 3 3 0 0 0 0 0 3 3 3 3 3
|
||||||
|
3 3 1 3 3 3 3 3 3 3 3 3 1 3 3
|
||||||
|
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
|
||||||
|
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
|
||||||
9
levels/three.lvl
Normal file
9
levels/three.lvl
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||||
|
0 0 2 0 0 0 0 0 0 0 2 0 0
|
||||||
|
0 0 0 2 0 0 0 0 0 2 0 0 0
|
||||||
|
0 0 0 5 5 5 5 5 5 5 0 0 0
|
||||||
|
0 0 5 5 0 5 5 5 0 5 5 0 0
|
||||||
|
0 5 5 5 5 5 5 5 5 5 5 5 0
|
||||||
|
0 3 0 1 1 1 1 1 1 1 0 3 0
|
||||||
|
0 3 0 3 0 0 0 0 0 3 0 3 0
|
||||||
|
0 0 0 0 4 4 0 4 4 0 0 0 0
|
||||||
8
levels/two.lvl
Normal file
8
levels/two.lvl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
|
||||||
|
1 0 5 5 0 5 5 0 5 5 0 5 5 0 1
|
||||||
|
1 5 5 5 5 5 5 5 5 5 5 5 5 5 1
|
||||||
|
1 0 3 3 0 3 3 0 3 3 0 3 3 0 1
|
||||||
|
1 3 3 3 3 3 3 3 3 3 3 3 3 3 1
|
||||||
|
1 0 2 2 0 2 2 0 2 2 0 2 2 0 1
|
||||||
|
1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
|
||||||
|
1 0 1 1 0 1 1 0 1 1 0 1 1 0 1
|
||||||
54
src/game.cpp
54
src/game.cpp
@@ -15,6 +15,14 @@ Game::Game(unsigned int width, unsigned int height)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
GameObject* player;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Game::Init()
|
void Game::Init()
|
||||||
{
|
{
|
||||||
// Load shaders
|
// Load shaders
|
||||||
@@ -27,12 +35,44 @@ void Game::Init()
|
|||||||
Renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
|
Renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
|
||||||
// load textures
|
// load textures
|
||||||
ResourceManager::LoadTexture("textures/awesomeface.png", true, "face");
|
ResourceManager::LoadTexture("textures/awesomeface.png", true, "face");
|
||||||
|
ResourceManager::LoadTexture("textures/background.jpg", false, "background");
|
||||||
|
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;
|
||||||
|
|
||||||
|
glm::vec2 playerPos = glm::vec2(this->Width /2 - PLAYER_SIZE.x /2.0f,
|
||||||
|
this->Height - PLAYER_SIZE.y);
|
||||||
|
player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Game::ProcessInput(float dt)
|
void Game::ProcessInput(float dt)
|
||||||
{
|
{
|
||||||
|
if(this->State == GAME_ACTIVE)
|
||||||
|
{
|
||||||
|
float velocity = PLAYER_VELOCITY * dt;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -43,5 +83,15 @@ void Game::Update(float dt)
|
|||||||
|
|
||||||
void Game::Render()
|
void Game::Render()
|
||||||
{
|
{
|
||||||
Renderer->DrawSprite(ResourceManager::GetTexture("face"), glm::vec2(200.0f, 200.0f), glm::vec2(300.0f, 400.0f), 45.0f, glm::vec3(0.0f, 1.0f,0.0f));
|
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);
|
||||||
|
|
||||||
|
player->Draw(*Renderer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
11
src/game.h
11
src/game.h
@@ -1,6 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
#include <glm/gtc/type_precision.hpp>
|
||||||
#include "spriterenderer.h"
|
#include "spriterenderer.h"
|
||||||
#include "resourcemanager.h"
|
#include "resourcemanager.h"
|
||||||
|
#include "gamelevel.h"
|
||||||
|
|
||||||
// Represent the curreent state of the game
|
// Represent the curreent state of the game
|
||||||
enum GameState {
|
enum GameState {
|
||||||
@@ -14,6 +19,12 @@ class Game {
|
|||||||
GameState State;
|
GameState State;
|
||||||
bool Keys[1024];
|
bool Keys[1024];
|
||||||
unsigned int Width, Height;
|
unsigned int Width, Height;
|
||||||
|
|
||||||
|
std::vector<GameLevel> Levels;
|
||||||
|
unsigned int Level;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// constructor / destructor
|
// constructor / destructor
|
||||||
Game(unsigned int width, unsigned int height);
|
Game(unsigned int width, unsigned int height);
|
||||||
~Game();
|
~Game();
|
||||||
|
|||||||
86
src/gamelevel.cpp
Normal file
86
src/gamelevel.cpp
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#include "gamelevel.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
void GameLevel::Load(const char* file, unsigned int levelWidth, unsigned int levelHeight)
|
||||||
|
{
|
||||||
|
// clear old data
|
||||||
|
this->Bricks.clear();
|
||||||
|
//load from file
|
||||||
|
unsigned int tileCode;
|
||||||
|
GameLevel level;
|
||||||
|
std::string line;
|
||||||
|
std::ifstream fstream(file);
|
||||||
|
std::vector<std::vector<unsigned int>> tileData;
|
||||||
|
|
||||||
|
if(fstream)
|
||||||
|
{
|
||||||
|
while(std::getline(fstream, line))
|
||||||
|
{
|
||||||
|
std::istringstream sstream(line);
|
||||||
|
std::vector<unsigned int> row;
|
||||||
|
while (sstream >> tileCode)
|
||||||
|
row.push_back(tileCode);
|
||||||
|
tileData.push_back(row);
|
||||||
|
}
|
||||||
|
if (tileData.size() > 0)
|
||||||
|
this->init(tileData, levelWidth, levelHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameLevel::init(std::vector<std::vector<unsigned int>> tileData, unsigned int lvlWidth, unsigned int lvlHeight)
|
||||||
|
{
|
||||||
|
// calculate dimensions
|
||||||
|
unsigned int height = tileData.size();
|
||||||
|
unsigned int width = tileData[0].size();
|
||||||
|
float unit_width = lvlWidth / static_cast<float>(width);
|
||||||
|
float unit_height = lvlHeight / height;
|
||||||
|
// initialize level tiles based on tiledata
|
||||||
|
for (unsigned int y = 0; y < height; ++y)
|
||||||
|
{
|
||||||
|
for(unsigned int x = 0; x < width; ++x)
|
||||||
|
{
|
||||||
|
// Check block type from level data
|
||||||
|
if(tileData[y][x] == 1) // solid
|
||||||
|
{
|
||||||
|
glm::vec2 pos(unit_width * x, unit_height * y);
|
||||||
|
glm::vec2 size(unit_width, unit_height);
|
||||||
|
GameObject obj(pos, size, ResourceManager::GetTexture("block_solid"), glm::vec3(0.8f, 0.8f, 0.7f));
|
||||||
|
|
||||||
|
obj.IsSolid = true;
|
||||||
|
this->Bricks.push_back(obj);
|
||||||
|
}
|
||||||
|
else if(tileData[y][x] > 1)
|
||||||
|
{
|
||||||
|
glm::vec3 color = glm::vec3(1.0f);
|
||||||
|
if (tileData[y][x] == 2)
|
||||||
|
color = glm::vec3(0.2f, 0.6f, 1.0f);
|
||||||
|
else if (tileData[y][x] == 3)
|
||||||
|
color = glm::vec3(0.0f, 0.7f, 0.0f);
|
||||||
|
else if (tileData[y][x] == 4)
|
||||||
|
color = glm::vec3(0.8f, 0.8f, 0.4f);
|
||||||
|
else if (tileData[y][x] == 5)
|
||||||
|
color = glm::vec3(1.0f, 0.5f, 0.0f);
|
||||||
|
|
||||||
|
glm::vec2 pos(unit_width * x, unit_height * y);
|
||||||
|
glm::vec2 size(unit_width , unit_height );
|
||||||
|
this->Bricks.push_back(GameObject(pos, size, ResourceManager::GetTexture("block"), color));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameLevel::Draw(SpriteRenderer& renderer)
|
||||||
|
{
|
||||||
|
for(GameObject& tile : this->Bricks)
|
||||||
|
if(!tile.Destroyed)
|
||||||
|
tile.Draw(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GameLevel::IsCompleted()
|
||||||
|
{
|
||||||
|
for (GameObject& tile : this->Bricks)
|
||||||
|
if(!tile.IsSolid && !tile.Destroyed)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
21
src/gamelevel.h
Normal file
21
src/gamelevel.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include "gameobject.h"
|
||||||
|
#include "resourcemanager.h"
|
||||||
|
|
||||||
|
class GameLevel
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::vector<GameObject> Bricks;
|
||||||
|
// constructor
|
||||||
|
GameLevel(){}
|
||||||
|
// loads level from file
|
||||||
|
void Load (const char* file, unsigned int levelWidth, unsigned int levelHeight);
|
||||||
|
// render level
|
||||||
|
void Draw(SpriteRenderer& renderer);
|
||||||
|
// Check if the level is completed (all non-solid tiles are destroyed)
|
||||||
|
bool IsCompleted();
|
||||||
|
private:
|
||||||
|
// initialize level from tile data
|
||||||
|
void init(std::vector<std::vector<unsigned int>> tileData, unsigned int levelWidth, unsigned int levelHeight);
|
||||||
|
};
|
||||||
12
src/gameobject.cpp
Normal file
12
src/gameobject.cpp
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include "gameobject.h"
|
||||||
|
|
||||||
|
GameObject::GameObject()
|
||||||
|
: 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) { }
|
||||||
|
|
||||||
|
void GameObject::Draw(SpriteRenderer &renderer)
|
||||||
|
{
|
||||||
|
renderer.DrawSprite(this->Sprite, this->Position, this->Size, this->Rotation, this->Color);
|
||||||
|
}
|
||||||
23
src/gameobject.h
Normal file
23
src/gameobject.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "texture2d.h"
|
||||||
|
#include "spriterenderer.h"
|
||||||
|
|
||||||
|
class GameObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
glm::vec2 Position, Size, Velocity;
|
||||||
|
glm::vec3 Color;
|
||||||
|
float Rotation;
|
||||||
|
bool IsSolid;
|
||||||
|
bool Destroyed;
|
||||||
|
// Render state
|
||||||
|
Texture2D Sprite;
|
||||||
|
// Constructor
|
||||||
|
GameObject();
|
||||||
|
GameObject(glm::vec2 pos, glm::vec2 size, Texture2D sprite, glm::vec3 color = glm::vec3(1.0f), glm::vec2 Velocity = glm::vec2(0.0f, 0.0f));
|
||||||
|
// draw sprite
|
||||||
|
virtual void Draw(SpriteRenderer& renderer);
|
||||||
|
};
|
||||||
BIN
textures/background.jpg
LFS
Normal file
BIN
textures/background.jpg
LFS
Normal file
Binary file not shown.
BIN
textures/block.png
LFS
Normal file
BIN
textures/block.png
LFS
Normal file
Binary file not shown.
BIN
textures/block_solid.png
LFS
Normal file
BIN
textures/block_solid.png
LFS
Normal file
Binary file not shown.
BIN
textures/paddle.png
LFS
Normal file
BIN
textures/paddle.png
LFS
Normal file
Binary file not shown.
Reference in New Issue
Block a user