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 | |
---|
31 | extern "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 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | CreateFactory(EffectContainer); |
---|
43 | |
---|
44 | EffectContainer::EffectContainer(BaseObject* creator) |
---|
45 | : BaseObject(creator) |
---|
46 | , lua_(NULL) |
---|
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 (std::vector<WorldEntity*>::const_iterator it = this->effects_.begin(); it != this->effects_.end(); ++it) |
---|
92 | if (i == index) |
---|
93 | return (*it); |
---|
94 | return NULL; |
---|
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 (std::vector<WorldEntity*>::const_iterator it = this->effects_.begin(); it != this->effects_.end(); ++it) |
---|
106 | { |
---|
107 | (*it)->setMainState(result); |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|