Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/worldentities/EffectContainer.cc @ 6187

Last change on this file since 6187 was 6187, checked in by rgrieder, 14 years ago

Added better engine effects: you can still add any WorldEntity to the MultiStateEngine, but you have to enclose it in an EffectContainer that has one XMLParameter called "condition". There you can write conditions containing the following words: and, or, not, idle, normal, boost, brake. The last four words are mutually exclusive states.
For an example see spaceship_assff.oxt

  • Property svn:eol-style set to native
File size: 3.3 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 "EffectContainer.h"
30
31extern "C" {
32#include <lua.h>
33}
34
35#include "core/CoreIncludes.h"
36#include "core/LuaState.h"
37#include "core/XMLPort.h"
38
39namespace orxonox
40{
41    CreateFactory(EffectContainer);
42
43    EffectContainer::EffectContainer(BaseObject* creator)
44        : StaticEntity(creator)
45        , lua_(NULL)
46    {
47        RegisterObject(EffectContainer);
48    }
49
50    EffectContainer::~EffectContainer()
51    {
52    }
53
54    void EffectContainer::XMLPort(Element& xmlelement, XMLPort::Mode mode)
55    {
56        SUPER(EffectContainer, XMLPort, xmlelement, mode);
57        XMLPortParam(EffectContainer, "condition", setCondition, getCondition, xmlelement, mode);
58        XMLPortObject(EffectContainer, WorldEntity, "", addEffect, getEffect, xmlelement, mode);
59    }
60
61    void EffectContainer::setCondition(const std::string& condition)
62    {
63        // TODO: Watch out for code injection!
64        this->condition_ = condition;
65        if (this->lua_)
66        {
67            this->lua_->doString(
68                this->functionName_ + " = function() \n"
69                "    return (" + condition + ") \n"
70                "end"
71            );
72        }
73    }
74
75    void EffectContainer::setLuaState(LuaState* state, const std::string& functionName)
76    {
77        this->functionName_ = functionName;
78        this->lua_ = state;
79        this->setCondition(this->condition_);
80    }
81
82    void EffectContainer::addEffect(WorldEntity* effect)
83    {
84        this->effects_.push_back(effect);
85    }
86
87    WorldEntity* EffectContainer::getEffect(unsigned int index) const
88    {
89        unsigned int i = 0;
90        for (std::vector<WorldEntity*>::const_iterator it = this->effects_.begin(); it != this->effects_.end(); ++it)
91            if (i == index)
92                return (*it);
93        return NULL;
94    }
95
96    void EffectContainer::updateCondition()
97    {
98        if (this->lua_)
99        {
100            lua_getglobal(this->lua_->getInternalLuaState(), this->functionName_.c_str());
101            lua_call(this->lua_->getInternalLuaState(), 0, 1);
102            bool result = (bool)lua_toboolean(this->lua_->getInternalLuaState(), -1);
103            lua_pop(this->lua_->getInternalLuaState(), 1);
104            for (std::vector<WorldEntity*>::const_iterator it = this->effects_.begin(); it != this->effects_.end(); ++it)
105            {
106                (*it)->setMainState(result);
107            }
108        }
109    }
110}
Note: See TracBrowser for help on using the repository browser.