author | terom |
Wed, 17 Dec 2008 01:13:26 +0000 | |
changeset 383 | 2a57f0a871b0 |
parent 342 | 4abe994e61d5 |
child 408 | e6cfc44266af |
permissions | -rw-r--r-- |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
1 |
|
236 | 2 |
#include "Player.hh" |
3 |
#include "Weapons.hh" |
|
199 | 4 |
#include "Engine.hh" |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
5 |
#include "Graphics.hh" |
198 | 6 |
|
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
7 |
#include <cstdlib> |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
8 |
#include <ClanLib/display.h> |
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
9 |
#include <algorithm> |
216 | 10 |
#include <string> |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
11 |
#include <cassert> |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
12 |
|
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
13 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
14 |
// player static state |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
15 |
bool Player::skin_loaded = false; |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
16 |
CL_Surface Player::skin_surface; |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
17 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
18 |
// XXX: give these better names and move elsewhere |
342 | 19 |
const int img_num_aim = 5; |
219 | 20 |
const int img_num_step = 4; |
248 | 21 |
const int img_height = 20; |
22 |
const int img_width = 17; |
|
216 | 23 |
|
198 | 24 |
Player::Player(GameState &state, Vector position, bool visible) : |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
25 |
PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0), PLAYER, PLAYER_COLLISION_ELASTICITY), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
26 |
state(state), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
27 |
visible(visible), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
28 |
weapons(buildWeaponsList()), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
29 |
selectedWeapon(0), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
30 |
rope(*this), |
300 | 31 |
health(PLAYER_HEALTH), |
32 |
respawn_timer(PLAYER_RESPAWN_DELAY), |
|
307
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
33 |
animation_step(0), |
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
34 |
kills(0), |
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
35 |
deaths(0) |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
36 |
{ |
236 | 37 |
// XXX: populate weapons from somewhere else |
38 |
||
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
39 |
// build the player's shape |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
40 |
// XXX: these dimensions are incorrect... |
198 | 41 |
std::vector<Vector> shape(4); |
42 |
shape[0] = Vector(0,-9); |
|
43 |
shape[1] = Vector(6,0); |
|
44 |
shape[2] = Vector(0,9); |
|
45 |
shape[3] = Vector(-6,0); |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
46 |
|
198 | 47 |
// Initialize the shape of the player (salmiakki shape) |
48 |
setShape(shape); |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
49 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
50 |
// add to GameState players list |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
51 |
state.addPlayer(this); |
300 | 52 |
|
53 |
// connect respawn timer |
|
54 |
respawn_slot = respawn_timer.sig_tick().connect(this, &Player::respawn); |
|
55 |
||
56 |
// spawn |
|
57 |
spawn(position); |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
58 |
} |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
59 |
|
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
60 |
Player::~Player (void) { |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
61 |
state.removePlayer(this); |
198 | 62 |
} |
300 | 63 |
|
64 |
void Player::spawn (Vector position) { |
|
65 |
// dig hole |
|
66 |
world.removeGround(position, PLAYER_DIG_RADIUS); |
|
67 |
||
68 |
// update position |
|
69 |
setPosition(position); |
|
70 |
||
330 | 71 |
// reset health |
72 |
health = PLAYER_HEALTH; |
|
73 |
||
74 |
// XXX: reload weapons |
|
75 |
||
300 | 76 |
// enable |
77 |
enable(); |
|
78 |
} |
|
79 |
||
302 | 80 |
void Player::die (bool start_timer) { |
307
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
81 |
|
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
82 |
deaths++; // Increment death counter |
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
83 |
|
302 | 84 |
// we don't have a rope anymore |
85 |
rope.release(); |
|
86 |
||
300 | 87 |
// disable our PhysicsObject |
88 |
disable(); |
|
302 | 89 |
|
90 |
// XXX: PhysicsObject::reset |
|
91 |
this->velocity = Vector(0, 0); |
|
300 | 92 |
|
302 | 93 |
if (start_timer) { |
94 |
// start respawn timer |
|
95 |
respawn_timer.fire_once(); |
|
96 |
} |
|
300 | 97 |
} |
98 |
||
99 |
void Player::respawn (TimeMS dt) { |
|
100 |
(void) dt; |
|
101 |
||
102 |
// XXX: ... |
|
103 |
spawn(Vector(PLAYER_INITIAL_X, PLAYER_INITIAL_Y)); |
|
104 |
} |
|
236 | 105 |
|
272 | 106 |
void Player::handleDig (Vector pos, float radius) { |
250
84c553d61291
fixed digging, now bugs differently. Changed image_drawing to draw image 1:1
nireco
parents:
248
diff
changeset
|
107 |
// calculate new position and velocity |
272 | 108 |
Vector digPosition = pos + getDirection() * PROJECTILE_START_DISTANCE; |
271
bf6784a95b08
touch up weapon/projectile with comments and slight tweaking
terom
parents:
270
diff
changeset
|
109 |
|
bf6784a95b08
touch up weapon/projectile with comments and slight tweaking
terom
parents:
270
diff
changeset
|
110 |
// remove directly |
270
d17126e20de7
Changed dig to use removeGround again. The problem with the digging is the fact that input isn't handled properly.
saiam
parents:
265
diff
changeset
|
111 |
world.removeGround(digPosition, radius); |
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
112 |
} |
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
113 |
|
276
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
114 |
void Player::handleFireWeapon (Weapon *weapon, Vector position, Vector velocity) { |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
115 |
weaponFired(weapon); |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
116 |
|
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
117 |
new Projectile(this, position, velocity, weapon); |
199 | 118 |
} |
237 | 119 |
|
120 |
void Player::handleChangeWeapon (unsigned int weaponIndex) { |
|
121 |
assert(weaponIndex < weapons.size()); |
|
122 |
||
123 |
selectedWeapon = weaponIndex; |
|
124 |
} |
|
276
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
125 |
|
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
126 |
void Player::weaponFired (Weapon *weapon) { |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
127 |
// apply recoil |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
128 |
applyForce(-getDirection() * weapon->getRecoil()); |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
129 |
|
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
130 |
// reload weapon |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
131 |
weapon->reload(); |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
132 |
} |
199 | 133 |
|
221 | 134 |
void Player::printDebugInfo (void) { |
135 |
Engine::log(DEBUG, "layer.debug") << "In air: " << this->inAir; |
|
136 |
} |
|
137 |
||
138 |
void Player::tick (TimeMS dt) { |
|
139 |
// let PhysicsObject execute |
|
140 |
PhysicsObject::tick(dt); |
|
300 | 141 |
|
221 | 142 |
// tick current weapon reload |
236 | 143 |
if (getCurrentWeapon()) |
144 |
getCurrentWeapon()->tickReload(dt); |
|
221 | 145 |
} |
241 | 146 |
|
147 |
void Player::handleRopeState (RopeState state) { |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
148 |
(void) state; |
241 | 149 |
} |
150 |
||
151 |
void Player::handleRopeLength (float length) { |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
152 |
(void) length; |
241 | 153 |
} |
199 | 154 |
|
300 | 155 |
/* |
156 |
* LocalPlayer |
|
157 |
*/ |
|
236 | 158 |
void LocalPlayer::fireWeapon (Weapon *weapon) { |
235 | 159 |
// calculate new position and velocity |
160 |
Vector shotPosition = position + getDirection() * PROJECTILE_START_DISTANCE; |
|
236 | 161 |
Vector shotVelocity = velocity + getDirection() * weapon->getSpeed(); |
258
833ad8d7db8b
The first weapon now has a recoil so that you can fly. There is a bug with facing when you are flying with recoil.
saiam
parents:
257
diff
changeset
|
162 |
|
223 | 163 |
// execute |
276
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
164 |
handleFireWeapon(weapon, shotPosition, shotVelocity); |
223 | 165 |
} |
237 | 166 |
|
167 |
void LocalPlayer::changeWeapon (int delta) { |
|
168 |
// need to handle negative deltas |
|
169 |
handleChangeWeapon((weapons.size() + selectedWeapon + delta) % weapons.size()); |
|
170 |
} |
|
223 | 171 |
|
275
fa44b905bc2e
Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents:
274
diff
changeset
|
172 |
void LocalPlayer::handleInput (PlayerInput input, TimeMS dt) { |
299 | 173 |
// if we are dead, we can't do anything |
174 |
if (!isAlive()) |
|
175 |
return; |
|
176 |
||
221 | 177 |
// Movement force, vertical is always zero |
178 |
Vector move_force = Vector(0, 0); |
|
199 | 179 |
|
221 | 180 |
// Crosshair angle change |
181 |
float aim_delta = 0; |
|
199 | 182 |
|
221 | 183 |
// handle movement left/right by applying a horizontal force, but limit the player's speed |
235 | 184 |
// also update facing if needed |
262 | 185 |
if (!((input & INPUT_MOVE_LEFT) && (input & INPUT_MOVE_RIGHT))) { |
186 |
if (input & INPUT_MOVE_LEFT) { |
|
187 |
if (velocity.x > -PLAYER_MAX_SPEED) |
|
188 |
move_force.x -= PLAYER_MOVE_FORCE; |
|
189 |
||
264 | 190 |
setFacing(FACING_LEFT); |
262 | 191 |
} |
221 | 192 |
|
262 | 193 |
if (input & INPUT_MOVE_RIGHT) { |
194 |
if (velocity.x < PLAYER_MAX_SPEED) |
|
195 |
move_force.x += PLAYER_MOVE_FORCE; |
|
196 |
||
264 | 197 |
setFacing(FACING_RIGHT); |
262 | 198 |
} |
221 | 199 |
} |
200 |
||
201 |
// handle aim by creating a aim angle delta |
|
202 |
if (input & INPUT_AIM_UP) |
|
293 | 203 |
aim_delta += CROSSHAIR_ANGLE_SPEED*dt; |
221 | 204 |
|
205 |
if (input & INPUT_AIM_DOWN) |
|
293 | 206 |
aim_delta -= CROSSHAIR_ANGLE_SPEED*dt; |
221 | 207 |
|
208 |
// handle jumping by invoking the jump method |
|
209 |
// XXX: the direction should ideally be given using some other method |
|
210 |
if (input & INPUT_JUMP) { |
|
211 |
if (input & INPUT_MOVE_LEFT) |
|
199 | 212 |
jump(-1); |
221 | 213 |
|
214 |
else if (input & INPUT_MOVE_RIGHT) |
|
199 | 215 |
jump(1); |
221 | 216 |
|
199 | 217 |
else |
218 |
jump(0); |
|
219 |
} |
|
221 | 220 |
|
221 |
// outsource digging to Player::handleDig, since this modifies the Terrain and Network needs to know |
|
235 | 222 |
if (input & INPUT_DIG) |
300 | 223 |
handleDig(position, PLAYER_DIG_RADIUS); |
221 | 224 |
|
237 | 225 |
// change weapon back/forth |
226 |
if (input & INPUT_CHANGE_PREV) |
|
227 |
changeWeapon(-1); |
|
228 |
||
229 |
if (input & INPUT_CHANGE_NEXT) |
|
230 |
changeWeapon(+1); |
|
213 | 231 |
|
221 | 232 |
// validate shoot events, and then outsource to handleShoot so Network can intercept it |
236 | 233 |
if ((input & INPUT_SHOOT) && getCurrentWeapon()->canShoot()) |
234 |
fireWeapon(getCurrentWeapon()); |
|
221 | 235 |
|
241 | 236 |
// rope throw+release+changeLength, but supress spurious events |
225 | 237 |
if (input & INPUT_ROPE) |
235 | 238 |
rope.throwRope(); |
225 | 239 |
|
241 | 240 |
if (input & INPUT_UNROPE && rope.getState() != ROPE_FOLDED) |
229 | 241 |
rope.release(); |
242 |
||
241 | 243 |
if (input & INPUT_ROPE_UP && rope.getState() == ROPE_FIXED) |
235 | 244 |
rope.changeLength(-ROPE_GROWTH_RATE); |
231 | 245 |
|
241 | 246 |
if (input & INPUT_ROPE_DOWN && rope.getState() == ROPE_FIXED) |
235 | 247 |
rope.changeLength(ROPE_GROWTH_RATE); |
231 | 248 |
|
235 | 249 |
// XXX: how should this be written? What does this do? Probably broken under network play |
221 | 250 |
if (move_force.x != 0) |
251 |
animation_step = (animation_step + 1) % img_num_step; |
|
199 | 252 |
|
221 | 253 |
// apply aim delta |
254 |
if (aim_delta) |
|
255 |
changeAim(aim_delta); |
|
219 | 256 |
|
221 | 257 |
// apply force |
258 |
if (!move_force.zero()) |
|
275
fa44b905bc2e
Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents:
274
diff
changeset
|
259 |
applyForce(move_force, dt); |
300 | 260 |
|
261 |
// suicide? |
|
262 |
if (input & INPUT_SUICIDE) |
|
263 |
die(); |
|
199 | 264 |
} |
265 |
||
236 | 266 |
Weapon* Player::getCurrentWeapon() { |
235 | 267 |
return weapons[selectedWeapon % weapons.size()]; |
212 | 268 |
} |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
269 |
|
276
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
270 |
Weapon* Player::getWeapon (WeaponID id) { |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
271 |
if (id < weapons.size()) |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
272 |
return weapons[id]; |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
273 |
else |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
274 |
return NULL; |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
275 |
} |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
276 |
|
295 | 277 |
void Player::onCollision (Vector collisionPoint, PhysicsObject *other) { |
296
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
295
diff
changeset
|
278 |
(void) collisionPoint; |
295 | 279 |
|
280 |
if (other == NULL) |
|
281 |
return; |
|
282 |
||
283 |
// Currently we handle only player-player collision here. |
|
284 |
// Player-projectile collision is handled in projectile's onCollision. |
|
298 | 285 |
// XXX: not completely network-safe |
295 | 286 |
if (other->getType() == PLAYER) { |
335 | 287 |
//Vector normal = this->getPosition() - other->getPosition(); |
288 |
//this->bounce(normal); |
|
298 | 289 |
|
295 | 290 |
// Move the player back to its previous position so that players won't |
291 |
// stuff inside each others when having a collision walk, e.g. |
|
335 | 292 |
//this->setPosition(this->getPreviousPosition()); |
295 | 293 |
} |
294 |
} |
|
295 |
||
322 | 296 |
Vector Player::getPivotForce () { |
297 |
Vector direction = this->pivot->getPosition() - this->getPosition(); |
|
298 |
float dirLength = direction.length(); |
|
299 |
if (dirLength >= this->rope.getLength()) |
|
300 |
return direction / dirLength * ROPE_FORCE; |
|
301 |
else |
|
302 |
return Vector(0,0); |
|
303 |
} |
|
304 |
||
308
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
305 |
void Player::takeDamage (Projectile *source) { |
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
306 |
health -= source->getDamage(); |
296
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
295
diff
changeset
|
307 |
|
308
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
308 |
if (health <= 0) { |
309 | 309 |
if (this != source->getOwner()) { |
310 |
source->addKillToOwner(); |
|
311 |
} |
|
300 | 312 |
die(); |
308
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
313 |
} |
296
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
295
diff
changeset
|
314 |
|
308
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
315 |
Engine::log(DEBUG, "player.take_damage") << this << ": damage=" << source->getDamage() << ", health=" << health; |
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
316 |
Engine::log(DEBUG, "player.take_damage") << this << ": kills=" << kills << ", deaths=" << deaths; |
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
317 |
} |
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
318 |
|
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
319 |
float Player::getHealthPercent () const { |
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
320 |
return std::max(0.0f, this->health * 100 / (float) PLAYER_HEALTH); |
312 | 321 |
} |
322 |
||
308
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
323 |
void Player::addKill () { |
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
307
diff
changeset
|
324 |
kills++; |
289 | 325 |
} |
326 |
||
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
327 |
void Player::draw (Graphics *g, PixelCoordinate camera) { |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
328 |
CL_GraphicContext *gc = g->get_gc(); |
299 | 329 |
|
330 |
if (!isAlive()) |
|
331 |
return; |
|
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
332 |
|
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
333 |
// draw rope behind player |
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
334 |
rope.draw(g, camera); |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
335 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
336 |
// animation indexes |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
337 |
int aim_img_idx = (int)((1 - (getAim() + KG_PI / 2) / KG_PI) * img_num_aim); |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
338 |
int step_img_idx = animation_step % img_num_step; |
217 | 339 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
340 |
// load skin image if not yet loaded |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
341 |
if (!skin_loaded) { |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
342 |
skin_surface = CL_Surface(PLAYER_SKIN_PATH); |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
343 |
skin_loaded = true; |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
344 |
} |
221 | 345 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
346 |
// calulate where to draw the worm |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
347 |
CL_Rectf destination( |
264 | 348 |
position.x + (facing == FACING_RIGHT ? -1 : 1) * img_width / 2 - camera.x, |
349 |
position.y - img_height / 2 - camera.y, |
|
350 |
position.x + (facing == FACING_RIGHT ? 1 : -1) * img_width / 2 - camera.x, |
|
351 |
position.y + img_height / 2 - camera.y |
|
221 | 352 |
); |
217 | 353 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
354 |
// draw the correct animation frame from the skin |
342 | 355 |
// skin_surface.set_color(CL_Color(255, 0, 255)); |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
356 |
skin_surface.draw_subpixel( |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
357 |
CL_Rectf( |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
358 |
step_img_idx * img_width, |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
359 |
aim_img_idx * img_height, |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
360 |
(1 + step_img_idx) * img_width, |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
361 |
(aim_img_idx + 1) * img_height |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
362 |
), destination, gc |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
363 |
); |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
364 |
|
261 | 365 |
// draw a proper crosshair-box |
366 |
PixelCoordinate crosshair = getCoordinate() + world.getPixelCoordinate(getDirection() * PLAYER_CROSSHAIR_DISTANCE) - camera; |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
367 |
|
261 | 368 |
gc->draw_rect( |
369 |
CL_Rectf( |
|
370 |
crosshair.x - PLAYER_CROSSHAIR_WIDTH / 2, |
|
371 |
crosshair.y - PLAYER_CROSSHAIR_WIDTH / 2, |
|
372 |
crosshair.x + PLAYER_CROSSHAIR_WIDTH / 2, |
|
373 |
crosshair.y + PLAYER_CROSSHAIR_WIDTH / 2 |
|
374 |
), CL_Color::red |
|
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
375 |
); |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
376 |
} |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
377 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
378 |
void LocalPlayer::draw (Graphics *g, bool displayWeapon, PixelCoordinate camera) { |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
379 |
// superclass draw |
248 | 380 |
Player::draw(g, camera); |
219 | 381 |
|
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
382 |
// display weapon name? |
299 | 383 |
if (isAlive() && displayWeapon && getCurrentWeapon()) { |
236 | 384 |
const std::string weaponName = getCurrentWeapon()->getName(); |
385 |
||
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
255
diff
changeset
|
386 |
PixelCoordinate pc = getCoordinate() - camera; |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
387 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
388 |
// XXX: fix magic constants once we know how big the worm is |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
389 |
g->getSimpleFont().draw( |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
390 |
pc.x - g->getSimpleFont().get_width(weaponName) / 2, |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
391 |
pc.y - 20, |
236 | 392 |
weaponName, |
233
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
393 |
g->get_gc() |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
394 |
); |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
395 |
} |
ff4ecea83cf5
start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents:
231
diff
changeset
|
396 |
} |
235 | 397 |