- trampoline stuff
authorRyan Flegel <rflegel@gmail.com>
Sun, 16 May 2004 05:48:28 +0000 (05:48 +0000)
committerRyan Flegel <rflegel@gmail.com>
Sun, 16 May 2004 05:48:28 +0000 (05:48 +0000)
SVN-Revision: 1208

src/collision.h
src/gameobjs.cpp
src/gameobjs.h
src/player.cpp
src/world.cpp

index f813f2a..b9e8410 100644 (file)
@@ -31,7 +31,8 @@ enum
 {
   CO_BULLET,
   CO_BADGUY,
-  CO_PLAYER
+  CO_PLAYER,
+  CO_TRAMPOLINE
 };
 
 enum CollisionType {
index a48412f..3f23894 100644 (file)
@@ -223,7 +223,7 @@ void load_object_gfx()
   {
     char sprite_name[16];
     sprintf(sprite_name, "trampoline-%i", i+1);
-    img_trampoline[0] = sprite_manager->load(sprite_name);
+    img_trampoline[i] = sprite_manager->load(sprite_name);
   }
 }
 
@@ -238,6 +238,15 @@ Trampoline::init(float x, float y)
 }
 
 void
+Trampoline::draw()
+{
+  img_trampoline[0]->draw((int)base.x, (int)base.y);
+
+  if (debug_mode)
+    fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75, 75, 0, 150);
+}
+
+void
 Trampoline::action(double frame_ratio)
 {
   physic.apply(frame_ratio, base.x, base.y);
@@ -253,19 +262,50 @@ Trampoline::action(double frame_ratio)
   else
     physic.enable_gravity(true);
 
-  // TODO:
-  // If HELD
-  //   - move with tux
-  // If jumped on
-  //   - compress springs (reduce height)
 }
 
+// TODO:
+// If HELD
+//   - move with tux
+// If jumped on
+//   - compress springs (reduce height)
+
 void
-Trampoline::draw()
+Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
 {
-  img_trampoline[0]->draw((int)base.x, (int)base.y);
-}
+  Player* pplayer_c = NULL;
+  switch (c_object)
+  {
+    case CO_PLAYER:
+      pplayer_c = (Player*) p_c_object;
+
+      if (type == COLLISION_NORMAL)
+      {
+        // TODO: Pick up if HELD
+      }
+
+      else if (type == COLLISION_SQUISH)
+      {
+        // TODO: compress springs
+        // TODO: launch tux, if necessary
+
+        base.y = pplayer_c->base.y + pplayer_c->base.height;
+        base.height = (32 - (int)pplayer_c->base.y % 32);
+        if (base.height < 16)
+        {
+          base.height = 32;
 
+          pplayer_c->physic.set_velocity_y(8);
+        }
+      }
+
+      break;
+
+    default:
+      break;
+    
+  }
+}
 
 /* EOF */
 
index 2276faa..97a25b7 100644 (file)
@@ -27,6 +27,7 @@
 #include "timer.h"
 #include "scene.h"
 #include "physic.h"
+#include "collision.h"
 
 enum ObjectType { OBJ_NONE, OBJ_BADGUY, OBJ_TRAMPOLINE };
 
@@ -127,6 +128,8 @@ class Trampoline : public GameObject
     init(data.x, data.y);
   };
 
+  void collision(void *p_c_object, int c_object, CollisionType type);
+
   Physic physic;
 
  private:
index d285849..c04bd03 100644 (file)
@@ -18,6 +18,7 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include <math.h>
+#include <cassert>
 #include "gameloop.h"
 #include "globals.h"
 #include "player.h"
@@ -643,6 +644,7 @@ void
 Player::collision(void* p_c_object, int c_object)
 {
   BadGuy* pbad_c = NULL;
+  Trampoline* ptramp_c = NULL;
 
   switch (c_object)
     {
@@ -693,6 +695,25 @@ Player::collision(void* p_c_object, int c_object)
           player_status.score_multiplier++;
         }
       break;
+
+    case CO_TRAMPOLINE:
+      ptramp_c = (Trampoline*) p_c_object;
+      
+      if (physic.get_velocity_x() > 0) // RIGHT
+      {
+        physic.set_velocity_x(0);
+        base.x = ptramp_c->base.x - base.width;
+      }
+      else if (physic.get_velocity_x() < 0) // LEFT
+      {
+        physic.set_velocity_x(0);
+        base.x = ptramp_c->base.x + ptramp_c->base.width;
+      }
+      else
+      {
+      }
+      break;
+
     default:
       break;
     }
index 7ca3e96..7db4ed5 100644 (file)
@@ -499,6 +499,25 @@ World::collision_handler()
           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
         }
     }
+
+  // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
+  for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
+  {
+    if (rectcollision((*i)->base, tux.base))
+    {
+      if (tux.previous_base.y < tux.base.y &&
+          tux.previous_base.y + tux.previous_base.height 
+          < (*i)->base.y + (*i)->base.height/2)
+      {
+        (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
+      }
+      else
+      {
+        tux.collision(*i, CO_TRAMPOLINE);
+        (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
+      }
+    }
+  }
 }
 
 void