[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[4838] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[4839] | 18 | #include "element_2d.h" |
---|
[4840] | 19 | #include "render_2d.h" |
---|
[1853] | 20 | |
---|
[4843] | 21 | #include "graphics_engine.h" |
---|
| 22 | #include "p_node.h" |
---|
[4858] | 23 | #include "load_param.h" |
---|
| 24 | #include "tinyxml.h" |
---|
| 25 | #include "class_list.h" |
---|
[5082] | 26 | #include "list.h" |
---|
[4843] | 27 | |
---|
[1856] | 28 | using namespace std; |
---|
[1853] | 29 | |
---|
[1856] | 30 | |
---|
[3245] | 31 | /** |
---|
[4838] | 32 | * standard constructor |
---|
| 33 | * @todo this constructor is not jet implemented - do it |
---|
[3245] | 34 | */ |
---|
[5084] | 35 | Element2D::Element2D (Element2D* parent) |
---|
[3365] | 36 | { |
---|
[5084] | 37 | this->init(parent); |
---|
[3365] | 38 | } |
---|
[1853] | 39 | |
---|
[3245] | 40 | /** |
---|
[4838] | 41 | * standard deconstructor |
---|
[3245] | 42 | */ |
---|
[4838] | 43 | Element2D::~Element2D () |
---|
[3543] | 44 | { |
---|
| 45 | // delete what has to be deleted here |
---|
[4840] | 46 | Render2D::getInstance()->unregisterElement2D(this); |
---|
[5088] | 47 | |
---|
| 48 | if (this->toCoordinate != NULL) |
---|
| 49 | delete this->toCoordinate; |
---|
| 50 | if (this->toDirection != NULL) |
---|
| 51 | delete this->toDirection; |
---|
[3543] | 52 | } |
---|
[4843] | 53 | |
---|
| 54 | |
---|
[4858] | 55 | /** |
---|
| 56 | * initializes a Element2D |
---|
| 57 | */ |
---|
[5084] | 58 | void Element2D::init(Element2D* parent) |
---|
[4847] | 59 | { |
---|
| 60 | this->setClassID(CL_ELEMENT_2D, "Element2D"); |
---|
| 61 | |
---|
[4861] | 62 | this->setVisibility(true); |
---|
[5068] | 63 | this->setActiveness(true); |
---|
[4856] | 64 | this->setPosition2D(0,0); |
---|
[4861] | 65 | this->setAlignment(E2D_ALIGN_NONE); |
---|
[4862] | 66 | this->layer = E2D_TOP; |
---|
[5084] | 67 | |
---|
[5082] | 68 | this->setParentMode2D(E2D_PARENT_ALL); |
---|
[5084] | 69 | this->children = new tList<Element2D>; |
---|
[5082] | 70 | this->bRelCoorChanged = true; |
---|
| 71 | this->bRelDirChanged = true; |
---|
[5088] | 72 | this->toCoordinate = NULL; |
---|
| 73 | this->toDirection = NULL; |
---|
[5084] | 74 | if (parent == NULL) |
---|
| 75 | this->parent = NULL; |
---|
| 76 | // else |
---|
| 77 | // this->setParent2D(parent); |
---|
[5082] | 78 | |
---|
[4847] | 79 | Render2D::getInstance()->registerElement2D(this); |
---|
| 80 | } |
---|
| 81 | |
---|
[4843] | 82 | /** |
---|
[4858] | 83 | * Loads the Parameters of an Element2D from... |
---|
| 84 | * @param root The XML-element to load from |
---|
| 85 | */ |
---|
| 86 | void Element2D::loadParams(const TiXmlElement* root) |
---|
| 87 | { |
---|
| 88 | LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment) |
---|
| 89 | .describe("loads the alignment: (either: center, left, right or screen-center)"); |
---|
| 90 | |
---|
| 91 | LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer) |
---|
| 92 | .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); |
---|
| 93 | |
---|
| 94 | LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode) |
---|
| 95 | .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); |
---|
| 96 | |
---|
| 97 | LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D) |
---|
| 98 | .describe("the _relative_ position (away from alignment) this 2d-element shows"); |
---|
| 99 | |
---|
[4860] | 100 | LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility) |
---|
| 101 | .describe("if the Element is visible or not"); |
---|
[4858] | 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * sets the alignment of the 2D-element in form of a String |
---|
| 106 | * @param alignment the alignment @see loadParams |
---|
| 107 | */ |
---|
| 108 | void Element2D::setAlignment(const char* alignment) |
---|
| 109 | { |
---|
| 110 | if (!strcmp(alignment, "center")) |
---|
| 111 | this->setAlignment(E2D_ALIGN_CENTER); |
---|
| 112 | else if (!strcmp(alignment, "left")) |
---|
| 113 | this->setAlignment(E2D_ALIGN_LEFT); |
---|
| 114 | else if (!strcmp(alignment, "right")) |
---|
| 115 | this->setAlignment(E2D_ALIGN_RIGHT); |
---|
| 116 | else if (!strcmp(alignment, "screen-center")) |
---|
| 117 | this->setAlignment(E2D_ALIGN_SCREEN_CENTER); |
---|
| 118 | } |
---|
| 119 | |
---|
[4862] | 120 | |
---|
[4858] | 121 | /** |
---|
[4862] | 122 | * moves a Element to another layer |
---|
| 123 | * @param layer the Layer this is drawn on |
---|
| 124 | */ |
---|
| 125 | void Element2D::setLayer(E2D_LAYER layer) |
---|
| 126 | { |
---|
| 127 | Render2D::getInstance()->moveToLayer(this, layer); |
---|
| 128 | this->layer = layer; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
[4858] | 132 | * sets the layer onto which this 2D-element is projected to. |
---|
| 133 | * @param layer the layer @see loadParams |
---|
| 134 | */ |
---|
| 135 | void Element2D::setLayer(const char* layer) |
---|
| 136 | { |
---|
| 137 | if (!strcmp(layer, "top")) |
---|
| 138 | this->setLayer(E2D_TOP); |
---|
| 139 | else if (!strcmp(layer, "medium")) |
---|
| 140 | this->setLayer(E2D_MEDIUM); |
---|
| 141 | else if (!strcmp(layer, "bottom")) |
---|
| 142 | this->setLayer(E2D_BOTTOM); |
---|
| 143 | else if (!strcmp(layer, "below-all")) |
---|
| 144 | this->setLayer(E2D_BELOW_ALL); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * sets a node, this 2D-Element should be shown upon |
---|
| 150 | * @param bindNode the name of the Node (should be existing) |
---|
| 151 | */ |
---|
| 152 | void Element2D::setBindNode(const char* bindNode) |
---|
| 153 | { |
---|
| 154 | const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE)); |
---|
| 155 | if (tmpBindNode != NULL) |
---|
| 156 | this->bindNode = tmpBindNode; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
[4843] | 160 | * this sets the position of the Element on the screen. |
---|
[4847] | 161 | * Use this in the your tick function, if you want the element to be automatically positioned. |
---|
[5084] | 162 | * |
---|
| 163 | * @todo must be in update |
---|
[4843] | 164 | */ |
---|
| 165 | void Element2D::positioning() |
---|
| 166 | { |
---|
[4856] | 167 | // setting the Position of this 2D-Element. |
---|
| 168 | if (this->alignment == E2D_ALIGN_SCREEN_CENTER) |
---|
[4843] | 169 | { |
---|
[4856] | 170 | absPos2D.x = GraphicsEngine::getInstance()->getResolutionX()/2 + this->relPos2D[0]; |
---|
| 171 | absPos2D.y = GraphicsEngine::getInstance()->getResolutionY()/2 + this->relPos2D[1]; |
---|
| 172 | absPos2D.depth = 0; |
---|
| 173 | } |
---|
| 174 | else if (this->bindNode) |
---|
| 175 | { |
---|
[4843] | 176 | GLdouble projectPos[3]; |
---|
[4847] | 177 | gluProject(this->bindNode->getAbsCoor().x, |
---|
| 178 | this->bindNode->getAbsCoor().y, |
---|
| 179 | this->bindNode->getAbsCoor().z, |
---|
| 180 | GraphicsEngine::modMat, |
---|
| 181 | GraphicsEngine::projMat, |
---|
| 182 | GraphicsEngine::viewPort, |
---|
| 183 | projectPos, |
---|
| 184 | projectPos+1, |
---|
| 185 | projectPos+2); |
---|
| 186 | absPos2D.x = projectPos[0] + this->relPos2D[0]; |
---|
| 187 | absPos2D.y = GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relPos2D[1]; |
---|
| 188 | absPos2D.depth = projectPos[2]; |
---|
[4843] | 189 | } |
---|
| 190 | else |
---|
| 191 | { |
---|
[4847] | 192 | absPos2D.x = this->relPos2D[0]; |
---|
| 193 | absPos2D.y = this->relPos2D[1]; |
---|
| 194 | absPos2D.depth = 0; |
---|
[4843] | 195 | } |
---|
[4847] | 196 | } |
---|
[4843] | 197 | |
---|
[5081] | 198 | |
---|
| 199 | void Element2D::setRelCoor2D (const Vector& relCoord) |
---|
| 200 | { |
---|
[5082] | 201 | this->relCoordinate = relCoord; |
---|
| 202 | this->bRelCoorChanged = true; |
---|
[5081] | 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | void Element2D::setRelCoor2D (float x, float y, float z) |
---|
| 207 | { |
---|
[5082] | 208 | this->setAbsCoor2D(Vector(x,y,z)); |
---|
[5081] | 209 | } |
---|
| 210 | |
---|
| 211 | void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias) |
---|
| 212 | { |
---|
[5082] | 213 | if (likely(this->toCoordinate == NULL)) |
---|
| 214 | this->toCoordinate = new Vector(); |
---|
| 215 | |
---|
| 216 | *this->toCoordinate = relCoordSoft; |
---|
| 217 | this->bias = bias; |
---|
[5081] | 218 | } |
---|
| 219 | |
---|
[5082] | 220 | void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias) |
---|
[5081] | 221 | { |
---|
[5082] | 222 | this->setRelCoorSoft2D(Vector(x, y, depth), bias); |
---|
[5081] | 223 | } |
---|
| 224 | |
---|
| 225 | void Element2D::setAbsCoor2D (const Vector& absCoord) |
---|
| 226 | { |
---|
[5082] | 227 | if( likely(this->parentMode & E2D_PARENT_MOVEMENT)) |
---|
| 228 | { |
---|
| 229 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 230 | if (likely(this->parent != NULL)) |
---|
| 231 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
| 232 | else |
---|
| 233 | this->relCoordinate = absCoord; |
---|
| 234 | } |
---|
| 235 | if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) |
---|
| 236 | { |
---|
| 237 | if (likely(this->parent != NULL)) |
---|
| 238 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
| 239 | else |
---|
| 240 | this->relCoordinate = absCoord; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | this->bRelCoorChanged = true; |
---|
[5081] | 244 | } |
---|
| 245 | |
---|
| 246 | void Element2D::setAbsCoor2D (float x, float y, float depth) |
---|
| 247 | { |
---|
[5082] | 248 | this->setAbsCoor2D(Vector(x, y, depth)); |
---|
[5081] | 249 | } |
---|
| 250 | |
---|
[5083] | 251 | void Element2D::shiftCoor2D (const Vector& shift) |
---|
[5081] | 252 | { |
---|
[5082] | 253 | this->relCoordinate += shift; |
---|
| 254 | this->bRelCoorChanged = true; |
---|
| 255 | |
---|
[5081] | 256 | } |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | void Element2D::setRelDir2D (float relDir) |
---|
| 260 | { |
---|
[5082] | 261 | this->relDirection = relDir; |
---|
| 262 | this->bRelDirChanged = true; |
---|
[5081] | 263 | } |
---|
| 264 | |
---|
| 265 | void Element2D::setRelDirSoft2D(float relDirSoft, float bias) |
---|
| 266 | { |
---|
[5082] | 267 | if (likely(this->toDirection == NULL)) |
---|
| 268 | this->toDirection = new float; |
---|
| 269 | |
---|
| 270 | *this->toDirection = relDirSoft; |
---|
| 271 | this->bias = bias; |
---|
[5081] | 272 | } |
---|
| 273 | |
---|
| 274 | void Element2D::setAbsDir2D (float absDir) |
---|
| 275 | { |
---|
[5082] | 276 | if (likely(this->parent != NULL)) |
---|
| 277 | this->relDirection = absDir - this->parent->getAbsDir2D(); |
---|
| 278 | else |
---|
| 279 | this->relDirection = absDir; |
---|
| 280 | |
---|
| 281 | this->bRelDirChanged = true; |
---|
[5081] | 282 | } |
---|
| 283 | |
---|
[5083] | 284 | void Element2D::shiftDir2D (float shiftDir) |
---|
[5081] | 285 | { |
---|
[5082] | 286 | this->relDirection = this->relDirection + shiftDir; |
---|
| 287 | this->bRelDirChanged = true; |
---|
[5081] | 288 | } |
---|
| 289 | |
---|
| 290 | |
---|
| 291 | void Element2D::addChild2D (Element2D* child, int parentingMod) |
---|
| 292 | { |
---|
[5082] | 293 | if( likely(child->parent != NULL)) |
---|
| 294 | { |
---|
[5084] | 295 | PRINTF(4)("Element2D::addChild() - reparenting node: removing it and adding it again\n"); |
---|
[5082] | 296 | child->parent->children->remove(child); |
---|
| 297 | } |
---|
| 298 | child->parentMode = parentMode; |
---|
| 299 | child->parent = this; |
---|
| 300 | this->children->add(child); |
---|
| 301 | child->parentCoorChanged(); |
---|
[5081] | 302 | } |
---|
| 303 | |
---|
| 304 | void Element2D::addChild2D (const char* childName) |
---|
| 305 | { |
---|
[5082] | 306 | Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D)); |
---|
| 307 | if (childNode != NULL) |
---|
| 308 | this->addChild2D(childNode); |
---|
[5081] | 309 | } |
---|
| 310 | |
---|
| 311 | void Element2D::removeChild2D (Element2D* child) |
---|
| 312 | { |
---|
[5082] | 313 | child->remove2D(); |
---|
| 314 | this->children->remove(child); |
---|
| 315 | child->parent = NULL; |
---|
[5081] | 316 | } |
---|
| 317 | |
---|
| 318 | void Element2D::remove2D() |
---|
| 319 | { |
---|
[5082] | 320 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
| 321 | Element2D* pn = iterator->nextElement(); |
---|
| 322 | |
---|
| 323 | while( pn != NULL) |
---|
| 324 | { |
---|
| 325 | NullElement2D::getInstance()->addChild2D(pn, pn->getParentMode2D()); |
---|
| 326 | pn = iterator->nextElement(); |
---|
| 327 | } |
---|
| 328 | delete iterator; |
---|
| 329 | this->parent->children->remove(this); |
---|
[5081] | 330 | } |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | void Element2D::setParent2D (Element2D* parent) |
---|
| 334 | { |
---|
[5082] | 335 | parent->addChild2D(this); |
---|
[5081] | 336 | } |
---|
| 337 | |
---|
| 338 | void Element2D::setParent2D (const char* parentName) |
---|
| 339 | { |
---|
[5082] | 340 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
| 341 | if (parentNode != NULL) |
---|
| 342 | parentNode->addChild2D(this); |
---|
| 343 | |
---|
[5081] | 344 | } |
---|
| 345 | |
---|
| 346 | |
---|
[5082] | 347 | void Element2D::softReparent2D(Element2D* parentNode, float bias) |
---|
[5081] | 348 | { |
---|
[5082] | 349 | if (this->parent == parentNode) |
---|
| 350 | return; |
---|
| 351 | |
---|
| 352 | if (likely(this->toCoordinate == NULL)) |
---|
| 353 | { |
---|
| 354 | this->toCoordinate = new Vector(); |
---|
| 355 | *this->toCoordinate = this->getRelCoor2D(); |
---|
| 356 | } |
---|
| 357 | if (likely(this->toDirection == NULL)) |
---|
| 358 | { |
---|
| 359 | this->toDirection = new float; |
---|
| 360 | *this->toDirection = this->getRelDir2D(); |
---|
| 361 | } |
---|
| 362 | this->bias = bias; |
---|
| 363 | |
---|
| 364 | |
---|
| 365 | Vector tmpV = this->getAbsCoor2D(); |
---|
| 366 | float tmpQ = this->getAbsDir2D(); |
---|
| 367 | |
---|
| 368 | parentNode->addChild2D(this); |
---|
| 369 | |
---|
| 370 | if (this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
| 371 | ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor())); |
---|
| 372 | else |
---|
| 373 | this->setRelCoor2D(tmpV - parentNode->getAbsCoor2D()); |
---|
| 374 | |
---|
| 375 | this->setRelDir2D(tmpQ - parentNode->getAbsDir2D()); |
---|
[5081] | 376 | } |
---|
| 377 | |
---|
[5082] | 378 | void Element2D::softReparent2D(const char* parentName, float bias) |
---|
[5081] | 379 | { |
---|
[5082] | 380 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
| 381 | if (parentNode != NULL) |
---|
| 382 | this->softReparent2D(parentNode, bias); |
---|
[5081] | 383 | } |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | void Element2D::setParentMode2D (const char* parentingMode) |
---|
| 387 | { |
---|
[5082] | 388 | this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode)); |
---|
[5081] | 389 | } |
---|
| 390 | |
---|
| 391 | |
---|
| 392 | void Element2D::update2D (float dt) |
---|
| 393 | { |
---|
[5083] | 394 | if( likely(this->parent != NULL)) |
---|
| 395 | { |
---|
| 396 | // movement for nodes with smoothMove enabled |
---|
| 397 | if (unlikely(this->toCoordinate != NULL)) |
---|
| 398 | { |
---|
| 399 | Vector moveVect = (*this->toCoordinate - this->getRelCoor2D()) *dt*bias; |
---|
[5082] | 400 | |
---|
[5083] | 401 | if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA)) |
---|
| 402 | { |
---|
| 403 | this->shiftCoor2D(moveVect); |
---|
| 404 | } |
---|
| 405 | else |
---|
| 406 | { |
---|
| 407 | delete this->toCoordinate; |
---|
| 408 | this->toCoordinate = NULL; |
---|
| 409 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | if (unlikely(this->toDirection != NULL)) |
---|
| 413 | { |
---|
| 414 | float rotFlot = (*this->toDirection - this->getRelDir2D()) *dt*bias; |
---|
| 415 | if (likely(rotFlot >= .001))//PNODE_ITERATION_DELTA)) |
---|
| 416 | { |
---|
| 417 | this->shiftDir2D(rotFlot); |
---|
| 418 | } |
---|
| 419 | else |
---|
| 420 | { |
---|
| 421 | delete this->toDirection; |
---|
| 422 | this->toDirection = NULL; |
---|
| 423 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | // MAIN UPDATE ///////////////////////////////////// |
---|
| 428 | this->lastAbsCoordinate = this->absCoordinate; |
---|
| 429 | |
---|
[5084] | 430 | PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[5083] | 431 | |
---|
| 432 | |
---|
| 433 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
| 434 | { |
---|
| 435 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
| 436 | this->prevRelCoordinate = this->relCoordinate; |
---|
| 437 | this->absDirection = this->relDirection + parent->getAbsDir2D();; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | if(likely(this->parentMode & PNODE_MOVEMENT)) |
---|
| 441 | { |
---|
| 442 | /* update the current absCoordinate */ |
---|
| 443 | this->prevRelCoordinate = this->relCoordinate; |
---|
| 444 | this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate; |
---|
| 445 | } |
---|
| 446 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
| 447 | { |
---|
| 448 | /* update the current absCoordinate */ |
---|
| 449 | this->prevRelCoordinate = this->relCoordinate; |
---|
| 450 | this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D())); |
---|
| 451 | this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D())); |
---|
| 452 | |
---|
| 453 | } |
---|
| 454 | ///////////////////////////////////////////////// |
---|
| 455 | } |
---|
| 456 | else |
---|
| 457 | { |
---|
[5084] | 458 | PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[5083] | 459 | if (this->bRelCoorChanged) |
---|
| 460 | this->absCoordinate = this->relCoordinate; |
---|
| 461 | if (this->bRelDirChanged) |
---|
| 462 | this->absDirection = this->getAbsDir2D() * this->relDirection; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | if(this->children->getSize() > 0) |
---|
| 466 | { |
---|
| 467 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
| 468 | Element2D* pn = iterator->nextElement(); |
---|
| 469 | while( pn != NULL) |
---|
| 470 | { |
---|
| 471 | /* if this node has changed, make sure, that all children are updated also */ |
---|
| 472 | if( likely(this->bRelCoorChanged)) |
---|
| 473 | pn->parentCoorChanged (); |
---|
| 474 | if( likely(this->bRelDirChanged)) |
---|
| 475 | pn->parentDirChanged (); |
---|
| 476 | |
---|
| 477 | pn->update2D(dt); |
---|
| 478 | //pn = this->children->nextElement(); |
---|
| 479 | pn = iterator->nextElement(); |
---|
| 480 | } |
---|
| 481 | delete iterator; |
---|
| 482 | } |
---|
| 483 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
| 484 | this->bRelCoorChanged = false; |
---|
| 485 | this->bRelDirChanged = false; |
---|
[5081] | 486 | } |
---|
| 487 | |
---|
| 488 | |
---|
| 489 | void Element2D::debug (unsigned int depth, unsigned int level) const |
---|
| 490 | { |
---|
[5082] | 491 | for (unsigned int i = 0; i < level; i++) |
---|
| 492 | PRINT(0)(" |"); |
---|
| 493 | if (this->children->getSize() > 0) |
---|
| 494 | PRINT(0)(" +"); |
---|
| 495 | else |
---|
| 496 | PRINT(0)(" -"); |
---|
| 497 | PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s\n", |
---|
| 498 | this->getClassName(), |
---|
| 499 | this->getName(), |
---|
| 500 | this->absCoordinate.x, |
---|
| 501 | this->absCoordinate.y, |
---|
| 502 | this->relCoordinate.x, |
---|
| 503 | this->relCoordinate.y, |
---|
| 504 | this->getAbsDir2D(), |
---|
| 505 | Element2D::parentingModeToChar2D(parentMode)); |
---|
| 506 | if (depth >= 2 || depth == 0) |
---|
| 507 | { |
---|
| 508 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
| 509 | //PNode* pn = this->children->enumerate (); |
---|
| 510 | Element2D* pn = iterator->nextElement(); |
---|
| 511 | while( pn != NULL) |
---|
| 512 | { |
---|
| 513 | if (depth == 0) |
---|
| 514 | pn->debug(0, level + 1); |
---|
| 515 | else |
---|
| 516 | pn->debug(depth - 1, level +1); |
---|
| 517 | pn = iterator->nextElement(); |
---|
| 518 | } |
---|
| 519 | delete iterator; |
---|
| 520 | } |
---|
[5081] | 521 | } |
---|
| 522 | |
---|
[5082] | 523 | #include "color.h" |
---|
| 524 | |
---|
[5081] | 525 | void Element2D::debugDraw2D(unsigned int depth, float size, Vector color) const |
---|
| 526 | { |
---|
[5082] | 527 | |
---|
[5081] | 528 | } |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | // helper functions // |
---|
[5082] | 532 | const char* Element2D::parentingModeToChar2D(int parentingMode) |
---|
[5081] | 533 | { |
---|
[5082] | 534 | if (parentingMode == E2D_PARENT_LOCAL_ROTATE) |
---|
| 535 | return "local-rotate"; |
---|
| 536 | else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT) |
---|
| 537 | return "rotate-movement"; |
---|
| 538 | else if (parentingMode == E2D_PARENT_MOVEMENT) |
---|
| 539 | return "movement"; |
---|
| 540 | else if (parentingMode == E2D_PARENT_ALL) |
---|
| 541 | return "all"; |
---|
| 542 | else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE) |
---|
| 543 | return "rotate-and-move"; |
---|
[5081] | 544 | } |
---|
| 545 | |
---|
[5082] | 546 | E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode) |
---|
[5081] | 547 | { |
---|
[5082] | 548 | if (!strcmp(parentingMode, "local-rotate")) |
---|
| 549 | return (E2D_PARENT_LOCAL_ROTATE); |
---|
| 550 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
| 551 | return (E2D_PARENT_ROTATE_MOVEMENT); |
---|
| 552 | else if (!strcmp(parentingMode, "movement")) |
---|
| 553 | return (E2D_PARENT_MOVEMENT); |
---|
| 554 | else if (!strcmp(parentingMode, "all")) |
---|
| 555 | return (E2D_PARENT_ALL); |
---|
| 556 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
| 557 | return (E2D_PARENT_ROTATE_AND_MOVE); |
---|
[5081] | 558 | } |
---|
| 559 | |
---|
| 560 | |
---|
| 561 | |
---|
| 562 | |
---|
| 563 | |
---|
| 564 | |
---|
[4847] | 565 | /** |
---|
| 566 | * ticks the 2d-Element |
---|
| 567 | * @param dt the time elapsed since the last tick |
---|
| 568 | */ |
---|
| 569 | void Element2D::tick(float dt) |
---|
| 570 | { |
---|
| 571 | this->positioning(); |
---|
[4843] | 572 | } |
---|
[5082] | 573 | |
---|
| 574 | |
---|
| 575 | |
---|
| 576 | |
---|
| 577 | |
---|
| 578 | |
---|
| 579 | |
---|
| 580 | NullElement2D* NullElement2D::singletonRef = 0; |
---|
| 581 | |
---|
| 582 | /** |
---|
| 583 | * creates the one and only NullElement2D |
---|
| 584 | * @param absCoordinate the cordinate of the Parent (normally Vector(0,0,0)) |
---|
| 585 | */ |
---|
| 586 | NullElement2D::NullElement2D () |
---|
| 587 | { |
---|
| 588 | this->setClassID(CL_NULL_PARENT, "NullElement2D"); |
---|
| 589 | this->setName("NullElement2D"); |
---|
| 590 | |
---|
| 591 | this->setParentMode2D(E2D_PARENT_ALL); |
---|
| 592 | NullElement2D::singletonRef = this; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | |
---|
| 596 | /** |
---|
| 597 | * standard deconstructor |
---|
| 598 | */ |
---|
| 599 | NullElement2D::~NullElement2D () |
---|
| 600 | { |
---|
| 601 | //delete singletonRef; |
---|
| 602 | NullElement2D::singletonRef = NULL; |
---|
| 603 | } |
---|