src/Rope.cc
author terom
Mon, 08 Dec 2008 18:36:06 +0000
changeset 304 d0f60a97a85e
parent 296 4d3ebaa29430
child 305 56799ec8d7be
permissions -rw-r--r--
fix NetworkServerProjectile::onHitPlayer to call super
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) : 
296
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 285
diff changeset
     9
    PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), ROPE, 0.00, false), player(player), state(ROPE_FOLDED) 
235
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
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
    27
    setPosition (player.getPosition());
235
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
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 265
diff changeset
    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
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    43
    // attached to something!
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    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
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
    48
    setPosition (collisionPoint);
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    49
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    50
    // set player's pivot
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    51
    player.setPivot(this);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    52
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    53
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    54
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    55
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    56
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    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
355e46effa41 Rope works
ekku
parents: 228
diff changeset
    60
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    61
    state = ROPE_FOLDED;
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    62
    
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    63
    // player doesn't have a pivot anymore
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    64
    player.setPivot(NULL);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    65
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    66
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    67
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    68
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    69
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    70
void Rope::changeLength (float delta) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    71
    // change length
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    72
    length += delta;
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    73
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    74
    // minimum length
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    75
    if (length < 0)
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    76
        length = 0;
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    77
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    78
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    79
    player.handleRopeLength(length);
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
    80
}
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
    81
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    82
RopeState Rope::getState (void) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    83
    return state;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    84
}
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    85
        
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    86
float Rope::getLength (void) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    87
    return length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    88
}
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    89
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    90
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
    91
    // update physics enabled/disabled state
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    92
    if (new_state == ROPE_FOLDED || new_state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    93
        disable();
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    94
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    95
    else // new_state == ROPE_FLYING
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    96
        enable();
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    97
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    98
    // update player.pivot
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    99
    if (new_state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   100
        player.setPivot(this);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   101
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   102
    else if (this->state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   103
        player.setPivot(NULL);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   104
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   105
    // update position stuff
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 263
diff changeset
   106
    updatePhysics(position, velocity, true, FACING_RIGHT, 0);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   107
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   108
    // update vars
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   109
    this->state = new_state;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   110
    this->length = new_length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   111
}
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   112
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   113
void Rope::updateLength (float length) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   114
    // update length
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   115
    this->length = length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   116
}
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   117
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   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
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   120
        return ROPE_FORCE;
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   121
    else
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   122
        return 0;
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   123
}
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
   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
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   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
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   137
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   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
0c3d58912e1b Rope fix.
ekku
parents: 252
diff changeset
   152
        // Rope shouldn't be ticking if it is folded, but this can still happen
0c3d58912e1b Rope fix.
ekku
parents: 252
diff changeset
   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
}