author | terom |
Sun, 07 Dec 2008 20:25:57 +0000 | |
changeset 257 | 549783d71e51 |
parent 255 | 99431fdb0dc8 |
child 258 | 833ad8d7db8b |
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> |
216 | 9 |
#include <string> |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
10 |
#include <cassert> |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
11 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
12 |
// player static state |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
13 |
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
|
14 |
CL_Surface Player::skin_surface; |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
15 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
16 |
// XXX: give these better names and move elsewhere |
248 | 17 |
const int img_num_aim = 2; |
219 | 18 |
const int img_num_step = 4; |
248 | 19 |
const int img_height = 20; |
20 |
const int img_width = 17; |
|
216 | 21 |
|
198 | 22 |
Player::Player(GameState &state, Vector position, bool visible) : |
236 | 23 |
PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible), |
239
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
237
diff
changeset
|
24 |
weapons(buildWeaponsList()), selectedWeapon(0), animation_step(0), rope(*this) |
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
|
25 |
{ |
236 | 26 |
// XXX: populate weapons from somewhere else |
27 |
||
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
28 |
// 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
|
29 |
// XXX: these dimensions are incorrect... |
198 | 30 |
std::vector<Vector> shape(4); |
31 |
shape[0] = Vector(0,-9); |
|
32 |
shape[1] = Vector(6,0); |
|
33 |
shape[2] = Vector(0,9); |
|
34 |
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
|
35 |
|
198 | 36 |
// Initialize the shape of the player (salmiakki shape) |
37 |
setShape(shape); |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
38 |
|
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
39 |
// XXX: this should be a PhysicsObject constructor arg |
198 | 40 |
collision_elasticity = PLAYER_COLLISION_ELASTICITY; |
41 |
} |
|
236 | 42 |
|
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
43 |
void Player::handleDig (Vector position, float radius) { |
235 | 44 |
// XXX: clean this bit up |
250
84c553d61291
fixed digging, now bugs differently. Changed image_drawing to draw image 1:1
nireco
parents:
248
diff
changeset
|
45 |
// calculate new position and velocity |
84c553d61291
fixed digging, now bugs differently. Changed image_drawing to draw image 1:1
nireco
parents:
248
diff
changeset
|
46 |
Vector shotPosition = position + getDirection() * PROJECTILE_START_DISTANCE; |
84c553d61291
fixed digging, now bugs differently. Changed image_drawing to draw image 1:1
nireco
parents:
248
diff
changeset
|
47 |
// execute |
84c553d61291
fixed digging, now bugs differently. Changed image_drawing to draw image 1:1
nireco
parents:
248
diff
changeset
|
48 |
new Projectile(state, shotPosition, Vector(0, 0), false, radius, 1); |
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
49 |
} |
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
50 |
|
236 | 51 |
void Player::handleCreateProjectile (Weapon *weapon, Vector position, Vector velocity) { |
52 |
new Projectile(state, position, velocity, true, weapon->getExplosionRadius()); |
|
199 | 53 |
} |
237 | 54 |
|
55 |
void Player::handleChangeWeapon (unsigned int weaponIndex) { |
|
56 |
assert(weaponIndex < weapons.size()); |
|
57 |
||
58 |
selectedWeapon = weaponIndex; |
|
59 |
} |
|
199 | 60 |
|
221 | 61 |
void Player::printDebugInfo (void) { |
62 |
Engine::log(DEBUG, "layer.debug") << "In air: " << this->inAir; |
|
63 |
} |
|
64 |
||
65 |
void Player::tick (TimeMS dt) { |
|
66 |
// let PhysicsObject execute |
|
67 |
PhysicsObject::tick(dt); |
|
68 |
||
69 |
// tick current weapon reload |
|
236 | 70 |
if (getCurrentWeapon()) |
71 |
getCurrentWeapon()->tickReload(dt); |
|
221 | 72 |
} |
241 | 73 |
|
74 |
void Player::handleRopeState (RopeState state) { |
|
75 |
||
76 |
} |
|
77 |
||
78 |
void Player::handleRopeLength (float length) { |
|
79 |
||
80 |
} |
|
199 | 81 |
|
236 | 82 |
void LocalPlayer::fireWeapon (Weapon *weapon) { |
223 | 83 |
// update reload timer |
236 | 84 |
weapon->reload(); |
223 | 85 |
|
235 | 86 |
// calculate new position and velocity |
87 |
Vector shotPosition = position + getDirection() * PROJECTILE_START_DISTANCE; |
|
236 | 88 |
Vector shotVelocity = velocity + getDirection() * weapon->getSpeed(); |
223 | 89 |
|
90 |
// execute |
|
91 |
handleCreateProjectile(weapon, shotPosition, shotVelocity); |
|
92 |
} |
|
237 | 93 |
|
94 |
void LocalPlayer::changeWeapon (int delta) { |
|
95 |
// need to handle negative deltas |
|
96 |
handleChangeWeapon((weapons.size() + selectedWeapon + delta) % weapons.size()); |
|
97 |
} |
|
223 | 98 |
|
221 | 99 |
void LocalPlayer::handleInput (PlayerInput input) { |
100 |
// Movement force, vertical is always zero |
|
101 |
Vector move_force = Vector(0, 0); |
|
199 | 102 |
|
221 | 103 |
// Crosshair angle change |
104 |
float aim_delta = 0; |
|
199 | 105 |
|
221 | 106 |
// handle movement left/right by applying a horizontal force, but limit the player's speed |
235 | 107 |
// also update facing if needed |
221 | 108 |
if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED)) { |
109 |
setFacing(false); |
|
110 |
move_force.x -= PLAYER_MOVE_FORCE; |
|
111 |
||
112 |
} |
|
113 |
||
114 |
if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED)) { |
|
115 |
setFacing(true); |
|
116 |
move_force.x += PLAYER_MOVE_FORCE; |
|
117 |
} |
|
118 |
||
119 |
// handle aim by creating a aim angle delta |
|
120 |
if (input & INPUT_AIM_UP) |
|
121 |
aim_delta += CROSSHAIR_ANGLE_SPEED; |
|
122 |
||
123 |
if (input & INPUT_AIM_DOWN) |
|
124 |
aim_delta -= CROSSHAIR_ANGLE_SPEED; |
|
125 |
||
126 |
// handle jumping by invoking the jump method |
|
127 |
// XXX: the direction should ideally be given using some other method |
|
128 |
if (input & INPUT_JUMP) { |
|
129 |
if (input & INPUT_MOVE_LEFT) |
|
199 | 130 |
jump(-1); |
221 | 131 |
|
132 |
else if (input & INPUT_MOVE_RIGHT) |
|
199 | 133 |
jump(1); |
221 | 134 |
|
199 | 135 |
else |
136 |
jump(0); |
|
137 |
} |
|
221 | 138 |
|
139 |
// outsource digging to Player::handleDig, since this modifies the Terrain and Network needs to know |
|
235 | 140 |
if (input & INPUT_DIG) |
221 | 141 |
handleDig(position, 15); |
142 |
||
237 | 143 |
// change weapon back/forth |
144 |
if (input & INPUT_CHANGE_PREV) |
|
145 |
changeWeapon(-1); |
|
146 |
||
147 |
if (input & INPUT_CHANGE_NEXT) |
|
148 |
changeWeapon(+1); |
|
213 | 149 |
|
221 | 150 |
// validate shoot events, and then outsource to handleShoot so Network can intercept it |
236 | 151 |
if ((input & INPUT_SHOOT) && getCurrentWeapon()->canShoot()) |
152 |
fireWeapon(getCurrentWeapon()); |
|
221 | 153 |
|
241 | 154 |
// rope throw+release+changeLength, but supress spurious events |
225 | 155 |
if (input & INPUT_ROPE) |
235 | 156 |
rope.throwRope(); |
225 | 157 |
|
241 | 158 |
if (input & INPUT_UNROPE && rope.getState() != ROPE_FOLDED) |
229 | 159 |
rope.release(); |
160 |
||
241 | 161 |
if (input & INPUT_ROPE_UP && rope.getState() == ROPE_FIXED) |
235 | 162 |
rope.changeLength(-ROPE_GROWTH_RATE); |
231 | 163 |
|
241 | 164 |
if (input & INPUT_ROPE_DOWN && rope.getState() == ROPE_FIXED) |
235 | 165 |
rope.changeLength(ROPE_GROWTH_RATE); |
231 | 166 |
|
235 | 167 |
// XXX: how should this be written? What does this do? Probably broken under network play |
221 | 168 |
if (move_force.x != 0) |
169 |
animation_step = (animation_step + 1) % img_num_step; |
|
199 | 170 |
|
221 | 171 |
// apply aim delta |
172 |
if (aim_delta) |
|
173 |
changeAim(aim_delta); |
|
219 | 174 |
|
221 | 175 |
// apply force |
176 |
if (!move_force.zero()) |
|
177 |
applyForce(move_force); |
|
199 | 178 |
} |
179 |
||
236 | 180 |
Weapon* Player::getCurrentWeapon() { |
235 | 181 |
return weapons[selectedWeapon % weapons.size()]; |
212 | 182 |
} |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
183 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
184 |
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
|
185 |
CL_GraphicContext *gc = g->get_gc(); |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
186 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
187 |
// animation indexes |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
188 |
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
|
189 |
int step_img_idx = animation_step % img_num_step; |
217 | 190 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
191 |
// 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
|
192 |
if (!skin_loaded) { |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
193 |
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
|
194 |
skin_loaded = true; |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
195 |
} |
221 | 196 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
197 |
// 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
|
198 |
CL_Rectf destination( |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
199 |
position.x + (getFacing() ? -1 : 1) * img_width / 2 - camera.x, |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
200 |
position.y - img_height / 2 - camera.y, |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
201 |
position.x + (getFacing() ? 1 : -1) * img_width / 2 - camera.x, |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
202 |
position.y + img_height / 2 - camera.y |
221 | 203 |
); |
217 | 204 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
205 |
// draw the correct animation frame from the skin |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
206 |
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
|
207 |
CL_Rectf( |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
208 |
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
|
209 |
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
|
210 |
(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
|
211 |
(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
|
212 |
), destination, gc |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
213 |
); |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
214 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
215 |
// draw "crosshair", a half-line from our position to 10px away |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
216 |
Vector crosshair = getDirection() * PLAYER_CROSSHAIR_LENGTH; |
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
|
217 |
|
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
|
218 |
PixelCoordinate aim_start = getCoordinate() + world.getPixelCoordinate(crosshair / 2) - camera; |
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
|
219 |
PixelCoordinate aim_end = getCoordinate() + world.getPixelCoordinate(crosshair) - camera; |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
220 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
221 |
gc->draw_line(aim_start.x, aim_start.y, aim_end.x, aim_end.y, CL_Color::black); |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
222 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
223 |
// draw rope |
248 | 224 |
rope.draw(g, camera); |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
225 |
} |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
226 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
227 |
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
|
228 |
// superclass draw |
248 | 229 |
Player::draw(g, camera); |
219 | 230 |
|
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
|
231 |
// display weapon name? |
236 | 232 |
if (displayWeapon && getCurrentWeapon()) { |
233 |
const std::string weaponName = getCurrentWeapon()->getName(); |
|
234 |
||
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
|
235 |
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
|
236 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
250
diff
changeset
|
237 |
// 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
|
238 |
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
|
239 |
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
|
240 |
pc.y - 20, |
236 | 241 |
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
|
242 |
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
|
243 |
); |
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
|
244 |
} |
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
|
245 |
} |
235 | 246 |