touch up weapon/projectile with comments and slight tweaking
authorterom
Sun, 07 Dec 2008 23:10:30 +0000
changeset 271 bf6784a95b08
parent 270 d17126e20de7
child 272 97de051edbcf
touch up weapon/projectile with comments and slight tweaking
src/Network/Server.cc
src/Player.cc
src/Projectile.cc
src/Projectile.hh
src/Weapon.cc
src/Weapon.hh
src/Weapons.cc
--- a/src/Network/Server.cc	Sun Dec 07 22:48:46 2008 +0000
+++ b/src/Network/Server.cc	Sun Dec 07 23:10:30 2008 +0000
@@ -93,7 +93,7 @@
     Engine::log(INFO, "server_player.create_projectile") << "weapon='" << weapon->getName() << "', position=" << position << ", velocity=" << velocity;
 
     // create new NetworkServerProjectile object
-    new NetworkServerProjectile(server, position, velocity, weapon->getExplosionRadius(), weapon->getRadius(), weapon->getExpireTicks());
+    new NetworkServerProjectile(server, position, velocity, weapon->getExplosionRadius(), weapon->getRadius(), weapon->getExpire());
 }
 
 void NetworkServerPlayer::handleChangeWeapon (unsigned int weaponIndex) {
--- a/src/Player.cc	Sun Dec 07 22:48:46 2008 +0000
+++ b/src/Player.cc	Sun Dec 07 23:10:30 2008 +0000
@@ -41,16 +41,15 @@
 }
 
 void Player::handleDig (Vector position, float radius) {
-    // XXX: clean this bit up
     // calculate new position and velocity
     Vector digPosition = position + getDirection() * PROJECTILE_START_DISTANCE;
+    
+    // remove directly
     world.removeGround(digPosition, radius);
-    // execute
-    //    new Projectile(state, shotPosition, Vector(0, 0), false, radius, 0);
 }
 
 void Player::handleCreateProjectile (Weapon *weapon, Vector position, Vector velocity) {
-    new Projectile(state, position, velocity, true, weapon->getExplosionRadius(), weapon->getRadius(), weapon->getExpireTicks());
+    new Projectile(state, position, velocity, weapon);
 }
         
 void Player::handleChangeWeapon (unsigned int weaponIndex) {
--- a/src/Projectile.cc	Sun Dec 07 22:48:46 2008 +0000
+++ b/src/Projectile.cc	Sun Dec 07 23:10:30 2008 +0000
@@ -2,22 +2,42 @@
 #include "Graphics.hh"
 #include "Timer.hh"
 
-Projectile::Projectile (GameState &state, Vector position, Vector velocity, bool visible, float explosionRadius, float radius, TickCount age) :
-    PhysicsObject(state.world, PLAYER_MASS, position, velocity, PROJECTILE), state(state), visible(visible), explosionRadius(explosionRadius), radius(radius), age(age)
+Projectile::Projectile (GameState &state, Vector position, Vector velocity, float explosionRadius, float radius, 
+        TickCount expire, bool visible) :
+    PhysicsObject(state.world, PLAYER_MASS, position, velocity, PROJECTILE), state(state), visible(visible),
+    explosionRadius(explosionRadius), radius(radius), expire(expire)
 {
+    initialize();
+}
+    
+Projectile::Projectile (GameState &state, Vector position, Vector velocity, Weapon *weapon, bool visible) :
+    PhysicsObject(state.world, PLAYER_MASS, position, velocity, PROJECTILE), state(state), visible(visible),
+    explosionRadius(weapon->getExplosionRadius()), radius(weapon->getRadius()), expire(weapon->getExpire())
+{
+    initialize();
+}
+    
+void Projectile::initialize (void) {
+    // set birth tick
     birth_tick = world.tick_timer.get_ticks();
-    // Don't think these are needed anymore
+
+    // XXX: projectiles should be particles?
     std::vector<Vector> shape(4);
+
     shape[0] = Vector(0,        -radius );
     shape[1] = Vector(+radius,  0       );
     shape[2] = Vector(0,        +radius );
     shape[3] = Vector(-radius,  0       );
+
     setShape(shape);
+    
+    // XXX: get this as an argument
+    collision_elasticity = 0.9;
 
-    collision_elasticity = 0.9; // = shotType.elasticity
+    // add to state
     state.addProjectile(this);
 }
-    
+
 Projectile::~Projectile (void) {
     state.projectiles.remove(this);
 }
@@ -36,7 +56,7 @@
    
 void Projectile::tick (TimeMS dt) {
     // expire projectiles
-    if (world.tick_timer.get_ticks() > birth_tick + age)
+    if (world.tick_timer.get_ticks() > birth_tick + expire)
         onDestroy(position, true);
 
     // super
--- a/src/Projectile.hh	Sun Dec 07 22:48:46 2008 +0000
+++ b/src/Projectile.hh	Sun Dec 07 23:10:30 2008 +0000
@@ -10,22 +10,67 @@
 
 class Projectile : public PhysicsObject {
 protected:
+    /**
+     * Reference to the world that we live in
+     */
     GameState &state;
+
+    /**
+     * Projectiles can be inivisble, e.g. for digging
+     */
     bool visible;
+    
+    /**
+     * The projectile is diamond-shaped, and this is the dimanond's "radius"
+     */
     float radius;
+
+    /**
+     * The radius of earth removed by this projectile on impact
+     */
     float explosionRadius;
 
+    /**
+     * How many ticks this projectile lasts before being expiring
+     */
+    TickCount expire;
+
+    /**
+     * The tick we were spawned at
+     */
+    TickCount birth_tick;
+
 public:
-    TickCount birth_tick;
-    TickCount age;
+    /**
+     * Construct a Projectile using the given parameters and add it to the world's list of projectiles
+     */
+    Projectile (GameState &state, Vector position, Vector velocity, float explosionRadius, float radius,
+            TickCount expire, bool visible = true);
 
-    Projectile (GameState &state, Vector position, Vector velocity, bool visible, float explosionRadius, float radius, TickCount age=1000000000);
+    /**
+     * Like above, but using the parameters of the given weapon
+     *
+     * @see Projectile
+     */
+    Projectile (GameState &state, Vector position, Vector velocity, Weapon *weapon, bool visible = true);
+
+    /**
+     * Removes this from the world's list of projectiles
+     */
     virtual ~Projectile (void);
-
+    
+    /**
+     * Draw
+     */
     virtual void draw (Graphics *g, PixelCoordinate camera) const;
 
 protected:
     /**
+     * Initialize shape and add to world projectiles list
+     */
+    void initialize (void);
+
+    /**
      * Removes ground at given position if applicable, and destroys this PhysicsObject.
      *
      * This is overriden by Network.
@@ -38,7 +83,7 @@
     virtual void onCollision (Vector collisionPoint);
 
     /**
-     * If we have expired, call onDestory without removingGround
+     * If we have expired, call onDestory and removeGround
      */
     virtual void tick (TimeMS dt);
 };
--- a/src/Weapon.cc	Sun Dec 07 22:48:46 2008 +0000
+++ b/src/Weapon.cc	Sun Dec 07 23:10:30 2008 +0000
@@ -1,18 +1,21 @@
 #include "Weapon.hh"
 
-Weapon::Weapon(TickCount age, float velocity, float recoil, float explosionRadius, float radius, int reloadTime, std::string name) : 
-    age(age), velocity(velocity), recoil(recoil), explosionRadius(explosionRadius), radius(radius), reloadTime(reloadTime), name(name), reloadTimer(0) 
+Weapon::Weapon(WeaponID id, TickCount expire, float velocity, float recoil, float explosionRadius, float radius,
+        TimeMS reloadTime, std::string name) : 
+    id(id), expire(expire), velocity(velocity), recoil(recoil), explosionRadius(explosionRadius), radius(radius),
+    reloadTime(reloadTime), name(name), reloadTimer(0) 
 {
 
 }
 
 void Weapon::tickReload (TimeMS dt) {
     reloadTimer -= dt;
+
     if (reloadTimer < 0)
         reloadTimer = 0;
 }
 
-bool Weapon::canShoot() {
+bool Weapon::canShoot() const {
     return (reloadTimer == 0);
 }
 
--- a/src/Weapon.hh	Sun Dec 07 22:48:46 2008 +0000
+++ b/src/Weapon.hh	Sun Dec 07 23:10:30 2008 +0000
@@ -3,6 +3,7 @@
 
 // forward-declare
 class Weapon;
+typedef unsigned int WeaponID;
 
 #include "GameState.hh"
 #include "Projectile.hh"
@@ -11,36 +12,85 @@
 
 class Weapon {
 protected:
+    /**
+     *  weapon's index in player's weapons list
+     */
+    WeaponID id;
+
+    /**
+     * weapon name
+     */
     std::string name;
+
+    /**
+     * projectile velocity
+     */
     float velocity;
-    float explosionRadius, radius;
-    int reloadTime; //in ms
+
+    /**
+     * radius of damage done on impact
+     */
+    float explosionRadius;
+
+    /**
+     * radius of projectile itself
+     */
+    float radius;
+    
+    /**
+     * how long it takes to reload
+     */
+    TimeMS reloadTime;
+    
+    /**
+     * recoil force, backwards
+     */
     float recoil;
-
+    
+    /**
+     * XXX: unused
+     */
     int clipSize;
 
-    bool visible;
-    TickCount age;
+    /**
+     * How many ticks projectiles last
+     */
+    TickCount expire;
+
+    /**
+     * current reload state
+     */
     int reloadTimer;
 
 public:
-    Weapon (TickCount age, float velocity, float recoil, float explosionRadius, float radius, int reloadTime, std::string name);
+    /**
+     * Create a weapon with the given parameters
+     */
+    Weapon (WeaponID id, TickCount expire, float velocity, float recoil, float explosionRadius, float radius, TimeMS reloadTime, std::string name);
     
-    // advance the reload timer
+    /**
+     * Decrement the reload timer, if it's still going
+     */
     void tickReload (TimeMS dt);
     
-    // can the weapon be fired (not reloading, have a clip, etc)
-    bool canShoot (void);
+    /**
+     * Can the weapon be fired (not reloading, have a clip, etc)
+     */
+    bool canShoot (void) const;
     
-    // get weapon parameters
-    std::string getName (void) { return name; }
-    float getSpeed (void) { return velocity; }
-    float getRecoil (void) { return recoil; }
-    float getExplosionRadius (void) { return explosionRadius; }
-    float getRadius (void) { return radius; }
-    TickCount getExpireTicks (void) { return age; }
+    /*
+     * Get weapon parameters
+     */
+    std::string getName (void) const { return name; }
+    float getSpeed (void) const { return velocity; }
+    float getRecoil (void) const { return recoil; }
+    float getExplosionRadius (void) const { return explosionRadius; }
+    float getRadius (void) const { return radius; }
+    TickCount getExpire (void) const { return expire; }
     
-    // start reloading
+    /**
+     * Start reloading
+     */
     void reload (void);
 };
 
--- a/src/Weapons.cc	Sun Dec 07 22:48:46 2008 +0000
+++ b/src/Weapons.cc	Sun Dec 07 23:10:30 2008 +0000
@@ -21,11 +21,12 @@
 
 std::vector<Weapon*> buildWeaponsList (void) {
     std::vector<Weapon*> weapons;
+    int idx = 0;
 
     for (WeaponParams *wp = WEAPON_PARAMS; 
          wp->age || wp->speed || wp->recoil || wp->explosionRadius || wp->radius || wp->reloadTime; 
-         wp++) {
-        weapons.push_back(new Weapon(wp->age, wp->speed, wp->recoil, wp->explosionRadius, wp->radius, wp->reloadTime, wp->name));
+         wp++, idx++) {
+        weapons.push_back(new Weapon(idx, wp->age, wp->speed, wp->recoil, wp->explosionRadius, wp->radius, wp->reloadTime, wp->name));
     }
 
     return weapons;