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 | * Oli Scheuss |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "DroneController.h" |
---|
30 | #include "worldentities/Drone.h" |
---|
31 | #include "util/Math.h" |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/Executor.h" |
---|
35 | #include "worldentities/ControllableEntity.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | /** |
---|
40 | @brief |
---|
41 | Constructor. |
---|
42 | */ |
---|
43 | CreateFactory(DroneController); |
---|
44 | |
---|
45 | static const float ACTION_INTERVAL = 1.0f; |
---|
46 | |
---|
47 | DroneController::DroneController(BaseObject* creator) : ArtificialController(creator) |
---|
48 | { |
---|
49 | /* // Place your code here: |
---|
50 | // - make sure to register the object in the factory |
---|
51 | // - do any kind of initialisation |
---|
52 | RegisterObject(DroneController); |
---|
53 | |
---|
54 | // this checks that our creator really is a drone |
---|
55 | // and saves the pointer to the drone for the controlling commands |
---|
56 | assert(dynamic_cast<Drone*>(creator)!=0); |
---|
57 | this->setControllableEntity(dynamic_cast<Drone*>(creator)); |
---|
58 | */ |
---|
59 | RegisterObject(DroneController); |
---|
60 | |
---|
61 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this))); |
---|
62 | } |
---|
63 | |
---|
64 | DroneController::~DroneController() |
---|
65 | { |
---|
66 | } |
---|
67 | |
---|
68 | void DroneController::setPawn(Pawn* pawn){ |
---|
69 | pawnpointer_ = pawn; |
---|
70 | } |
---|
71 | |
---|
72 | const Pawn* DroneController::getPawn(unsigned int index) const |
---|
73 | { |
---|
74 | if(index == 0) return pawnpointer_; |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | |
---|
78 | void DroneController::action() |
---|
79 | { |
---|
80 | float random; |
---|
81 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
82 | |
---|
83 | // search enemy |
---|
84 | random = rnd(maxrand); |
---|
85 | if (random < 15 && (!this->target_)) |
---|
86 | this->searchNewTarget(); |
---|
87 | |
---|
88 | // forget enemy |
---|
89 | random = rnd(maxrand); |
---|
90 | if (random < 5 && (this->target_)) |
---|
91 | this->forgetTarget(); |
---|
92 | |
---|
93 | // next enemy |
---|
94 | random = rnd(maxrand); |
---|
95 | if (random < 10 && (this->target_)) |
---|
96 | this->searchNewTarget(); |
---|
97 | |
---|
98 | //fly somewhere |
---|
99 | random = rnd(maxrand); |
---|
100 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
101 | this->searchRandomTargetPosition(); |
---|
102 | |
---|
103 | // stop flying |
---|
104 | random = rnd(maxrand); |
---|
105 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
106 | this->bHasTargetPosition_ = false; |
---|
107 | |
---|
108 | // fly somewhere else |
---|
109 | random = rnd(maxrand); |
---|
110 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
111 | this->searchRandomTargetPosition(); |
---|
112 | |
---|
113 | /* // shoot |
---|
114 | random = rnd(maxrand); |
---|
115 | if (random < 75 && (this->target_ && !this->bShooting_)) |
---|
116 | this->bShooting_ = true; |
---|
117 | |
---|
118 | // stop shooting |
---|
119 | random = rnd(maxrand); |
---|
120 | if (random < 25 && (this->bShooting_)) */ |
---|
121 | this->bShooting_ = false; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | @brief |
---|
127 | The controlling happens here. This method defines what the controller has to do each tick. |
---|
128 | @param dt |
---|
129 | The duration of the tick. |
---|
130 | */ |
---|
131 | void DroneController::tick(float dt) |
---|
132 | { |
---|
133 | // Place your code here: |
---|
134 | // - steering commands |
---|
135 | |
---|
136 | |
---|
137 | Drone *myDrone = static_cast<Drone*>(this->getControllableEntity()); |
---|
138 | |
---|
139 | if(myDrone != NULL) { |
---|
140 | |
---|
141 | setTargetPosition(this->getControllableEntity()->getPosition()); |
---|
142 | /* |
---|
143 | myDrone->setRotationThrust(25); |
---|
144 | myDrone->setAuxilaryThrust(30); |
---|
145 | myDrone->rotateYaw(10*dt); */ |
---|
146 | } |
---|
147 | |
---|
148 | SUPER(AIController, tick, dt); |
---|
149 | |
---|
150 | // you can use the following commands for steering |
---|
151 | // - moveFrontBack, moveRightLeft, moveUpDown |
---|
152 | // - rotatePitch, rotateYaw, rotateRoll |
---|
153 | // - apply the to myDrone (e.g. myDrone->rotateYaw(..) ) |
---|
154 | |
---|
155 | } |
---|
156 | } |
---|