src/Rope.cc
author ekku
Sun, 07 Dec 2008 19:52:12 +0000
changeset 252 25054ce94d07
parent 248 e40ef56dc62c
child 254 0c3d58912e1b
permissions -rw-r--r--
Rope is released if the ground on the pivot point is destroyed.
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>
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
     6
#include <stdexcept>
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
     7
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
     8
Rope::Rope(Player &player) : 
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
     9
    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
    10
{
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    11
    // XXX: better shape
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    12
    std::vector<Vector> shape(4);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    13
    shape[0] = Vector(-1, -1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    14
    shape[1] = Vector(-1, 1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    15
    shape[2] = Vector(1, 1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    16
    shape[3] = Vector(1, -1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    17
    setShape(shape);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    18
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    19
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    20
void Rope::throwRope (void) {
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    21
    state = ROPE_FLYING;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    22
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    23
    // XXX: this should probably be more dynamic?
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    24
    length = ROPE_LENGTH;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    25
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    26
    // copy position + velocity from player
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    27
    position = player.getPosition();
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    28
    velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    29
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    30
    // we are FLYING
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    31
    inAir = true;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    32
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    33
    // enable the physics object
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    34
    enable();
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    35
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    36
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    37
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    38
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    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
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    41
    // attached to something!
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    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
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    47
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    48
    // set player's pivot
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    49
    player.setPivot(this);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    50
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    51
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    52
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    53
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    54
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    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
355e46effa41 Rope works
ekku
parents: 228
diff changeset
    58
235
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
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 241
diff changeset
   123
void Rope::draw (Graphics *g, Vector cam) 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(
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 241
diff changeset
   128
            player.getPosition().x-cam.x, player.getPosition().y-cam.y,
e40ef56dc62c scrolling looks like it works
nireco
parents: 241
diff changeset
   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
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   132
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   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
}