Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 18, 2008, 9:17:30 PM (16 years ago)
Author:
rgrieder
Message:

started implementing the GameStates. Not much for now, but most of the Orxonox.cc code has been copied to GSRoot, GSGraphics and GSLevel.
There is no level currently, but the main menu is shown. This is more of an svn save because I would really like to have Member ConsoleCommands.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/core/GameState.cc

    r1660 r1661  
    5656    /**
    5757    @brief
     58        Destructor only checks that we don't delete an active state.
     59    */
     60    GameState::~GameState()
     61    {
     62        if (this->bActive_)
     63        {
     64            if (this->parent_)
     65                this->requestState(this->parent_->getName());
     66            else
     67                this->requestState("");
     68        }
     69    }
     70
     71    /**
     72    @brief
    5873        Adds a child to the current tree. The Child can contain children of its own.
    5974        But you cannot a state tree that already has an active state.
     
    104119        // mark us as parent
    105120        state->parent_ = this;
     121    }
     122
     123    /**
     124    @brief
     125        Removes a child by instance. This splits the tree in two parts,
     126        each of them functional on its own.
     127    @param state
     128        GameState by instance pointer
     129    */
     130    void GameState::removeChild(GameState* state)
     131    {
     132        std::map<GameState*, GameState*>::iterator it = this->grandchildrenToChildren_.find(state);
     133        if (it != this->grandchildrenToChildren_.end())
     134        {
     135            if (state->isActive())
     136            {
     137                ThrowException(GameState, "Cannot remove active game state child '"
     138                    + state->getName() + "' from '" + name_ + "'.");
     139                //COUT(2) << "Warning: Cannot remove active game state child '" << state->getName()
     140                //    << "' from '" << name_ << "'." << std::endl;
     141            }
     142            else
     143            {
     144                for (std::map<GameState*, GameState*>::const_iterator it = state->grandchildrenToChildren_.begin();
     145                    it != state->grandchildrenToChildren_.end(); ++it)
     146                {
     147                    this->grandchildRemoved(it->first);
     148                }
     149                this->grandchildRemoved(state);
     150            }
     151        }
     152        else
     153        {
     154            ThrowException(GameState, "Game state '" + name_ + "' doesn't have a child named '"
     155                + state->getName() + "'. Removal skipped.");
     156            //COUT(2) << "Warning: Game state '" << name_ << "' doesn't have a child named '"
     157            //    << state->getName() << "'. Removal skipped." << std::endl;
     158        }
     159    }
     160
     161    /**
     162    @brief
     163        Removes a child by name. This splits the tree in two parts,
     164        each of them functional on its own.
     165    @param state
     166        GameState by name
     167    */
     168
     169    void GameState::removeChild(const std::string& name)
     170    {
     171        GameState* state = checkState(name);
     172        if (state)
     173        {
     174            removeChild(state);
     175        }
     176        else
     177        {
     178            ThrowException(GameState, "GameState '" + name + "' doesn't exist.");
     179            //COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
     180        }
    106181    }
    107182
     
    126201    /**
    127202    @brief
     203        Tells a state that one of its children has removed a child. This is necessary
     204        to fill the internal maps correctly.
     205    @param child
     206        The child who notices this state.
     207    @param grandchild
     208        The child that has been removed.
     209    */
     210    void GameState::grandchildRemoved(GameState* grandchild)
     211    {
     212        // adjust the two maps correctly.
     213        this->allChildren_.erase(grandchild->getName());
     214        this->grandchildrenToChildren_.erase(grandchild);
     215        if (this->parent_)
     216            this->parent_->grandchildRemoved(grandchild);
     217    }
     218
     219    /**
     220    @brief
    128221        Checks whether a specific game states exists in the hierarchy.
    129222    @remarks
     
    191284    void GameState::requestState(const std::string& name)
    192285    {
    193         GameState* request = checkState(name);
    194         GameState* current = getCurrentState();
    195         if (request)
    196         {
     286        if (name == "")
     287        {
     288            // user would like to leave every state.
     289            GameState* current = getCurrentState();
    197290            if (current)
    198291            {
    199                 // There is already an active state
    200                 current->makeTransition(request);
    201             }
    202             else
    203             {
    204                 // no active state --> we have to activate the root node first.
     292                // Deactivate all states but root
    205293                GameState* root = getRootNode();
    206                 root->activate();
    207                 root->makeTransition(request);
    208             }
    209         }
    210         else
    211         {
    212             COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
     294                current->makeTransition(root);
     295                // Kick root too
     296                root->deactivate();
     297            }
     298        }
     299        else
     300        {
     301            GameState* request = checkState(name);
     302            GameState* current = getCurrentState();
     303            if (request)
     304            {
     305                if (current)
     306                {
     307                    // There is already an active state
     308                    current->makeTransition(request);
     309                }
     310                else
     311                {
     312                    // no active state --> we have to activate the root node first.
     313                    GameState* root = getRootNode();
     314                    root->activate();
     315                    root->makeTransition(request);
     316                }
     317            }
     318            else
     319            {
     320                COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
     321            }
    213322        }
    214323    }
Note: See TracChangeset for help on using the changeset viewer.