author | Tero Marttila <terom@fixme.fi> |
Tue, 13 Jan 2009 23:54:47 +0200 | |
changeset 394 | 82def222fe7d |
parent 377 | 01d3c340b372 |
child 408 | e6cfc44266af |
permissions | -rw-r--r-- |
225 | 1 |
#include "Player.hh" |
2 |
#include "Rope.hh" |
|
3 |
#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
|
4 |
#include "Graphics.hh" |
225 | 5 |
#include <math.h> |
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
6 |
#include <stdexcept> |
225 | 7 |
|
235 | 8 |
Rope::Rope(Player &player) : |
311
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
305
diff
changeset
|
9 |
PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), ROPE, 0.00, false), |
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
305
diff
changeset
|
10 |
player(player), |
440763821484
make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents:
305
diff
changeset
|
11 |
state(ROPE_FOLDED) |
235 | 12 |
{ |
13 |
// XXX: better shape |
|
225 | 14 |
std::vector<Vector> shape(4); |
15 |
shape[0] = Vector(-1, -1); |
|
16 |
shape[1] = Vector(-1, 1); |
|
17 |
shape[2] = Vector(1, 1); |
|
18 |
shape[3] = Vector(1, -1); |
|
19 |
setShape(shape); |
|
20 |
} |
|
21 |
||
235 | 22 |
void Rope::throwRope (void) { |
23 |
state = ROPE_FLYING; |
|
225 | 24 |
|
235 | 25 |
// XXX: this should probably be more dynamic? |
26 |
length = ROPE_LENGTH; |
|
27 |
||
28 |
// copy position + velocity from player |
|
285 | 29 |
setPosition (player.getPosition()); |
235 | 30 |
velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY; |
31 |
||
32 |
// we are FLYING |
|
33 |
inAir = true; |
|
34 |
||
35 |
// enable the physics object |
|
225 | 36 |
enable(); |
241 | 37 |
|
38 |
// inform network |
|
39 |
player.handleRopeState(state); |
|
225 | 40 |
} |
41 |
||
273 | 42 |
void Rope::onCollision (Vector collisionPoint, PhysicsObject *other) { |
322 | 43 |
// Fix the rope to another player if collided with it |
305 | 44 |
if (other != NULL) { |
328 | 45 |
// we collided with another object |
305 | 46 |
if (other->getType() == PLAYER) { |
328 | 47 |
// set our player's pivot to the object that we collided with |
305 | 48 |
Player *target = dynamic_cast<Player*>(other); |
328 | 49 |
|
50 |
// ignore if the rope hits ourself |
|
51 |
if (target == &this->player) |
|
305 | 52 |
return; |
328 | 53 |
|
54 |
// set player's pivot to the other player |
|
55 |
player.setPivot(target); |
|
56 |
||
57 |
// disable ourselves as we're no longer relevant, don't keep colliding with the player |
|
58 |
disable(); |
|
59 |
||
60 |
} else { |
|
61 |
// ignore other objects |
|
305 | 62 |
return; |
63 |
} |
|
328 | 64 |
|
65 |
} else { |
|
66 |
// Collided with terrain, set player's pivot to ourselves |
|
322 | 67 |
player.setPivot(this); |
305 | 68 |
} |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
273
diff
changeset
|
69 |
|
235 | 70 |
// attached to something! |
71 |
state = ROPE_FIXED; |
|
305 | 72 |
velocity = Vector(0,0); |
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
73 |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
74 |
// Ropes location will be used as the pivot point, so move the location to the collisionPoint. |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
75 |
// Currently the position is something like one pixel away from the collisionPoint where there isn't ground. |
328 | 76 |
setPosition(collisionPoint); |
241 | 77 |
|
78 |
// inform network |
|
79 |
player.handleRopeState(state); |
|
225 | 80 |
} |
81 |
||
82 |
void Rope::release (void) { |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
83 |
// Remove the rope from the PhysicsWorld |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
84 |
disable(); |
229 | 85 |
|
235 | 86 |
state = ROPE_FOLDED; |
228 | 87 |
|
88 |
// player doesn't have a pivot anymore |
|
235 | 89 |
player.setPivot(NULL); |
241 | 90 |
|
91 |
// inform network |
|
92 |
player.handleRopeState(state); |
|
225 | 93 |
} |
94 |
||
235 | 95 |
void Rope::changeLength (float delta) { |
241 | 96 |
// change length |
235 | 97 |
length += delta; |
241 | 98 |
|
99 |
// minimum length |
|
235 | 100 |
if (length < 0) |
101 |
length = 0; |
|
241 | 102 |
|
103 |
// inform network |
|
104 |
player.handleRopeLength(length); |
|
231 | 105 |
} |
106 |
||
225 | 107 |
RopeState Rope::getState (void) { |
235 | 108 |
return state; |
225 | 109 |
} |
241 | 110 |
|
111 |
float Rope::getLength (void) { |
|
112 |
return length; |
|
113 |
} |
|
114 |
||
328 | 115 |
Player *Rope::getPivotPlayer (void) { |
116 |
if (player.getPivot() == this) |
|
117 |
return NULL; |
|
118 |
else |
|
119 |
return dynamic_cast<Player*>(player.getPivot()); |
|
120 |
} |
|
121 |
||
122 |
void Rope::updateState (RopeState new_state, Vector position, Vector velocity, float new_length, Player *pivot_player) { |
|
241 | 123 |
// update physics enabled/disabled state |
124 |
if (new_state == ROPE_FOLDED || new_state == ROPE_FIXED) |
|
125 |
disable(); |
|
126 |
||
127 |
else // new_state == ROPE_FLYING |
|
128 |
enable(); |
|
129 |
||
328 | 130 |
// update player.pivot to either the given pivot_player, or this rope |
131 |
if (new_state == ROPE_FIXED) { |
|
132 |
if (pivot_player) |
|
133 |
player.setPivot(pivot_player); |
|
134 |
else |
|
135 |
player.setPivot(this); |
|
241 | 136 |
|
328 | 137 |
} else if (this->state == ROPE_FIXED) |
241 | 138 |
player.setPivot(NULL); |
139 |
||
140 |
// update position stuff |
|
264 | 141 |
updatePhysics(position, velocity, true, FACING_RIGHT, 0); |
241 | 142 |
|
143 |
// update vars |
|
144 |
this->state = new_state; |
|
145 |
this->length = new_length; |
|
146 |
} |
|
147 |
||
148 |
void Rope::updateLength (float length) { |
|
149 |
// update length |
|
150 |
this->length = length; |
|
151 |
} |
|
225 | 152 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
254
diff
changeset
|
153 |
void Rope::draw (Graphics *g, PixelCoordinate camera) { |
328 | 154 |
PixelCoordinate player_pos = player.getCoordinate() - camera; |
155 |
PixelCoordinate target_pos; |
|
377
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
328
diff
changeset
|
156 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
328
diff
changeset
|
157 |
// figure out what target is |
328 | 158 |
if (state == ROPE_FOLDED) { |
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
|
159 |
return; |
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
|
160 |
|
328 | 161 |
} else if (state == ROPE_FLYING) { |
162 |
// target is us |
|
163 |
target_pos = getCoordinate(); |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
254
diff
changeset
|
164 |
|
328 | 165 |
} else { // state == ROPE_FIXED |
166 |
// sanity-check |
|
167 |
if (player.getPivot() == NULL) |
|
168 |
throw Error("Rope::draw in state ROPE_FIXED, yet player.getPivot() is NULL"); |
|
169 |
||
170 |
// target is our pivot |
|
171 |
target_pos = player.getPivot()->getCoordinate(); |
|
172 |
} |
|
377
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
328
diff
changeset
|
173 |
|
328 | 174 |
// align with camera |
175 |
target_pos -= camera; |
|
176 |
||
177 |
// draw a line from the player to the target chosen above |
|
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
|
178 |
g->get_gc()->draw_line( |
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:
256
diff
changeset
|
179 |
player_pos.x, player_pos.y, |
328 | 180 |
target_pos.x, target_pos.y, |
377
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
328
diff
changeset
|
181 |
ROPE_COLOR_DARK |
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
|
182 |
); |
225 | 183 |
} |
184 |
||
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
185 |
void Rope::tick (TimeMS dt) { |
328 | 186 |
if (state == ROPE_FLYING) { |
187 |
// let PhysicsObject handle the flying stage |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
188 |
PhysicsObject::tick(dt); |
322 | 189 |
|
328 | 190 |
} else if (state == ROPE_FIXED) { |
191 |
// if player's pivot is some other player, then don't re-check the terrain |
|
322 | 192 |
if (player.getPivot() != this) |
193 |
return; |
|
194 |
||
328 | 195 |
// If there's no ground on the pivot point anymore, release the rope |
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
196 |
if (!world.collides(position)) { |
328 | 197 |
// XXX: move to some new method |
322 | 198 |
state = ROPE_FLYING; |
199 |
length = ROPE_LENGTH; |
|
200 |
inAir = true; |
|
201 |
player.setPivot(NULL); |
|
263
8c999cf4c182
weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents:
257
diff
changeset
|
202 |
player.handleRopeState(state); |
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
203 |
} |
328 | 204 |
} else { // state == ROPE_FOLDED |
205 |
// ignore ticks when folded |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
206 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
207 |
} |
328 | 208 |