author | nireco |
Mon, 08 Dec 2008 22:57:01 +0000 | |
changeset 325 | a19c78786d7f |
parent 322 | f94a5c192097 |
child 328 | 51d644c8d5a2 |
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) { |
45 |
if (other->getType() == PLAYER) { |
|
46 |
Player *target = dynamic_cast<Player*>(other); |
|
47 |
if (target == &(this->player)) |
|
48 |
return; |
|
322 | 49 |
this->player.pivot = target; |
305 | 50 |
} else if (other->getType() == PROJECTILE) { |
51 |
return; |
|
52 |
} |
|
322 | 53 |
} else { // Collided with terrain |
54 |
player.setPivot(this); |
|
305 | 55 |
} |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
273
diff
changeset
|
56 |
|
235 | 57 |
// attached to something! |
58 |
state = ROPE_FIXED; |
|
305 | 59 |
velocity = Vector(0,0); |
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
60 |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
61 |
// 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
|
62 |
// Currently the position is something like one pixel away from the collisionPoint where there isn't ground. |
285 | 63 |
setPosition (collisionPoint); |
241 | 64 |
|
65 |
// inform network |
|
66 |
player.handleRopeState(state); |
|
225 | 67 |
} |
68 |
||
69 |
void Rope::release (void) { |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
70 |
// Remove the rope from the PhysicsWorld |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
71 |
disable(); |
229 | 72 |
|
235 | 73 |
state = ROPE_FOLDED; |
228 | 74 |
|
75 |
// player doesn't have a pivot anymore |
|
235 | 76 |
player.setPivot(NULL); |
241 | 77 |
|
78 |
// inform network |
|
79 |
player.handleRopeState(state); |
|
225 | 80 |
} |
81 |
||
235 | 82 |
void Rope::changeLength (float delta) { |
241 | 83 |
// change length |
235 | 84 |
length += delta; |
241 | 85 |
|
86 |
// minimum length |
|
235 | 87 |
if (length < 0) |
88 |
length = 0; |
|
241 | 89 |
|
90 |
// inform network |
|
91 |
player.handleRopeLength(length); |
|
231 | 92 |
} |
93 |
||
225 | 94 |
RopeState Rope::getState (void) { |
235 | 95 |
return state; |
225 | 96 |
} |
241 | 97 |
|
98 |
float Rope::getLength (void) { |
|
99 |
return length; |
|
100 |
} |
|
101 |
||
102 |
void Rope::updateState (RopeState new_state, Vector position, Vector velocity, float new_length) { |
|
103 |
// update physics enabled/disabled state |
|
104 |
if (new_state == ROPE_FOLDED || new_state == ROPE_FIXED) |
|
105 |
disable(); |
|
106 |
||
107 |
else // new_state == ROPE_FLYING |
|
108 |
enable(); |
|
109 |
||
110 |
// update player.pivot |
|
111 |
if (new_state == ROPE_FIXED) |
|
112 |
player.setPivot(this); |
|
113 |
||
114 |
else if (this->state == ROPE_FIXED) |
|
115 |
player.setPivot(NULL); |
|
116 |
||
117 |
// update position stuff |
|
264 | 118 |
updatePhysics(position, velocity, true, FACING_RIGHT, 0); |
241 | 119 |
|
120 |
// update vars |
|
121 |
this->state = new_state; |
|
122 |
this->length = new_length; |
|
123 |
} |
|
124 |
||
125 |
void Rope::updateLength (float length) { |
|
126 |
// update length |
|
127 |
this->length = length; |
|
128 |
} |
|
225 | 129 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
254
diff
changeset
|
130 |
void Rope::draw (Graphics *g, PixelCoordinate camera) { |
235 | 131 |
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
|
132 |
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
|
133 |
|
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
|
134 |
PixelCoordinate player_pos = player.getCoordinate() - camera; |
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
|
135 |
PixelCoordinate self_pos = getCoordinate() - camera; |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
254
diff
changeset
|
136 |
|
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
|
137 |
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
|
138 |
player_pos.x, player_pos.y, |
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
|
139 |
self_pos.x, self_pos.y, |
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
|
140 |
CL_Color::black |
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
|
141 |
); |
225 | 142 |
} |
143 |
||
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
144 |
void Rope::tick (TimeMS dt) { |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
145 |
if (this->state == ROPE_FLYING) { |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
146 |
// super |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
147 |
PhysicsObject::tick(dt); |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
148 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
149 |
else if (this->state == ROPE_FIXED) { |
322 | 150 |
|
151 |
this->position = player.getPivot()->getPosition(); |
|
152 |
||
153 |
// If players pivot is not this rope but some other player don't do anything |
|
154 |
// (though it should be released atleast when the player dies) |
|
155 |
if (player.getPivot() != this) |
|
156 |
return; |
|
157 |
||
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
158 |
// If there's not ground on the pivot point anymore, release the rope |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
159 |
if (!world.collides(position)) { |
322 | 160 |
state = ROPE_FLYING; |
161 |
length = ROPE_LENGTH; |
|
162 |
inAir = true; |
|
163 |
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
|
164 |
player.handleRopeState(state); |
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
165 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
166 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
167 |
else { // ROPE_FOLDED |
254 | 168 |
// Rope shouldn't be ticking if it is folded, but this can still happen |
169 |
// immediately after the rope has been released |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
170 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
171 |
} |