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 "stdincl.h" |
---|
23 | //#include "collision.h" |
---|
24 | #include "objModel.h" |
---|
25 | #include "list.h" |
---|
26 | #include "weapon.h" |
---|
27 | #include "track_manager.h" |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | CREATE_FACTORY(Player); |
---|
32 | |
---|
33 | /** |
---|
34 | \brief creates a new Player |
---|
35 | \param isFree if the player is free |
---|
36 | */ |
---|
37 | Player::Player(bool isFree) : WorldEntity(isFree) |
---|
38 | { |
---|
39 | this->model = new OBJModel("../data/models/reaplow.obj"); |
---|
40 | this->weapons = new tList<Weapon>(); |
---|
41 | this->activeWeapon = NULL; |
---|
42 | |
---|
43 | travelSpeed = 15.0; |
---|
44 | velocity = Vector(); |
---|
45 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
46 | bFire = false; |
---|
47 | acceleration = 10.0; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | \brief destructs the player, deletes alocated memory |
---|
52 | */ |
---|
53 | Player::~Player () |
---|
54 | { |
---|
55 | Weapon* w = this->weapons->enumerate(); |
---|
56 | while( w != NULL) |
---|
57 | { |
---|
58 | delete w; |
---|
59 | w = this->weapons->nextElement(); |
---|
60 | } |
---|
61 | delete this->weapons; |
---|
62 | |
---|
63 | //delete this->velocity; |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | \brief creates a new Player from Xml Data |
---|
69 | \param root the xml element containing player data |
---|
70 | |
---|
71 | \todo add more parameters to load |
---|
72 | */ |
---|
73 | Player::Player(TiXmlElement* root) |
---|
74 | { |
---|
75 | char* temp; |
---|
76 | const char* string; |
---|
77 | string = grabParameter( root, "name"); |
---|
78 | if( string == NULL) |
---|
79 | { |
---|
80 | PRINTF0("Player is missing a proper 'name'\n"); |
---|
81 | string = "Unknown"; |
---|
82 | temp = new char[strlen(string + 2)]; |
---|
83 | strcpy( temp, string); |
---|
84 | this->setName( temp); |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | temp = new char[strlen(string + 2)]; |
---|
89 | strcpy( temp, string); |
---|
90 | this->setName( temp); |
---|
91 | } |
---|
92 | |
---|
93 | this->model = NULL; |
---|
94 | string = grabParameter( root, "model"); |
---|
95 | if( string != NULL) |
---|
96 | this->model = new OBJModel( string); |
---|
97 | else |
---|
98 | { |
---|
99 | PRINTF0("Player is missing a proper 'model'\n"); |
---|
100 | this->model = new OBJModel( "../data/models/reaplow.obj"); |
---|
101 | } |
---|
102 | if( this->model == NULL) |
---|
103 | { |
---|
104 | PRINTF0("Player model '%s' could not be loaded\n", string); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | \brief adds a weapon to the weapon list of player |
---|
110 | \param weapon to add |
---|
111 | */ |
---|
112 | void Player::addWeapon(Weapon* weapon) |
---|
113 | { |
---|
114 | this->weapons->add(weapon); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | /** |
---|
119 | \brief removes a weapon from the player |
---|
120 | \param weapon to remove |
---|
121 | */ |
---|
122 | void Player::removeWeapon(Weapon* weapon) |
---|
123 | { |
---|
124 | this->weapons->remove(weapon); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /** |
---|
129 | \brief effect that occurs after the player is spawned |
---|
130 | */ |
---|
131 | void Player::postSpawn () |
---|
132 | { |
---|
133 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | /** |
---|
138 | \brief the action occuring if the player left the game |
---|
139 | */ |
---|
140 | void Player::leftWorld () |
---|
141 | {} |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | /** |
---|
146 | \brief if the player is hit, call this function |
---|
147 | \param weapon hit by this weapon |
---|
148 | \param loc ?? |
---|
149 | */ |
---|
150 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
151 | { |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | /** |
---|
156 | \brief Collision with another Entity has this effect |
---|
157 | \param other the other colider |
---|
158 | \param ownhitflags ?? |
---|
159 | \param otherhitflags ?? |
---|
160 | */ |
---|
161 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
162 | { |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | /** |
---|
167 | \brief draws the player after transforming him. |
---|
168 | */ |
---|
169 | void Player::draw () |
---|
170 | { |
---|
171 | glMatrixMode(GL_MODELVIEW); |
---|
172 | glPushMatrix(); |
---|
173 | float matrix[4][4]; |
---|
174 | |
---|
175 | /* translate */ |
---|
176 | glTranslatef (this->getAbsCoor ().x, |
---|
177 | this->getAbsCoor ().y, |
---|
178 | this->getAbsCoor ().z); |
---|
179 | /* rotate */ |
---|
180 | this->getAbsDir ().matrix (matrix); |
---|
181 | glMultMatrixf((float*)matrix); |
---|
182 | |
---|
183 | this->model->draw(); |
---|
184 | glPopMatrix(); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /** |
---|
189 | \brief the function called for each passing timeSnap |
---|
190 | \param time The timespan passed since last update |
---|
191 | */ |
---|
192 | void Player::tick (float time) |
---|
193 | { |
---|
194 | // player controlled movement |
---|
195 | this->move (time); |
---|
196 | // weapon system manipulation |
---|
197 | this->fire(); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | /** |
---|
202 | \brief action if player moves |
---|
203 | \param time the timeslice since the last frame |
---|
204 | */ |
---|
205 | void Player::move (float time) |
---|
206 | { |
---|
207 | Vector accel(0.0, 0.0, 0.0); |
---|
208 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
209 | |
---|
210 | /* calculate the direction in which the craft is heading */ |
---|
211 | Vector direction (1.0, 0.0, 0.0); |
---|
212 | //direction = this->absDirection.apply (direction); |
---|
213 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
214 | //orthDirection = orthDirection.cross (direction); |
---|
215 | |
---|
216 | if( this->bUp && this->getRelCoor().x < 20) |
---|
217 | accel = accel+(direction*acceleration); |
---|
218 | if( this->bDown && this->getRelCoor().x > -5) |
---|
219 | accel = accel-(direction*acceleration); |
---|
220 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
---|
221 | accel = accel - (orthDirection*acceleration); |
---|
222 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
---|
223 | accel = accel + (orthDirection*acceleration); |
---|
224 | if( this->bAscend ) |
---|
225 | if( this->bDescend) {/* FIXME */} /* \todo up and down player movement */ |
---|
226 | |
---|
227 | Vector move = accel * time; |
---|
228 | this->shiftCoor (&move); |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | /** |
---|
233 | \brief weapon manipulation by the player |
---|
234 | */ |
---|
235 | void Player::fire() |
---|
236 | { |
---|
237 | if(this->bFire) |
---|
238 | { |
---|
239 | if(this->activeWeapon != NULL) |
---|
240 | this->activeWeapon->fire(); |
---|
241 | } |
---|
242 | if(this->bWeaponChange) |
---|
243 | { |
---|
244 | Weapon* w = this->weapons->enumerate(); |
---|
245 | this->activeWeapon = this->weapons->nextElement(this->activeWeapon); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | /** |
---|
251 | \brief The connection to the command node |
---|
252 | \param cmd the Command unit from witch to map |
---|
253 | |
---|
254 | here the commands are mapped to the players movement/weaponary |
---|
255 | */ |
---|
256 | void Player::command (Command* cmd) |
---|
257 | { |
---|
258 | PRINTF(3)("recieved command [%s]\n", cmd->cmd); |
---|
259 | if( !strcmp( cmd->cmd, "up")) this->bUp = !cmd->bUp; |
---|
260 | else if( !strcmp( cmd->cmd, "down")) this->bDown = !cmd->bUp; |
---|
261 | else if( !strcmp( cmd->cmd, "left")) this->bLeft = !cmd->bUp; |
---|
262 | else if( !strcmp( cmd->cmd, "right")) this->bRight = !cmd->bUp; |
---|
263 | else if( !strcmp( cmd->cmd, "fire")) this->bFire = !cmd->bUp; |
---|
264 | else if( !strcmp( cmd->cmd, "mode")) this->bWeaponChange = !cmd->bUp; |
---|
265 | } |
---|