Refactor BlockType

cpp
Elnu 2 years ago
parent e88ff1ebe6
commit c6308d5388

@ -1,17 +1,19 @@
#pragma once
#include <TileType.hpp>
#include <vector>
class BlockType {
public:
static BlockType i, j, l, o, s, t, z;
static BlockType* list[];
static BlockType* random() {
return list[rand() % 7];
}
TileType* tile_type;
std::vector<std::vector<bool>> grid;
uint width, height, starting_line;
bool rotate;
BlockType(TileType* _tile_type, const std::vector<std::vector<bool>> _grid, bool _rotate = true);
static BlockType* random();
static void init();
private:
inline static std::vector<BlockType> list = {};
inline static bool inited = false;
};

@ -1,7 +1,4 @@
#include <cstdlib>
#include <BlockType.hpp>
#include <TileType.hpp>
BlockType::BlockType(TileType* _tile_type, const std::vector<std::vector<bool>> _grid, bool _rotate) {
tile_type = _tile_type;
@ -25,4 +22,94 @@ BlockType::BlockType(TileType* _tile_type, const std::vector<std::vector<bool>>
height = y + 1 - starting_line;
}
}
}
BlockType* BlockType::random() {
init();
return &list[rand() % list.size()];
}
void BlockType::init() {
if (inited) {
return;
}
// I block
list.push_back(BlockType(new TileType(
sf::IntRect(0, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(0, TILE_SIZE, TILE_SIZE, TILE_SIZE)
), {
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
}
));
// J Block OK
list.push_back(BlockType(new TileType(
sf::IntRect(TILE_SIZE, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE)
), {
{1, 0, 0},
{1, 1, 1},
{0, 0, 0}
}
));
// L Block
list.push_back(BlockType(new TileType(
sf::IntRect(TILE_SIZE * 2, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 2, TILE_SIZE, TILE_SIZE, TILE_SIZE)
), {
{0, 0, 1},
{1, 1, 1},
{0, 0, 0}
}
));
// O Block
list.push_back(BlockType(new TileType(
sf::IntRect(TILE_SIZE * 3, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 3, TILE_SIZE, TILE_SIZE, TILE_SIZE)
), {
{1, 1},
{1, 1}
}, false
));
// S Block OK
list.push_back(BlockType(new TileType(
sf::IntRect(TILE_SIZE * 4, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 4, TILE_SIZE, TILE_SIZE, TILE_SIZE)
), {
{0, 1, 1},
{1, 1, 0},
{0, 0, 0}
}
));
// T Block OK
list.push_back(BlockType(new TileType(
sf::IntRect(TILE_SIZE * 5, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 5, TILE_SIZE, TILE_SIZE, TILE_SIZE)
), {
{0, 1, 0},
{1, 1, 1},
{0, 0, 0}
}
));
// Z Block OK
list.push_back(BlockType(new TileType(
sf::IntRect(TILE_SIZE * 6, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 6, TILE_SIZE, TILE_SIZE, TILE_SIZE)
), {
{1, 1, 0},
{0, 1, 1},
{0, 0, 0}
}
));
inited = true;
}

@ -16,73 +16,6 @@
#include <BlockType.hpp>
#include <Block.hpp>
TileType tile_type_0(
sf::IntRect(0, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(0, TILE_SIZE, TILE_SIZE, TILE_SIZE)
);
TileType tile_type_1(
sf::IntRect(TILE_SIZE, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE)
);
TileType tile_type_2(
sf::IntRect(TILE_SIZE * 2, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 2, TILE_SIZE, TILE_SIZE, TILE_SIZE)
);
TileType tile_type_3(
sf::IntRect(TILE_SIZE * 3, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 3, TILE_SIZE, TILE_SIZE, TILE_SIZE)
);
TileType tile_type_4(
sf::IntRect(TILE_SIZE * 4, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 4, TILE_SIZE, TILE_SIZE, TILE_SIZE)
);
TileType tile_type_5(
sf::IntRect(TILE_SIZE * 5, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 5, TILE_SIZE, TILE_SIZE, TILE_SIZE)
);
TileType tile_type_6(
sf::IntRect(TILE_SIZE * 6, 0, TILE_SIZE, TILE_SIZE),
sf::IntRect(TILE_SIZE * 6, TILE_SIZE, TILE_SIZE, TILE_SIZE)
);
// https://gamedev.stackexchange.com/a/17978
BlockType BlockType::i(&tile_type_0, {
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
});
BlockType BlockType::j(&tile_type_1, {
{1, 0, 0},
{1, 1, 1},
{0, 0, 0}
});
BlockType BlockType::l(&tile_type_2, {
{0, 0, 1},
{1, 1, 1},
{0, 0, 0}
});
BlockType BlockType::o(&tile_type_3, {
{1, 1},
{1, 1}
}, false);
BlockType BlockType::s(&tile_type_4, {
{0, 1, 1},
{1, 1, 0},
{0, 0, 0}
});
BlockType BlockType::t(&tile_type_5, {
{0, 1, 0},
{1, 1, 1},
{0, 0, 0}
});
BlockType BlockType::z(&tile_type_6, {
{1, 1, 0},
{0, 1, 1},
{0, 0, 0}
});
BlockType* BlockType::list[] = {&i, &j, &l, &o, &s, &t, &z};
uint get_level(int lines) {
return std::min(lines / LINES_PER_LEVEL, 15);
}

Loading…
Cancel
Save