src/Projectile.cc
author saiam
Mon, 08 Dec 2008 00:05:45 +0000
changeset 273 eeb699e1d908
parent 272 97de051edbcf
child 276 87434abc1ba1
permissions -rw-r--r--
Made forceq to contain time again.
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     1
#include "Projectile.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: 226
diff changeset
     2
#include "Graphics.hh"
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
     3
#include "Timer.hh"
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     4
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
     5
Projectile::Projectile (GameState &state, Vector position, Vector velocity, float explosionRadius, float radius, 
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
     6
        TickCount expire, bool visible) :
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
     7
    PhysicsObject(state.world, PLAYER_MASS, position, velocity, PROJECTILE), state(state), visible(visible),
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
     8
    explosionRadius(explosionRadius), radius(radius), expire(expire)
265
d97bf6790c22 PhysicsObject enum
ekku
parents: 263
diff changeset
     9
{
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    10
    initialize();
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    11
}
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    12
    
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    13
Projectile::Projectile (GameState &state, Vector position, Vector velocity, Weapon *weapon, bool visible) :
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    14
    PhysicsObject(state.world, PLAYER_MASS, position, velocity, PROJECTILE), state(state), visible(visible),
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    15
    explosionRadius(weapon->getExplosionRadius()), radius(weapon->getRadius()), expire(weapon->getExpire())
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    16
{
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    17
    initialize();
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    18
}
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    19
    
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    20
void Projectile::initialize (void) {
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    21
    // set birth tick
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    22
    birth_tick = world.tick_timer.get_ticks();
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    23
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    24
    // XXX: projectiles should be particles?
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    25
    std::vector<Vector> shape(4);
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    26
263
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    27
    shape[0] = Vector(0,        -radius );
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    28
    shape[1] = Vector(+radius,  0       );
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    29
    shape[2] = Vector(0,        +radius );
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    30
    shape[3] = Vector(-radius,  0       );
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    31
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    32
    setShape(shape);
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    33
    
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    34
    // XXX: get this as an argument
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    35
    collision_elasticity = 0.9;
211
d5d52fb191e4 some minor fixes, shots now explode in collisionpoint
nireco
parents: 208
diff changeset
    36
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    37
    // add to state
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    38
    state.addProjectile(this);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    39
}
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    40
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    41
Projectile::~Projectile (void) {
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    42
    state.projectiles.remove(this);
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    43
}
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    44
 
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    45
void Projectile::onDestroy (Vector position, bool removeGround) {
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    46
    if (removeGround)
263
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    47
        world.removeGround(position, explosionRadius);
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    48
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    49
    destroy();
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    50
}
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    51
272
97de051edbcf Added PhysicsObject* attribute to onCollision
saiam
parents: 271
diff changeset
    52
void Projectile::onCollision (Vector collisionPoint, PhysicsObject *other) {
252
25054ce94d07 Rope is released if the ground on the pivot point is destroyed.
ekku
parents: 248
diff changeset
    53
    onDestroy(collisionPoint, true);
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    54
}
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    55
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    56
   
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    57
void Projectile::tick (TimeMS dt) {
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    58
    // expire projectiles
271
bf6784a95b08 touch up weapon/projectile with comments and slight tweaking
terom
parents: 265
diff changeset
    59
    if (world.tick_timer.get_ticks() > birth_tick + expire)
226
381487d07d17 projectiles remove ground when expiring -> fixed digging
terom
parents: 224
diff changeset
    60
        onDestroy(position, true);
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    61
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    62
    // super
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    63
    PhysicsObject::tick(dt);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    64
}
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    65
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 252
diff changeset
    66
void Projectile::draw(Graphics *g, PixelCoordinate camera) const {
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: 226
diff changeset
    67
    CL_GraphicContext *gc = g->get_gc();
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 226
diff changeset
    68
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    69
    if (visible) {
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: 255
diff changeset
    70
        PixelCoordinate pos = getCoordinate() - camera;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 252
diff changeset
    71
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    72
        CL_Quad projectile(
263
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    73
                pos.x,          pos.y - radius,
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    74
                pos.x + radius, pos.y,
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    75
                pos.x,          pos.y + radius,
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 257
diff changeset
    76
                pos.x - radius, pos.y
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 252
diff changeset
    77
        );
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    78
        
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    79
        gc->fill_quad(projectile, CL_Color::green);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    80
    }
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    81
}
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    82