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 | /** |
---|
35 | \brief creates a new Player |
---|
36 | \param isFree if the player is free |
---|
37 | */ |
---|
38 | Player::Player() : WorldEntity() |
---|
39 | { |
---|
40 | this->weapons = new tList<Weapon>(); |
---|
41 | this->activeWeapon = NULL; |
---|
42 | /* |
---|
43 | this is the debug player - actualy we would have to make a new |
---|
44 | class derivated from Player for each player. for now, we just use |
---|
45 | the player.cc for debug also |
---|
46 | */ |
---|
47 | this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN); |
---|
48 | travelSpeed = 15.0; |
---|
49 | velocity = new Vector(); |
---|
50 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
51 | bFire = false; |
---|
52 | acceleration = 10.0; |
---|
53 | //weapons: |
---|
54 | Weapon* wp = new TestGun(this, new Vector(-1.1, 0.0, 2.6), new Quaternion()); |
---|
55 | this->weapons->add(wp); |
---|
56 | this->activeWeapon = wp; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | \brief destructs the player, deletes alocated memory |
---|
61 | */ |
---|
62 | Player::~Player () |
---|
63 | { |
---|
64 | /* do not delete the weapons, they are contained in the pnode tree |
---|
65 | and will be deleted there. |
---|
66 | this only frees the memory allocated to save the list. |
---|
67 | */ |
---|
68 | delete this->weapons; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | \brief adds a weapon to the weapon list of player |
---|
74 | \param weapon to add |
---|
75 | */ |
---|
76 | void Player::addWeapon(Weapon* weapon) |
---|
77 | { |
---|
78 | this->weapons->add(weapon); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | \brief removes a weapon from the player |
---|
84 | \param weapon to remove |
---|
85 | */ |
---|
86 | void Player::removeWeapon(Weapon* weapon) |
---|
87 | { |
---|
88 | this->weapons->remove(weapon); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | \brief effect that occurs after the player is spawned |
---|
94 | */ |
---|
95 | void Player::postSpawn () |
---|
96 | { |
---|
97 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | \brief the action occuring if the player left the game |
---|
103 | */ |
---|
104 | void Player::leftWorld () |
---|
105 | {} |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | \brief if the player is hit, call this function |
---|
111 | \param weapon hit by this weapon |
---|
112 | \param loc ?? |
---|
113 | */ |
---|
114 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
115 | { |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /** |
---|
120 | \brief Collision with another Entity has this effect |
---|
121 | \param other the other colider |
---|
122 | \param ownhitflags ?? |
---|
123 | \param otherhitflags ?? |
---|
124 | */ |
---|
125 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
126 | { |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | \brief draws the player after transforming him. |
---|
132 | */ |
---|
133 | void Player::draw () |
---|
134 | { |
---|
135 | glMatrixMode(GL_MODELVIEW); |
---|
136 | glPushMatrix(); |
---|
137 | float matrix[4][4]; |
---|
138 | |
---|
139 | /* translate */ |
---|
140 | glTranslatef (this->getAbsCoor ().x, |
---|
141 | this->getAbsCoor ().y, |
---|
142 | this->getAbsCoor ().z); |
---|
143 | /* rotate */ |
---|
144 | this->getAbsDir ().matrix (matrix); |
---|
145 | glMultMatrixf((float*)matrix); |
---|
146 | |
---|
147 | this->model->draw(); |
---|
148 | glPopMatrix(); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | /** |
---|
153 | \brief the function called for each passing timeSnap |
---|
154 | \param time The timespan passed since last update |
---|
155 | */ |
---|
156 | void Player::tick (float time) |
---|
157 | { |
---|
158 | /* link tick to weapon */ |
---|
159 | this->activeWeapon->tick(time); |
---|
160 | // player controlled movement |
---|
161 | this->move(time); |
---|
162 | // weapon system manipulation |
---|
163 | this->weapon(); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | /** |
---|
168 | \brief action if player moves |
---|
169 | \param time the timeslice since the last frame |
---|
170 | */ |
---|
171 | void Player::move (float time) |
---|
172 | { |
---|
173 | Vector accel(0.0, 0.0, 0.0); |
---|
174 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
175 | |
---|
176 | /* calculate the direction in which the craft is heading */ |
---|
177 | Vector direction (1.0, 0.0, 0.0); |
---|
178 | //direction = this->absDirection.apply (direction); |
---|
179 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
180 | //orthDirection = orthDirection.cross (direction); |
---|
181 | |
---|
182 | if( this->bUp && this->getRelCoor()->x < 20) |
---|
183 | accel = accel+(direction*acceleration); |
---|
184 | if( this->bDown && this->getRelCoor()->x > -5) |
---|
185 | accel = accel-(direction*acceleration); |
---|
186 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor()->z*2) |
---|
187 | accel = accel - (orthDirection*acceleration); |
---|
188 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor()->z*2) |
---|
189 | accel = accel + (orthDirection*acceleration); |
---|
190 | if( this->bAscend ) |
---|
191 | if( this->bDescend) {/* FIXME */} /* \todo up and down player movement */ |
---|
192 | |
---|
193 | Vector move = accel * time; |
---|
194 | this->shiftCoor (&move); |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | /** |
---|
199 | \brief weapon manipulation by the player |
---|
200 | */ |
---|
201 | void Player::weapon() |
---|
202 | { |
---|
203 | if( this->bFire) |
---|
204 | { |
---|
205 | if(this->activeWeapon != NULL) |
---|
206 | this->activeWeapon->fire(); |
---|
207 | } |
---|
208 | if( this->bWeaponChange && this->weapons->getSize() > 1) |
---|
209 | { |
---|
210 | PRINTF(1)("changing the weapon of the player: deactivate old, activate new\n"); |
---|
211 | this->activeWeapon->deactivate(); |
---|
212 | //this->weapons->enumerate(); FIX: strang weapon change... |
---|
213 | this->activeWeapon = this->weapons->nextElement(this->activeWeapon); |
---|
214 | this->activeWeapon->activate(); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | /** |
---|
220 | \brief The connection to the command node |
---|
221 | \param cmd the Command unit from witch to map |
---|
222 | |
---|
223 | here the commands are mapped to the players movement/weaponary |
---|
224 | */ |
---|
225 | void Player::command (Command* cmd) |
---|
226 | { |
---|
227 | PRINTF(3)("recieved command [%s]\n", cmd->cmd); |
---|
228 | if( !strcmp( cmd->cmd, "up")) this->bUp = !cmd->bUp; |
---|
229 | else if( !strcmp( cmd->cmd, "down")) this->bDown = !cmd->bUp; |
---|
230 | else if( !strcmp( cmd->cmd, "left")) this->bLeft = !cmd->bUp; |
---|
231 | else if( !strcmp( cmd->cmd, "right")) this->bRight = !cmd->bUp; |
---|
232 | else if( !strcmp( cmd->cmd, "fire")) this->bFire = !cmd->bUp; |
---|
233 | else if( !strcmp( cmd->cmd, "mode")) this->bWeaponChange = !cmd->bUp; |
---|
234 | } |
---|