PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
authornireco
Wed, 03 Dec 2008 14:25:23 +0000
changeset 182 84675387ca74
parent 181 62e0c2cfb714
child 183 e31ff2152774
PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
src/proto2/Config.hh
src/proto2/GameState.cc
src/proto2/GameState.hh
src/proto2/Graphics.cc
src/proto2/Input.hh
src/proto2/Physics.cc
src/proto2/Physics.hh
src/proto2/Terrain.cc
src/proto2/Terrain.hh
--- a/src/proto2/Config.hh	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/Config.hh	Wed Dec 03 14:25:23 2008 +0000
@@ -19,7 +19,7 @@
 
 // Constants affecting physics
 const float MAP_GRAVITY = 1200.0;
-const float COLLISION_ELASTICITY = 0.3; // TODO: This could be
+const float PLAYER_COLLISION_ELASTICITY = 0.3; // TODO: This could be
                                         // different for different
                                         // objects
 
--- a/src/proto2/GameState.cc	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/GameState.cc	Wed Dec 03 14:25:23 2008 +0000
@@ -1,9 +1,23 @@
 
 #include "GameState.hh"
 #include "Engine.hh"
+#include "Config.hh"
 
+/**
+ * shoots the selected weapon.
+ * TODO: selection and weapon information
+ */
 void Player::shoot (void) {
-    this->state.addProjectile(new Shot(this->state, this->position, true));
+    // here should be somehow considered which projectile it is
+    if(!canShoot())
+        return;
+    reloadTimer += 0;
+    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
+            Vector(-std::cos(aim), -std::sin(aim));
+    float shotspeed = 100*PHYSICS_TICK_MS;
+    Vector shotRelativeVelocity = unitVectorAim * shotspeed;
+    Vector shotVelocity = this->velocity + shotRelativeVelocity;
+    this->state.addProjectile(new Shot(this->state, this->position, shotVelocity, true));
 }
 
 void Player::handleMove (PlayerInput_Move input) {
@@ -50,6 +64,10 @@
 
         world.removeGround(position, 15);
     }
+
+    if (input & INPUT_SHOOT) {
+        this->shoot();
+    }
 }
 
 void Player::debugInfo (void) {
@@ -57,5 +75,36 @@
 }
 
 void Shot::onCollision() {
-//    world.removeGround(position, 20);
+    world.removeGround(position, 20);
 }
+
+void Shot::draw(CL_GraphicContext *gc) {
+
+
+    CL_Quad player(
+                   (position).x+1, (position).y+1,
+                   (position).x-1, (position).y+1,
+                   (position).x+1, (position).y-1,
+                   (position).x-1, (position).y-1
+                   );
+
+    gc->fill_quad(player, CL_Color::green);
+
+    const uint16_t chlen = 10;
+    uint16_t x = player.center().x;
+    uint16_t y = player.center().y;
+    if(target_visible) {
+        if (facingRight) {
+            gc->draw_line(x, y,
+                          x + std::cos(aim)*chlen,
+                          y - std::sin(aim)*chlen,
+                          CL_Color::black);
+        } else {
+            gc->draw_line(x, y,
+                          x - std::cos(aim)*chlen,
+                          y - std::sin(aim)*chlen,
+                          CL_Color::black);
+        }
+    }
+}
+
--- a/src/proto2/GameState.hh	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/GameState.hh	Wed Dec 03 14:25:23 2008 +0000
@@ -27,7 +27,8 @@
         shape[2] = Vector(0,9); 
         shape[3] = Vector(-6,0);
         // Initialize the shape of the player (salmiakki shape)
-        setShape(shape);		
+        setShape(shape);
+        collision_elasticity = PLAYER_COLLISION_ELASTICITY;
     }
     
     void debugInfo ();
@@ -42,9 +43,10 @@
     bool visible;
     uint32_t birth_tick;
     uint32_t death_tick;
+    bool target_visible;
 public:
-    Shot(GameState &state, Vector position, bool visible) :
-        PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
+    Shot(GameState &state, Vector position, Vector velocity, bool visible) :
+        PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, velocity), state(state), visible(visible) {
         // Looks kind of dumb, for ammunition to have shape
         std::vector<Vector> shape(4);
         shape[0] = Vector(-1, -1);
@@ -52,9 +54,12 @@
         shape[2] = Vector(1, 1);
         shape[3] = Vector(1, -1);
         setShape(shape);
+        target_visible = true;
+        collision_elasticity = 0.9; // = shotType.elasticity
     }
 private:
     virtual void onCollision();
+    virtual void draw(CL_GraphicContext *gc);
 };
 
 class LocalPlayer : public Player {
--- a/src/proto2/Graphics.cc	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/Graphics.cc	Wed Dec 03 14:25:23 2008 +0000
@@ -54,7 +54,7 @@
     
     if (keyboard.get_keycode(CL_KEY_F)) {
         Engine::log(DEBUG, "Graphics.check_input") << "Fire!";
-        player->shoot();
+        input_move |= INPUT_SHOOT;
     }
    
     if (keyboard.get_keycode(CL_KEY_M))
@@ -72,19 +72,7 @@
     gc->clear(CL_Color::white);
 
     // Draw terrain
-    state.drawTerrain(gc);
-
-    // Draw players
-    for (std::list<Player*>::iterator it = state.player_list.begin(); it != state.player_list.end(); it++) {
-        Player *p = *it;
-        p->draw(gc);
-    }        
-
-    // Draw projectiles
-    for (std::list<PhysicsObject*>::iterator it = state.projectiles.begin(); it != state.projectiles.end(); it++) {
-        PhysicsObject *po = *it;
-        po->draw(gc);
-    }        
+    state.draw(gc);
 
     // Flip window buffer, LIEK NAO
     win.flip(0);
--- a/src/proto2/Input.hh	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/Input.hh	Wed Dec 03 14:25:23 2008 +0000
@@ -13,6 +13,7 @@
 
     INPUT_MOVE_JUMP     = 0x0010,
     INPUT_MOVE_DIG      = 0x0020,
+    INPUT_SHOOT         = 0x0040,
 };
 
 typedef uint16_t PlayerInput_Move;
--- a/src/proto2/Physics.cc	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/Physics.cc	Wed Dec 03 14:25:23 2008 +0000
@@ -17,7 +17,7 @@
 }
 
 void PhysicsWorld::addObject (PhysicsObject *object) {
-    objects.push_back(object);
+    players.push_back(object);
 }
 
 void PhysicsWorld::addProjectile (PhysicsObject *projectile) {
@@ -27,7 +27,7 @@
 void PhysicsWorld::tick () {
     //    Engine::log(DEBUG, "physics.apply_force") << "*tick*";
 
-    for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) {
+    for (std::list<PhysicsObject*>::iterator i = players.begin(); i != players.end(); i++) {
         (*i)->tick(); 
     }
 
@@ -38,10 +38,23 @@
     return tick_counter;
 }
 
+void PhysicsWorld::draw(CL_GraphicContext *gc) {
+    Terrain::draw(gc);
+   
+    // Draw players
+    for (std::list<PhysicsObject*>::iterator it = players.begin(); it != players.end(); it++) {
+        (*it)->draw(gc);
+    }
+    // Draw projectiles
+    for (std::list<PhysicsObject*>::iterator it = projectiles.begin(); it != projectiles.end(); it++) {
+        (*it)->draw(gc);
+    }
+}
+
 PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, 
                               Vector position, Vector velocity)
     : world(world), position(position), velocity(velocity),
-      mass(mass), inAir(true), aim(0), facingRight(true) {
+      mass(mass), inAir(true), aim(0), facingRight(true), reloadTimer(0) {
     // TODO: Is thir the right way to do this?
     world.addObject(this);
 }
@@ -138,6 +151,11 @@
  */   
 void PhysicsObject::updatePosition () {
 
+    // Reloads weapon if not reloaded
+    reloadTimer -= PHYSICS_TICK_MS;
+    if(reloadTimer < 0)
+        reloadTimer = 0;
+
     // Add gravity to the force queue
     forceq.push(world.gravity);
     
@@ -264,7 +282,7 @@
         nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal);
         velocity = nvel;
         // We lose some of our speed on collision
-        this->velocity *= COLLISION_ELASTICITY;
+        this->velocity *= this->collision_elasticity;
     }
 }
 
@@ -356,6 +374,10 @@
     this->updatePosition();
 }
 
+bool PhysicsObject::canShoot() {
+    return this->reloadTimer <= 0;
+}
+
 void PhysicsObject::draw(CL_GraphicContext *gc) {
     CL_Quad player(
                    (position+shape[0]).x, (position+shape[0]).y,
--- a/src/proto2/Physics.hh	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/Physics.hh	Wed Dec 03 14:25:23 2008 +0000
@@ -37,8 +37,9 @@
     //    Terrain terrain;
 
 protected:
-    //std::vector<PlayerObject*> players;
-    std::vector<PhysicsObject*> objects;
+    std::list<PhysicsObject*> players;
+    std::list<PhysicsObject*> projectiles;
+//    std::vector<PhysicsObject*> objects;
 
     // Contains connections between signals and slots
     CL_SlotContainer slots;
@@ -77,9 +78,9 @@
      */
     uint32_t getTick();
 
+    virtual void draw(CL_GraphicContext *gc);
 
     // TODO This should probably be protected or in GameStat or in GameStatee
-    std::list<PhysicsObject*> projectiles;
 };
 
 /**
@@ -94,11 +95,15 @@
     Vector velocity;
     float mass;
     bool inAir; // Is the object "on the ground"
+    float collision_elasticity;
 
     // Attributes for players
     float aim; // Aim direction (half circle)
     bool facingRight; // Player facing
 
+    //
+    int reloadTimer;
+
     PhysicsObject(PhysicsWorld &world, float mass, Vector position, 
                   Vector velocity);
     ~PhysicsObject() {}
@@ -252,11 +257,17 @@
     void tick();
 
     /**
+     * @return whether this PhysicsObject can shoot or not
+     * This is in PhysicsObject for larpa-like shots
+     */
+    bool canShoot();
+
+    /**
      * Draw object
      *
      * @param gc CL_GraphicContext
      */
-    void draw(CL_GraphicContext *gc);
+    virtual void draw(CL_GraphicContext *gc);
 };
 
 // TODO: This could probably be moved somewhere else or removed
--- a/src/proto2/Terrain.cc	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/Terrain.cc	Wed Dec 03 14:25:23 2008 +0000
@@ -272,7 +272,7 @@
     this->generatePixelBuffer();
 }
 
-void Terrain::drawTerrain(CL_GraphicContext *gc) {
+void Terrain::draw(CL_GraphicContext *gc) {
     CL_Surface surf(this->pixbuf);
     surf.draw(0,0,gc);
 }
--- a/src/proto2/Terrain.hh	Wed Dec 03 12:21:38 2008 +0000
+++ b/src/proto2/Terrain.hh	Wed Dec 03 14:25:23 2008 +0000
@@ -140,7 +140,7 @@
      *
      * @param gc CL_GraphicContext
      */
-    void drawTerrain(CL_GraphicContext *gc);
+    virtual void draw(CL_GraphicContext *gc);
     /**
      * Draw part of the terrain for given graphiscontext.
      *