Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Almost working.

Location:
code/branches/tetris/src/modules/tetris
Files:
5 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}
  • code/branches/tetris/src/modules/tetris/Tetris.h

    r8488 r8537  
    5656        public:
    5757            Tetris(BaseObject* creator); //!< Constructor. Registers and initializes the object.
    58             virtual ~Tetris(); //!< Destructor. Cleans up, if initialized.
    59            
    60             virtual void tick(float dt);
     58            virtual ~Tetris(); //!< Destructor. Cleans up, if initialized.           
    6159
    6260            virtual void start(void); //!< Starts the Tetris minigame.
    6361            virtual void end(void); ///!< Ends the Tetris minigame.
    6462
     63            virtual void tick(float dt);
     64
    6565            virtual void spawnPlayer(PlayerInfo* player); //!< Spawns the input player.
    6666
    67             /**
    68             @brief Set the TetrisCenterpoint (the playing field).
    69             @param center A pointer to the TetrisCenterpoint to be set.
    70             */
    71             void setCenterpoint(TetrisCenterpoint* center)
    72                 { this->center_ = center; }
     67            void setCenterpoint(TetrisCenterpoint* center);
    7368
    7469            PlayerInfo* getPlayer(void) const; //!< Get the player.
     70
     71            bool isValidMove(TetrisStone* stone, const Vector3& position);
    7572
    7673        protected:
    7774            virtual void spawnPlayersIfRequested(); //!< Spawns player.
    7875
     76        private:
    7977            void startStone(void); //!< Starts with the first stone.
    8078            void createStone(void);
    8179            void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats.
    82             bool correctStonePos(TetrisStone* stone); //!< Check whether the supplied stone is in an allowed position
     80            bool correctStonePos(TetrisStone* stone, const Vector3& position); //!< Check whether the supplied stone is in an allowed position
    8381           
    8482            PlayerInfo* player_;
     
    8684            WeakPtr<TetrisCenterpoint> center_; //!< The playing field.
    8785            std::vector<TetrisStone*> stones_; //!< A list of all stones in play.
     86            std::vector< std::vector<bool> > grid_;
    8887            TetrisStone* activeStone_;
    8988           
  • code/branches/tetris/src/modules/tetris/TetrisCenterpoint.cc

    r8249 r8537  
    6868        SUPER(TetrisCenterpoint, XMLPort, xmlelement, mode);
    6969
    70         XMLPortParam(TetrisCenterpoint, "width", setWidth, getWidth, xmlelement, mode);
    71         XMLPortParam(TetrisCenterpoint, "height", setHeight, setWidth, xmlelement, mode);
    72         XMLPortParam(TetrisCenterpoint, "stoneSize", setStoneSize, getStoneSize, xmlelement, mode);
     70        XMLPortParam(TetrisCenterpoint, "width", setWidth, getWidth, xmlelement, mode); // die Breite
     71        XMLPortParam(TetrisCenterpoint, "height", setHeight, setWidth, xmlelement, mode); // die Grösse
     72        XMLPortParam(TetrisCenterpoint, "stoneSize", setStoneSize, getStoneSize, xmlelement, mode); 
    7373        XMLPortParam(TetrisCenterpoint, "stoneTemplate", setStoneTemplate, getStoneTemplate, xmlelement, mode);
    7474        XMLPortParam(TetrisCenterpoint, "stoneSpeed", setStoneSpeed, getStoneSpeed, xmlelement, mode);
  • code/branches/tetris/src/modules/tetris/TetrisStone.cc

    r8249 r8537  
    3737#include "core/XMLPort.h"
    3838
     39#include "Tetris.h"
     40
    3941namespace orxonox
    4042{
     
    5254        this->delay_ = false;
    5355        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);
    5462    }
    5563
     
    7684        {
    7785            const Vector3& position = this->getPosition();
    78             this->setPosition(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
     86            Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
     87            if(!this->tetris_->isValidMove(this, newPos))
     88                return;
     89
     90            //this->previousPosition_ = position;
     91            this->setPosition(newPos);
    7992            this->delay_ = true;
    8093            this->delayTimer_.startTimer();
  • code/branches/tetris/src/modules/tetris/TetrisStone.h

    r8249 r8537  
    5757            virtual ~TetrisStone() {}
    5858
     59            virtual void tick(float dt);
     60
    5961            virtual void moveFrontBack(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
    6062            virtual void moveRightLeft(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
     
    7577                { return this->size_; }
    7678
     79            const Vector3& getPreviousPosition() const
     80                { return this->previousPosition_; }
     81
     82            void setGame(Tetris* tetris)
     83                { assert(tetris); tetris_ = tetris; }
     84
    7785        private:
    7886            void enableMovement(void)
     
    8290            bool delay_;
    8391            Timer delayTimer_;
     92
     93            Vector3 previousPosition_;
     94            Tetris* tetris_;
    8495    };
    8596}
Note: See TracChangeset for help on using the changeset viewer.