From c6308d5388b686de8f29ab37d902db2f07511a83 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Thu, 10 Mar 2022 22:25:29 -0800 Subject: [PATCH] Refactor BlockType --- include/BlockType.hpp | 12 +++--- src/BlockType.cpp | 93 +++++++++++++++++++++++++++++++++++++++++-- src/Main.cpp | 67 ------------------------------- 3 files changed, 97 insertions(+), 75 deletions(-) diff --git a/include/BlockType.hpp b/include/BlockType.hpp index bb695d2..2bc78b1 100644 --- a/include/BlockType.hpp +++ b/include/BlockType.hpp @@ -1,17 +1,19 @@ #pragma once #include +#include 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> grid; uint width, height, starting_line; bool rotate; BlockType(TileType* _tile_type, const std::vector> _grid, bool _rotate = true); + + static BlockType* random(); + static void init(); + private: + inline static std::vector list = {}; + inline static bool inited = false; }; \ No newline at end of file diff --git a/src/BlockType.cpp b/src/BlockType.cpp index 63e8682..1694125 100644 --- a/src/BlockType.cpp +++ b/src/BlockType.cpp @@ -1,7 +1,4 @@ -#include - #include -#include BlockType::BlockType(TileType* _tile_type, const std::vector> _grid, bool _rotate) { tile_type = _tile_type; @@ -25,4 +22,94 @@ BlockType::BlockType(TileType* _tile_type, const std::vector> 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; } \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 9057829..88f6cc7 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -16,73 +16,6 @@ #include #include -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); }