| author | ekku |
| Sun, 07 Dec 2008 18:21:44 +0000 | |
| changeset 247 | b87f68be579f |
| parent 241 | e95b1602d836 |
| child 248 | e40ef56dc62c |
| 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 |
| 219 | 17 |
const int img_num_aim = 9; |
18 |
const int img_num_step = 4; |
|
19 |
const int img_height = 9; |
|
20 |
const int img_width = 10; |
|
| 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 |
45 |
float shotspeed = 1; //0*PHYSICS_TICK_MS; |
|
|
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
46 |
|
| 235 | 47 |
Vector shotVelocity = getDirection() * shotspeed; |
48 |
||
49 |
new Projectile(state, position, shotVelocity, false, radius, 1); |
|
|
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
50 |
} |
|
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
51 |
|
| 236 | 52 |
void Player::handleCreateProjectile (Weapon *weapon, Vector position, Vector velocity) {
|
53 |
new Projectile(state, position, velocity, true, weapon->getExplosionRadius()); |
|
| 199 | 54 |
} |
| 237 | 55 |
|
56 |
void Player::handleChangeWeapon (unsigned int weaponIndex) {
|
|
57 |
assert(weaponIndex < weapons.size()); |
|
58 |
||
59 |
selectedWeapon = weaponIndex; |
|
60 |
} |
|
| 199 | 61 |
|
| 221 | 62 |
void Player::printDebugInfo (void) {
|
63 |
Engine::log(DEBUG, "layer.debug") << "In air: " << this->inAir; |
|
64 |
} |
|
65 |
||
66 |
void Player::tick (TimeMS dt) {
|
|
67 |
// let PhysicsObject execute |
|
68 |
PhysicsObject::tick(dt); |
|
69 |
||
70 |
// tick current weapon reload |
|
| 236 | 71 |
if (getCurrentWeapon()) |
72 |
getCurrentWeapon()->tickReload(dt); |
|
| 221 | 73 |
} |
| 241 | 74 |
|
75 |
void Player::handleRopeState (RopeState state) {
|
|
76 |
||
77 |
} |
|
78 |
||
79 |
void Player::handleRopeLength (float length) {
|
|
80 |
||
81 |
} |
|
| 199 | 82 |
|
| 236 | 83 |
void LocalPlayer::fireWeapon (Weapon *weapon) {
|
| 223 | 84 |
// update reload timer |
| 236 | 85 |
weapon->reload(); |
| 223 | 86 |
|
| 235 | 87 |
// calculate new position and velocity |
88 |
Vector shotPosition = position + getDirection() * PROJECTILE_START_DISTANCE; |
|
| 236 | 89 |
Vector shotVelocity = velocity + getDirection() * weapon->getSpeed(); |
| 223 | 90 |
|
91 |
// execute |
|
92 |
handleCreateProjectile(weapon, shotPosition, shotVelocity); |
|
93 |
} |
|
| 237 | 94 |
|
95 |
void LocalPlayer::changeWeapon (int delta) {
|
|
96 |
// need to handle negative deltas |
|
97 |
handleChangeWeapon((weapons.size() + selectedWeapon + delta) % weapons.size()); |
|
98 |
} |
|
| 223 | 99 |
|
| 221 | 100 |
void LocalPlayer::handleInput (PlayerInput input) {
|
101 |
// Movement force, vertical is always zero |
|
102 |
Vector move_force = Vector(0, 0); |
|
| 199 | 103 |
|
| 221 | 104 |
// Crosshair angle change |
105 |
float aim_delta = 0; |
|
| 199 | 106 |
|
| 221 | 107 |
// handle movement left/right by applying a horizontal force, but limit the player's speed |
| 235 | 108 |
// also update facing if needed |
| 221 | 109 |
if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED)) {
|
110 |
setFacing(false); |
|
111 |
move_force.x -= PLAYER_MOVE_FORCE; |
|
112 |
||
113 |
} |
|
114 |
||
115 |
if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED)) {
|
|
116 |
setFacing(true); |
|
117 |
move_force.x += PLAYER_MOVE_FORCE; |
|
118 |
} |
|
119 |
||
120 |
// handle aim by creating a aim angle delta |
|
121 |
if (input & INPUT_AIM_UP) |
|
122 |
aim_delta += CROSSHAIR_ANGLE_SPEED; |
|
123 |
||
124 |
if (input & INPUT_AIM_DOWN) |
|
125 |
aim_delta -= CROSSHAIR_ANGLE_SPEED; |
|
126 |
||
127 |
// handle jumping by invoking the jump method |
|
128 |
// XXX: the direction should ideally be given using some other method |
|
129 |
if (input & INPUT_JUMP) {
|
|
130 |
if (input & INPUT_MOVE_LEFT) |
|
| 199 | 131 |
jump(-1); |
| 221 | 132 |
|
133 |
else if (input & INPUT_MOVE_RIGHT) |
|
| 199 | 134 |
jump(1); |
| 221 | 135 |
|
| 199 | 136 |
else |
137 |
jump(0); |
|
138 |
} |
|
| 221 | 139 |
|
140 |
// outsource digging to Player::handleDig, since this modifies the Terrain and Network needs to know |
|
| 235 | 141 |
if (input & INPUT_DIG) |
| 221 | 142 |
handleDig(position, 15); |
143 |
||
| 237 | 144 |
// change weapon back/forth |
145 |
if (input & INPUT_CHANGE_PREV) |
|
146 |
changeWeapon(-1); |
|
147 |
||
148 |
if (input & INPUT_CHANGE_NEXT) |
|
149 |
changeWeapon(+1); |
|
| 213 | 150 |
|
| 221 | 151 |
// validate shoot events, and then outsource to handleShoot so Network can intercept it |
| 236 | 152 |
if ((input & INPUT_SHOOT) && getCurrentWeapon()->canShoot()) |
153 |
fireWeapon(getCurrentWeapon()); |
|
| 221 | 154 |
|
| 241 | 155 |
// rope throw+release+changeLength, but supress spurious events |
| 225 | 156 |
if (input & INPUT_ROPE) |
| 235 | 157 |
rope.throwRope(); |
| 225 | 158 |
|
| 241 | 159 |
if (input & INPUT_UNROPE && rope.getState() != ROPE_FOLDED) |
| 229 | 160 |
rope.release(); |
161 |
||
| 241 | 162 |
if (input & INPUT_ROPE_UP && rope.getState() == ROPE_FIXED) |
| 235 | 163 |
rope.changeLength(-ROPE_GROWTH_RATE); |
| 231 | 164 |
|
| 241 | 165 |
if (input & INPUT_ROPE_DOWN && rope.getState() == ROPE_FIXED) |
| 235 | 166 |
rope.changeLength(ROPE_GROWTH_RATE); |
| 231 | 167 |
|
| 235 | 168 |
// XXX: how should this be written? What does this do? Probably broken under network play |
| 221 | 169 |
if (move_force.x != 0) |
170 |
animation_step = (animation_step + 1) % img_num_step; |
|
| 199 | 171 |
|
| 221 | 172 |
// apply aim delta |
173 |
if (aim_delta) |
|
174 |
changeAim(aim_delta); |
|
| 219 | 175 |
|
| 221 | 176 |
// apply force |
177 |
if (!move_force.zero()) |
|
178 |
applyForce(move_force); |
|
| 199 | 179 |
} |
180 |
||
| 236 | 181 |
Weapon* Player::getCurrentWeapon() {
|
| 235 | 182 |
return weapons[selectedWeapon % weapons.size()]; |
| 212 | 183 |
} |
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
184 |
|
|
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 |
void Player::draw (Graphics *g) {
|
|
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
|
186 |
CL_GraphicContext *gc = 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
|
187 |
|
| 216 | 188 |
int aim_img_idx = (int)((1 - (getAim()+KG_PI/2)/KG_PI)*img_num_aim); |
| 219 | 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 |
|
197 |
// XXX: this logic looks weird |
|
|
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
198 |
CL_Rectf destination(position.x - 4, position.y - 4, position.x + 5, position.y + 4); |
|
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
199 |
|
|
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
200 |
if (!getFacing()) {
|
|
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
201 |
destination = CL_Rect(position.x + 5, position.y - 4, position.x - 4, position.y + 4); |
| 217 | 202 |
} |
203 |
||
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
204 |
skin_surface.draw_subpixel( |
| 221 | 205 |
CL_Rectf( |
206 |
1 + step_img_idx * img_width, |
|
207 |
aim_img_idx * img_height + 1, |
|
208 |
1 + (1 + step_img_idx) * img_width, |
|
209 |
(aim_img_idx + 1) * img_height + 1 |
|
210 |
), |
|
211 |
destination, gc |
|
212 |
); |
|
| 217 | 213 |
|
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
214 |
const uint16_t chlen = 10; |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
215 |
uint16_t x = position.x; |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
216 |
uint16_t y = position.y; |
|
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
217 |
|
|
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
218 |
// draw "crosshair" |
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
219 |
if (facingRight) {
|
| 216 | 220 |
gc->draw_line(x + std::cos(aim)*chlen/2, |
221 |
y - std::sin(aim)*chlen/2, |
|
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
222 |
x + std::cos(aim)*chlen, |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
223 |
y - std::sin(aim)*chlen, |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
224 |
CL_Color::black); |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
225 |
} else {
|
| 216 | 226 |
gc->draw_line(x - std::cos(aim)*chlen/2, |
227 |
y - std::sin(aim)*chlen/2, |
|
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
228 |
x - std::cos(aim)*chlen, |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
229 |
y - std::sin(aim)*chlen, |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
230 |
CL_Color::black); |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
231 |
} |
|
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
|
232 |
|
|
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
|
233 |
rope.draw(g); |
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
234 |
} |
|
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
235 |
|
|
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
|
236 |
void LocalPlayer::draw (Graphics *g, bool displayWeapon) {
|
|
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
|
237 |
// superclass draw |
|
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 |
Player::draw(g); |
| 219 | 239 |
|
|
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
|
240 |
// display weapon name? |
| 236 | 241 |
if (displayWeapon && getCurrentWeapon()) {
|
242 |
const std::string weaponName = getCurrentWeapon()->getName(); |
|
243 |
||
|
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
|
244 |
g->getSimpleFont().draw( |
| 236 | 245 |
position.x - g->getSimpleFont().get_width(weaponName) / 2, |
| 237 | 246 |
position.y - 20, |
| 236 | 247 |
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
|
248 |
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
|
249 |
); |
|
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
|
250 |
} |
|
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
|
251 |
} |
| 235 | 252 |