Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8563


Ignore:
Timestamp:
May 24, 2011, 9:07:33 PM (13 years ago)
Author:
dafrick
Message:

Works now.

Location:
code/branches/tetris/src/modules/tetris
Files:
4 edited

Legend:

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

    r8537 r8563  
    8787        SUPER(Tetris, tick, dt);
    8888
    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)
     89        if(this->activeStone_ != NULL)
     90        {
     91            std::pair<bool, TetrisStone*> valid = this->isValidMove(this->activeStone_, this->activeStone_->getPosition());
     92            if(!valid.first)
     93            {
     94                this->activeStone_->setVelocity(Vector3::ZERO);
     95                if(valid.second != NULL)
     96                {
     97                    Vector3 position = Vector3(this->activeStone_->getPosition().x, valid.second->getPosition().y+this->center_->getStoneSize(), this->activeStone_->getPosition().z);
     98                    this->activeStone_->setPosition(position);
     99                }
     100                this->createStone();
     101                this->startStone();
     102            }
     103        }
     104    }
     105
     106    std::pair<bool, TetrisStone*> Tetris::isValidMove(TetrisStone* stone, const Vector3& position)
    99107    {
    100108        assert(stone);
     109
     110        std::pair<bool, TetrisStone*> valid = std::pair<bool, TetrisStone*>(true, NULL);
    101111       
    102112        if(position.x < this->center_->getStoneSize()/2.0)  //!< If the stone touches the left edge of the level
    103             return false;
     113            valid.first = false;
    104114        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);
     115            valid.first = false;
     116        else if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
     117        {
     118            valid.first = false;
     119            stone->setPosition(Vector3(stone->getPosition().x, this->center_->getStoneSize()/2.0, stone->getPosition().z));
     120        }
     121
     122        for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
     123        {
     124            if(stone == *it)
     125                continue;
     126
     127            const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
     128
     129            if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y))
     130            {
     131                stone->setVelocity(Vector3::ZERO);
     132                this->createStone();
     133                this->startStone();
     134                valid.first = false;
     135                return valid;
     136            }// This case applies if the stones overlap completely
     137            else if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize()))
     138            {
     139                valid.first = false;
     140                valid.second = *it;
     141                return valid;
     142            }// This case applies if the stones overlap partially vertically
     143        }
     144
     145        return valid;
    117146    }
    118147
     
    232261    /**
    233262    @brief
    234         Validate the stone position.
    235     @return
    236         Returns whether the supplied stone is in the correct position.
    237     */
    238     bool Tetris::correctStonePos(TetrisStone* stone, const Vector3& position)
    239     {
    240         assert(stone);
    241 
    242         for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
    243         {
    244             if(stone == *it)
    245                 continue;
    246 
    247             Vector3 currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
    248            
    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;
    263     }
    264 
    265     /**
    266     @brief
    267263        Get the player.
    268264    @return
     
    281277    {
    282278        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         }*/
    291279    }
    292280
  • code/branches/tetris/src/modules/tetris/Tetris.h

    r8537 r8563  
    6969            PlayerInfo* getPlayer(void) const; //!< Get the player.
    7070
    71             bool isValidMove(TetrisStone* stone, const Vector3& position);
     71            std::pair<bool, TetrisStone*> isValidMove(TetrisStone* stone, const Vector3& position);
    7272
    7373        protected:
     
    7878            void createStone(void);
    7979            void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats.
    80             bool correctStonePos(TetrisStone* stone, const Vector3& position); //!< Check whether the supplied stone is in an allowed position
    8180           
    8281            PlayerInfo* player_;
  • code/branches/tetris/src/modules/tetris/TetrisStone.cc

    r8537 r8563  
    5454        this->delay_ = false;
    5555        this->delayTimer_.setTimer(0.2f, false, createExecutor(createFunctor(&TetrisStone::enableMovement, this)));
    56         this->previousPosition_ = Vector3::ZERO;
    57     }
    58 
    59     void TetrisStone::tick(float dt)
    60     {
    61         SUPER(TetrisStone, tick, dt);
    6256    }
    6357
     
    7064    void TetrisStone::moveFrontBack(const Vector2& value)
    7165    {
    72        
     66        if(value.x < 0)
     67        {
     68            this->setVelocity(this->getVelocity()*1.1);
     69        }
    7370    }
    7471
     
    8582            const Vector3& position = this->getPosition();
    8683            Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
    87             if(!this->tetris_->isValidMove(this, newPos))
     84            if(!this->tetris_->isValidMove(this, newPos).first)
    8885                return;
    8986
  • code/branches/tetris/src/modules/tetris/TetrisStone.h

    r8537 r8563  
    5757            virtual ~TetrisStone() {}
    5858
    59             virtual void tick(float dt);
    60 
    6159            virtual void moveFrontBack(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
    6260            virtual void moveRightLeft(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
     
    7775                { return this->size_; }
    7876
    79             const Vector3& getPreviousPosition() const
    80                 { return this->previousPosition_; }
    81 
    8277            void setGame(Tetris* tetris)
    8378                { assert(tetris); tetris_ = tetris; }
     
    9186            Timer delayTimer_;
    9287
    93             Vector3 previousPosition_;
    9488            Tetris* tetris_;
    9589    };
Note: See TracChangeset for help on using the changeset viewer.