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 | #include "weapon.h" |
---|
24 | #include "test_gun.h" |
---|
25 | #include "world.h" |
---|
26 | |
---|
27 | #include "list.h" |
---|
28 | #include "stdincl.h" |
---|
29 | |
---|
30 | |
---|
31 | #include "projectile.h" |
---|
32 | |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | CREATE_FACTORY(Player); |
---|
36 | |
---|
37 | /** |
---|
38 | \brief creates a new Player |
---|
39 | \param isFree if the player is free |
---|
40 | */ |
---|
41 | Player::Player() : WorldEntity(), PhysicsInterface(this) |
---|
42 | { |
---|
43 | /* |
---|
44 | this is the debug player - actualy we would have to make a new |
---|
45 | class derivated from Player for each player. for now, we just use |
---|
46 | the player.cc for debug also |
---|
47 | */ |
---|
48 | this->setClassName("Player"); |
---|
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 | this->bWeaponChange = false; |
---|
55 | acceleration = 10.0; |
---|
56 | //weapons: |
---|
57 | this->weaponMan = new WeaponManager(); |
---|
58 | Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0); |
---|
59 | Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1); |
---|
60 | |
---|
61 | this->weaponMan->addWeapon(wpRight, W_CONFIG0, W_SLOT0); |
---|
62 | this->weaponMan->addWeapon(wpLeft, W_CONFIG1, W_SLOT1); |
---|
63 | this->weaponMan->addWeapon(wpRight, W_CONFIG2); |
---|
64 | this->weaponMan->addWeapon(wpLeft, W_CONFIG2); |
---|
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 | \brief creates a new Player from Xml Data |
---|
81 | \param root the xml element containing player data |
---|
82 | |
---|
83 | \todo add more parameters to load |
---|
84 | */ |
---|
85 | Player::Player(const TiXmlElement* root) : WorldEntity(root), PhysicsInterface(this) |
---|
86 | { |
---|
87 | this->setClassName("Player"); |
---|
88 | this->weapons = new tList<Weapon>(); |
---|
89 | this->activeWeapon = NULL; |
---|
90 | /* |
---|
91 | this is the debug player - actualy we would have to make a new |
---|
92 | class derivated from Player for each player. for now, we just use |
---|
93 | the player.cc for debug also |
---|
94 | */ |
---|
95 | travelSpeed = 15.0; |
---|
96 | velocity = new Vector(); |
---|
97 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
98 | bFire = false; |
---|
99 | this->bWeaponChange = false; |
---|
100 | acceleration = 10.0; |
---|
101 | //weapons: |
---|
102 | this->weaponMan = new WeaponManager(); |
---|
103 | Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0); |
---|
104 | Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1); |
---|
105 | |
---|
106 | this->weaponMan->addWeapon(wpRight, W_CONFIG0, W_SLOT0); |
---|
107 | this->weaponMan->addWeapon(wpLeft, W_CONFIG1, W_SLOT1); |
---|
108 | this->weaponMan->addWeapon(wpRight, W_CONFIG2); |
---|
109 | this->weaponMan->addWeapon(wpLeft, W_CONFIG2); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | \brief adds a weapon to the weapon list of player |
---|
114 | \param weapon to add |
---|
115 | */ |
---|
116 | void Player::addWeapon(Weapon* weapon) |
---|
117 | { |
---|
118 | this->weaponMan->addWeapon(weapon); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | \brief removes a weapon from the player |
---|
124 | \param weapon to remove |
---|
125 | */ |
---|
126 | void Player::removeWeapon(Weapon* weapon) |
---|
127 | { |
---|
128 | this->weaponMan->removeWeapon(weapon); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | \brief effect that occurs after the player is spawned |
---|
134 | */ |
---|
135 | void Player::postSpawn () |
---|
136 | { |
---|
137 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | /** |
---|
142 | \brief the action occuring if the player left the game |
---|
143 | */ |
---|
144 | void Player::leftWorld () |
---|
145 | {} |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | /** |
---|
150 | \brief if the player is hit, call this function |
---|
151 | \param weapon hit by this weapon |
---|
152 | \param loc ?? |
---|
153 | */ |
---|
154 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
155 | { |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | \brief Collision with another Entity has this effect |
---|
161 | \param other the other colider |
---|
162 | \param ownhitflags ?? |
---|
163 | \param otherhitflags ?? |
---|
164 | */ |
---|
165 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
166 | { |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | /** |
---|
171 | \brief draws the player after transforming him. |
---|
172 | */ |
---|
173 | void Player::draw () |
---|
174 | { |
---|
175 | glMatrixMode(GL_MODELVIEW); |
---|
176 | glPushMatrix(); |
---|
177 | float matrix[4][4]; |
---|
178 | |
---|
179 | /* translate */ |
---|
180 | glTranslatef (this->getAbsCoor ().x, |
---|
181 | this->getAbsCoor ().y, |
---|
182 | this->getAbsCoor ().z); |
---|
183 | /* rotate */ |
---|
184 | this->getAbsDir ().matrix (matrix); |
---|
185 | glMultMatrixf((float*)matrix); |
---|
186 | |
---|
187 | this->model->draw(); |
---|
188 | glPopMatrix(); |
---|
189 | |
---|
190 | this->weaponMan->draw(); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /** |
---|
195 | \brief the function called for each passing timeSnap |
---|
196 | \param time The timespan passed since last update |
---|
197 | */ |
---|
198 | void Player::tick (float time) |
---|
199 | { |
---|
200 | printf("%p\n", this); |
---|
201 | this->getRelCoor().debug(); |
---|
202 | |
---|
203 | /* link tick to weapon */ |
---|
204 | //this->activeWeapon->tick(time); |
---|
205 | //this->activeWeaponL->tick(time); //FIX FIX DELETE REMOVE |
---|
206 | this->weaponMan->tick(time); |
---|
207 | // player controlled movement |
---|
208 | this->move(time); |
---|
209 | // weapon system manipulation |
---|
210 | this->weapon(); |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | /** |
---|
215 | \brief action if player moves |
---|
216 | \param time the timeslice since the last frame |
---|
217 | */ |
---|
218 | void Player::move (float time) |
---|
219 | { |
---|
220 | Vector accel(0.0, 0.0, 0.0); |
---|
221 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
222 | /* calculate the direction in which the craft is heading */ |
---|
223 | Vector direction (1.0, 0.0, 0.0); |
---|
224 | //direction = this->absDirection.apply (direction); |
---|
225 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
226 | //orthDirection = orthDirection.cross (direction); |
---|
227 | |
---|
228 | if( this->bUp && this->getRelCoor().x < 20) |
---|
229 | accel = accel+(direction*acceleration); |
---|
230 | if( this->bDown && this->getRelCoor().x > -5) |
---|
231 | accel = accel-(direction*acceleration); |
---|
232 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
---|
233 | accel = accel - (orthDirection*acceleration); |
---|
234 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
---|
235 | accel = accel + (orthDirection*acceleration); |
---|
236 | if( this->bAscend ) |
---|
237 | if( this->bDescend) {/* FIXME */} /* \todo up and down player movement */ |
---|
238 | |
---|
239 | Vector move = accel * time; |
---|
240 | this->shiftCoor (move); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | /** |
---|
245 | \brief weapon manipulation by the player |
---|
246 | */ |
---|
247 | void Player::weapon() |
---|
248 | { |
---|
249 | if( this->bFire) |
---|
250 | { |
---|
251 | this->weaponMan->fire(); |
---|
252 | } |
---|
253 | if( this->bWeaponChange) |
---|
254 | { |
---|
255 | this->weaponMan->nextWeaponConf(); |
---|
256 | this->bWeaponChange = false; |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | /** |
---|
262 | \brief The connection to the command node |
---|
263 | \param cmd the Command unit from witch to map |
---|
264 | |
---|
265 | here the commands are mapped to the players movement/weaponary |
---|
266 | */ |
---|
267 | void Player::command (Command* cmd) |
---|
268 | { |
---|
269 | PRINTF(3)("recieved command [%s]\n", cmd->cmd); |
---|
270 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_UP)) this->bUp = !cmd->bUp; |
---|
271 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_DOWN)) this->bDown = !cmd->bUp; |
---|
272 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_LEFT)) this->bLeft = !cmd->bUp; |
---|
273 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_RIGHT)) this->bRight = !cmd->bUp; |
---|
274 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_FIRE)) this->bFire = !cmd->bUp; |
---|
275 | if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_NEXT_WEAPON)) if(cmd->bUp) this->bWeaponChange = !this->bWeaponChange; |
---|
276 | } |
---|