move weapons definition out to Weapons.cc
authorterom
Sat, 06 Dec 2008 23:47:13 +0000
changeset 236 0048ba274152
parent 235 0a0c729365ee
child 237 3d5465bcb67d
move weapons definition out to Weapons.cc
src/Player.cc
src/Player.hh
src/Weapon.cc
src/Weapon.hh
src/Weapons.cc
src/Weapons.hh
--- a/src/Player.cc	Sat Dec 06 23:29:06 2008 +0000
+++ b/src/Player.cc	Sat Dec 06 23:47:13 2008 +0000
@@ -1,5 +1,6 @@
 
-#include "Player.hh" 
+#include "Player.hh"
+#include "Weapons.hh"
 #include "Engine.hh"
 #include "Graphics.hh"
 
@@ -19,15 +20,11 @@
 const int img_width = 10;
 
 Player::Player(GameState &state, Vector position, bool visible) : 
-    PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible), weapons(),
-    selectedWeapon(0), changing(false), animation_step(0), rope(*this) 
+    PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible),
+    weapons(buildWeaponsList()), selectedWeapon(0), changing(false), animation_step(0), rope(*this) 
 {
-    // TODO: arsenal's size should be affected by some value
-    // and weapons should be loaded from somewhere, not generated here
-    for (int i = 0; i < 5; i++) {
-        weapons.push_back(Weapon(state, 10000, (5 - i) * 40 + 30, i * 6 + 5, i * 100 + 50, "asdf"));
-    }
-    
+    // XXX: populate weapons from somewhere else
+
     // build the player's shape
     // XXX: these dimensions are incorrect...
     std::vector<Vector> shape(4);
@@ -42,7 +39,7 @@
     // XXX: this should be a PhysicsObject constructor arg
     collision_elasticity = PLAYER_COLLISION_ELASTICITY;
 }
- 
+
 void Player::handleDig (Vector position, float radius) {
     // XXX: clean this bit up
     float shotspeed = 1; //0*PHYSICS_TICK_MS;
@@ -52,8 +49,8 @@
     new Projectile(state, position, shotVelocity, false, radius, 1);
 }
 
-void Player::handleCreateProjectile (Weapon &weapon, Vector position, Vector velocity) {
-    new Projectile(state, position, velocity, true, weapon.getExplosionRadius());
+void Player::handleCreateProjectile (Weapon *weapon, Vector position, Vector velocity) {
+    new Projectile(state, position, velocity, true, weapon->getExplosionRadius());
 }
 
 void Player::printDebugInfo (void) {
@@ -65,16 +62,17 @@
     PhysicsObject::tick(dt);
     
     // tick current weapon reload
-    getWeapon().tickReload(dt);
+    if (getCurrentWeapon())
+        getCurrentWeapon()->tickReload(dt);
 }
 
-void LocalPlayer::fireWeapon (Weapon &weapon) {
+void LocalPlayer::fireWeapon (Weapon *weapon) {
     // update reload timer
-    weapon.reload();
+    weapon->reload();
     
     // calculate new position and velocity
     Vector shotPosition = position + getDirection() * PROJECTILE_START_DISTANCE;
-    Vector shotVelocity = velocity + getDirection() * weapon.getSpeed();
+    Vector shotVelocity = velocity + getDirection() * weapon->getSpeed();
 
     // execute
     handleCreateProjectile(weapon, shotPosition, shotVelocity);
@@ -131,8 +129,8 @@
     }
 
     // validate shoot events, and then outsource to handleShoot so Network can intercept it
-    if ((input & INPUT_SHOOT) && getWeapon().canShoot())
-        fireWeapon(getWeapon());
+    if ((input & INPUT_SHOOT) && getCurrentWeapon()->canShoot())
+        fireWeapon(getCurrentWeapon());
     
     // rope throw+release+changeLength
     if (input & INPUT_ROPE)
@@ -160,7 +158,7 @@
         applyForce(move_force);
 }
 
-Weapon& Player::getWeapon() {
+Weapon* Player::getCurrentWeapon() {
     return weapons[selectedWeapon % weapons.size()];
 }
 
@@ -220,11 +218,13 @@
     Player::draw(g);
 
     // display weapon name?
-    if (displayWeapon) {
+    if (displayWeapon && getCurrentWeapon()) {
+        const std::string weaponName = getCurrentWeapon()->getName();
+
         g->getSimpleFont().draw(
-                position.x - g->getSimpleFont().get_width(getWeapon().getName()) / 2,
+                position.x - g->getSimpleFont().get_width(weaponName) / 2,
                 position.y + 10,
-                getWeapon().getName(),
+                weaponName,
                 g->get_gc()
         );
     }
--- a/src/Player.hh	Sat Dec 06 23:29:06 2008 +0000
+++ b/src/Player.hh	Sat Dec 06 23:47:13 2008 +0000
@@ -21,7 +21,7 @@
 
     protected:
         bool visible;
-        std::vector<Weapon> weapons;
+        std::vector<Weapon*> weapons;
         unsigned int selectedWeapon; //unsigned for x%sW not to fail
         bool changing;
 
@@ -31,15 +31,15 @@
         // default constructor for use with virtual inheritance... it's not defined
         Player (void);
         Player (GameState &state, Vector position, bool visible); 
-        
+
         // used by the network code to execute actions for players
         virtual void handleDig (Vector position, float radius);
-        virtual void handleCreateProjectile (Weapon &weapon, Vector position, Vector velocity);
+        virtual void handleCreateProjectile (Weapon *weapon, Vector position, Vector velocity);
         
         /*
          * The currently selected weapon
          */
-        Weapon& getWeapon();
+        Weapon* getCurrentWeapon();
 
     public:
         /*
@@ -62,7 +62,10 @@
 
 class LocalPlayer : public virtual Player {
     private:
-        void fireWeapon (Weapon &weapon);
+        /*
+         * Calculates projectil position/velocity and calls handleCreateProjectile
+         */
+        void fireWeapon (Weapon *weapon);
 
     public:
         /*
--- a/src/Weapon.cc	Sat Dec 06 23:29:06 2008 +0000
+++ b/src/Weapon.cc	Sat Dec 06 23:47:13 2008 +0000
@@ -1,26 +1,11 @@
 #include "Weapon.hh"
 
-Weapon::Weapon(GameState &st, TickCount age, float velocity, float explosionRadius, int reloadTime, std::string name) : 
-    st(st), age(age), velocity(velocity), explosionRadius(explosionRadius), reloadTime(reloadTime), name(name), reloadTimer(0) 
+Weapon::Weapon(TickCount age, float velocity, float explosionRadius, int reloadTime, std::string name) : 
+    age(age), velocity(velocity), explosionRadius(explosionRadius), reloadTime(reloadTime), name(name), reloadTimer(0) 
 {
 
 }
 
-Weapon::Weapon(const Weapon& orig) : 
-    st(orig.st), age(orig.age), velocity(orig.velocity), explosionRadius(orig.explosionRadius), reloadTime(orig.reloadTime), name(orig.name) 
-{
-
-}
-
-Weapon& Weapon::operator= (const Weapon& orig) {
-    st = orig.st;
-    age = orig.age;
-    velocity = orig.velocity;
-    explosionRadius = orig.explosionRadius;
-    reloadTime = orig.reloadTime;
-    name = orig.name;
-}
-
 void Weapon::tickReload (TimeMS dt) {
     reloadTimer -= dt;
     if (reloadTimer < 0)
--- a/src/Weapon.hh	Sat Dec 06 23:29:06 2008 +0000
+++ b/src/Weapon.hh	Sat Dec 06 23:47:13 2008 +0000
@@ -11,7 +11,6 @@
 
 class Weapon {
 protected:
-    GameState &st;
     std::string name;
     float velocity;
     float explosionRadius;
@@ -24,9 +23,7 @@
     int reloadTimer;
 
 public:
-    Weapon (GameState &st, TickCount age, float velocity, float explosionRadius, int reloadTime, std::string name);
-    Weapon (const Weapon& orig);
-    Weapon& operator= (const Weapon& orig);
+    Weapon (TickCount age, float velocity, float explosionRadius, int reloadTime, std::string name);
     
     // advance the reload timer
     void tickReload (TimeMS dt);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Weapons.cc	Sat Dec 06 23:47:13 2008 +0000
@@ -0,0 +1,29 @@
+
+#include "Weapons.hh"
+
+static struct WeaponParams {
+    TickCount age;
+    float speed;
+    float explosionRadius;
+    TickCount reloadTime;
+    std::string name;
+} WEAPON_PARAMS[] = {
+    /*  age     speed           expRadius   reloadTime      name        */
+    {   10000,  5 * 80 + 30,    0 * 6 + 5,  0 * 100 + 50,   "Weapon 1"  },
+    {   10000,  4 * 80 + 30,    1 * 6 + 5,  1 * 100 + 50,   "Weapon 2"  },
+    {   10000,  3 * 80 + 30,    2 * 6 + 5,  2 * 100 + 50,   "Weapon 3"  },
+    {   10000,  2 * 80 + 30,    3 * 6 + 5,  3 * 100 + 50,   "Weapon 4"  },
+    {   10000,  1 * 80 + 30,    4 * 6 + 5,  4 * 100 + 50,   "Weapon 5"  },
+    {   0,      0,              0,          0,              ""          }
+};
+
+std::vector<Weapon*> buildWeaponsList (void) {
+    std::vector<Weapon*> weapons;
+
+    for (WeaponParams *wp = WEAPON_PARAMS; wp->age || wp->speed || wp->explosionRadius || wp->reloadTime; wp++) {
+        weapons.push_back(new Weapon(wp->age, wp->speed, wp->explosionRadius, wp->reloadTime, wp->name));
+    }
+
+    return weapons;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Weapons.hh	Sat Dec 06 23:47:13 2008 +0000
@@ -0,0 +1,12 @@
+#ifndef WEAPONS_HH
+#define WEAPONS_HH
+
+#include "Weapon.hh"
+#include <vector>
+
+/**
+ * A collection of simple weapons
+ */
+std::vector<Weapon*> buildWeaponsList (void);
+
+#endif