From f6c46e25bf859f374a06e3d578dc7fa3770bd629 Mon Sep 17 00:00:00 2001 From: Joshua Saxby <joshua.a.saxby@gmail.com> Date: Sun, 9 Feb 2020 21:54:49 +0000 Subject: [PATCH] Add lookup table for sprites and new image-referencing using it --- source/main.cpp | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 39d2f35..9d2b6b8 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -272,6 +272,36 @@ const uint8_t PIPE_VERTICAL_SPRITE[] __attribute__ ((aligned (4))) = { 0, 1, 0, }; +/* + * this lookup table maps shapes and directions to specific sprite images + * NOTE: this table only handles shapes which have different sprites for every + * direction, which is almost all of them, but does not include Square or Pipe. + */ +MicroBitImage SPRITE_SHEET[SHAPE_TYPES_COUNT][4] = { + // FLAT_L: N, E, S, W + { + MicroBitImage((ImageData*)FLAT_L_NORTH_SPRITE), + MicroBitImage((ImageData*)FLAT_L_EAST_SPRITE), + MicroBitImage((ImageData*)FLAT_L_SOUTH_SPRITE), + MicroBitImage((ImageData*)FLAT_L_WEST_SPRITE), + }, + // TALL_L: N, E, S, W + { + MicroBitImage((ImageData*)TALL_L_NORTH_SPRITE), + MicroBitImage((ImageData*)TALL_L_EAST_SPRITE), + MicroBitImage((ImageData*)TALL_L_SOUTH_SPRITE), + MicroBitImage((ImageData*)TALL_L_WEST_SPRITE), + }, + // UPSIDE_DOWN_T: N, E, S, W + { + MicroBitImage((ImageData*)UPSIDE_DOWN_T_NORTH_SPRITE), + MicroBitImage((ImageData*)UPSIDE_DOWN_T_EAST_SPRITE), + MicroBitImage((ImageData*)UPSIDE_DOWN_T_SOUTH_SPRITE), + MicroBitImage((ImageData*)UPSIDE_DOWN_T_WEST_SPRITE), + }, +}; + + /** * @brief Stores the shape and colour information for a Block in the game. */ @@ -299,22 +329,23 @@ private: Block::Block() : shape((Shape)UBIT.random(SHAPE_TYPES_COUNT)) // choose a random shape - , facing(RotationDirection::NORTH) // for now, all shapes face North + , facing((RotationDirection)UBIT.random(4)) // choose a random direction {} MicroBitImage Block::image() const { - // NOTE: This doesn't handle rotation direction yet + // retrieve the correct image sprite switch (this->shape) { - case Shape::FLAT_L: - return MicroBitImage((ImageData*)FLAT_L_NORTH_SPRITE); - case Shape::TALL_L: - return MicroBitImage((ImageData*)TALL_L_NORTH_SPRITE); - case Shape::UPSIDE_DOWN_T: - return MicroBitImage((ImageData*)UPSIDE_DOWN_T_NORTH_SPRITE); case Shape::SQUARE: + // Squares always look the same regardless of which direction they face return MicroBitImage((ImageData*)SQUARE_SPRITE); case Shape::PIPE: - return MicroBitImage((ImageData*)PIPE_NORTH_SPRITE); + // we only need to check if it's horizontal or vertical if it's Pipe + return (this->facing % 2u) == 0u ? + MicroBitImage((ImageData*)PIPE_HORIZONTAL_SPRITE) : // North/South + MicroBitImage((ImageData*)PIPE_VERTICAL_SPRITE); // East/West + default: + // all other types (Flat L, Tall L, Upside Down T) use the lookup table + return SPRITE_SHEET[this->shape][this->facing]; } } -- GitLab