T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
authorsaiam
Mon, 24 Nov 2008 23:04:22 +0000
changeset 108 1b93045a5b0a
parent 107 505bfa531496
child 109 b3cd48ef2e0f
T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
src/proto2/GameState.cc
src/proto2/GameState.hh
src/proto2/Graphics.cc
src/proto2/Physics.cc
src/proto2/Physics.hh
--- a/src/proto2/GameState.cc	Mon Nov 24 22:20:32 2008 +0000
+++ b/src/proto2/GameState.cc	Mon Nov 24 23:04:22 2008 +0000
@@ -3,32 +3,45 @@
 #include "Engine.hh"
 
 void Player::handleMove (PlayerInput_Move input) {
-    float fx = 0;
+    float fx = 0; // Force in x-direction
+    float da = 0; // Crosshair angle
 
     // handle left/right
     if (input & INPUT_MOVE_LEFT)
-            fx -= PLAYER_MOVE_FORCE;
+        fx -= PLAYER_MOVE_FORCE;
 
     if (input & INPUT_MOVE_RIGHT)
-            fx += PLAYER_MOVE_FORCE;
-    
-    // we behave differently depending on if we're in the air or on the ground
-    if (inAir) {
-        // apply horizontal force
-        if (fx)
-            applyForce(Vector(fx, 0), INPUT_INTERVAL_MS);
+        fx += PLAYER_MOVE_FORCE;
 
-    } else {
-        // walk right
-        if (fx)
-            this->position = walk(fx > 0);
+    if (input & INPUT_MOVE_UP)
+        da += CROSSHAIR_ANGLE_SPEED;
+
+    if (input & INPUT_MOVE_DOWN)
+        da -= CROSSHAIR_ANGLE_SPEED;
+
+    // Player facing
+    if (fx < 0) setFacing(false);
+    else if (fx > 0) setFacing(true);
+
+    this->changeAim(da); // Move crosshair
+
+        // we behave differently depending on if we're in the air or on the ground
+        if (inAir) {
+            // apply horizontal force
+            if (fx)
+                applyForce(Vector(fx, 0), INPUT_INTERVAL_MS);
+
+        } else {
+            // walk right
+            if (fx)
+                this->position = walk(fx > 0);
         
-        // jump?
-        if (input & INPUT_MOVE_JUMP)
-            jump();    
-    }
+            // jump?
+            if (input & INPUT_MOVE_JUMP)
+                jump();    
+        }
 }
 
 void Player::debugInfo (void) {
-	Engine::log(DEBUG, "Player.debugInfo") << "In air: " << this->inAir;
+    Engine::log(DEBUG, "Player.debugInfo") << "In air: " << this->inAir;
 }
--- a/src/proto2/GameState.hh	Mon Nov 24 22:20:32 2008 +0000
+++ b/src/proto2/GameState.hh	Mon Nov 24 23:04:22 2008 +0000
@@ -6,6 +6,9 @@
 
 #include <list>
 #include <stdexcept>
+#include <cmath>
+
+const float KG_PI = 3.14159265;
 
 // in cells/kg
 const uint16_t MAP_WIDTH = 800;
@@ -16,77 +19,83 @@
 const float PLAYER_INITIAL_X = 400.0;
 const float PLAYER_INITIAL_Y = 300.0;
 
+const float CROSSHAIR_ANGLE_SPEED = PI/40; // TODO: Adjust this
+const float PLAYER_AIM_MIN = -KG_PI/2; // TODO: -- || --
+const float PLAYER_AIM_MAX = KG_PI/2; // TODO: -- || --
+
 // forward-declare GameState
 class GameState;
 
 class Player : public PhysicsObject {
-    protected:
-        GameState &state;
-        bool visible;
-
-    public:
-        Player(GameState &state, Vector position, bool visible) : 
-            PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
+protected:
+    GameState &state;
+    bool visible;
+    
+public:
+    Player(GameState &state, Vector position, bool visible) : 
+        PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
+            
+        std::vector<Vector> shape(4);
+        shape[0] = Vector(0,-18);
+        shape[1] = Vector(6,-9);
+        shape[2] = Vector(0,0); 
+        shape[3] = Vector(-6,-9);
+        // Initialize the shape of the player (salmiakki shape)
+        setShape(shape);		
+    }
+    
+    void debugInfo ();
+    
+public:
+    virtual void handleMove (PlayerInput_Move input);
+    
 
-			std::vector<Vector> shape(4);
-			shape[0] = Vector(0,-18);
-			shape[1] = Vector(6,-9);
-			shape[2] = Vector(0,0); 
-			shape[3] = Vector(-6,-9);
-			// Initialize the shape of the player (salmiakki shape)
-			setShape(shape);		
-		}
-
-		void debugInfo ();
-    
-    public:
-        virtual void handleMove (PlayerInput_Move input);
 };
 
 class LocalPlayer : public Player {
-    protected:
-        LocalPlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
+protected:
+    LocalPlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
 };
 
 class RemotePlayer : public Player {
-    protected:
-        RemotePlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
+protected:
+    RemotePlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
 };
 
 class GameState : public PhysicsWorld {
-    public:
-        std::list<Player*> player_list;
+public:
+    std::list<Player*> player_list;
 
-        // only one local player is supported
-        LocalPlayer *local_player;
+    // only one local player is supported
+    LocalPlayer *local_player;
 
-        GameState (void) : PhysicsWorld(Vector(0, MAP_GRAVITY), Vector(MAP_WIDTH, MAP_HEIGHT)), local_player(NULL) {
+    GameState (void) : PhysicsWorld(Vector(0, MAP_GRAVITY), Vector(MAP_WIDTH, MAP_HEIGHT)), local_player(NULL) {
 
-        }
+    }
        
-        /*
-         * This will return NULL if we don't have a local player - yet
-         */
-        LocalPlayer *getLocalPlayer (void) {
-            return local_player;
-        }
+    /*
+     * This will return NULL if we don't have a local player - yet
+     */
+    LocalPlayer *getLocalPlayer (void) {
+        return local_player;
+    }
         
-        void newLocalPlayer (LocalPlayer *player) {
-            if (local_player)
-                throw std::logic_error("newLocalPlayer called even though we already have a local player");
+    void newLocalPlayer (LocalPlayer *player) {
+        if (local_player)
+            throw std::logic_error("newLocalPlayer called even though we already have a local player");
 
-            player_list.push_back(player);
-
-            local_player = player;
-        }
+        player_list.push_back(player);
 
-        void newRemotePlayer (RemotePlayer *player) {
-            player_list.push_back(player);
-        }
+        local_player = player;
+    }
 
-        void removePlayer (Player *player) {
-            player_list.remove(player);
-        }
+    void newRemotePlayer (RemotePlayer *player) {
+        player_list.push_back(player);
+    }
+
+    void removePlayer (Player *player) {
+        player_list.remove(player);
+    }
 };
 
 #endif
--- a/src/proto2/Graphics.cc	Mon Nov 24 22:20:32 2008 +0000
+++ b/src/proto2/Graphics.cc	Mon Nov 24 23:04:22 2008 +0000
@@ -2,6 +2,7 @@
 #include "Graphics.hh"
 #include "Physics.hh"
 #include "GameState.hh"
+#include <cmath>
 
 Graphics::Graphics (Engine &engine, GameState &state) :
     engine(engine), 
@@ -44,9 +45,9 @@
     
     // stop on escape
     if (keyboard.get_keycode(CL_KEY_ESCAPE)) {
-            engine.stop();
+        engine.stop();
 
-            return;
+        return;
     }
      
     // ignore if we don't have a local player
@@ -55,16 +56,22 @@
     
     // handle movement
     if (keyboard.get_keycode(CL_KEY_LEFT))
-            input_move |= INPUT_MOVE_LEFT;
+        input_move |= INPUT_MOVE_LEFT;
 
     if (keyboard.get_keycode(CL_KEY_RIGHT))
-            input_move |= INPUT_MOVE_RIGHT;
+        input_move |= INPUT_MOVE_RIGHT;
+
+    if (keyboard.get_keycode(CL_KEY_UP))
+        input_move |= INPUT_MOVE_UP;
+
+    if (keyboard.get_keycode(CL_KEY_DOWN))
+        input_move |= INPUT_MOVE_DOWN;
 
     if (keyboard.get_keycode(CL_KEY_RSHIFT))
-            input_move |= INPUT_MOVE_JUMP;
+        input_move |= INPUT_MOVE_JUMP;
 
-	if (keyboard.get_keycode(CL_KEY_I))
-		player->debugInfo();
+    if (keyboard.get_keycode(CL_KEY_I))
+        player->debugInfo();
     
     // apply movement if applicable
     if (input_move)
@@ -87,17 +94,26 @@
     for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
         Player *p = *it;
         
-		Vector loc = p->getPosition();
-		std::vector<Vector> &shape = p->getShape();
+        Vector loc = p->getPosition();
+        bool right = p->getFacing();
+        float aim = p->getAim();
+        std::vector<Vector> &shape = p->getShape();
         // draw quad
         gc->fill_quad(
-            CL_Quad(
-                (loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY,
-                (loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY,
-                (loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY,
-                (loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY
-           	), CL_Color::green
-        );
+                      CL_Quad(
+                              (loc.x+shape[0].x) * factorX, (loc.y+shape[0].y) * factorY,
+                              (loc.x+shape[1].x) * factorX, (loc.y+shape[1].y) * factorY,
+                              (loc.x+shape[2].x) * factorX, (loc.y+shape[2].y) * factorY,
+                              (loc.x+shape[3].x) * factorX, (loc.y+shape[3].y) * factorY
+                              ), CL_Color::green
+                      );
+
+        // Draw crosshair
+        if (right) {
+            gc->draw_line(loc.x, loc.y, loc.x + std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black);
+        } else {
+            gc->draw_line(loc.x, loc.y, loc.x - std::cos(aim)*5, loc.y - std::sin(aim)*5, CL_Color::black);
+        }
     }
 
     // flip window buffer, LIEK NAO
--- a/src/proto2/Physics.cc	Mon Nov 24 22:20:32 2008 +0000
+++ b/src/proto2/Physics.cc	Mon Nov 24 23:04:22 2008 +0000
@@ -35,7 +35,7 @@
 }
 
 PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity)
-	: world(world), mass(mass), position(position), velocity(velocity) {
+    : world(world), mass(mass), position(position), velocity(velocity), facing(3) {
 
 	this->inAir = true;
     world.addObject(this);
@@ -400,20 +400,28 @@
     return (force/mass);
 }
 
-/**
- * Adds force to the force queue. Force queue is emptied on each
- * tick. Forces that last over one tick are also handled.
- * @param force Force vector.
- * @param dt The time the force is applied.
- */
 void PhysicsObject::applyForce (Force force, TimeMS dt) {
-    // XXX: dt is not used? Is it assumed to be the same as the integrate() dt?
+    // XXX: dt is not used? It is here because we might want forces to
+    // apply longer than one tick in the future.
     
     // Add applied force to the queue
     forceq.push(force);
 	this->inAir = true;
 }
 
+void PhysicsObject::changeAim(float da) {
+    this->aim += da;
+
+    if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX;
+    if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN;
+    Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim;
+}
+
+void PhysicsObject::setFacing(bool right) {
+    Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right;
+    this->facing = right;
+}
+
 void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) {
     this->position = position;
     this->velocity = velocity;
@@ -424,6 +432,14 @@
     return this->position;
 }
 
+bool PhysicsObject::getFacing() {
+    return this->facing;
+}
+
+float PhysicsObject::getAim() {
+    return this->aim;
+}
+
 std::vector<Vector>& PhysicsObject::getShape () {
     return this->shape;
 }
--- a/src/proto2/Physics.hh	Mon Nov 24 22:20:32 2008 +0000
+++ b/src/proto2/Physics.hh	Mon Nov 24 23:04:22 2008 +0000
@@ -67,28 +67,46 @@
     // Whether the object (worms mainly) is in the air 
     // or firmly on the ground. Affects to physics.
     bool inAir;
+    float aim;
+    uint8_t facing;
 
-	// Shape of the object. We use a polygon with 4 edges
-	// to make easy to draw with Clanlib. The coordinates
-	// are relative to the center point.
-	std::vector<Vector> shape;
-
+    // Shape of the object. We use a polygon with 4 edges
+    // to make easy to draw with Clanlib. The coordinates
+    // are relative to the center point.
+    std::vector<Vector> shape;
+    
     // Force queue that is emptied on every tick
     std::queue<Force> forceq;
     Vector posAfterTick;
     Vector velAfterTick;
 
-	/**
-	 * @param shape Corners of the four sided polygon.
-	 */
+    /**
+     * @param shape Corners of the four sided polygon.
+     */
     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
-    
+   
+
     /**
-     * Used to handle in-air movement
-     */    
+     * Adds force to the force queue. Force queue is emptied on each
+     * tick. Forces that last over one tick are also handled. This
+     * function is only used to handle in air movement.
+     *
+     * @param force Force vector.  
+     * @param dt The time the force is applied.
+     */
     virtual void applyForce (Force force, TimeMS dt);
 
     /**
+     * Changes player aim
+     */
+    void changeAim(float da);
+
+    /**
+     * Set player facing.
+     */ 
+    void setFacing(bool right);
+
+    /**
      * Called on network clients to sync state from server
      */
     void updatePhysics (Vector position, Vector velocity, bool inAir);
@@ -123,9 +141,12 @@
 
 public:
     Vector getPosition (void);
-	std::vector<Vector>& getShape(void);
-	void setShape (std::vector<Vector> shape);
-    
+    std::vector<Vector>& getShape(void);
+    void setShape (std::vector<Vector> shape);
+
+    bool getFacing(void);
+    float getAim(void);
+
     void tick (void);
 };