[2839] | 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 "PongAI.h" |
---|
| 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "objects/worldentities/ControllableEntity.h" |
---|
| 34 | #include "objects/worldentities/PongBall.h" |
---|
| 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
| 38 | CreateUnloadableFactory(PongAI); |
---|
| 39 | |
---|
| 40 | PongAI::PongAI(BaseObject* creator) : Controller(creator) |
---|
| 41 | { |
---|
| 42 | RegisterObject(PongAI); |
---|
| 43 | |
---|
| 44 | this->ball_ = 0; |
---|
| 45 | this->randomOffset_ = 0; |
---|
| 46 | this->relHysteresisOffset_ = 0.02; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void PongAI::tick(float dt) |
---|
| 50 | { |
---|
| 51 | if (!this->ball_ || !this->getControllableEntity()) |
---|
| 52 | return; |
---|
| 53 | |
---|
| 54 | ControllableEntity* bat = this->getControllableEntity(); |
---|
| 55 | |
---|
| 56 | Vector3 mypos = bat->getPosition(); |
---|
| 57 | Vector3 ballpos = this->ball_->getPosition(); |
---|
| 58 | Vector3 ballvel = this->ball_->getVelocity(); |
---|
| 59 | float hysteresisOffset = this->relHysteresisOffset_ * this->ball_->getFieldDimension().y; |
---|
| 60 | |
---|
| 61 | // Check in which direction the ball is flying |
---|
| 62 | if ((mypos.x > 0 && ballvel.x < 0) || (mypos.x < 0 && ballvel.x > 0)) |
---|
| 63 | { |
---|
| 64 | // Ball is flying away |
---|
| 65 | this->calculateRandomOffset(); |
---|
| 66 | |
---|
| 67 | if (mypos.z > hysteresisOffset) |
---|
| 68 | bat->moveFrontBack(1); |
---|
| 69 | else if (mypos.z < -hysteresisOffset) |
---|
| 70 | bat->moveFrontBack(-1); |
---|
| 71 | } |
---|
| 72 | else if (ballvel.x == 0) |
---|
| 73 | { |
---|
| 74 | // Ball is standing still |
---|
| 75 | this->calculateRandomOffset(); |
---|
| 76 | } |
---|
| 77 | else |
---|
| 78 | { |
---|
| 79 | // Ball is approaching |
---|
| 80 | float desiredZValue = ballpos.z + this->randomOffset_; |
---|
| 81 | |
---|
| 82 | if (mypos.z > desiredZValue + hysteresisOffset) |
---|
| 83 | bat->moveFrontBack(1); |
---|
| 84 | else if (mypos.z < desiredZValue - hysteresisOffset) |
---|
| 85 | bat->moveFrontBack(-1); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void PongAI::calculateRandomOffset() |
---|
| 90 | { |
---|
| 91 | this->randomOffset_ = rnd(-0.45, 0.45) * this->ball_->getBatLength() * this->ball_->getFieldDimension().y; |
---|
| 92 | } |
---|
| 93 | } |
---|