src/Projectile.cc
author saiam
Sun, 07 Dec 2008 19:02:45 +0000
changeset 249 5647f58e37cd
parent 248 e40ef56dc62c
child 252 25054ce94d07
permissions -rw-r--r--
Removed some unnecessary TODOs
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
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
     5
Projectile::Projectile(GameState &state, Vector position, Vector velocity, bool visible, float radius, TickCount age) :
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
     6
    PhysicsObject(state.world, PLAYER_MASS, position, velocity), state(state), visible(visible), radius(radius), age(age) {
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
     7
    birth_tick = world.tick_timer.get_ticks();
211
d5d52fb191e4 some minor fixes, shots now explode in collisionpoint
nireco
parents: 208
diff changeset
     8
    // Don't think these are needed anymore
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     9
    std::vector<Vector> shape(4);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    10
    shape[0] = Vector(-1, -1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    11
    shape[1] = Vector(-1, 1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    12
    shape[2] = Vector(1, 1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    13
    shape[3] = Vector(1, -1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    14
    setShape(shape);
211
d5d52fb191e4 some minor fixes, shots now explode in collisionpoint
nireco
parents: 208
diff changeset
    15
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    16
    target_visible = false;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    17
    collision_elasticity = 0.9; // = shotType.elasticity
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
    18
    state.addProjectile(this);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    19
}
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
    20
    
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
    21
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
    22
    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
    23
}
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    24
 
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    25
void Projectile::onDestroy (Vector position, bool removeGround) {
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    26
    if (removeGround)
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    27
        world.removeGround(position, radius);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    28
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    29
    destroy();
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    30
}
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    31
201
135616467a0a rename Shot -> Projectile
terom
parents: 200
diff changeset
    32
void Projectile::onCollision() {
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    33
    onDestroy(position, true);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    34
}
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    35
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    36
   
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    37
void Projectile::tick (TimeMS dt) {
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    38
    // expire projectiles
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    39
    if (world.tick_timer.get_ticks() > birth_tick + age)
226
381487d07d17 projectiles remove ground when expiring -> fixed digging
terom
parents: 224
diff changeset
    40
        onDestroy(position, true);
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    41
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    42
    // super
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    43
    PhysicsObject::tick(dt);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    44
}
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    45
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    46
void Projectile::draw(Graphics *g, Vector cam) 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
    47
    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
    48
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    49
    if (visible) {
204
4c386e9c950f Removed one memory leak.
saiam
parents: 201
diff changeset
    50
  
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    51
        CL_Quad projectile(
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    52
                           (int)((position).x+1-cam.x), (int)((position).y+1-cam.y),
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    53
                           (int)((position).x-1-cam.x), (int)((position).y+1-cam.y),
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    54
                           (int)((position).x+1-cam.x), (int)((position).y-1-cam.y),
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    55
                           (int)((position).x-1-cam.x), (int)((position).y-1-cam.y)
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    56
                           );
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    57
        
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    58
        gc->fill_quad(projectile, CL_Color::green);
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    59
        
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    60
        const uint16_t chlen = 10;
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    61
        int x = projectile.center().x -cam.x;
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    62
        int y = projectile.center().y -cam.y;
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    63
        if (target_visible) {
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    64
            if (facingRight) {
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    65
                gc->draw_line(x, y,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    66
                              x + std::cos(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    67
                              y - std::sin(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    68
                              CL_Color::black);
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    69
            } else {
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    70
                gc->draw_line(x, y,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    71
                              x - std::cos(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    72
                              y - std::sin(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    73
                              CL_Color::black);
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    74
            }
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    75
        }
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    76
    }
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    77
}
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    78