author | ekku |
Sat, 06 Dec 2008 21:39:52 +0000 | |
changeset 231 | 5085764e1dbb |
parent 229 | 355e46effa41 |
child 233 | ff4ecea83cf5 |
permissions | -rw-r--r-- |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
1 |
|
199 | 2 |
#include "Engine.hh" |
198 | 3 |
#include "Player.hh" |
4 |
||
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
5 |
#include <cstdlib> |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
6 |
#include <ClanLib/display.h> |
216 | 7 |
#include <string> |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
8 |
#include <cassert> |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
9 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
10 |
// player static state |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
11 |
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
|
12 |
CL_Surface Player::skin_surface; |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
13 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
14 |
// XXX: give these better names and move elsewhere |
219 | 15 |
const int img_num_aim = 9; |
16 |
const int img_num_step = 4; |
|
17 |
const int img_height = 9; |
|
18 |
const int img_width = 10; |
|
216 | 19 |
|
198 | 20 |
Player::Player(GameState &state, Vector position, bool visible) : |
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
|
21 |
PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible), arsenal(), |
225 | 22 |
selectedWeapon(0), changing(false), 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
|
23 |
{ |
212 | 24 |
// TODO: arsenal's size should be affected by some value |
25 |
// and weapons should be loaded from somewhere, not generated here |
|
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
26 |
for (int i = 0; i < 5; i++) { |
221 | 27 |
arsenal.push_back(Weapon(state, 10000, (5 - i) * 40 + 30, i * 6 + 5, i * 100 + 50, "asdf")); |
212 | 28 |
} |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
29 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
30 |
// 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
|
31 |
// XXX: these dimensions are incorrect... |
198 | 32 |
std::vector<Vector> shape(4); |
33 |
shape[0] = Vector(0,-9); |
|
34 |
shape[1] = Vector(6,0); |
|
35 |
shape[2] = Vector(0,9); |
|
36 |
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
|
37 |
|
198 | 38 |
// Initialize the shape of the player (salmiakki shape) |
39 |
setShape(shape); |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
40 |
|
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
41 |
// XXX: this should be a PhysicsObject constructor arg |
198 | 42 |
collision_elasticity = PLAYER_COLLISION_ELASTICITY; |
43 |
} |
|
44 |
||
209
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
45 |
void Player::handleDig (Vector position, float radius) { |
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
46 |
Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : |
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
47 |
Vector(-std::cos(aim), -std::sin(aim)); |
231 | 48 |
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
|
49 |
Vector shotVelocity = unitVectorAim*shotspeed; |
216 | 50 |
new Projectile(this->state, this->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
|
51 |
|
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
52 |
} |
68cc4248a508
sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents:
208
diff
changeset
|
53 |
|
223 | 54 |
void Player::handleCreateProjectile (Weapon &weapon, Vector position, Vector velocity) { |
55 |
new Projectile(state, position, velocity, true, weapon.getExplosionRadius()); |
|
199 | 56 |
} |
57 |
||
221 | 58 |
void Player::printDebugInfo (void) { |
59 |
Engine::log(DEBUG, "layer.debug") << "In air: " << this->inAir; |
|
60 |
} |
|
61 |
||
62 |
void Player::tick (TimeMS dt) { |
|
63 |
// let PhysicsObject execute |
|
64 |
PhysicsObject::tick(dt); |
|
65 |
||
66 |
// tick current weapon reload |
|
67 |
getWeapon().tickReload(dt); |
|
68 |
} |
|
199 | 69 |
|
223 | 70 |
void LocalPlayer::fireWeapon (Weapon &weapon) { |
71 |
// update reload timer |
|
72 |
weapon.reload(); |
|
73 |
||
74 |
// calculate unit vector |
|
75 |
Vector unitVectorAim = (facingRight ? |
|
76 |
Vector(std::cos(aim), -std::sin(aim)) : |
|
77 |
Vector(-std::cos(aim), -std::sin(aim)) |
|
78 |
); |
|
79 |
||
80 |
// XXX: what does the PHYSICS_TICK_MS stuff mean? |
|
81 |
float shotspeed = weapon.getVelocity() * PHYSICS_TICK_MS / 2; |
|
82 |
||
83 |
Vector shotRelativeVelocity = unitVectorAim * shotspeed; |
|
84 |
Vector shotVelocity = this->velocity + shotRelativeVelocity; |
|
85 |
Vector shotPosition = this->position + unitVectorAim * 10; |
|
86 |
||
87 |
// execute |
|
88 |
handleCreateProjectile(weapon, shotPosition, shotVelocity); |
|
89 |
} |
|
90 |
||
221 | 91 |
void LocalPlayer::handleInput (PlayerInput input) { |
92 |
// Movement force, vertical is always zero |
|
93 |
Vector move_force = Vector(0, 0); |
|
199 | 94 |
|
221 | 95 |
// Crosshair angle change |
96 |
float aim_delta = 0; |
|
199 | 97 |
|
221 | 98 |
// handle movement left/right by applying a horizontal force, but limit the player's speed |
99 |
if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED)) { |
|
100 |
setFacing(false); |
|
101 |
move_force.x -= PLAYER_MOVE_FORCE; |
|
102 |
||
103 |
} |
|
104 |
||
105 |
if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED)) { |
|
106 |
setFacing(true); |
|
107 |
move_force.x += PLAYER_MOVE_FORCE; |
|
108 |
} |
|
109 |
||
110 |
// handle aim by creating a aim angle delta |
|
111 |
if (input & INPUT_AIM_UP) |
|
112 |
aim_delta += CROSSHAIR_ANGLE_SPEED; |
|
113 |
||
114 |
if (input & INPUT_AIM_DOWN) |
|
115 |
aim_delta -= CROSSHAIR_ANGLE_SPEED; |
|
116 |
||
117 |
// handle jumping by invoking the jump method |
|
118 |
// XXX: the direction should ideally be given using some other method |
|
119 |
if (input & INPUT_JUMP) { |
|
120 |
if (input & INPUT_MOVE_LEFT) |
|
199 | 121 |
jump(-1); |
221 | 122 |
|
123 |
else if (input & INPUT_MOVE_RIGHT) |
|
199 | 124 |
jump(1); |
221 | 125 |
|
199 | 126 |
else |
127 |
jump(0); |
|
128 |
} |
|
221 | 129 |
|
130 |
// outsource digging to Player::handleDig, since this modifies the Terrain and Network needs to know |
|
131 |
if (input & INPUT_DIG) { |
|
132 |
handleDig(position, 15); |
|
199 | 133 |
|
201 | 134 |
// Should create Projectile which destroys ground, but also should be destroyed then, |
199 | 135 |
// but it doesn't. |
136 |
// But this now just segfaults |
|
201 | 137 |
// world.addObject(new Projectile(state, position, true)); |
199 | 138 |
} |
221 | 139 |
|
140 |
// XXX: currently not network safe |
|
212 | 141 |
if (input & INPUT_CHANGE) { |
221 | 142 |
if (changing) { |
213 | 143 |
|
144 |
} else { |
|
145 |
changing = true; |
|
221 | 146 |
selectedWeapon = (selectedWeapon + 1) % arsenal.size(); |
213 | 147 |
Engine::log(DEBUG, "Player.cc:input ") << "changed weapon " << selectedWeapon; |
148 |
} |
|
149 |
} else { |
|
150 |
changing = false; |
|
212 | 151 |
} |
221 | 152 |
|
153 |
// validate shoot events, and then outsource to handleShoot so Network can intercept it |
|
223 | 154 |
if (input & INPUT_SHOOT && getWeapon().canShoot()) |
155 |
fireWeapon(getWeapon()); |
|
221 | 156 |
|
225 | 157 |
if (input & INPUT_ROPE) |
158 |
rope.shoot(); |
|
159 |
||
229 | 160 |
if (input & INPUT_UNROPE) |
161 |
rope.release(); |
|
162 |
||
231 | 163 |
if (input & INPUT_ROPE_UP) |
164 |
rope.changeLength(true); |
|
165 |
||
166 |
if (input & INPUT_ROPE_DOWN) |
|
167 |
rope.changeLength(false); |
|
168 |
||
221 | 169 |
// XXX: how should this be written? |
170 |
if (move_force.x != 0) |
|
171 |
animation_step = (animation_step + 1) % img_num_step; |
|
199 | 172 |
|
221 | 173 |
// apply aim delta |
174 |
if (aim_delta) |
|
175 |
changeAim(aim_delta); |
|
219 | 176 |
|
221 | 177 |
// apply force |
178 |
if (!move_force.zero()) |
|
179 |
applyForce(move_force); |
|
199 | 180 |
} |
181 |
||
212 | 182 |
Weapon& Player::getWeapon() { |
221 | 183 |
return arsenal[selectedWeapon % arsenal.size()]; |
212 | 184 |
} |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
185 |
|
221 | 186 |
void Player::draw (CL_GraphicContext *gc) { |
216 | 187 |
int aim_img_idx = (int)((1 - (getAim()+KG_PI/2)/KG_PI)*img_num_aim); |
219 | 188 |
int step_img_idx = animation_step%img_num_step; |
217 | 189 |
|
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
190 |
// 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
|
191 |
if (!skin_loaded) { |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
192 |
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
|
193 |
skin_loaded = true; |
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
194 |
} |
221 | 195 |
|
196 |
// XXX: this logic looks weird |
|
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
197 |
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
|
198 |
|
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
199 |
if (!getFacing()) { |
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
200 |
destination = CL_Rect(position.x + 5, position.y - 4, position.x - 4, position.y + 4); |
217 | 201 |
} |
202 |
||
220
1c92222af6d3
use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents:
219
diff
changeset
|
203 |
skin_surface.draw_subpixel( |
221 | 204 |
CL_Rectf( |
205 |
1 + step_img_idx * img_width, |
|
206 |
aim_img_idx * img_height + 1, |
|
207 |
1 + (1 + step_img_idx) * img_width, |
|
208 |
(aim_img_idx + 1) * img_height + 1 |
|
209 |
), |
|
210 |
destination, gc |
|
211 |
); |
|
217 | 212 |
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
213 |
const uint16_t chlen = 10; |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
214 |
uint16_t x = position.x; |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
215 |
uint16_t y = position.y; |
218
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
216 |
|
86d22fe82b30
add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents:
217
diff
changeset
|
217 |
// draw "crosshair" |
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
218 |
if (facingRight) { |
216 | 219 |
gc->draw_line(x + std::cos(aim)*chlen/2, |
220 |
y - std::sin(aim)*chlen/2, |
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
221 |
x + std::cos(aim)*chlen, |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
222 |
y - std::sin(aim)*chlen, |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
223 |
CL_Color::black); |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
224 |
} else { |
216 | 225 |
gc->draw_line(x - std::cos(aim)*chlen/2, |
226 |
y - std::sin(aim)*chlen/2, |
|
215
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
227 |
x - std::cos(aim)*chlen, |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
228 |
y - std::sin(aim)*chlen, |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
229 |
CL_Color::black); |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
230 |
} |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
231 |
} |
69de8d9fcc0a
image is working.. well.. run it from build/src folder.
nireco
parents:
213
diff
changeset
|
232 |
|
219 | 233 |