-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLevel.cpp
More file actions
95 lines (87 loc) · 2.26 KB
/
Copy pathGameLevel.cpp
File metadata and controls
95 lines (87 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "GameLevel.h"
#include "GameLevel.h"
#include <fstream>
#include <sstream>
#include <iostream>
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::Draw(Sprite& renderer)
{
for (GameObj& tile : this->Bricks)
{
if (!tile.destroyed)
tile.Draw(renderer);
}
}
bool GameLevel::isCompleted()
{
for (GameObj& tile : this->Bricks)
{
if (!tile.isSolid && !tile.destroyed)
{
return false;
}
}
return true;
}
void GameLevel::init(std::vector<std::vector<unsigned int>> tileData, unsigned int levelWidth, unsigned int levelHeight)
{
unsigned int height = tileData.size();
unsigned int width = tileData[0].size();
float unit_width = levelWidth / static_cast<float>(width);
float unit_height = levelHeight / height;
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x)
{
if (tileData[y][x] == 1)
{
glm::vec2 pos(unit_width * x, unit_height * y);
glm::vec2 size(unit_width, unit_height);
GameObj 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)
{
// Colors the tile
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(GameObj(pos, size, ResourceManager::GetTexture("block"), color));
}
}
}
}