Advertisement
Ember

tetromino

Jun 20th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. // Game namespace ---------------------------------------------------------------------
  2. class Tetris
  3. {
  4. public:
  5.     // A tetromino --------------------------------------------------------------------
  6.     class Block
  7.     {
  8.     public:
  9.         // A piece of a tetromino -----------------------------------------------------
  10.         struct Piece
  11.         {
  12.             // Simply the position
  13.             Byte2 Position;
  14.         };
  15.  
  16.         // A rotation state -----------------------------------------------------------
  17.         struct State
  18.         {
  19.             // State building object forward declaration
  20.             class Builder;
  21.             // Four pieces to make a block
  22.             Piece Piece[4];
  23.             // Helper to build up a state I guess
  24.             Builder& Build();
  25.         };
  26.  
  27.         // Rotation state builder object ----------------------------------------------
  28.         class State::Builder
  29.         {
  30.         public:
  31.             // The state we're building
  32.             State& Handle;
  33.             // A counter
  34.             Byte Slot;
  35.  
  36.         public:
  37.             // Default constructor
  38.             Builder(State& state) : Handle(state), Slot(0) {};
  39.             // Destructor
  40.             ~Builder() = default;
  41.  
  42.             // Build operator!
  43.             Builder& operator () (Byte x, Byte y)
  44.             {
  45.                 // Add the position to the state
  46.                 Handle.Piece[Slot].Position.x = x;
  47.                 Handle.Piece[Slot].Position.y = y;
  48.                 // Ready to next slot!
  49.                 Slot++;
  50.  
  51.                 return *this;
  52.             }
  53.         };
  54.  
  55.     public:
  56.         // Four rotation states and their pieces makes a tetromino
  57.         State State[4];
  58.         Byte Rotation;
  59.     private:
  60.         // Eurgh
  61.         Byte Padding[3];
  62.  
  63.     public:
  64.         // Default constructor
  65.         Block() : State(), Rotation() {};
  66.         // Destructor
  67.         ~Block() = default;
  68.  
  69.         // No methods for now
  70.     };
  71.  
  72.     static Void CreateBlock()
  73.     {
  74.         // An L tetromino
  75.         Block tetromino;
  76.         // -- //
  77.         tetromino.State[0].Build()(0, 1)(1, 1)(2, 1)(3, 1);
  78.     }
  79. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement