1 | /* |
---|
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. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: Christian Meyer |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PLAYER |
---|
17 | |
---|
18 | #include "player.h" |
---|
19 | |
---|
20 | #include "track_manager.h" |
---|
21 | #include "objModel.h" |
---|
22 | #include "resource_manager.h" |
---|
23 | |
---|
24 | #include "weapon_manager.h" |
---|
25 | #include "weapon.h" |
---|
26 | #include "test_gun.h" |
---|
27 | #include "world.h" |
---|
28 | |
---|
29 | #include "list.h" |
---|
30 | #include "stdincl.h" |
---|
31 | |
---|
32 | #include "event_handler.h" |
---|
33 | |
---|
34 | #include "projectile.h" |
---|
35 | #include "event.h" |
---|
36 | |
---|
37 | |
---|
38 | using namespace std; |
---|
39 | |
---|
40 | CREATE_FACTORY(Player); |
---|
41 | |
---|
42 | /** |
---|
43 | \brief creates a new Player |
---|
44 | \param isFree if the player is free |
---|
45 | */ |
---|
46 | Player::Player() |
---|
47 | { |
---|
48 | /* |
---|
49 | this is the debug player - actualy we would have to make a new |
---|
50 | class derivated from Player for each player. for now, we just use |
---|
51 | the player.cc for debug also |
---|
52 | */ |
---|
53 | this->init(); |
---|
54 | |
---|
55 | //weapons: |
---|
56 | this->weaponMan = new WeaponManager(); |
---|
57 | Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0); |
---|
58 | Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1); |
---|
59 | |
---|
60 | this->weaponMan->addWeapon(wpRight, W_CONFIG0, W_SLOT0); |
---|
61 | this->weaponMan->addWeapon(wpLeft, W_CONFIG1, W_SLOT1); |
---|
62 | this->weaponMan->addWeapon(wpRight, W_CONFIG2); |
---|
63 | this->weaponMan->addWeapon(wpLeft, W_CONFIG2); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | \brief destructs the player, deletes alocated memory |
---|
69 | */ |
---|
70 | Player::~Player () |
---|
71 | { |
---|
72 | /* do not delete the weapons, they are contained in the pnode tree |
---|
73 | and will be deleted there. |
---|
74 | this only frees the memory allocated to save the list. |
---|
75 | */ |
---|
76 | delete this->weaponMan; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | \brief creates a new Player from Xml Data |
---|
82 | \param root the xml element containing player data |
---|
83 | |
---|
84 | \todo add more parameters to load |
---|
85 | */ |
---|
86 | Player::Player(const TiXmlElement* root) |
---|
87 | { |
---|
88 | this->init(); |
---|
89 | this->loadParams(root); |
---|
90 | |
---|
91 | //weapons: |
---|
92 | this->weaponMan = new WeaponManager(); |
---|
93 | Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0); |
---|
94 | Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1); |
---|
95 | |
---|
96 | this->weaponMan->addWeapon(wpRight, W_CONFIG0, W_SLOT0); |
---|
97 | this->weaponMan->addWeapon(wpLeft, W_CONFIG1, W_SLOT1); |
---|
98 | this->weaponMan->addWeapon(wpRight, W_CONFIG2); |
---|
99 | this->weaponMan->addWeapon(wpLeft, W_CONFIG2); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * initializes a Player |
---|
104 | */ |
---|
105 | void Player::init() |
---|
106 | { |
---|
107 | this->setClassID(CL_PLAYER, "Player"); |
---|
108 | |
---|
109 | this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN); |
---|
110 | travelSpeed = 15.0; |
---|
111 | velocity = new Vector(); |
---|
112 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
113 | bFire = false; |
---|
114 | this->bWeaponChange = false; |
---|
115 | acceleration = 10.0; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /** |
---|
120 | * |
---|
121 | * @param root the XML-element to load the Player's properties from |
---|
122 | */ |
---|
123 | void Player::loadParams(const TiXmlElement* root) |
---|
124 | { |
---|
125 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | \brief adds a weapon to the weapon list of player |
---|
134 | \param weapon to add |
---|
135 | */ |
---|
136 | void Player::addWeapon(Weapon* weapon) |
---|
137 | { |
---|
138 | this->weaponMan->addWeapon(weapon); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | /** |
---|
143 | \brief removes a weapon from the player |
---|
144 | \param weapon to remove |
---|
145 | */ |
---|
146 | void Player::removeWeapon(Weapon* weapon) |
---|
147 | { |
---|
148 | this->weaponMan->removeWeapon(weapon); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | /** |
---|
153 | \brief effect that occurs after the player is spawned |
---|
154 | */ |
---|
155 | void Player::postSpawn () |
---|
156 | { |
---|
157 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | /** |
---|
162 | \brief the action occuring if the player left the game |
---|
163 | */ |
---|
164 | void Player::leftWorld () |
---|
165 | {} |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | /** |
---|
170 | \brief if the player is hit, call this function |
---|
171 | \param weapon hit by this weapon |
---|
172 | \param loc ?? |
---|
173 | */ |
---|
174 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
175 | { |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | /** |
---|
180 | \brief Collision with another Entity has this effect |
---|
181 | \param other the other colider |
---|
182 | \param ownhitflags ?? |
---|
183 | \param otherhitflags ?? |
---|
184 | */ |
---|
185 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
186 | { |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /** |
---|
191 | \brief draws the player after transforming him. |
---|
192 | */ |
---|
193 | void Player::draw () |
---|
194 | { |
---|
195 | glMatrixMode(GL_MODELVIEW); |
---|
196 | glPushMatrix(); |
---|
197 | float matrix[4][4]; |
---|
198 | |
---|
199 | /* translate */ |
---|
200 | glTranslatef (this->getAbsCoor ().x, |
---|
201 | this->getAbsCoor ().y, |
---|
202 | this->getAbsCoor ().z); |
---|
203 | /* rotate */ |
---|
204 | this->getAbsDir ().matrix (matrix); |
---|
205 | glMultMatrixf((float*)matrix); |
---|
206 | |
---|
207 | this->model->draw(); |
---|
208 | glPopMatrix(); |
---|
209 | |
---|
210 | this->weaponMan->draw(); |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | /** |
---|
215 | \brief the function called for each passing timeSnap |
---|
216 | \param time The timespan passed since last update |
---|
217 | */ |
---|
218 | void Player::tick (float time) |
---|
219 | { |
---|
220 | //printf("%p\n", this); |
---|
221 | //this->getRelCoor().debug(); |
---|
222 | |
---|
223 | /* link tick to weapon */ |
---|
224 | //this->activeWeapon->tick(time); |
---|
225 | //this->activeWeaponL->tick(time); //FIX FIX DELETE REMOVE |
---|
226 | this->weaponMan->tick(time); |
---|
227 | // player controlled movement |
---|
228 | this->move(time); |
---|
229 | // weapon system manipulation |
---|
230 | this->weapon(); |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | /** |
---|
235 | \brief action if player moves |
---|
236 | \param time the timeslice since the last frame |
---|
237 | */ |
---|
238 | void Player::move (float time) |
---|
239 | { |
---|
240 | Vector accel(0.0, 0.0, 0.0); |
---|
241 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
242 | /* calculate the direction in which the craft is heading */ |
---|
243 | Vector direction (1.0, 0.0, 0.0); |
---|
244 | //direction = this->absDirection.apply (direction); |
---|
245 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
246 | //orthDirection = orthDirection.cross (direction); |
---|
247 | |
---|
248 | if( this->bUp && this->getRelCoor().x < 20) |
---|
249 | accel = accel+(direction*acceleration); |
---|
250 | if( this->bDown && this->getRelCoor().x > -5) |
---|
251 | accel = accel -(direction*acceleration); |
---|
252 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
---|
253 | accel = accel - (orthDirection*acceleration); |
---|
254 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
---|
255 | accel = accel + (orthDirection*acceleration); |
---|
256 | if( this->bAscend ) |
---|
257 | if( this->bDescend) {/* FIXME */} /* \todo up and down player movement */ |
---|
258 | |
---|
259 | Vector move = accel * time; |
---|
260 | this->shiftCoor (move); |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | /** |
---|
265 | \brief weapon manipulation by the player |
---|
266 | */ |
---|
267 | void Player::weapon() |
---|
268 | { |
---|
269 | if( this->bFire) |
---|
270 | { |
---|
271 | this->weaponMan->fire(); |
---|
272 | } |
---|
273 | if( this->bWeaponChange) |
---|
274 | { |
---|
275 | this->weaponMan->nextWeaponConf(); |
---|
276 | this->bWeaponChange = false; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | /** |
---|
282 | \brief The connection to the command node |
---|
283 | \param cmd the Command unit from witch to map |
---|
284 | |
---|
285 | here the commands are mapped to the players movement/weaponary |
---|
286 | */ |
---|
287 | void Player::command (Command* cmd) |
---|
288 | { |
---|
289 | PRINTF(3)("recieved command [%s]\n", cmd->cmd); |
---|
290 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_UP)) this->bUp = !cmd->bUp; |
---|
291 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_DOWN)) this->bDown = !cmd->bUp; |
---|
292 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_LEFT)) this->bLeft = !cmd->bUp; |
---|
293 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_RIGHT)) this->bRight = !cmd->bUp; |
---|
294 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_FIRE)) this->bFire = !cmd->bUp; |
---|
295 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_NEXT_WEAPON)) if(cmd->bUp) this->bWeaponChange = !this->bWeaponChange; |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * @todo switch statement ?? |
---|
300 | */ |
---|
301 | void Player::process(const Event &event) |
---|
302 | { |
---|
303 | if( event.type == KeyMapper::PEV_UP) |
---|
304 | { |
---|
305 | this->bUp = event.bPressed; |
---|
306 | } |
---|
307 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
308 | { |
---|
309 | this->bDown = event.bPressed; |
---|
310 | } |
---|
311 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
312 | { |
---|
313 | this->bRight= event.bPressed; |
---|
314 | } |
---|
315 | else if( event.type == KeyMapper::PEV_LEFT) |
---|
316 | { |
---|
317 | this->bLeft = event.bPressed; |
---|
318 | } |
---|
319 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
320 | { |
---|
321 | this->bFire = event.bPressed; |
---|
322 | } |
---|
323 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON) |
---|
324 | { |
---|
325 | if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
326 | } |
---|
327 | |
---|
328 | } |
---|