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 "WaypointPatrolController.h" |
---|
30 | |
---|
31 | #include "util/Math.h" |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/ObjectList.h" |
---|
34 | #include "core/XMLPort.h" |
---|
35 | #include "objects/worldentities/pawns/Pawn.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | CreateFactory(WaypointPatrolController); |
---|
40 | |
---|
41 | WaypointPatrolController::WaypointPatrolController(BaseObject* creator) : WaypointController(creator) |
---|
42 | { |
---|
43 | RegisterObject(WaypointPatrolController); |
---|
44 | |
---|
45 | this->team_ = 0; |
---|
46 | this->alertnessradius_ = 500; |
---|
47 | |
---|
48 | this->patrolTimer_.setTimer(rnd(), true, this, createExecutor(createFunctor(&WaypointPatrolController::searchEnemy))); |
---|
49 | } |
---|
50 | |
---|
51 | void WaypointPatrolController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
52 | { |
---|
53 | SUPER(WaypointPatrolController, XMLPort, xmlelement, mode); |
---|
54 | |
---|
55 | XMLPortParam(WaypointPatrolController, "alertnessradius", setAlertnessRadius, getAlertnessRadius, xmlelement, mode).defaultValues(500.0f); |
---|
56 | XMLPortParam(WaypointPatrolController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0); |
---|
57 | } |
---|
58 | |
---|
59 | void WaypointPatrolController::tick(float dt) |
---|
60 | { |
---|
61 | if (!this->isActive()) |
---|
62 | return; |
---|
63 | |
---|
64 | if (this->target_) |
---|
65 | { |
---|
66 | this->aimAtTarget(); |
---|
67 | |
---|
68 | if (this->bHasTargetPosition_) |
---|
69 | this->moveToTargetPosition(); |
---|
70 | |
---|
71 | if (this->getControllableEntity() && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0)) |
---|
72 | this->getControllableEntity()->fire(0); |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | SUPER(WaypointPatrolController, tick, dt); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | void WaypointPatrolController::searchEnemy() |
---|
81 | { |
---|
82 | this->patrolTimer_.setInterval(rnd()); |
---|
83 | |
---|
84 | if (!this->getControllableEntity()) |
---|
85 | return; |
---|
86 | |
---|
87 | Vector3 myposition = this->getControllableEntity()->getPosition(); |
---|
88 | float shortestsqdistance = (unsigned int)-1; |
---|
89 | |
---|
90 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
91 | { |
---|
92 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
93 | continue; |
---|
94 | |
---|
95 | float sqdistance = it->getPosition().squaredDistance(myposition); |
---|
96 | if (sqdistance < shortestsqdistance) |
---|
97 | { |
---|
98 | shortestsqdistance = sqdistance; |
---|
99 | this->target_ = (*it); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | if (shortestsqdistance > (this->alertnessradius_ * this->alertnessradius_)) |
---|
104 | this->target_ = 0; |
---|
105 | } |
---|
106 | } |
---|