author | ekku |
Sun, 07 Dec 2008 19:52:12 +0000 | |
changeset 252 | 25054ce94d07 |
parent 248 | e40ef56dc62c |
child 254 | 0c3d58912e1b |
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) : |
9 |
PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), false), player(player), state(ROPE_FOLDED) |
|
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 |
|
27 |
position = player.getPosition(); |
|
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 |
||
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
40 |
void Rope::onCollision (Vector collisionPoint) { |
235 | 41 |
// attached to something! |
42 |
state = ROPE_FIXED; |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
43 |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
44 |
// 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
|
45 |
// Currently the position is something like one pixel away from the collisionPoint where there isn't ground. |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
46 |
this->position = collisionPoint; |
228 | 47 |
|
48 |
// set player's pivot |
|
235 | 49 |
player.setPivot(this); |
241 | 50 |
|
51 |
// inform network |
|
52 |
player.handleRopeState(state); |
|
225 | 53 |
} |
54 |
||
55 |
void Rope::release (void) { |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
56 |
// Remove the rope from the PhysicsWorld |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
57 |
disable(); |
229 | 58 |
|
235 | 59 |
state = ROPE_FOLDED; |
228 | 60 |
|
61 |
// player doesn't have a pivot anymore |
|
235 | 62 |
player.setPivot(NULL); |
241 | 63 |
|
64 |
// inform network |
|
65 |
player.handleRopeState(state); |
|
225 | 66 |
} |
67 |
||
235 | 68 |
void Rope::changeLength (float delta) { |
241 | 69 |
// change length |
235 | 70 |
length += delta; |
241 | 71 |
|
72 |
// minimum length |
|
235 | 73 |
if (length < 0) |
74 |
length = 0; |
|
241 | 75 |
|
76 |
// inform network |
|
77 |
player.handleRopeLength(length); |
|
231 | 78 |
} |
79 |
||
225 | 80 |
RopeState Rope::getState (void) { |
235 | 81 |
return state; |
225 | 82 |
} |
241 | 83 |
|
84 |
float Rope::getLength (void) { |
|
85 |
return length; |
|
86 |
} |
|
87 |
||
88 |
void Rope::updateState (RopeState new_state, Vector position, Vector velocity, float new_length) { |
|
89 |
// update physics enabled/disabled state |
|
90 |
if (new_state == ROPE_FOLDED || new_state == ROPE_FIXED) |
|
91 |
disable(); |
|
92 |
||
93 |
else // new_state == ROPE_FLYING |
|
94 |
enable(); |
|
95 |
||
96 |
// update player.pivot |
|
97 |
if (new_state == ROPE_FIXED) |
|
98 |
player.setPivot(this); |
|
99 |
||
100 |
else if (this->state == ROPE_FIXED) |
|
101 |
player.setPivot(NULL); |
|
102 |
||
103 |
// update position stuff |
|
104 |
updatePhysics(position, velocity, true, false, 0); |
|
105 |
||
106 |
// update vars |
|
107 |
this->state = new_state; |
|
108 |
this->length = new_length; |
|
109 |
} |
|
110 |
||
111 |
void Rope::updateLength (float length) { |
|
112 |
// update length |
|
113 |
this->length = length; |
|
114 |
} |
|
225 | 115 |
|
228 | 116 |
float Rope::getPivotForce (PhysicsObject *bob) { |
235 | 117 |
if ((position - player.getPosition()).length() >= length) |
118 |
return ROPE_FORCE; |
|
228 | 119 |
else |
120 |
return 0; |
|
121 |
} |
|
122 |
||
248 | 123 |
void Rope::draw (Graphics *g, Vector cam) const { |
235 | 124 |
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
|
125 |
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
|
126 |
|
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 |
g->get_gc()->draw_line( |
248 | 128 |
player.getPosition().x-cam.x, player.getPosition().y-cam.y, |
129 |
position.x-cam.x, position.y-cam.y, |
|
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
|
130 |
CL_Color::black |
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
|
131 |
); |
225 | 132 |
} |
133 |
||
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
134 |
void Rope::tick (TimeMS dt) { |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
135 |
if (this->state == ROPE_FLYING) { |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
136 |
// super |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
137 |
PhysicsObject::tick(dt); |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
138 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
139 |
else if (this->state == ROPE_FIXED) { |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
140 |
// 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
|
141 |
if (!world.collides(position)) { |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
142 |
release(); |
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 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
145 |
else { // ROPE_FOLDED |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
146 |
throw std::logic_error("Rope shouldn't be ticking if it is folded"); |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
147 |
} |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
248
diff
changeset
|
148 |
} |