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