src/Rope.cc
author nireco
Mon, 08 Dec 2008 22:57:01 +0000
changeset 325 a19c78786d7f
parent 322 f94a5c192097
child 328 51d644c8d5a2
permissions -rw-r--r--
kills, deaths and weapon name is displayed in right place
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) : 
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 305
diff changeset
     9
    PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), ROPE, 0.00, false), 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 305
diff changeset
    10
    player(player), 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 305
diff changeset
    11
    state(ROPE_FOLDED) 
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    12
{
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    13
    // XXX: better shape
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    14
    std::vector<Vector> shape(4);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    15
    shape[0] = Vector(-1, -1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    16
    shape[1] = Vector(-1, 1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    17
    shape[2] = Vector(1, 1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    18
    shape[3] = Vector(1, -1);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    19
    setShape(shape);
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    20
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    21
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    22
void Rope::throwRope (void) {
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    23
    state = ROPE_FLYING;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    24
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    25
    // XXX: this should probably be more dynamic?
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    26
    length = ROPE_LENGTH;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    27
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    28
    // copy position + velocity from player
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
    29
    setPosition (player.getPosition());
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    30
    velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    31
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    32
    // we are FLYING
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    33
    inAir = true;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    34
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    35
    // enable the physics object
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    36
    enable();
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    37
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    38
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    39
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    40
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    41
273
eeb699e1d908 Made forceq to contain time again.
saiam
parents: 265
diff changeset
    42
void Rope::onCollision (Vector collisionPoint, PhysicsObject *other) {
322
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
    43
    // Fix the rope to another player if collided with it
305
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    44
    if (other != NULL) {
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    45
        if (other->getType() == PLAYER) {
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    46
            Player *target = dynamic_cast<Player*>(other);
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    47
            if (target == &(this->player))
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    48
                return;
322
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
    49
            this->player.pivot = target;            
305
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    50
        } else if (other->getType() == PROJECTILE) {
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    51
            return;
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    52
        }
322
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
    53
    } else { // Collided with terrain
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
    54
        player.setPivot(this);
305
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    55
    }
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 273
diff changeset
    56
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    57
    // attached to something!
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    58
    state = ROPE_FIXED;
305
56799ec8d7be Weapon damage and some other stuff
ekku
parents: 296
diff changeset
    59
    velocity = Vector(0,0);
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
    60
        
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
    61
    // 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
    62
    // 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
    63
    setPosition (collisionPoint);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    64
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    65
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    66
    player.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    67
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    68
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    69
void Rope::release (void) {
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
    70
    // Remove the rope from the PhysicsWorld
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
    71
    disable();
229
355e46effa41 Rope works
ekku
parents: 228
diff changeset
    72
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    73
    state = ROPE_FOLDED;
228
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    74
    
dbc1bb7a98b5 Rope has forces
ekku
parents: 225
diff changeset
    75
    // player doesn't have a pivot anymore
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    76
    player.setPivot(NULL);
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.handleRopeState(state);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    80
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    81
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    82
void Rope::changeLength (float delta) {
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    83
    // change length
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    84
    length += delta;
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
    // minimum length
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    87
    if (length < 0)
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    88
        length = 0;
241
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
    // inform network
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    91
    player.handleRopeLength(length);
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
    92
}
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
    93
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    94
RopeState Rope::getState (void) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    95
    return state;
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
    96
}
241
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
float Rope::getLength (void) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
    99
    return length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   100
}
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
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
   103
    // update physics enabled/disabled state
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   104
    if (new_state == ROPE_FOLDED || new_state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   105
        disable();
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   106
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   107
    else // new_state == ROPE_FLYING
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   108
        enable();
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
    // update player.pivot
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   111
    if (new_state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   112
        player.setPivot(this);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   113
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   114
    else if (this->state == ROPE_FIXED)
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   115
        player.setPivot(NULL);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   116
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   117
    // update position stuff
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 263
diff changeset
   118
    updatePhysics(position, velocity, true, FACING_RIGHT, 0);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   119
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   120
    // update vars
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   121
    this->state = new_state;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   122
    this->length = new_length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   123
}
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   124
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   125
void Rope::updateLength (float length) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   126
    // update length
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   127
    this->length = length;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 235
diff changeset
   128
}
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   129
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 254
diff changeset
   130
void Rope::draw (Graphics *g, PixelCoordinate camera) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   131
    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
   132
        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
   133
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
   134
    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
   135
    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
   136
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
   137
    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
   138
        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
   139
        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
   140
        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
   141
    );
225
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   142
}
22ecb9cb9245 Rope can be drawn.
ekku
parents:
diff changeset
   143
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   144
void Rope::tick (TimeMS dt) {
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   145
    if (this->state == ROPE_FLYING) {
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   146
        // super
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   147
        PhysicsObject::tick(dt); 
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   148
    }
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   149
    else if (this->state == ROPE_FIXED) {
322
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   150
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   151
        this->position = player.getPivot()->getPosition();
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   152
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   153
        // If players pivot is not this rope but some other player don't do anything 
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   154
        // (though it should be released atleast when the player dies)
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   155
        if (player.getPivot() != this)
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   156
            return;
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   157
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   158
        // 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
   159
        if (!world.collides(position)) { 
322
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   160
            state = ROPE_FLYING;
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   161
            length = ROPE_LENGTH;
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   162
            inAir = true;
f94a5c192097 Rope fixing
ekku
parents: 311
diff changeset
   163
            player.setPivot(NULL);
263
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
   164
            player.handleRopeState(state);
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   165
        }
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   166
    }
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   167
    else { // ROPE_FOLDED
254
0c3d58912e1b Rope fix.
ekku
parents: 252
diff changeset
   168
        // Rope shouldn't be ticking if it is folded, but this can still happen
0c3d58912e1b Rope fix.
ekku
parents: 252
diff changeset
   169
        // 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
   170
    }
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
   171
}