-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelset.cpp
More file actions
138 lines (121 loc) · 4.4 KB
/
levelset.cpp
File metadata and controls
138 lines (121 loc) · 4.4 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <fstream>
#include "entity.hpp"
#include "levelset.hpp"
#include "tileset.hpp"
#include "globals.hpp"
Level::Level(Levelset& ls, const std::string& name, std::istream& in)
: ls(&ls), name(name), tiles()
{
GetLine(in, tileset);
std::string line;
GetLine(in, line);
auto ary = Split(line);
if (ary.size() != 4)
throw std::runtime_error("Invalid level neighbor specifiaction");
std::copy(ary.begin(), ary.end(), neighbors.begin());
GetLine(in, line);
ary = Split(line);
if (ary.size() != 3)
throw std::runtime_error("Invalid level color");
color = { uint8_t(boost::lexical_cast<int>(ary[0])),
uint8_t(boost::lexical_cast<int>(ary[1])),
uint8_t(boost::lexical_cast<int>(ary[2])) };
for (size_t j = 0; j < HEIGHT; ++j)
{
getline(in, line);
if (!in.good()) throw std::runtime_error("level truncated");
size_t n = std::min(size_t(line.length()), size_t(WIDTH));
for (size_t i = 0; i < n; ++i)
tiles[i][j] = (line[i] == ' ') ? 0 : line[i];
}
GetLine(in, line);
while (line != "end")
{
ents.push_back(EntityFactory::Create(Split(line)));
GetLine(in, line);
}
}
Tileset& Level::Tileset() const
{
return Tileset::GetTileset(tileset);
}
void Level::Simul(double dt)
{
for (auto& ptr : ents)
ptr->Simul(dt);
}
bool N(char c, const std::string& n)
{
for (char x: n)
if (x == c) return true;
return false;
}
void Level::Render() const
{
auto& ts = Tileset();
auto ticks = SDL_GetTicks() / 50;
for (unsigned x = 0; x < WIDTH; ++x)
for (unsigned y = 0; y < HEIGHT; ++y)
if (tiles[x][y])
{
auto& el = ts.GetElement(tiles[x][y]);
int k = 0;
if (el.flags & Tileset::HAS_DIRECTION)
{
auto& n = el.neighbor;
if (x == WIDTH-1 || N(tiles[x+1][y], n)) k |= 1;
if (x == WIDTH-1 || y == 0 || N(tiles[x+1][y-1], n)) k |= 2;
if ( y == 0 || N(tiles[x] [y-1], n)) k |= 4;
if (x == 0 || y == 0 || N(tiles[x-1][y-1], n)) k |= 8;
if (x == 0 || N(tiles[x-1][y], n)) k |= 16;
if (x == 0 || y == HEIGHT-1 || N(tiles[x-1][y+1], n)) k |= 32;
if ( y == HEIGHT-1 || N(tiles[x] [y+1], n)) k |= 64;
if (x == WIDTH-1 || y == HEIGHT-1 || N(tiles[x+1][y+1], n)) k |= 128;
}
else
k = ticks % el.anim_length;
SDL_Rect s = { int(el.coords[k].x*8), int(el.coords[k].y*8), 8, 8 };
SDL_Rect d = { int(x*8*SCREEN_MUL), int(y*8*SCREEN_MUL),
8*SCREEN_MUL, 8*SCREEN_MUL };
if (el.flags & Tileset::COLORED)
SDL_SetTextureColorMod(ts.Texture(), color.r, color.g, color.b);
else
SDL_SetTextureColorMod(ts.Texture(), 255, 255, 255);
int flip = SDL_FLIP_NONE;
if (el.flags & Tileset::H_FLIP) flip |= SDL_FLIP_HORIZONTAL;
if (el.flags & Tileset::V_FLIP) flip |= SDL_FLIP_VERTICAL;
SDL_RenderCopyEx(renderer, ts.Texture(), &s, &d, 0, nullptr,
SDL_RendererFlip(flip));
}
for (auto& ptr : ents)
ptr->Render();
}
Levelset::Levelset(const std::string& file)
{
std::ifstream in(file);
std::string line;
GetLine(in, line);
auto ary = Split(line);
if (ary.size() != 4) throw std::runtime_error("Invalid levelset spec");
start_level = ary[0];
start_x = boost::lexical_cast<int>(ary[1]);
start_y = boost::lexical_cast<int>(ary[2]);
start_flipped = boost::lexical_cast<bool>(ary[3]);
while (GetLine(in, line))
{
levels.emplace(std::piecewise_construct,
std::forward_as_tuple(line),
std::forward_as_tuple(*this, line, in));
}
if (in.bad() || levels.size() == 0)
throw std::runtime_error("Levelset: no levels loaded");
}
void Levelset::SetRespawns() const
{
respawn_x = start_x;
respawn_y = start_y;
respawn_flip = start_flipped;
respawn_level = start_level;
}