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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "WorldEntity.h" |
---|
31 | |
---|
32 | #include <cassert> |
---|
33 | #include <OgreSceneManager.h> |
---|
34 | |
---|
35 | #include "core/CoreIncludes.h" |
---|
36 | #include "core/XMLPort.h" |
---|
37 | #include "util/Convert.h" |
---|
38 | #include "util/Exception.h" |
---|
39 | |
---|
40 | #include "objects/Scene.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | const Vector3 WorldEntity::FRONT = Vector3::NEGATIVE_UNIT_Z; |
---|
45 | const Vector3 WorldEntity::BACK = Vector3::UNIT_Z; |
---|
46 | const Vector3 WorldEntity::LEFT = Vector3::NEGATIVE_UNIT_X; |
---|
47 | const Vector3 WorldEntity::RIGHT = Vector3::UNIT_X; |
---|
48 | const Vector3 WorldEntity::DOWN = Vector3::NEGATIVE_UNIT_Y; |
---|
49 | const Vector3 WorldEntity::UP = Vector3::UNIT_Y; |
---|
50 | |
---|
51 | WorldEntity::WorldEntity(BaseObject* creator) : BaseObject(creator), Synchronisable(creator) |
---|
52 | { |
---|
53 | RegisterObject(WorldEntity); |
---|
54 | |
---|
55 | if (!this->getScene() || !this->getScene()->getRootSceneNode()) |
---|
56 | ThrowException(AbortLoading, "Can't create WorldEntity, no scene or no root-scenenode given."); |
---|
57 | |
---|
58 | this->node_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); |
---|
59 | |
---|
60 | this->parent_ = 0; |
---|
61 | this->parentID_ = OBJECTID_UNKNOWN; |
---|
62 | |
---|
63 | this->node_->setPosition(Vector3::ZERO); |
---|
64 | this->node_->setOrientation(Quaternion::IDENTITY); |
---|
65 | |
---|
66 | this->registerVariables(); |
---|
67 | } |
---|
68 | |
---|
69 | WorldEntity::~WorldEntity() |
---|
70 | { |
---|
71 | if (this->isInitialized()) |
---|
72 | { |
---|
73 | this->node_->detachAllObjects(); |
---|
74 | |
---|
75 | for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ) |
---|
76 | delete (*(it++)); |
---|
77 | |
---|
78 | if (this->parent_) |
---|
79 | this->detachFromParent(); |
---|
80 | |
---|
81 | this->node_->removeAllChildren(); |
---|
82 | |
---|
83 | if (this->getScene()->getSceneManager()) |
---|
84 | this->getScene()->getSceneManager()->destroySceneNode(this->node_->getName()); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
89 | { |
---|
90 | SUPER(WorldEntity, XMLPort, xmlelement, mode); |
---|
91 | |
---|
92 | XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition, xmlelement, mode, const Vector3&); |
---|
93 | XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&); |
---|
94 | XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode); |
---|
95 | XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode); |
---|
96 | XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode); |
---|
97 | XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode); |
---|
98 | XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode); |
---|
99 | XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&); |
---|
100 | XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode); |
---|
101 | |
---|
102 | XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode); |
---|
103 | } |
---|
104 | |
---|
105 | void WorldEntity::registerVariables() |
---|
106 | { |
---|
107 | REGISTERSTRING(this->mainStateName_, direction::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedMainState)); |
---|
108 | |
---|
109 | REGISTERDATA(this->bActive_, direction::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity)); |
---|
110 | REGISTERDATA(this->bVisible_, direction::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility)); |
---|
111 | |
---|
112 | REGISTERDATA(this->getScale3D().x, direction::toclient); |
---|
113 | REGISTERDATA(this->getScale3D().y, direction::toclient); |
---|
114 | REGISTERDATA(this->getScale3D().z, direction::toclient); |
---|
115 | |
---|
116 | REGISTERDATA(this->parentID_, direction::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent)); |
---|
117 | } |
---|
118 | |
---|
119 | void WorldEntity::updateParent() |
---|
120 | { |
---|
121 | if (this->parentID_ != OBJECTID_UNKNOWN) |
---|
122 | { |
---|
123 | WorldEntity* parent = dynamic_cast<WorldEntity*>(Synchronisable::getSynchronisable(this->parentID_)); |
---|
124 | if (parent) |
---|
125 | this->attachToParent(parent); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | void WorldEntity::attachNode(Ogre::SceneNode* node) |
---|
130 | { |
---|
131 | Ogre::Node* parent = node->getParent(); |
---|
132 | if (parent) |
---|
133 | parent->removeChild(node); |
---|
134 | this->node_->addChild(node); |
---|
135 | } |
---|
136 | |
---|
137 | void WorldEntity::detachNode(Ogre::SceneNode* node) |
---|
138 | { |
---|
139 | this->node_->removeChild(node); |
---|
140 | // this->getScene()->getRootSceneNode()->addChild(node); |
---|
141 | } |
---|
142 | |
---|
143 | void WorldEntity::attachToNode(Ogre::SceneNode* node) |
---|
144 | { |
---|
145 | Ogre::Node* parent = this->node_->getParent(); |
---|
146 | if (parent) |
---|
147 | parent->removeChild(this->node_); |
---|
148 | node->addChild(this->node_); |
---|
149 | } |
---|
150 | |
---|
151 | void WorldEntity::detachFromNode(Ogre::SceneNode* node) |
---|
152 | { |
---|
153 | node->removeChild(this->node_); |
---|
154 | // this->getScene()->getRootSceneNode()->addChild(this->node_); |
---|
155 | } |
---|
156 | |
---|
157 | void WorldEntity::attach(WorldEntity* object) |
---|
158 | { |
---|
159 | if (object == this) |
---|
160 | { |
---|
161 | COUT(2) << "Warning: Can't attach a WorldEntity to itself." << std::endl; |
---|
162 | return; |
---|
163 | } |
---|
164 | |
---|
165 | if (object->getParent()) |
---|
166 | object->detachFromParent(); |
---|
167 | |
---|
168 | this->attachNode(object->node_); |
---|
169 | |
---|
170 | this->children_.insert(object); |
---|
171 | object->parent_ = this; |
---|
172 | object->parentID_ = this->getObjectID(); |
---|
173 | } |
---|
174 | |
---|
175 | void WorldEntity::detach(WorldEntity* object) |
---|
176 | { |
---|
177 | this->detachNode(object->node_); |
---|
178 | this->children_.erase(object); |
---|
179 | object->parent_ = 0; |
---|
180 | object->parentID_ = OBJECTID_UNKNOWN; |
---|
181 | } |
---|
182 | |
---|
183 | WorldEntity* WorldEntity::getAttachedObject(unsigned int index) const |
---|
184 | { |
---|
185 | unsigned int i = 0; |
---|
186 | for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ++it) |
---|
187 | { |
---|
188 | if (i == index) |
---|
189 | return (*it); |
---|
190 | ++i; |
---|
191 | } |
---|
192 | return 0; |
---|
193 | } |
---|
194 | } |
---|