src/Rope.cc
author terom
Sun, 07 Dec 2008 01:18:59 +0000
changeset 241 e95b1602d836
parent 235 0a0c729365ee
child 248 e40ef56dc62c
permissions -rw-r--r--
implement the ROT (Rope Over TCP) protocol
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
     1
#include "Player.hh"
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
     2
#include "Rope.hh"      
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
     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
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
     5
#include <math.h>
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
     6
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
     7
Rope::Rope(Player &player) : 
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
     8
    PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), false), player(player), state(ROPE_FOLDED) 
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
     9
{
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    10
    // XXX: better shape
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    11
    std::vector<Vector> shape(4);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    12
    shape[0] = Vector(-1, -1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    13
    shape[1] = Vector(-1, 1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    14
    shape[2] = Vector(1, 1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    15
    shape[3] = Vector(1, -1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    16
    setShape(shape);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    17
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    18
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    19
void Rope::throwRope (void) {
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    20
    state = ROPE_FLYING;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    21
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    22
    // XXX: this should probably be more dynamic?
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    23
    length = ROPE_LENGTH;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    24
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    25
    // copy position + velocity from player
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    26
    position = player.getPosition();
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    27
    velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    28
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    29
    // we are FLYING
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    30
    inAir = true;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    31
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    32
    // enable the physics object
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    33
    enable();
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    34
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    35
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    36
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    37
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    38
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    39
void Rope::onCollision() {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    40
    // attached to something!
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    41
    state = ROPE_FIXED;
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    42
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    43
    // stop movement
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    44
    disable();
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    45
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    46
    // set player's pivot
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    47
    player.setPivot(this);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    48
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    49
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    50
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    51
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    52
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    53
void Rope::release (void) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    54
    // disable if we're flying
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    55
    if (state == ROPE_FLYING)
229
355e46effa41 Rope works
ekku
parents: 228
diff changeset
    56
        disable();
355e46effa41 Rope works
ekku
parents: 228
diff changeset
    57
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    58
    // TODO make it fly first and fold only after it's close to the player
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    59
    state = ROPE_FOLDED;
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    60
    
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    61
    // player doesn't have a pivot anymore
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    62
    player.setPivot(NULL);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    63
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    64
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    65
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    66
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    67
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    68
void Rope::changeLength (float delta) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    69
    // change length
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    70
    length += delta;
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    71
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    72
    // minimum length
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    73
    if (length < 0)
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    74
        length = 0;
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    75
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    76
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    77
    player.handleRopeLength(length);
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
    78
}
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
    79
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    80
RopeState Rope::getState (void) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    81
    return state;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    82
}
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    83
        
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    84
float Rope::getLength (void) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    85
    return length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    86
}
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    87
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    88
void Rope::updateState (RopeState new_state, Vector position, Vector velocity, float new_length) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    89
    // update physics enabled/disabled state
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    90
    if (new_state == ROPE_FOLDED || new_state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    91
        disable();
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    92
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    93
    else // new_state == ROPE_FLYING
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    94
        enable();
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    95
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    96
    // update player.pivot
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    97
    if (new_state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    98
        player.setPivot(this);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    99
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   100
    else if (this->state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   101
        player.setPivot(NULL);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   102
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   103
    // update position stuff
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   104
    updatePhysics(position, velocity, true, false, 0);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   105
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   106
    // update vars
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   107
    this->state = new_state;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   108
    this->length = new_length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   109
}
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   110
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   111
void Rope::updateLength (float length) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   112
    // update length
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   113
    this->length = length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   114
}
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   115
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   116
float Rope::getPivotForce (PhysicsObject *bob) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   117
    if ((position - player.getPosition()).length() >= length)
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   118
        return ROPE_FORCE;
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   119
    else
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   120
        return 0;
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   121
}
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   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
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   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
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   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
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   132
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   133