Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/GameState.cc @ 2848

Last change on this file since 2848 was 2844, checked in by rgrieder, 16 years ago

Implemented new GameState concept. It doesn't differ that much from the old one, but there's still lots of changes.
The most important change is that one GameState can occur multiple times in the hierarchy.

Short log:

  • No RootGameState anymore. Simply the highest is root.
  • Game::requestGameState(name) can refer to the parent, grandparent, great-grandparent, etc. or one of the children.
  • Requested states are saved. So if you select "level", the next request (even right after the call) will be relative to "level"
  • Game::popState() will simply unload the current one
  • Re added Main.cc again because Game as well as GameState have been moved to the core
  • Adapted all GameStates to the new system

Things should already work, except for network support because standalone only works with a little hack.
We can now start creating a better hierarchy.

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
31@brief
32    Implementation of GameState class.
33*/
34
35#include "GameState.h"
36#include <cassert>
37#include "util/Debug.h"
38#include "util/Exception.h"
39#include "Clock.h"
40
41namespace orxonox
42{
43    /**
44    @brief
45        Constructor only initialises variables and sets the name permanently.
46    */
47    GameState::GameState(const std::string& name)
48        : name_(name)
49        , parent_(0)
50    {
51        State temp = {false, false, false, false, false};
52        this->activity_ = temp;
53    }
54
55    /**
56    @brief
57        Destructor only checks that we don't delete an active state.
58    */
59    GameState::~GameState()
60    {
61        OrxAssert(this->activity_.active == false, "Deleting an active GameState is a very bad idea..");
62    }
63
64    /**
65    @brief
66        Adds a child to the current tree. The Child can contain children of its own.
67        But you cannot a state tree that already has an active state.
68    @param state
69        The state to be added.
70    */
71    void GameState::addChild(GameState* state)
72    {
73        assert(state != NULL);
74
75        std::map<std::string, GameState*>::const_iterator it = this->children_.find(state->getName());
76        if (it == this->children_.end())
77        {
78            this->children_[state->getName()] = state;
79            // mark us as parent
80            state->setParent(this);
81        }
82        else
83        {
84            ThrowException(GameState, "Cannot add two children with the same name");
85        }
86    }
87
88    /**
89    @brief
90        Removes a child by instance. This splits the tree in two parts,
91        each of them functional on its own.
92    @param state
93        GameState by instance pointer
94    */
95    void GameState::removeChild(GameState* state)
96    {
97        assert(state != NULL);
98
99        std::map<std::string, GameState*>::iterator it = this->children_.find(state->getName());
100        if (it != this->children_.end())
101            this->children_.erase(it);
102        else
103        {
104            ThrowException(GameState, "Game state '" + name_ + "' doesn't have a child named '"
105                + state->getName() + "'.");
106        }
107    }
108
109    void GameState::activateInternal()
110    {
111        this->activity_.activating = true;
112        this->activate();
113        this->activity_.activating = false;
114        this->activity_.active = true;
115    }
116
117    void GameState::deactivateInternal()
118    {
119        this->activity_.active = false;
120        this->activity_.deactivating = true;
121        this->activate();
122        this->activity_.deactivating = false;
123        this->activity_.suspended = false;
124        this->activity_.updating = false;
125    }
126
127    void GameState::updateInternal(const Clock& time)
128    {
129        this->activity_.updating = true;
130        this->update(time);
131        this->activity_.updating = false;
132    }
133}
Note: See TracBrowser for help on using the repository browser.