| 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 | *      Fabien Vultier | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file JumpEnemy.cc | 
|---|
| 31 | @brief All enemies in the minigame inherit from this class. This class manages the movement of all enemies and allows enemies to touch the figure to kill it. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "JumpEnemy.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/CoreIncludes.h" | 
|---|
| 37 | #include "core/XMLPort.h" | 
|---|
| 38 | #include "JumpFigure.h" | 
|---|
| 39 |  | 
|---|
| 40 | namespace orxonox | 
|---|
| 41 | { | 
|---|
| 42 | RegisterClass(JumpEnemy); | 
|---|
| 43 |  | 
|---|
| 44 | /** | 
|---|
| 45 | @brief | 
|---|
| 46 | Constructor. Registers and initializes the object. | 
|---|
| 47 | */ | 
|---|
| 48 | JumpEnemy::JumpEnemy(Context* context) : MovableEntity(context) | 
|---|
| 49 | { | 
|---|
| 50 | RegisterObject(JumpEnemy); | 
|---|
| 51 |  | 
|---|
| 52 | dead_ = false; | 
|---|
| 53 | figure_ = nullptr; | 
|---|
| 54 | width_ = 0.0; | 
|---|
| 55 | height_ = 0.0; | 
|---|
| 56 | setPosition(Vector3(0,0,0)); | 
|---|
| 57 | setVelocity(Vector3(0,0,0)); | 
|---|
| 58 | setAcceleration(Vector3(0,0,0)); | 
|---|
| 59 | setProperties(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | /** | 
|---|
| 63 | @brief | 
|---|
| 64 | Destructor. | 
|---|
| 65 | */ | 
|---|
| 66 | JumpEnemy::~JumpEnemy() | 
|---|
| 67 | { | 
|---|
| 68 | /*if (this->isInitialized()) | 
|---|
| 69 | { | 
|---|
| 70 | if (this->bDeleteBats_) | 
|---|
| 71 | delete this->figure_; | 
|---|
| 72 |  | 
|---|
| 73 | delete[] this->batID_; | 
|---|
| 74 | }*/ | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | //xml port for loading sounds | 
|---|
| 78 | void JumpEnemy::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 79 | { | 
|---|
| 80 | SUPER(JumpEnemy, XMLPort, xmlelement, mode); | 
|---|
| 81 |  | 
|---|
| 82 | XMLPortParam(JumpEnemy, "height", setHeight, getHeight, xmlelement, mode); | 
|---|
| 83 | XMLPortParam(JumpEnemy, "width", setWidth, getWidth, xmlelement, mode); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | /** | 
|---|
| 87 | @brief | 
|---|
| 88 | Is called every tick. | 
|---|
| 89 | Handles the movement of the ball and its interaction with the boundaries and bats. | 
|---|
| 90 | @param dt | 
|---|
| 91 | The time since the last tick. | 
|---|
| 92 | */ | 
|---|
| 93 | void JumpEnemy::tick(float dt) | 
|---|
| 94 | { | 
|---|
| 95 | SUPER(JumpEnemy, tick, dt); | 
|---|
| 96 |  | 
|---|
| 97 | // Get the current position, velocity and acceleration of the enemy. | 
|---|
| 98 | Vector3 position = getPosition(); | 
|---|
| 99 | Vector3 velocity = getVelocity(); | 
|---|
| 100 |  | 
|---|
| 101 | if ((position.x < leftBoundary_ && velocity.x < 0) || (position.x > rightBoundary_ && velocity.x > 0)) | 
|---|
| 102 | { | 
|---|
| 103 | velocity.x = -velocity.x; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | if ((position.z < lowerBoundary_ && velocity.z < 0) || (position.z > upperBoundary_ && velocity.z > 0)) | 
|---|
| 107 | { | 
|---|
| 108 | velocity.z = -velocity.z; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | // Set the position, velocity and acceleration of the enemy, if they have changed. | 
|---|
| 112 | if (velocity != getVelocity()) | 
|---|
| 113 | setVelocity(velocity); | 
|---|
| 114 | if (position != getPosition()) | 
|---|
| 115 | setPosition(position); | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | // Interact with Figure | 
|---|
| 119 | Vector3 enemyPosition = getPosition(); | 
|---|
| 120 |  | 
|---|
| 121 | if (figure_ != nullptr) | 
|---|
| 122 | { | 
|---|
| 123 | Vector3 figurePosition = figure_->getPosition(); | 
|---|
| 124 | if(figurePosition.x > enemyPosition.x-width_ && figurePosition.x < enemyPosition.x+width_ && figurePosition.z > enemyPosition.z-height_ && figurePosition.z < enemyPosition.z+height_) | 
|---|
| 125 | { | 
|---|
| 126 | touchFigure(); | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | void JumpEnemy::setProperties(float newLeftBoundary, float newRightBoundary, float newLowerBoundary, float newUpperBoundary, float newHSpeed, float newVSpeed) | 
|---|
| 132 | { | 
|---|
| 133 | leftBoundary_ = newLeftBoundary; | 
|---|
| 134 | rightBoundary_ = newRightBoundary; | 
|---|
| 135 | lowerBoundary_ = newLowerBoundary; | 
|---|
| 136 | upperBoundary_ = newUpperBoundary; | 
|---|
| 137 |  | 
|---|
| 138 | this->setVelocity(Vector3(newHSpeed,0,newVSpeed)); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void JumpEnemy::setFigure(JumpFigure* newFigure) | 
|---|
| 142 | { | 
|---|
| 143 | figure_ = newFigure; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void JumpEnemy::touchFigure() | 
|---|
| 147 | { | 
|---|
| 148 | if (dead_ == false) | 
|---|
| 149 | { | 
|---|
| 150 | figure_->CollisionWithEnemy(this); | 
|---|
| 151 | } | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|