code cleanup
authorterom
Sat, 06 Dec 2008 23:29:06 +0000
changeset 235 0a0c729365ee
parent 234 1dee8bd61c8e
child 236 0048ba274152
code cleanup
src/Config.hh
src/Graphics.cc
src/Graphics.hh
src/Input.cc
src/Input.hh
src/PhysicsObject.cc
src/PhysicsObject.hh
src/Player.cc
src/Player.hh
src/Rope.cc
src/Rope.hh
src/Weapon.hh
--- a/src/Config.hh	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Config.hh	Sat Dec 06 23:29:06 2008 +0000
@@ -41,6 +41,15 @@
                                         // different for different
                                         // objects
 
+// how far away from the player the projectile spawns
+const float PROJECTILE_START_DISTANCE = 10.0;
+
+const float ROPE_GROWTH_RATE = 5;
+const float ROPE_FORCE = 3500;
+const float ROPE_MASS = 10.0;   // same as player mass...?
+const float ROPE_LENGTH = 100.0;
+const float ROPE_VELOCITY = 500;
+
 // Graphical properties
 const CL_Color COLOR_EMPTY(86, 41, 0);
 const CL_Color COLOR_DIRT(144, 82, 23);
--- a/src/Graphics.cc	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Graphics.cc	Sat Dec 06 23:29:06 2008 +0000
@@ -8,7 +8,7 @@
     engine(engine), 
     state(state), 
     update_timer(GRAPHICS_UPDATE_INTERVAL_MS),
-    keyboard(get_ic()->get_keyboard()),
+    input(get_ic()->get_keyboard()),
     simple_font("Font2", engine.getResourceManager()) 
 {
 
@@ -24,7 +24,7 @@
     PlayerInput input_mask = 0;
     
     // update gui flags
-    this->flags = handleGuiInput(keyboard);
+    this->flags = input.readGuiInput();
 
     // quit?
     if (flags & GUI_INPUT_QUIT) {
@@ -42,7 +42,7 @@
         player->printDebugInfo();
     
     // build input_mask
-    input_mask = handlePlayerInput(keyboard);
+    input_mask = input.readPlayerInput();
     
     // apply input if there was any
     if (input_mask)
--- a/src/Graphics.hh	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Graphics.hh	Sat Dec 06 23:29:06 2008 +0000
@@ -25,8 +25,8 @@
     CL_SlotContainer slots;
     
     Timer update_timer;
-    
-    CL_InputDevice &keyboard;
+
+    Input input;
 
     // current GUI input state
     GuiInput flags;
--- a/src/Input.cc	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Input.cc	Sat Dec 06 23:29:06 2008 +0000
@@ -31,7 +31,13 @@
     {   GUI_INPUT_DEBUG_PLAYER,     CL_KEY_I                    },
 };
 
-static bool _checkInput (CL_InputDevice &keyboard, int keycode) {
+Input::Input (CL_InputDevice &keyboard) :
+    keyboard(keyboard)
+{
+
+}
+    
+bool Input::checkKeycode (int keycode) {
     if (keycode > 0)
         return keyboard.get_keycode(keycode);
 
@@ -42,24 +48,22 @@
         return true;
 }
 
-template <typename BitEnumType, typename BitMaskType> BitMaskType _buildMask (CL_InputDevice &keyboard,
-        InputKeymapEntry<BitEnumType> *keymap) 
-{
+template <typename BitEnumType, typename BitMaskType> BitMaskType Input::buildMask (InputKeymapEntry<BitEnumType> *keymap) {
     BitMaskType input_mask = 0;
 
     for (InputKeymapEntry<BitEnumType> *e = keymap; (e->keycode1 || e->keycode2) && e->input; e++) {
-        if (_checkInput(keyboard, e->keycode1) && _checkInput(keyboard, e->keycode2))
+        if (checkKeycode(e->keycode1) && checkKeycode(e->keycode2))
             input_mask |= e->input;
     }
     
     return input_mask;
 }
 
-PlayerInput handlePlayerInput (CL_InputDevice &keyboard) {
-    return _buildMask<PlayerInputBit, PlayerInput>(keyboard, INPUT_PLAYER_KEYMAP);
+PlayerInput Input::readPlayerInput (void) {
+    return buildMask<PlayerInputBit, PlayerInput>(INPUT_PLAYER_KEYMAP);
 }
 
-GuiInput handleGuiInput (CL_InputDevice &keyboard) {
-    return _buildMask<GuiInputBit, GuiInput>(keyboard, INPUT_GUI_KEYMAP);
+GuiInput Input::readGuiInput (void) {
+    return buildMask<GuiInputBit, GuiInput>(INPUT_GUI_KEYMAP);
 }
 
--- a/src/Input.hh	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Input.hh	Sat Dec 06 23:29:06 2008 +0000
@@ -35,10 +35,25 @@
 typedef uint16_t PlayerInput;
 typedef uint16_t GuiInput;
 
-/*
- * Handle keycodes
- */
-PlayerInput handlePlayerInput (CL_InputDevice &keyboard);
-GuiInput handleGuiInput (CL_InputDevice &keyboard);
+template <typename BitEnumType> struct InputKeymapEntry;
+
+class Input {
+    protected:
+        CL_InputDevice &keyboard;
+
+    public:
+        Input (CL_InputDevice &keyboard);
+    
+    private:
+        bool checkKeycode (int keycode);
+        template <typename BitEnumType, typename BitMaskType> BitMaskType buildMask (InputKeymapEntry<BitEnumType> *keymap);
+
+    public:
+        /*
+         * Reads the keyboard to determine current state of the Player/Gui input
+         */
+        PlayerInput readPlayerInput (void);
+        GuiInput readGuiInput (void);
+};
 
 #endif
--- a/src/PhysicsObject.cc	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/PhysicsObject.cc	Sat Dec 06 23:29:06 2008 +0000
@@ -327,6 +327,10 @@
     return this->aim;
 }
 
+Vector PhysicsObject::getDirection (void) {
+    return facingRight ? Vector(cos(aim), -sin(aim)) : Vector(-cos(aim), -sin(aim));
+}
+
 std::vector<Vector>& PhysicsObject::getShape () {
     return this->shape;
 }
--- a/src/PhysicsObject.hh	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/PhysicsObject.hh	Sat Dec 06 23:29:06 2008 +0000
@@ -206,6 +206,11 @@
     float getAim();
 
     /**
+     * Returns facing+aim as a unit vector
+     */
+    Vector getDirection (void);
+
+    /**
      *  Mark object as destroyed, it will be delete'd later
      */
     void destroy (void);
--- a/src/Player.cc	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Player.cc	Sat Dec 06 23:29:06 2008 +0000
@@ -19,13 +19,13 @@
 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), arsenal(),
+    PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible), weapons(),
     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++) {
-        arsenal.push_back(Weapon(state, 10000, (5 - i) * 40 + 30, i * 6 + 5, i * 100 + 50, "asdf"));
+        weapons.push_back(Weapon(state, 10000, (5 - i) * 40 + 30, i * 6 + 5, i * 100 + 50, "asdf"));
     }
     
     // build the player's shape
@@ -44,12 +44,12 @@
 }
  
 void Player::handleDig (Vector position, float radius) {
-    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
-            Vector(-std::cos(aim), -std::sin(aim));
-    float shotspeed = 1;//0*PHYSICS_TICK_MS;
-    Vector shotVelocity = unitVectorAim*shotspeed;
-    new Projectile(this->state, this->position, shotVelocity, false, radius, 1);
+    // XXX: clean this bit up
+    float shotspeed = 1; //0*PHYSICS_TICK_MS;
 
+    Vector shotVelocity = getDirection() * shotspeed;
+
+    new Projectile(state, position, shotVelocity, false, radius, 1);
 }
 
 void Player::handleCreateProjectile (Weapon &weapon, Vector position, Vector velocity) {
@@ -72,18 +72,9 @@
     // update reload timer
     weapon.reload();
     
-    // calculate unit vector
-    Vector unitVectorAim = (facingRight ? 
-        Vector(std::cos(aim), -std::sin(aim)) : 
-        Vector(-std::cos(aim), -std::sin(aim))
-    );
-    
-    // XXX: what does the PHYSICS_TICK_MS stuff mean?
-    float shotspeed = weapon.getVelocity() * PHYSICS_TICK_MS / 2;
-
-    Vector shotRelativeVelocity = unitVectorAim * shotspeed;
-    Vector shotVelocity = this->velocity + shotRelativeVelocity;
-    Vector shotPosition = this->position + unitVectorAim * 10;
+    // calculate new position and velocity
+    Vector shotPosition = position + getDirection() * PROJECTILE_START_DISTANCE;
+    Vector shotVelocity = velocity + getDirection() * weapon.getSpeed();
 
     // execute
     handleCreateProjectile(weapon, shotPosition, shotVelocity);
@@ -97,6 +88,7 @@
     float aim_delta = 0; 
 
     // handle movement left/right by applying a horizontal force, but limit the player's speed
+    // also update facing if needed
     if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED)) {
         setFacing(false);
         move_force.x -= PLAYER_MOVE_FORCE;
@@ -129,38 +121,33 @@
     }
     
     // outsource digging to Player::handleDig, since this modifies the Terrain and Network needs to know
-    if (input & INPUT_DIG) {
+    if (input & INPUT_DIG)
         handleDig(position, 15);
-
-        // Should create Projectile which destroys ground, but also should be destroyed then,
-        // but it doesn't.
-        // But this now just segfaults
-//        world.addObject(new Projectile(state, position, true));
-    }
     
     // XXX: currently not network safe
     if (input & INPUT_CHANGE) {
-        selectedWeapon = (selectedWeapon + 1) % arsenal.size();
+        selectedWeapon = (selectedWeapon + 1) % weapons.size();
         Engine::log(DEBUG, "player.input ") << "changed weapon to " << selectedWeapon;
     }
 
     // validate shoot events, and then outsource to handleShoot so Network can intercept it
-    if (input & INPUT_SHOOT && getWeapon().canShoot())
+    if ((input & INPUT_SHOOT) && getWeapon().canShoot())
         fireWeapon(getWeapon());
     
+    // rope throw+release+changeLength
     if (input & INPUT_ROPE)
-        rope.shoot();
+        rope.throwRope();
 
     if (input & INPUT_UNROPE)
         rope.release();
 
     if (input & INPUT_ROPE_UP)
-        rope.changeLength(true);
+        rope.changeLength(-ROPE_GROWTH_RATE);
 
     if (input & INPUT_ROPE_DOWN)
-        rope.changeLength(false);
+        rope.changeLength(ROPE_GROWTH_RATE);
 
-    // XXX: how should this be written?
+    // XXX: how should this be written? What does this do? Probably broken under network play
     if (move_force.x != 0) 
         animation_step = (animation_step + 1) % img_num_step;
 
@@ -174,7 +161,7 @@
 }
 
 Weapon& Player::getWeapon() {
-    return arsenal[selectedWeapon % arsenal.size()];
+    return weapons[selectedWeapon % weapons.size()];
 }
 
 void Player::draw (Graphics *g) {
@@ -242,3 +229,4 @@
         );
     }
 }
+
--- a/src/Player.hh	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Player.hh	Sat Dec 06 23:29:06 2008 +0000
@@ -21,7 +21,7 @@
 
     protected:
         bool visible;
-        std::vector<Weapon> arsenal;
+        std::vector<Weapon> weapons;
         unsigned int selectedWeapon; //unsigned for x%sW not to fail
         bool changing;
 
--- a/src/Rope.cc	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Rope.cc	Sat Dec 06 23:29:06 2008 +0000
@@ -4,7 +4,10 @@
 #include "Graphics.hh"
 #include <math.h>
 
-Rope::Rope(Player &climber) : PhysicsObject(climber.state.world, PLAYER_MASS, Vector(0,0), Vector(0,0), false), pl(climber), rs(FOLDED) {
+Rope::Rope(Player &player) : 
+    PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), false), player(player), state(ROPE_FOLDED) 
+{
+    // XXX: better shape
     std::vector<Vector> shape(4);
     shape[0] = Vector(-1, -1);
     shape[1] = Vector(-1, 1);
@@ -13,66 +16,70 @@
     setShape(shape);
 }
 
-void Rope::shoot(void) {
-
-    this->rs = FLYING;
+void Rope::throwRope (void) {
+    state = ROPE_FLYING;
 
-    this->length = 100;
-    this->position = pl.getPosition();
-    Vector direction = pl.getFacing() ? Vector( cos(pl.getAim()), -sin(pl.getAim()) ) 
-                                      : Vector( -cos(pl.getAim()), -sin(pl.getAim()) );
-    this->velocity = pl.getVelocity() + direction*500;
-    this->inAir = true;
-
+    // XXX: this should probably be more dynamic?
+    length = ROPE_LENGTH;
+    
+    // copy position + velocity from player
+    position = player.getPosition();
+    velocity = player.getVelocity() + player.getDirection() * ROPE_VELOCITY;
+    
+    // we are FLYING
+    inAir = true;
+    
+    // enable the physics object
     enable();
 }
 
 void Rope::onCollision() {
-    this->rs = FIXED;
+    // attached to something!
+    state = ROPE_FIXED;
 
     // stop movement
     disable();
 
     // set player's pivot
-    pl.setPivot(this);
+    player.setPivot(this);
 }
 
 void Rope::release (void) {
-
-    if (this->rs == FLYING)
+    // disable if we're flying
+    if (state == ROPE_FLYING)
         disable();
 
-    // TODO make it fly first and fold only 
-    // after it's close to the player
-    this->rs = FOLDED;
+    // TODO make it fly first and fold only after it's close to the player
+    state = ROPE_FOLDED;
     
     // player doesn't have a pivot anymore
-    pl.setPivot(NULL);
+    player.setPivot(NULL);
 }
 
-void Rope::changeLength (bool shorten) {
-    this->length += shorten ? -5 : 5;
-    if (this->length < 0)
-        this->length = 0;
+void Rope::changeLength (float delta) {
+    length += delta;
+
+    if (length < 0)
+        length = 0;
 }
 
 RopeState Rope::getState (void) {
-    return this->rs;
+    return state;
 }
 
 float Rope::getPivotForce (PhysicsObject *bob) {
-    if ( (this->position - pl.getPosition()).length() >= this->length)
-        return 3500;
+    if ((position - player.getPosition()).length() >= length)
+        return ROPE_FORCE;
     else
         return 0;
 }
 
 void Rope::draw (Graphics *g) const {
-    if (rs == FOLDED)
+    if (state == ROPE_FOLDED)
         return;
 
     g->get_gc()->draw_line(
-            pl.getPosition().x, pl.getPosition().y,
+            player.getPosition().x, player.getPosition().y,
             position.x, position.y, 
             CL_Color::black
     );
--- a/src/Rope.hh	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Rope.hh	Sat Dec 06 23:29:06 2008 +0000
@@ -9,27 +9,61 @@
 #include "PhysicsObject.hh"
 #include "GraphicsPointer.hh"
 
-enum RopeState { FOLDED, FLYING, FIXED };
+enum RopeState {
+    ROPE_FOLDED, 
+    ROPE_FLYING, 
+    ROPE_FIXED 
+};
 
 class Rope : public PhysicsObject {
-private:
-    Player &pl;
-    // How long is the rope in its static state
-    float length;
-    virtual void onCollision (void);
-    RopeState rs;
-    
-protected:
-    virtual float getPivotForce (PhysicsObject *bob);
+    private:
+        // the owner
+        Player &player;
 
-public:
-    Rope(Player &climber);
+        // How long is the rope in its unstrected state
+        float length;
+        
+        // basic state
+        RopeState state;
+        
+    protected:
+        /*
+         * Attach the rope, so disable the PhysicsObject and change state
+         */
+        virtual void onCollision (void);
 
-    void shoot (void);
-    void release (void);
-    void changeLength (bool shorten);
-    RopeState getState (void);
-    virtual void draw (Graphics *c) const;
+        /*
+         * If the rope is currently longer than length, this returns ROPE_FORCE, else 0
+         */
+        virtual float getPivotForce (PhysicsObject *bob);
+
+    public:
+        Rope(Player &player);
+        
+        /*
+         * Throw the rope, so it flies up and away: o._-*
+         */
+        void throwRope (void);
+
+        /*
+         * Release the rope, so if it's currently fixed or flying, then fold it 
+         */
+        void release (void);
+
+        /*
+         * Climb up/down the rope
+         */
+        void changeLength (float delta);
+        
+        /*
+         * Current state
+         */
+        RopeState getState (void);
+        
+        /*
+         * Just draws it
+         */ 
+        virtual void draw (Graphics *c) const;
 };
 
 #endif
--- a/src/Weapon.hh	Sat Dec 06 22:51:51 2008 +0000
+++ b/src/Weapon.hh	Sat Dec 06 23:29:06 2008 +0000
@@ -36,7 +36,7 @@
     
     // get weapon parameters
     std::string getName (void) { return name; }
-    float getVelocity (void) { return velocity; }
+    float getSpeed (void) { return velocity; }
     float getExplosionRadius (void) { return explosionRadius; }
     TickCount getExpireTicks (void) { return age; }