Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapon2/src/core/RootGameState.cc @ 2481

Last change on this file since 2481 was 2103, checked in by rgrieder, 16 years ago

Merged r2101 (objecthierarchy) to trunk.

  • Property svn:eol-style set to native
File size: 5.2 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#include "RootGameState.h"
30
31#include "util/Debug.h"
32#include "util/Exception.h"
33#include "Core.h"
34#include "Clock.h"
35#include "CommandLine.h"
36
37namespace orxonox
38{
39    SetCommandLineArgument(state, "gui").shortcut("s");
40
41    RootGameState::RootGameState(const std::string& name)
42        : GameState<GameStateBase>(name)
43        , stateRequest_("")
44    {
45    }
46
47    RootGameState::~RootGameState()
48    {
49    }
50
51    /**
52    @brief
53        Internal method that actually makes the state transition. Since it is internal,
54        the method can assume certain things to be granted (like 'this' is always active).
55    */
56    void RootGameState::makeTransition(GameStateBase* source, GameStateBase* destination)
57    {
58        if (source != 0)
59        {
60            // transition was not initiated by root itself
61            this->activeChild_ = 0;
62        }
63
64        if (destination == this)
65        {
66            // this marks the end of the game.
67            return;
68        }
69
70        // Check for 'destination' in the children map first
71        std::map<GameStateBase*, GameStateBase*>::const_iterator it
72            = this->grandchildrenToChildren_.find(destination);
73        if (it != this->grandchildrenToChildren_.end())
74        {
75            OrxAssert(dynamic_cast<GameStateBase*>(it->second) != 0,
76                "There was a mix with RootGameState and GameState, could not cast.");
77            GameStateBase* child = static_cast<GameStateBase*>(it->second);
78            // child state. Don't use 'state', might be a grandchild!
79            this->activeChild_ = child;
80            child->makeTransition(this, destination);
81        }
82        else
83        {
84            // root doesn't have a parent..
85            OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root.");
86        }
87    }
88
89    void RootGameState::gotoState(const std::string& name)
90    {
91        GameStateBase* request = getState(name);
92        if (request)
93        {
94            GameStateBase* current = getCurrentState();
95            if (current)
96            {
97                current->makeTransition(0, request);
98            }
99            else
100            {
101                // Root is not yet active. This is a violation.
102                ThrowException(GameState, "Activate Root before requesting a state.");
103            }
104        }
105        else
106        {
107            COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
108        }
109    }
110
111    /**
112    @brief
113        Makes a state transition according to the state tree. You can choose any state
114        in the tree to do the call. The function finds the current state on its own.
115    @param state
116        The state to be entered, has to exist in the tree.
117    */
118    void RootGameState::requestState(const std::string& name)
119    {
120        this->stateRequest_ = name;
121    }
122
123    /**
124    @brief
125        Starts the game. The little 'while' denotes the main loop.
126        Whenever the root state is selected, the game ends.
127    @param name
128        State to start with (usually main menu or specified by command line)
129    */
130    void RootGameState::start()
131    {
132#ifdef NDEBUG
133        try
134        {
135#endif
136            // start global orxonox time
137            Clock clock;
138
139            // create the Core settings to configure the output level
140            Core::getInstance();
141
142            this->activate();
143
144            // get initial state from command line
145            gotoState(CommandLine::getValue("state"));
146
147            while (this->activeChild_)
148            {
149                clock.capture();
150
151                this->tick(clock);
152
153                if (this->stateRequest_ != "")
154                    gotoState(stateRequest_);
155            }
156
157            this->deactivate();
158#ifdef NDEBUG
159        }
160        // Note: These are all unhandled exceptions that should not have made its way here!
161        // almost complete game catch block to display the messages appropriately.
162        catch (std::exception& ex)
163        {
164            COUT(1) << ex.what() << std::endl;
165            COUT(1) << "Program aborted." << std::endl;
166        }
167        // anything that doesn't inherit from std::exception
168        catch (...)
169        {
170            COUT(1) << "An unidentifiable exception has occured. Program aborted." << std::endl;
171        }
172#endif
173    }
174}
Note: See TracBrowser for help on using the repository browser.