1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: Christian Meyer |
---|
16 | */ |
---|
17 | |
---|
18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PLAYER |
---|
19 | |
---|
20 | #include "player.h" |
---|
21 | |
---|
22 | #include "track_manager.h" |
---|
23 | #include "objModel.h" |
---|
24 | #include "resource_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 | using namespace std; |
---|
33 | |
---|
34 | CREATE_FACTORY(Player); |
---|
35 | |
---|
36 | /** |
---|
37 | \brief creates a new Player |
---|
38 | \param isFree if the player is free |
---|
39 | */ |
---|
40 | Player::Player() : WorldEntity() |
---|
41 | { |
---|
42 | this->weapons = new tList<Weapon>(); |
---|
43 | this->activeWeapon = NULL; |
---|
44 | /* |
---|
45 | this is the debug player - actualy we would have to make a new |
---|
46 | class derivated from Player for each player. for now, we just use |
---|
47 | the player.cc for debug also |
---|
48 | */ |
---|
49 | this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN); |
---|
50 | travelSpeed = 15.0; |
---|
51 | velocity = new Vector(); |
---|
52 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
53 | bFire = false; |
---|
54 | acceleration = 10.0; |
---|
55 | //weapons: |
---|
56 | Weapon* wp = new TestGun(this, new Vector(-1.1, 0.0, 2.6), new Quaternion()); |
---|
57 | this->weapons->add(wp); |
---|
58 | this->activeWeapon = wp; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | \brief destructs the player, deletes alocated memory |
---|
63 | */ |
---|
64 | Player::~Player () |
---|
65 | { |
---|
66 | /* do not delete the weapons, they are contained in the pnode tree |
---|
67 | and will be deleted there. |
---|
68 | this only frees the memory allocated to save the list. |
---|
69 | */ |
---|
70 | delete this->weapons; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /** |
---|
75 | \brief creates a new Player from Xml Data |
---|
76 | \param root the xml element containing player data |
---|
77 | |
---|
78 | \todo add more parameters to load |
---|
79 | */ |
---|
80 | Player::Player(TiXmlElement* root) |
---|
81 | { |
---|
82 | char* temp; |
---|
83 | const char* string; |
---|
84 | string = grabParameter( root, "name"); |
---|
85 | if( string == NULL) |
---|
86 | { |
---|
87 | PRINTF0("Player is missing a proper 'name'\n"); |
---|
88 | string = "Unknown"; |
---|
89 | temp = new char[strlen(string + 2)]; |
---|
90 | strcpy( temp, string); |
---|
91 | this->setName( temp); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | temp = new char[strlen(string + 2)]; |
---|
96 | strcpy( temp, string); |
---|
97 | this->setName( temp); |
---|
98 | } |
---|
99 | |
---|
100 | this->model = NULL; |
---|
101 | string = grabParameter( root, "model"); |
---|
102 | if( string != NULL) |
---|
103 | this->model = (Model*)ResourceManager::getInstance()->load(string, OBJ, RP_CAMPAIGN); |
---|
104 | else |
---|
105 | { |
---|
106 | PRINTF0("Player is missing a proper 'model'\n"); |
---|
107 | this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN); |
---|
108 | } |
---|
109 | if( this->model == NULL) |
---|
110 | { |
---|
111 | PRINTF0("Player model '%s' could not be loaded\n", string); |
---|
112 | } |
---|
113 | |
---|
114 | this->weapons = new tList<Weapon>(); |
---|
115 | this->activeWeapon = NULL; |
---|
116 | /* |
---|
117 | this is the debug player - actualy we would have to make a new |
---|
118 | class derivated from Player for each player. for now, we just use |
---|
119 | the player.cc for debug also |
---|
120 | */ |
---|
121 | travelSpeed = 15.0; |
---|
122 | velocity = new Vector(); |
---|
123 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
124 | bFire = false; |
---|
125 | acceleration = 10.0; |
---|
126 | //weapons: |
---|
127 | Weapon* wp = new TestGun(this, new Vector(-1.1, 0.0, 2.6), new Quaternion()); |
---|
128 | this->weapons->add(wp); |
---|
129 | this->activeWeapon = wp; |
---|
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->weapons->add(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->weapons->remove(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 | |
---|
211 | |
---|
212 | /** |
---|
213 | \brief the function called for each passing timeSnap |
---|
214 | \param time The timespan passed since last update |
---|
215 | */ |
---|
216 | void Player::tick (float time) |
---|
217 | { |
---|
218 | /* link tick to weapon */ |
---|
219 | this->activeWeapon->tick(time); |
---|
220 | // player controlled movement |
---|
221 | this->move(time); |
---|
222 | // weapon system manipulation |
---|
223 | this->weapon(); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | \brief action if player moves |
---|
229 | \param time the timeslice since the last frame |
---|
230 | */ |
---|
231 | void Player::move (float time) |
---|
232 | { |
---|
233 | Vector accel(0.0, 0.0, 0.0); |
---|
234 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
235 | |
---|
236 | /* calculate the direction in which the craft is heading */ |
---|
237 | Vector direction (1.0, 0.0, 0.0); |
---|
238 | //direction = this->absDirection.apply (direction); |
---|
239 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
240 | //orthDirection = orthDirection.cross (direction); |
---|
241 | |
---|
242 | if( this->bUp && this->getRelCoor()->x < 20) |
---|
243 | accel = accel+(direction*acceleration); |
---|
244 | if( this->bDown && this->getRelCoor()->x > -5) |
---|
245 | accel = accel-(direction*acceleration); |
---|
246 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor()->z*2) |
---|
247 | accel = accel - (orthDirection*acceleration); |
---|
248 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor()->z*2) |
---|
249 | accel = accel + (orthDirection*acceleration); |
---|
250 | if( this->bAscend ) |
---|
251 | if( this->bDescend) {/* FIXME */} /* \todo up and down player movement */ |
---|
252 | |
---|
253 | Vector move = accel * time; |
---|
254 | this->shiftCoor (&move); |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | /** |
---|
259 | \brief weapon manipulation by the player |
---|
260 | */ |
---|
261 | void Player::weapon() |
---|
262 | { |
---|
263 | if( this->bFire) |
---|
264 | { |
---|
265 | if(this->activeWeapon != NULL) |
---|
266 | this->activeWeapon->fire(); |
---|
267 | } |
---|
268 | if( this->bWeaponChange && this->weapons->getSize() > 1) |
---|
269 | { |
---|
270 | PRINTF(1)("changing the weapon of the player: deactivate old, activate new\n"); |
---|
271 | this->activeWeapon->deactivate(); |
---|
272 | //this->weapons->enumerate(); FIX: strang weapon change... |
---|
273 | this->activeWeapon = this->weapons->nextElement(this->activeWeapon); |
---|
274 | this->activeWeapon->activate(); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /** |
---|
280 | \brief The connection to the command node |
---|
281 | \param cmd the Command unit from witch to map |
---|
282 | |
---|
283 | here the commands are mapped to the players movement/weaponary |
---|
284 | */ |
---|
285 | void Player::command (Command* cmd) |
---|
286 | { |
---|
287 | PRINTF(3)("recieved command [%s]\n", cmd->cmd); |
---|
288 | if( !strcmp( cmd->cmd, "up")) this->bUp = !cmd->bUp; |
---|
289 | else if( !strcmp( cmd->cmd, "down")) this->bDown = !cmd->bUp; |
---|
290 | else if( !strcmp( cmd->cmd, "left")) this->bLeft = !cmd->bUp; |
---|
291 | else if( !strcmp( cmd->cmd, "right")) this->bRight = !cmd->bUp; |
---|
292 | else if( !strcmp( cmd->cmd, "fire")) this->bFire = !cmd->bUp; |
---|
293 | else if( !strcmp( cmd->cmd, "mode")) this->bWeaponChange = !cmd->bUp; |
---|
294 | } |
---|