Digging now uses projectiles.
authorsaiam
Fri, 05 Dec 2008 00:30:59 +0000
changeset 208 7709571e1131
parent 207 00d4a679de6c
child 209 68cc4248a508
Digging now uses projectiles.
src/PhysicsObject.hh
src/PhysicsWorld.cc
src/PhysicsWorld.hh
src/Player.cc
src/Projectile.cc
src/Projectile.hh
--- a/src/PhysicsObject.hh	Thu Dec 04 23:44:45 2008 +0000
+++ b/src/PhysicsObject.hh	Fri Dec 05 00:30:59 2008 +0000
@@ -22,7 +22,7 @@
 class PhysicsObject {
 protected:
     // This probably shouldn't be done this way.
-    PhysicsWorld &world;
+
 
     Vector position;
     Vector velocity;
@@ -152,6 +152,8 @@
     bool possibleLocation(Vector loc);
 
 public:
+    PhysicsWorld &world;
+
     /**
      * Get current object position.
      *
--- a/src/PhysicsWorld.cc	Thu Dec 04 23:44:45 2008 +0000
+++ b/src/PhysicsWorld.cc	Fri Dec 05 00:30:59 2008 +0000
@@ -24,7 +24,7 @@
  */
 bool isDestroyedProjectile (Projectile* po) { 
     //    return po->isDestroyed();
-    if (po->isDestroyed()) {
+    if (po->isDestroyed() || (po->world.tick_timer.get_ticks() > (po->birth_tick + po->age))) {
         Engine::log(DEBUG, "PhysicsWorld.isDestroyedProjectile") << "Destroying projectile: " << po;
         delete po;
         return true;
--- a/src/PhysicsWorld.hh	Thu Dec 04 23:44:45 2008 +0000
+++ b/src/PhysicsWorld.hh	Fri Dec 05 00:30:59 2008 +0000
@@ -26,10 +26,8 @@
     friend class PhysicsObject;
 
 private:
-    Timer tick_timer;
 
-    //    Terrain terrain;
-
+    
 protected:
     std::list<PhysicsObject*> players;
     std::list<Projectile*> projectiles;
@@ -46,6 +44,10 @@
 
 public:
 
+    // Someone is going to kill me for this
+    Timer tick_timer;
+
+
     PhysicsWorld(Vector gravity, Vector dimensions);
 
     // TODO: Replace addObject with these?
--- a/src/Player.cc	Thu Dec 04 23:44:45 2008 +0000
+++ b/src/Player.cc	Fri Dec 05 00:30:59 2008 +0000
@@ -29,7 +29,7 @@
     float shotspeed = 100*PHYSICS_TICK_MS/2;
     Vector shotRelativeVelocity = unitVectorAim * shotspeed;
     Vector shotVelocity = this->velocity + shotRelativeVelocity;
-    new Projectile(this->state, this->position, shotVelocity, true);
+    new Projectile(this->state, this->position, shotVelocity, true, 20);
 }
 
 void Player::handleMove (PlayerInput_Move input) {
@@ -86,7 +86,12 @@
 }
 
 void Player::handleDig (Vector position, float radius) {
-    world.removeGround(position, radius);
+    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
+            Vector(-std::cos(aim), -std::sin(aim));
+    float shotspeed = 40*PHYSICS_TICK_MS;
+    Vector shotVelocity = unitVectorAim*shotspeed;
+    new Projectile(this->state, this->position, shotVelocity, false, radius, 1);
+    //   world.removeGround(position, radius);
 }
 
 void Player::debugInfo (void) {
--- a/src/Projectile.cc	Thu Dec 04 23:44:45 2008 +0000
+++ b/src/Projectile.cc	Fri Dec 05 00:30:59 2008 +0000
@@ -1,7 +1,9 @@
 #include "Projectile.hh"
+#include "Timer.hh"
 
-Projectile::Projectile(GameState &state, Vector position, Vector velocity, bool visible) :
-    PhysicsObject(state.world, PLAYER_MASS, position, velocity), state(state), visible(visible), destroyed(false) {
+Projectile::Projectile(GameState &state, Vector position, Vector velocity, bool visible, float radius, TickCount age) :
+    PhysicsObject(state.world, PLAYER_MASS, position, velocity), state(state), visible(visible), radius(radius), destroyed(false), age(age) {
+    birth_tick = world.tick_timer.get_ticks();
     // Looks kind of dumb, for ammunition to have shape
     std::vector<Vector> shape(4);
     shape[0] = Vector(-1, -1);
@@ -16,7 +18,7 @@
 
  
 void Projectile::onCollision() {
-    world.removeGround(position, 20);
+    world.removeGround(position, radius);
     this->destroyed = true;
 }
 
@@ -25,31 +27,32 @@
 }
 
 void Projectile::draw(CL_GraphicContext *gc) {
-
+    if (visible) {
   
-    CL_Quad player(
-                   (int)((position).x+1), (int)((position).y+1),
-                   (int)((position).x-1), (int)((position).y+1),
-                   (int)((position).x+1), (int)((position).y-1),
-                   (int)((position).x-1), (int)((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);
+        CL_Quad projectile(
+                           (int)((position).x+1), (int)((position).y+1),
+                           (int)((position).x-1), (int)((position).y+1),
+                           (int)((position).x+1), (int)((position).y-1),
+                           (int)((position).x-1), (int)((position).y-1)
+                           );
+        
+        gc->fill_quad(projectile, CL_Color::green);
+        
+        const uint16_t chlen = 10;
+        uint16_t x = projectile.center().x;
+        uint16_t y = projectile.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/Projectile.hh	Thu Dec 04 23:44:45 2008 +0000
+++ b/src/Projectile.hh	Fri Dec 05 00:30:59 2008 +0000
@@ -5,17 +5,21 @@
 
 #include "PhysicsObject.hh"
 #include "GameState.hh"
+#include "Timer.hh"
 
 class Projectile : public PhysicsObject {
 protected:
     GameState &state;
     bool visible;
-    uint32_t birth_tick;
-    uint32_t death_tick;
     bool target_visible;
     bool destroyed;
+    float radius;
 public:
-    Projectile(GameState &state, Vector position, Vector velocity, bool visible);
+
+    TickCount birth_tick;
+    TickCount age;
+
+    Projectile(GameState &state, Vector position, Vector velocity, bool visible, float radius, TickCount age=1000000000);
     ~Projectile() {};
 
     bool isDestroyed (void);