Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/orxonox/worldentities/EffectContainer.cc @ 12396

Last change on this file since 12396 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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