| author | ekku |
| Mon, 08 Dec 2008 16:46:48 +0000 | |
| changeset 295 | 4d3adfbec077 |
| parent 285 | c080c8c70333 |
| child 296 | 4d3ebaa29430 |
| 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) : |
| 265 | 9 |
PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), ROPE, false), player(player), state(ROPE_FOLDED) |
| 235 | 10 |
{
|
11 |
// XXX: better shape |
|
| 225 | 12 |
std::vector<Vector> shape(4); |
13 |
shape[0] = Vector(-1, -1); |
|
14 |
shape[1] = Vector(-1, 1); |
|
15 |
shape[2] = Vector(1, 1); |
|
16 |
shape[3] = Vector(1, -1); |
|
17 |
setShape(shape); |
|
18 |
} |
|
19 |
||
| 235 | 20 |
void Rope::throwRope (void) {
|
21 |
state = ROPE_FLYING; |
|
| 225 | 22 |
|
| 235 | 23 |
// XXX: this should probably be more dynamic? |
24 |
length = ROPE_LENGTH; |
|
25 |
||
26 |
// copy position + velocity from player |
|
| 285 | 27 |
setPosition (player.getPosition()); |
| 235 | 28 |
velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY; |
29 |
||
30 |
// we are FLYING |
|
31 |
inAir = true; |
|
32 |
||
33 |
// enable the physics object |
|
| 225 | 34 |
enable(); |
| 241 | 35 |
|
36 |
// inform network |
|
37 |
player.handleRopeState(state); |
|
| 225 | 38 |
} |
39 |
||
| 273 | 40 |
void Rope::onCollision (Vector collisionPoint, PhysicsObject *other) {
|
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
273
diff
changeset
|
41 |
(void) other; |
|
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
273
diff
changeset
|
42 |
|
| 235 | 43 |
// attached to something! |
44 |
state = ROPE_FIXED; |
|
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
45 |
|
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
46 |
// 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
|
47 |
// Currently the position is something like one pixel away from the collisionPoint where there isn't ground. |
| 285 | 48 |
setPosition (collisionPoint); |
| 228 | 49 |
|
50 |
// set player's pivot |
|
| 235 | 51 |
player.setPivot(this); |
| 241 | 52 |
|
53 |
// inform network |
|
54 |
player.handleRopeState(state); |
|
| 225 | 55 |
} |
56 |
||
57 |
void Rope::release (void) {
|
|
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
58 |
// Remove the rope from the PhysicsWorld |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
59 |
disable(); |
| 229 | 60 |
|
| 235 | 61 |
state = ROPE_FOLDED; |
| 228 | 62 |
|
63 |
// player doesn't have a pivot anymore |
|
| 235 | 64 |
player.setPivot(NULL); |
| 241 | 65 |
|
66 |
// inform network |
|
67 |
player.handleRopeState(state); |
|
| 225 | 68 |
} |
69 |
||
| 235 | 70 |
void Rope::changeLength (float delta) {
|
| 241 | 71 |
// change length |
| 235 | 72 |
length += delta; |
| 241 | 73 |
|
74 |
// minimum length |
|
| 235 | 75 |
if (length < 0) |
76 |
length = 0; |
|
| 241 | 77 |
|
78 |
// inform network |
|
79 |
player.handleRopeLength(length); |
|
| 231 | 80 |
} |
81 |
||
| 225 | 82 |
RopeState Rope::getState (void) {
|
| 235 | 83 |
return state; |
| 225 | 84 |
} |
| 241 | 85 |
|
86 |
float Rope::getLength (void) {
|
|
87 |
return length; |
|
88 |
} |
|
89 |
||
90 |
void Rope::updateState (RopeState new_state, Vector position, Vector velocity, float new_length) {
|
|
91 |
// update physics enabled/disabled state |
|
92 |
if (new_state == ROPE_FOLDED || new_state == ROPE_FIXED) |
|
93 |
disable(); |
|
94 |
||
95 |
else // new_state == ROPE_FLYING |
|
96 |
enable(); |
|
97 |
||
98 |
// update player.pivot |
|
99 |
if (new_state == ROPE_FIXED) |
|
100 |
player.setPivot(this); |
|
101 |
||
102 |
else if (this->state == ROPE_FIXED) |
|
103 |
player.setPivot(NULL); |
|
104 |
||
105 |
// update position stuff |
|
| 264 | 106 |
updatePhysics(position, velocity, true, FACING_RIGHT, 0); |
| 241 | 107 |
|
108 |
// update vars |
|
109 |
this->state = new_state; |
|
110 |
this->length = new_length; |
|
111 |
} |
|
112 |
||
113 |
void Rope::updateLength (float length) {
|
|
114 |
// update length |
|
115 |
this->length = length; |
|
116 |
} |
|
| 225 | 117 |
|
| 228 | 118 |
float Rope::getPivotForce (PhysicsObject *bob) {
|
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
273
diff
changeset
|
119 |
if ((position - bob->getPosition()).length() >= length) |
| 235 | 120 |
return ROPE_FORCE; |
| 228 | 121 |
else |
122 |
return 0; |
|
123 |
} |
|
124 |
||
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
254
diff
changeset
|
125 |
void Rope::draw (Graphics *g, PixelCoordinate camera) {
|
| 235 | 126 |
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
|
127 |
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
|
128 |
|
|
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
|
129 |
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
|
130 |
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
|
131 |
|
|
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 |
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
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
); |
| 225 | 137 |
} |
138 |
||
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
139 |
void Rope::tick (TimeMS dt) {
|
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
140 |
if (this->state == ROPE_FLYING) {
|
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
141 |
// super |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
142 |
PhysicsObject::tick(dt); |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
143 |
} |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
144 |
else if (this->state == ROPE_FIXED) {
|
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
145 |
// 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
|
146 |
if (!world.collides(position)) {
|
|
256
eb4975026402
Rope starts falling when the ground underneath is destroyd
ekku
parents:
255
diff
changeset
|
147 |
this->state = ROPE_FLYING; |
|
263
8c999cf4c182
weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents:
257
diff
changeset
|
148 |
player.handleRopeState(state); |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
149 |
} |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
150 |
} |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
151 |
else { // ROPE_FOLDED
|
| 254 | 152 |
// Rope shouldn't be ticking if it is folded, but this can still happen |
153 |
// 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
|
154 |
} |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
155 |
} |