• Main Page
  • Classes
  • Files
  • File List
  • File Members

code/include/TetrisPiece.h

Go to the documentation of this file.
00001 /*
00002  * TetriCycle
00003  * Copyright (C) 2009, 2010 Cale Scholl
00004  *
00005  * This file is part of TetriCycle.
00006  *
00007  * TetriCycle is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU Lesser General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * TetriCycle is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with TetriCycle.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 /** @file TetrisPiece.h
00022  * @brief Defines the TetrisPiece classes.
00023  * @author Cale Scholl / calvinss4
00024  */
00025 
00026 #pragma once
00027 #ifndef __TETRISPIECE_H__
00028 #define __TETRISPIECE_H__
00029 
00030 #include <gctypes.h> // for u8
00031 
00032 #include "mt.h"               // for genrand()
00033 #include "defines_Powerup.h"  // for PowerupId
00034 
00035 #define DEFAULT_BLOCKS_PER_PIECE 4
00036 
00037 /// Enumerates all tetris piece types.
00038 enum TetrisPieceId
00039 {
00040   TETRISPIECE_ID_NONE = -1,
00041   TETRISPIECE_ID_O, ///< square piece
00042   TETRISPIECE_ID_I, ///< line piece
00043   TETRISPIECE_ID_S, ///< reverse-squiggly piece
00044   TETRISPIECE_ID_Z, ///< squiggly piece
00045   TETRISPIECE_ID_L, ///< L piece
00046   TETRISPIECE_ID_J, ///< reverse-L piece
00047   TETRISPIECE_ID_T, ///< T piece
00048   TETRISPIECE_ID_JUNK, ///< junk piece (3x3 square with hollow center)
00049   TETRISPIECE_ID_MAX,
00050   TETRISPIECE_ID_RAND_MAX = 7
00051 };
00052 
00053 /// Every piece fits into a 4x4 grid.
00054 struct TetrisPieceDesc
00055 {
00056   u8 map[4][4];
00057 };
00058 
00059 /// Pieces have up to 4 possible rotations.
00060 extern TetrisPieceDesc g_pieceDesc[TETRISPIECE_ID_MAX][4];
00061 
00062 /// A tetris piece.
00063 class TetrisPiece
00064 {
00065 public:
00066   const TetrisPieceDesc& GetPieceDescription() { return *desc; }
00067   TetrisPieceId GetPieceId() { return pieceId; }
00068   PowerupId GetPowerupId() { return powerupId; }
00069   int GetRotation() { return rotation; }
00070   int GetX() { return x; }
00071   int GetY() { return y; }
00072 
00073   void InitPiece(TetrisPieceId id, int width = 0)
00074   {
00075     pieceId = id;
00076     rotation = 0;
00077     _ResetDesc();
00078     downctr = 0;
00079     if (width)
00080     {
00081       x = (width >> 1) - 2; // (playfield_width / 2) - 2 
00082       y = -1;
00083     }
00084   }
00085   void SetPowerupId(PowerupId id) { powerupId = id; }
00086   void SetRotation(int rot) { rotation = rot, _ResetDesc(); }
00087   void SetX(int xval) { x = xval; }
00088   void SetY(int yval) { y = yval; }
00089   void IncrementDownCounter() { ++downctr; }
00090   bool IsAccelEnabled() { return downctr > 0; }
00091 
00092   static TetrisPieceId GetNextId()
00093   {
00094     return (TetrisPieceId)(int)(genrand() * TETRISPIECE_ID_RAND_MAX);
00095   }
00096 
00097 private:
00098   const TetrisPieceDesc *desc;
00099   PowerupId powerupId;   ///< associated powerup
00100   TetrisPieceId pieceId; ///< tetris piece type
00101   u8 rotation;           ///< current rotation, in range [0-3]
00102   u8 downctr;            ///< number of times piece has moved down
00103 
00104   // The playfield is a playfield_width x playfield_height grid 
00105   // (default: 10x20); the upper left corner corresponds to (0,0).
00106   // (piece.x, piece.y) is a playfield coordinate that corresponds to the 
00107   // upper left corner of the tetris piece's 4x4 grid.
00108   s8 x; ///< playfield x-coordinate
00109   s8 y; ///< playfield y-coordinate
00110 
00111   void _ResetDesc() { desc = &g_pieceDesc[pieceId][rotation]; }
00112 };
00113 
00114 /// Connectivity information used for powerup pieces.
00115 /**
00116  * Describes how many individual blocks (0-4) of a tetris piece are still 
00117  * connected; once all the blocks have been cleared, the player gains the
00118  * associated powerup.
00119  */
00120 struct TetrisPieceConnectivityInfo
00121 {
00122   TetrisPieceConnectivityInfo(PowerupId pupId = POWERUP_ID_NONE,
00123                               u8 maxCount = DEFAULT_BLOCKS_PER_PIECE) : 
00124     powerupId(pupId),
00125     counter(maxCount) {}
00126 
00127   PowerupId powerupId;
00128   u8 counter;
00129 };
00130 
00131 /// An individual block that composes a tetris piece.
00132 /** 
00133  * All blocks composing a tetris piece point to the same 
00134  * TetrisPieceConnectivityInfo.
00135  */
00136 class TetrisPieceBlock
00137 {
00138 public:
00139   TetrisPieceBlock(TetrisPieceId id = TETRISPIECE_ID_NONE,
00140                    TetrisPieceConnectivityInfo *info = NULL) : 
00141     pieceId(id),
00142     connectivityInfo(info) {}
00143 
00144   TetrisPieceId pieceId;
00145   TetrisPieceConnectivityInfo *connectivityInfo;
00146 
00147   void SetConnectivityInfo(TetrisPieceConnectivityInfo *info)
00148   {
00149     connectivityInfo = info;
00150   }
00151 
00152   void Free()
00153   {
00154     delete connectivityInfo;
00155     connectivityInfo = NULL;
00156   }
00157 };
00158 
00159 #endif // __TETRISPIECE_H__

Generated on Wed Oct 20 2010 17:06:58 for TetriCycle by  doxygen 1.7.1