[Orxonox-commit 5621] r10281 - in code/trunk: data/defaultConfig src/orxonox src/orxonox/gamestates src/orxonox/gametypes

landauf at orxonox.net landauf at orxonox.net
Tue Feb 24 22:54:25 CET 2015


Author: landauf
Date: 2015-02-24 22:54:24 +0100 (Tue, 24 Feb 2015)
New Revision: 10281

Added:
   code/trunk/src/orxonox/gamestates/GSLevelMemento.h
Modified:
   code/trunk/data/defaultConfig/keybindings.ini
   code/trunk/src/orxonox/OrxonoxPrereqs.h
   code/trunk/src/orxonox/gamestates/GSLevel.cc
   code/trunk/src/orxonox/gamestates/GSLevel.h
   code/trunk/src/orxonox/gametypes/Gametype.cc
   code/trunk/src/orxonox/gametypes/Gametype.h
Log:
added command 'reloadLevel' (by default on F5) which reloads the level while the player's camera remains at the same position

Modified: code/trunk/data/defaultConfig/keybindings.ini
===================================================================
--- code/trunk/data/defaultConfig/keybindings.ini	2015-02-19 11:39:06 UTC (rev 10280)
+++ code/trunk/data/defaultConfig/keybindings.ini	2015-02-24 21:54:24 UTC (rev 10281)
@@ -34,8 +34,8 @@
 KeyF2="OverlayGroup toggleVisibility Stats"
 KeyF3="OrxonoxOverlay show QuestGUI"
 KeyF4="OrxonoxOverlay show PickupInventory"
-KeyF5="startchat"
-KeyF6=
+KeyF5="reloadLevel"
+KeyF6="startchat"
 KeyF7=
 KeyF8=
 KeyF9=

Modified: code/trunk/src/orxonox/OrxonoxPrereqs.h
===================================================================
--- code/trunk/src/orxonox/OrxonoxPrereqs.h	2015-02-19 11:39:06 UTC (rev 10280)
+++ code/trunk/src/orxonox/OrxonoxPrereqs.h	2015-02-24 21:54:24 UTC (rev 10281)
@@ -76,6 +76,8 @@
     class PlayerManager;
     class Radar;
     class Scene;
+    class GSLevelMemento;
+    struct GSLevelMementoState;
 
     // chat
     class ChatHistory;

Modified: code/trunk/src/orxonox/gamestates/GSLevel.cc
===================================================================
--- code/trunk/src/orxonox/gamestates/GSLevel.cc	2015-02-19 11:39:06 UTC (rev 10280)
+++ code/trunk/src/orxonox/gamestates/GSLevel.cc	2015-02-24 21:54:24 UTC (rev 10281)
@@ -28,6 +28,7 @@
  */
 
 #include "GSLevel.h"
+#include "GSLevelMemento.h"
 
 #include <OgreCompositorManager.h>
 
@@ -36,6 +37,7 @@
 #include "core/input/InputState.h"
 #include "core/input/KeyBinderManager.h"
 #include "core/Core.h"
+#include "core/CoreIncludes.h"
 #include "core/Game.h"
 #include "core/GameMode.h"
 #include "core/GUIManager.h"
@@ -53,9 +55,11 @@
 
     static const std::string __CC_startMainMenu_name = "startMainMenu";
     static const std::string __CC_changeGame_name = "changeGame";
+    static const std::string __CC_reloadLevel_name = "reloadLevel";
 
     SetConsoleCommand(__CC_startMainMenu_name, &GSLevel::startMainMenu).deactivate();
     SetConsoleCommand(__CC_changeGame_name, &GSLevel::changeGame).defaultValues("").deactivate();
+    SetConsoleCommand(__CC_reloadLevel_name, &GSLevel::reloadLevel).deactivate();
 
     GSLevel::GSLevel(const GameStateInfo& info)
         : GameState(info)
@@ -107,7 +111,10 @@
         }
 
         if (GameMode::isStandalone())
+        {
             ModifyConsoleCommand(__CC_changeGame_name).activate();
+            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(this).activate();
+        }
     }
 
     void GSLevel::deactivate()
@@ -139,7 +146,10 @@
         }
 
         if (GameMode::isStandalone())
+        {
             ModifyConsoleCommand(__CC_changeGame_name).deactivate();
+            ModifyConsoleCommand(__CC_reloadLevel_name).setObject(NULL).deactivate();
+        }
     }
 
     void GSLevel::update(const Clock& time)
@@ -186,6 +196,30 @@
             orxout(internal_info) << " Try harder!" << endl;
     }
 
+    void GSLevel::reloadLevel()
+    {
+        // export all states
+        std::vector<GSLevelMementoState*> states;
+        for (ObjectList<GSLevelMemento>::iterator it = ObjectList<GSLevelMemento>::begin(); it != ObjectList<GSLevelMemento>::end(); ++it)
+        {
+            GSLevelMementoState* state = it->exportMementoState();
+            if (state)
+                states.push_back(state);
+        }
+
+        // reload level (or better: reload the whole gamestate)
+        this->deactivate();
+        this->activate();
+
+        // import all states
+        for (ObjectList<GSLevelMemento>::iterator it = ObjectList<GSLevelMemento>::begin(); it != ObjectList<GSLevelMemento>::end(); ++it)
+            it->importMementoState(states);
+
+        // delete states
+        for (size_t i = 0; i < states.size(); ++i)
+            delete states[i];
+    }
+
     /**
     @brief
         Starts the MainMenu.
@@ -213,4 +247,15 @@
         Game::getInstance().popState();
         Game::getInstance().requestStates("standalone, level");
     }
+
+
+
+    ///////////////////////////////////////////////////////////////////////////
+
+    RegisterAbstractClass(GSLevelMemento).inheritsFrom(Class(OrxonoxInterface));
+
+    GSLevelMemento::GSLevelMemento()
+    {
+        RegisterObject(GSLevelMemento);
+    }
 }

Modified: code/trunk/src/orxonox/gamestates/GSLevel.h
===================================================================
--- code/trunk/src/orxonox/gamestates/GSLevel.h	2015-02-19 11:39:06 UTC (rev 10280)
+++ code/trunk/src/orxonox/gamestates/GSLevel.h	2015-02-24 21:54:24 UTC (rev 10281)
@@ -50,6 +50,8 @@
         static void startMainMenu(void); //!< Starts the MainMenu
         static void changeGame(const std::string& level); //!< Terminates the current game and starts a new game.
 
+        void reloadLevel();
+
     protected:
         void loadLevel();
         void unloadLevel();

Added: code/trunk/src/orxonox/gamestates/GSLevelMemento.h
===================================================================
--- code/trunk/src/orxonox/gamestates/GSLevelMemento.h	                        (rev 0)
+++ code/trunk/src/orxonox/gamestates/GSLevelMemento.h	2015-02-24 21:54:24 UTC (rev 10281)
@@ -0,0 +1,69 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _GSLevelMemento_H__
+#define _GSLevelMemento_H__
+
+#include "OrxonoxPrereqs.h"
+
+#include "core/class/OrxonoxInterface.h"
+
+namespace orxonox
+{
+    /**
+        @brief This class is an interface for all instances that want to maintain a state beyond the reloading of a level.
+    */
+    class _OrxonoxExport GSLevelMemento : virtual public OrxonoxInterface
+    {
+        friend class GSLevel;
+
+        public:
+            GSLevelMemento(); // Implemented in GSLevel.cc
+
+        protected:
+            /**
+             * Returns the state of this memento. Returns NULL if no state needed to persist.
+             */
+            virtual GSLevelMementoState* exportMementoState() = 0;
+
+            /**
+             * A list of the states of all mementos. must find the correct one by itself.
+             */
+            virtual void importMementoState(const std::vector<GSLevelMementoState*>& states) = 0;
+    };
+
+    /**
+        @brief Represents the state of a memento. Needs to be inherited by the actual state.
+    */
+    struct _OrxonoxExport GSLevelMementoState
+    {
+        virtual ~GSLevelMementoState() {} // virtual destructor for RTTI
+    };
+}
+
+#endif /* _GSLevelMemento_H__ */


Property changes on: code/trunk/src/orxonox/gamestates/GSLevelMemento.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: code/trunk/src/orxonox/gametypes/Gametype.cc
===================================================================
--- code/trunk/src/orxonox/gametypes/Gametype.cc	2015-02-19 11:39:06 UTC (rev 10280)
+++ code/trunk/src/orxonox/gametypes/Gametype.cc	2015-02-24 21:54:24 UTC (rev 10281)
@@ -44,6 +44,7 @@
 #include "worldentities/pawns/Spectator.h"
 #include "worldentities/pawns/Pawn.h"
 #include "overlays/OverlayGroup.h"
+#include "Scene.h"
 
 namespace orxonox
 {
@@ -524,4 +525,68 @@
     {
         GSLevel::startMainMenu();
     }
+
+    GSLevelMementoState* Gametype::exportMementoState()
+    {
+        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
+        {
+            if (it->first->isHumanPlayer() && it->first->getControllableEntity() && it->first->getControllableEntity()->getCamera())
+            {
+                Camera* camera = it->first->getControllableEntity()->getCamera();
+
+                GametypeMementoState* state = new GametypeMementoState();
+                state->cameraPosition_ = camera->getWorldPosition();
+                state->cameraOrientation_ = camera->getWorldOrientation();
+                state->sceneName_ = camera->getScene()->getName();
+                return state;
+            }
+        }
+
+        return NULL;
+    }
+
+    void Gametype::importMementoState(const std::vector<GSLevelMementoState*>& states)
+    {
+        // find correct memento state
+        GametypeMementoState* state = NULL;
+        for (size_t i = 0; i < states.size(); ++i)
+        {
+            state = dynamic_cast<GametypeMementoState*>(states[i]);
+            if (state)
+                break;
+        }
+
+        if (!state)
+            return;
+
+        // find correct scene
+        Scene* scene = NULL;
+        for (ObjectList<Scene>::iterator it = ObjectList<Scene>::begin(); it != ObjectList<Scene>::end(); ++it)
+        {
+            if (it->getName() == state->sceneName_)
+            {
+                scene = *it;
+                break;
+            }
+        }
+
+        if (!scene)
+        {
+            orxout(internal_warning) << "Could not find scene with name " << state->sceneName_ << endl;
+            return;
+        }
+
+        // find correct player and assign default entity with original position & orientation
+        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
+        {
+            if (it->first->isHumanPlayer())
+            {
+                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(scene->getContext());
+                entity->setPosition(state->cameraPosition_);
+                entity->setOrientation(state->cameraOrientation_);
+                it->first->startControl(entity);
+                break;
+            }
+        }
+    }
 }

Modified: code/trunk/src/orxonox/gametypes/Gametype.h
===================================================================
--- code/trunk/src/orxonox/gametypes/Gametype.h	2015-02-19 11:39:06 UTC (rev 10280)
+++ code/trunk/src/orxonox/gametypes/Gametype.h	2015-02-24 21:54:24 UTC (rev 10281)
@@ -40,6 +40,7 @@
 #include "tools/interfaces/Tickable.h"
 #include "infos/GametypeInfo.h"
 #include "tools/Timer.h"
+#include "gamestates/GSLevelMemento.h"
 
 namespace orxonox
 {
@@ -62,7 +63,7 @@
         int killed_;
     };
 
-    class _OrxonoxExport Gametype : public BaseObject, public Tickable
+    class _OrxonoxExport Gametype : public BaseObject, public Tickable, public GSLevelMemento
     {
         friend class PlayerInfo;
 
@@ -171,6 +172,9 @@
             virtual void spawnPlayersIfRequested();
             virtual void spawnDeadPlayersIfRequested();
 
+            virtual GSLevelMementoState* exportMementoState();
+            virtual void importMementoState(const std::vector<GSLevelMementoState*>& states);
+
             SmartPtr<GametypeInfo> gtinfo_;
 
             bool bAutoStart_;
@@ -200,6 +204,16 @@
             /* HACK HACK HACK */
             Timer showMenuTimer_;
     };
+
+    /**
+        @brief Keeps position and orientation of the camera, as well as the name of current scene.
+    */
+    struct _OrxonoxExport GametypeMementoState : public GSLevelMementoState
+    {
+        Vector3 cameraPosition_;
+        Quaternion cameraOrientation_;
+        std::string sceneName_;
+    };
 }
 
 #endif /* _Gametype_H__ */




More information about the Orxonox-commit mailing list