author | terom |
Sun, 07 Dec 2008 01:18:59 +0000 | |
changeset 241 | e95b1602d836 |
parent 235 | 0a0c729365ee |
child 248 | e40ef56dc62c |
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> |
6 |
||
235 | 7 |
Rope::Rope(Player &player) : |
8 |
PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), false), player(player), state(ROPE_FOLDED) |
|
9 |
{ |
|
10 |
// XXX: better shape |
|
225 | 11 |
std::vector<Vector> shape(4); |
12 |
shape[0] = Vector(-1, -1); |
|
13 |
shape[1] = Vector(-1, 1); |
|
14 |
shape[2] = Vector(1, 1); |
|
15 |
shape[3] = Vector(1, -1); |
|
16 |
setShape(shape); |
|
17 |
} |
|
18 |
||
235 | 19 |
void Rope::throwRope (void) { |
20 |
state = ROPE_FLYING; |
|
225 | 21 |
|
235 | 22 |
// XXX: this should probably be more dynamic? |
23 |
length = ROPE_LENGTH; |
|
24 |
||
25 |
// copy position + velocity from player |
|
26 |
position = player.getPosition(); |
|
27 |
velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY; |
|
28 |
||
29 |
// we are FLYING |
|
30 |
inAir = true; |
|
31 |
||
32 |
// enable the physics object |
|
225 | 33 |
enable(); |
241 | 34 |
|
35 |
// inform network |
|
36 |
player.handleRopeState(state); |
|
225 | 37 |
} |
38 |
||
39 |
void Rope::onCollision() { |
|
235 | 40 |
// attached to something! |
41 |
state = ROPE_FIXED; |
|
228 | 42 |
|
43 |
// stop movement |
|
225 | 44 |
disable(); |
228 | 45 |
|
46 |
// set player's pivot |
|
235 | 47 |
player.setPivot(this); |
241 | 48 |
|
49 |
// inform network |
|
50 |
player.handleRopeState(state); |
|
225 | 51 |
} |
52 |
||
53 |
void Rope::release (void) { |
|
235 | 54 |
// disable if we're flying |
55 |
if (state == ROPE_FLYING) |
|
229 | 56 |
disable(); |
57 |
||
235 | 58 |
// TODO make it fly first and fold only after it's close to the player |
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 |
||
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
|
123 |
void Rope::draw (Graphics *g) 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( |
235 | 128 |
player.getPosition().x, player.getPosition().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
|
129 |
position.x, position.y, |
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 |