[Orxonox-commit 3889] r8563 - code/branches/tetris/src/modules/tetris
dafrick at orxonox.net
dafrick at orxonox.net
Tue May 24 21:07:34 CEST 2011
Author: dafrick
Date: 2011-05-24 21:07:33 +0200 (Tue, 24 May 2011)
New Revision: 8563
Modified:
code/branches/tetris/src/modules/tetris/Tetris.cc
code/branches/tetris/src/modules/tetris/Tetris.h
code/branches/tetris/src/modules/tetris/TetrisStone.cc
code/branches/tetris/src/modules/tetris/TetrisStone.h
Log:
Works now.
Modified: code/branches/tetris/src/modules/tetris/Tetris.cc
===================================================================
--- code/branches/tetris/src/modules/tetris/Tetris.cc 2011-05-24 17:52:02 UTC (rev 8562)
+++ code/branches/tetris/src/modules/tetris/Tetris.cc 2011-05-24 19:07:33 UTC (rev 8563)
@@ -86,34 +86,63 @@
{
SUPER(Tetris, tick, dt);
- if(this->activeStone_ != NULL && !this->isValidMove(this->activeStone_, this->activeStone_->getPosition()))
+ if(this->activeStone_ != NULL)
{
- this->activeStone_->setVelocity(Vector3::ZERO);
- //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true;
- this->createStone();
- this->startStone();
+ std::pair<bool, TetrisStone*> valid = this->isValidMove(this->activeStone_, this->activeStone_->getPosition());
+ if(!valid.first)
+ {
+ this->activeStone_->setVelocity(Vector3::ZERO);
+ if(valid.second != NULL)
+ {
+ Vector3 position = Vector3(this->activeStone_->getPosition().x, valid.second->getPosition().y+this->center_->getStoneSize(), this->activeStone_->getPosition().z);
+ this->activeStone_->setPosition(position);
+ }
+ this->createStone();
+ this->startStone();
+ }
}
}
- bool Tetris::isValidMove(TetrisStone* stone, const Vector3& position)
+ std::pair<bool, TetrisStone*> Tetris::isValidMove(TetrisStone* stone, const Vector3& position)
{
assert(stone);
+
+ std::pair<bool, TetrisStone*> valid = std::pair<bool, TetrisStone*>(true, NULL);
if(position.x < this->center_->getStoneSize()/2.0) //!< If the stone touches the left edge of the level
- return false;
+ valid.first = false;
else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level
- return false;
-
- if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
+ valid.first = false;
+ else if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
{
- stone->setVelocity(Vector3::ZERO);
- //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true;
- this->createStone();
- this->startStone();
- return false;
+ valid.first = false;
+ stone->setPosition(Vector3(stone->getPosition().x, this->center_->getStoneSize()/2.0, stone->getPosition().z));
}
- return this->correctStonePos(stone, position);
+ for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
+ {
+ if(stone == *it)
+ continue;
+
+ const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
+
+ if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y))
+ {
+ stone->setVelocity(Vector3::ZERO);
+ this->createStone();
+ this->startStone();
+ valid.first = false;
+ return valid;
+ }// This case applies if the stones overlap completely
+ else if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize()))
+ {
+ valid.first = false;
+ valid.second = *it;
+ return valid;
+ }// This case applies if the stones overlap partially vertically
+ }
+
+ return valid;
}
/**
@@ -231,39 +260,6 @@
/**
@brief
- Validate the stone position.
- @return
- Returns whether the supplied stone is in the correct position.
- */
- bool Tetris::correctStonePos(TetrisStone* stone, const Vector3& position)
- {
- assert(stone);
-
- for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
- {
- if(stone == *it)
- continue;
-
- Vector3 currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
-
- if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y))
- {
- stone->setVelocity(Vector3::ZERO);
- this->createStone();
- this->startStone();
- return false;
- }// This case applies if the stones overlap completely
- if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize()))
- {
- return false;
- }// This case applies if the stones overlap partially vertically
- }
-
- return true;
- }
-
- /**
- @brief
Get the player.
@return
Returns a pointer to the player. If there is no player, NULL is returned.
@@ -280,14 +276,6 @@
void Tetris::setCenterpoint(TetrisCenterpoint* center)
{
this->center_ = center;
-
- /*this->grid_.resize(this->center_->getWidth());
- for(std::vector< std::vector<bool> >::iterator it = this->grid_.begin(); it != this->grid_.end(); it++)
- {
- (*it).resize(this->center_->getHeight());
- for(std::vector<bool>::iterator it2 = (*it).begin(); it2 != (*it).end(); it2++)
- (*it).insert(it2, false);
- }*/
}
}
Modified: code/branches/tetris/src/modules/tetris/Tetris.h
===================================================================
--- code/branches/tetris/src/modules/tetris/Tetris.h 2011-05-24 17:52:02 UTC (rev 8562)
+++ code/branches/tetris/src/modules/tetris/Tetris.h 2011-05-24 19:07:33 UTC (rev 8563)
@@ -68,7 +68,7 @@
PlayerInfo* getPlayer(void) const; //!< Get the player.
- bool isValidMove(TetrisStone* stone, const Vector3& position);
+ std::pair<bool, TetrisStone*> isValidMove(TetrisStone* stone, const Vector3& position);
protected:
virtual void spawnPlayersIfRequested(); //!< Spawns player.
@@ -77,7 +77,6 @@
void startStone(void); //!< Starts with the first stone.
void createStone(void);
void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats.
- bool correctStonePos(TetrisStone* stone, const Vector3& position); //!< Check whether the supplied stone is in an allowed position
PlayerInfo* player_;
Modified: code/branches/tetris/src/modules/tetris/TetrisStone.cc
===================================================================
--- code/branches/tetris/src/modules/tetris/TetrisStone.cc 2011-05-24 17:52:02 UTC (rev 8562)
+++ code/branches/tetris/src/modules/tetris/TetrisStone.cc 2011-05-24 19:07:33 UTC (rev 8563)
@@ -53,14 +53,8 @@
this->size_ = 10.0f;
this->delay_ = false;
this->delayTimer_.setTimer(0.2f, false, createExecutor(createFunctor(&TetrisStone::enableMovement, this)));
- this->previousPosition_ = Vector3::ZERO;
}
- void TetrisStone::tick(float dt)
- {
- SUPER(TetrisStone, tick, dt);
- }
-
/**
@brief
Overloaded the function to rotate the stone.
@@ -69,7 +63,10 @@
*/
void TetrisStone::moveFrontBack(const Vector2& value)
{
-
+ if(value.x < 0)
+ {
+ this->setVelocity(this->getVelocity()*1.1);
+ }
}
/**
@@ -84,7 +81,7 @@
{
const Vector3& position = this->getPosition();
Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
- if(!this->tetris_->isValidMove(this, newPos))
+ if(!this->tetris_->isValidMove(this, newPos).first)
return;
//this->previousPosition_ = position;
Modified: code/branches/tetris/src/modules/tetris/TetrisStone.h
===================================================================
--- code/branches/tetris/src/modules/tetris/TetrisStone.h 2011-05-24 17:52:02 UTC (rev 8562)
+++ code/branches/tetris/src/modules/tetris/TetrisStone.h 2011-05-24 19:07:33 UTC (rev 8563)
@@ -56,8 +56,6 @@
TetrisStone(BaseObject* creator); //!< Constructor. Registers and initializes the object.
virtual ~TetrisStone() {}
- virtual void tick(float dt);
-
virtual void moveFrontBack(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
virtual void moveRightLeft(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
@@ -76,9 +74,6 @@
float getSize(void) const
{ return this->size_; }
- const Vector3& getPreviousPosition() const
- { return this->previousPosition_; }
-
void setGame(Tetris* tetris)
{ assert(tetris); tetris_ = tetris; }
@@ -90,7 +85,6 @@
bool delay_;
Timer delayTimer_;
- Vector3 previousPosition_;
Tetris* tetris_;
};
}
More information about the Orxonox-commit
mailing list