author | nireco |
Sat, 31 Jan 2009 12:33:08 +0200 | |
changeset 443 | 5d1119729f58 |
parent 428 | 712b943195a6 |
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 |
|
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
6 |
#include <cstdlib> |
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
7 |
#include <algorithm> |
216 | 8 |
#include <string> |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
9 |
#include <cassert> |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
10 |
|
417
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
diff
changeset
|
11 |
#if GRAPHICS_ENABLED |
423
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
417
diff
changeset
|
12 |
|
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
417
diff
changeset
|
13 |
#include <ClanLib/display.h> |
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
417
diff
changeset
|
14 |
#include "Graphics/Graphics.hh" |
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
417
diff
changeset
|
15 |
|
417
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
diff
changeset
|
16 |
/* |
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
diff
changeset
|
17 |
* Static draw-related state |
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
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 |
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
|
20 |
CL_Surface Player::skin_surface; |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
21 |
|
417
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
diff
changeset
|
22 |
#endif |
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
diff
changeset
|
23 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
24 |
// XXX: give these better names and move elsewhere |
342 | 25 |
const int img_num_aim = 5; |
219 | 26 |
const int img_num_step = 4; |
248 | 27 |
const int img_height = 20; |
28 |
const int img_width = 17; |
|
216 | 29 |
|
198 | 30 |
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
|
31 |
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
|
32 |
state(state), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
33 |
visible(visible), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
34 |
weapons(buildWeaponsList()), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
35 |
selectedWeapon(0), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
36 |
rope(*this), |
300 | 37 |
health(PLAYER_HEALTH), |
38 |
respawn_timer(PLAYER_RESPAWN_DELAY), |
|
307
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
39 |
animation_step(0), |
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
40 |
kills(0), |
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
41 |
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
|
42 |
{ |
236 | 43 |
// XXX: populate weapons from somewhere else |
44 |
||
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
45 |
// 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
|
46 |
// XXX: these dimensions are incorrect... |
198 | 47 |
std::vector<Vector> shape(4); |
48 |
shape[0] = Vector(0,-9); |
|
49 |
shape[1] = Vector(6,0); |
|
50 |
shape[2] = Vector(0,9); |
|
51 |
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
|
52 |
|
198 | 53 |
// Initialize the shape of the player (salmiakki shape) |
54 |
setShape(shape); |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
55 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
56 |
// 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
|
57 |
state.addPlayer(this); |
300 | 58 |
|
59 |
// connect respawn timer |
|
60 |
respawn_slot = respawn_timer.sig_tick().connect(this, &Player::respawn); |
|
61 |
||
62 |
// spawn |
|
63 |
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
|
64 |
} |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
65 |
|
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
272
diff
changeset
|
66 |
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
|
67 |
state.removePlayer(this); |
198 | 68 |
} |
300 | 69 |
|
70 |
void Player::spawn (Vector position) { |
|
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
71 |
// dig hole to make room |
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
|
72 |
world.terrain.removeGround(position, PLAYER_DIG_RADIUS); |
300 | 73 |
|
330 | 74 |
// reset health |
75 |
health = PLAYER_HEALTH; |
|
76 |
||
77 |
// XXX: reload weapons |
|
78 |
||
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
79 |
// resume at our new position |
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
80 |
resume(position); |
300 | 81 |
} |
82 |
||
302 | 83 |
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
|
84 |
|
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
85 |
deaths++; // Increment death counter |
5b4806c61342
Deaths should be counted to Player::deaths parameter. Kills not counted yet.
saiam
parents:
302
diff
changeset
|
86 |
|
302 | 87 |
// we don't have a rope anymore |
88 |
rope.release(); |
|
89 |
||
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
90 |
// reset/disable our PhysicsObject |
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
91 |
reset(); |
302 | 92 |
|
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; |
|
409
1a03ff151abc
add --terrain-seed and --terrain-size arguments, plus bugfixes
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
101 |
|
1a03ff151abc
add --terrain-seed and --terrain-size arguments, plus bugfixes
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
102 |
// 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
|
103 |
spawn(world.dimensions / 2); |
300 | 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 |
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
|
111 |
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
|
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 |
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
160 |
Vector shotPosition = getPosition() + getDirection() * PROJECTILE_START_DISTANCE; |
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
161 |
Vector shotVelocity = getVelocity() + 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))) { |
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
186 |
// XXX: this should be physics... |
262 | 187 |
if (input & INPUT_MOVE_LEFT) { |
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
188 |
if (getVelocity().x > -PLAYER_MAX_SPEED) |
262 | 189 |
move_force.x -= PLAYER_MOVE_FORCE; |
190 |
||
264 | 191 |
setFacing(FACING_LEFT); |
262 | 192 |
} |
221 | 193 |
|
262 | 194 |
if (input & INPUT_MOVE_RIGHT) { |
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
195 |
if (getVelocity().x < PLAYER_MAX_SPEED) |
262 | 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) |
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
224 |
handleDig(getPosition(), 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 |
||
417
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
diff
changeset
|
328 |
#if GRAPHICS_ENABLED |
412
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
329 |
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
|
330 |
CL_GraphicContext *gc = display.get_gc(); |
299 | 331 |
|
332 |
if (!isAlive()) |
|
333 |
return; |
|
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
334 |
|
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
335 |
// 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
|
336 |
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
|
337 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
338 |
// animation indexes |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
339 |
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
|
340 |
int step_img_idx = animation_step % img_num_step; |
217 | 341 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
342 |
// 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
|
343 |
if (!skin_loaded) { |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
344 |
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
|
345 |
skin_loaded = true; |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
346 |
} |
428
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
347 |
|
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
348 |
// screen position |
712b943195a6
clean up PhysicsObject.hh a bit, make some things private
Tero Marttila <terom@fixme.fi>
parents:
423
diff
changeset
|
349 |
PixelCoordinate position = getCoordinate(); |
221 | 350 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
351 |
// 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
|
352 |
CL_Rectf destination( |
264 | 353 |
position.x + (facing == FACING_RIGHT ? -1 : 1) * img_width / 2 - camera.x, |
354 |
position.y - img_height / 2 - camera.y, |
|
355 |
position.x + (facing == FACING_RIGHT ? 1 : -1) * img_width / 2 - camera.x, |
|
356 |
position.y + img_height / 2 - camera.y |
|
221 | 357 |
); |
217 | 358 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
359 |
// draw the correct animation frame from the skin |
342 | 360 |
// 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
|
361 |
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
|
362 |
CL_Rectf( |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
363 |
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
|
364 |
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
|
365 |
(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
|
366 |
(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
|
367 |
), destination, gc |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
368 |
); |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
369 |
|
261 | 370 |
// 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
|
371 |
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
|
372 |
|
261 | 373 |
gc->draw_rect( |
374 |
CL_Rectf( |
|
375 |
crosshair.x - PLAYER_CROSSHAIR_WIDTH / 2, |
|
376 |
crosshair.y - PLAYER_CROSSHAIR_WIDTH / 2, |
|
377 |
crosshair.x + PLAYER_CROSSHAIR_WIDTH / 2, |
|
378 |
crosshair.y + PLAYER_CROSSHAIR_WIDTH / 2 |
|
379 |
), CL_Color::red |
|
313
2562466a946d
draw rope behind player, fix LocalPlayer::draw warnings, don't show negative health
terom
parents:
312
diff
changeset
|
380 |
); |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
381 |
} |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
382 |
|
412
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
383 |
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
|
384 |
// superclass draw |
412
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
385 |
Player::draw(display, camera); |
219 | 386 |
|
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
|
387 |
// display weapon name? |
299 | 388 |
if (isAlive() && displayWeapon && getCurrentWeapon()) { |
236 | 389 |
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
|
390 |
|
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
391 |
// position |
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
392 |
PixelCoordinate pc = getCoordinate() - camera; |
236 | 393 |
|
412
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
394 |
// get font |
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
395 |
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
|
396 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
397 |
// 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
|
398 |
font.draw( |
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
399 |
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
|
400 |
pc.y - 20, |
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
401 |
weaponName, |
721c60072091
new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents:
409
diff
changeset
|
402 |
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
|
403 |
); |
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
|
404 |
} |
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
|
405 |
} |
417
c503e0c6a740
support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents:
412
diff
changeset
|
406 |
#endif |
235 | 407 |