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