Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 23, 2011, 3:06:08 PM (13 years ago)
Author:
catherine
Message:

Almost working.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/tetris/src/modules/tetris/Tetris.cc

    r8488 r8537  
    8282       
    8383    }
    84    
     84
    8585    void Tetris::tick(float dt)
    8686    {
    8787        SUPER(Tetris, tick, dt);
    88        
    89         TetrisStone* stone = this->activeStone_;
    90         if(stone != NULL)
    91         {
    92             // Get the current position of the active stone
    93             Vector3 position = stone->getPosition();
    94            
    95             if(position.x < this->center_->getStoneSize()/2.0)  //!< If the stone touches the left edge of the level
    96                 position.x = this->center_->getStoneSize()/2.0;
    97             else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level
    98                 position.x = (this->center_->getWidth()-0.5)*this->center_->getStoneSize();
    99 
    100             if(!this->correctStonePos(stone)) //!< If the stone touches another stone
    101             {
    102                 stone->setVelocity(Vector3::ZERO);
    103                 this->createStone();
    104                 this->startStone();
    105             }
    106 
    107             if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
    108             {
    109                 position.y = this->center_->getStoneSize()/2.0;
    110                 stone->setVelocity(Vector3::ZERO);
    111                 this->createStone();
    112                 this->startStone();
    113             }
    114            
    115             stone->setPosition(position);
    116         }
     88
     89        if(this->activeStone_ != NULL && !this->isValidMove(this->activeStone_, this->activeStone_->getPosition()))
     90        {
     91            this->activeStone_->setVelocity(Vector3::ZERO);
     92            //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true;
     93            this->createStone();
     94            this->startStone();
     95        }
     96    }
     97
     98    bool Tetris::isValidMove(TetrisStone* stone, const Vector3& position)
     99    {
     100        assert(stone);
     101       
     102        if(position.x < this->center_->getStoneSize()/2.0)  //!< If the stone touches the left edge of the level
     103            return false;
     104        else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level
     105            return false;
     106       
     107        if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
     108        {
     109            stone->setVelocity(Vector3::ZERO);
     110            //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true;
     111            this->createStone();
     112            this->startStone();
     113            return false;
     114        }
     115
     116        return this->correctStonePos(stone, position);
    117117    }
    118118
     
    135135        }
    136136
    137         // Start the timer. After it has expired the ball is started.
     137        // Start the timer. After it has expired the stone is started.
    138138        this->starttimer_.startTimer();
    139139
     
    227227        float yPos = (this->center_->getHeight()-0.5)*this->center_->getStoneSize();
    228228        stone->setPosition(xPos, yPos, 0.0f);
     229        stone->setGame(this);
    229230    }
    230231
     
    235236        Returns whether the supplied stone is in the correct position.
    236237    */
    237     bool Tetris::correctStonePos(TetrisStone* stone)
    238     {
     238    bool Tetris::correctStonePos(TetrisStone* stone, const Vector3& position)
     239    {
     240        assert(stone);
     241
    239242        for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
    240243        {
    241             TetrisStone* currentStone = it->_Ptr(); //!< Gives access to the current stone in the list
    242             Vector3 currentStonePosition = it->_Ptr()->getPosition(); //!< Saves the position of the currentStone
    243             Vector3 stonePosition = stone->getPosition(); //!< Saves the position of the supplied stone
     244            if(stone == *it)
     245                continue;
     246
     247            Vector3 currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
    244248           
    245             // @TODO:   Use the TetrisStone member functions to check both stones for an overlap.
    246             //          Also make sure to correct the stone position accordingly.
    247             //
    248             // This case applies if the stones overlap completely
    249             //if((stonePosition.x == currentStonePosition.x) && (stonePosition.y == currentStonePosition.y))
    250             // This case applies if the stones overlap partially vertically
    251             //if(stonePosition.y - stone->getHeight()/2 < currentStonePosition.y + currentStone->getHeight()/2)
    252 
    253            
    254         }
     249            if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y))
     250            {
     251                stone->setVelocity(Vector3::ZERO);
     252                this->createStone();
     253                this->startStone();
     254                return false;
     255            }// This case applies if the stones overlap completely
     256            if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize()))
     257            {
     258                return false;
     259            }// This case applies if the stones overlap partially vertically
     260        }
     261
     262        return true;
    255263    }
    256264
     
    266274    }
    267275
     276    /**
     277    @brief Set the TetrisCenterpoint (the playing field).
     278    @param center A pointer to the TetrisCenterpoint to be set.
     279    */
     280    void Tetris::setCenterpoint(TetrisCenterpoint* center)
     281    {
     282        this->center_ = center;
     283
     284        /*this->grid_.resize(this->center_->getWidth());
     285        for(std::vector< std::vector<bool> >::iterator it = this->grid_.begin(); it != this->grid_.end(); it++)
     286        {
     287            (*it).resize(this->center_->getHeight());
     288            for(std::vector<bool>::iterator it2 = (*it).begin(); it2 != (*it).end(); it2++)
     289                (*it).insert(it2, false);
     290        }*/
     291    }
     292
    268293}
Note: See TracChangeset for help on using the changeset viewer.