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