- added stay-on-platform into the level fileformat
[supertux.git] / src / badguy.cpp
index 4c30fe6..2d1de29 100644 (file)
@@ -1,14 +1,24 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
+//  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+//  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
 //
-// C Implementation: badguy
-//
-// Description:
-//
-//
-// Author: Tobias Glaesser <tobi.web@gmx.de> & Bill Kendrick, (C) 2004
-//
-// Copyright: See COPYING file that comes with this distribution
-//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
 //
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+//  02111-1307, USA.
 
 #include <iostream>
 #include <math.h>
@@ -134,7 +144,7 @@ std::string badguykind_to_string(BadGuyKind kind)
 }
 
 void
-BadGuy::init(float x, float y, BadGuyKind kind_)
+BadGuy::init(float x, float y, BadGuyKind kind_, bool stay_on_platform_)
 {
   base.x   = x;
   base.y   = y;    
@@ -143,6 +153,7 @@ BadGuy::init(float x, float y, BadGuyKind kind_)
   base.xm  = 0;
   base.ym  = 0;
 
+  stay_on_platform = stay_on_platform_;
   mode     = NORMAL;
   dying    = DYING_NOT;
   kind     = kind_;
@@ -163,7 +174,7 @@ BadGuy::init(float x, float y, BadGuyKind kind_)
     physic.set_velocity(-1.3, 0);
     set_sprite(img_mrbomb_left, img_mrbomb_right, 4);
   } else if (kind == BAD_LAPTOP) {
-    physic.set_velocity(-1.3, 0);
+    physic.set_velocity(-.8, 0);
     set_sprite(img_laptop_left, img_laptop_right, 4, 5);
   } else if(kind == BAD_MONEY) {
     set_sprite(img_jumpy_left_up, img_jumpy_left_up, 1);
@@ -215,10 +226,11 @@ BadGuy::action_bsod(float frame_ratio)
 
   // jump when we're about to fall
   if (physic.get_velocity_y() == 0 && 
-          !issolid(base.x+base.width/2, base.y + base.height)) {
-    physic.enable_gravity(true);
-    physic.set_velocity(physic.get_velocity_x(), BSODJUMP);
-  }
+      !issolid(base.x+base.width/2, base.y + base.height)) 
+    {
+      physic.enable_gravity(true);
+      physic.set_velocity(physic.get_velocity_x(), BSODJUMP);
+    }
 
   // Handle dying timer:
   if (dying == DYING_SQUISHED && !timer.check())
@@ -280,7 +292,7 @@ BadGuy::action_laptop(float frame_ratio)
 
           mode=KICK;
           set_sprite(img_laptop_flat_left, img_laptop_flat_right, 1);
-          physic.set_velocity((dir == LEFT) ? -8 : 8, -8);
+          physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5);
           play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER);
         }
     }
@@ -308,7 +320,7 @@ BadGuy::action_laptop(float frame_ratio)
         {
           mode = NORMAL;
           set_sprite(img_laptop_left, img_laptop_right, 4, 5);
-          physic.set_velocity( (dir == LEFT) ? -1.3 : 1.3, 0);
+          physic.set_velocity( (dir == LEFT) ? -.8 : .8, 0);
         }
     }
 }
@@ -368,10 +380,23 @@ BadGuy::fall()
           if (physic.get_velocity_y() < 0)
             {
               base.y = int((base.y + base.height)/32) * 32 - base.height;
-              physic.set_velocity(physic.get_velocity_x(), 0);
+              physic.set_velocity_y(0);
             }
           // no gravity anymore please
           physic.enable_gravity(false);
+
+          if (stay_on_platform && mode == NORMAL)
+            {
+              if (!issolid(base.x + ((dir == LEFT) ? 0 : base.width),
+                           base.y + base.height))
+                {
+                  physic.set_velocity_x(-physic.get_velocity_x());
+                  if (dir == LEFT)
+                    dir = RIGHT;
+                  else
+                    dir = LEFT;
+                }
+            }
         }
     }
   else
@@ -411,7 +436,7 @@ BadGuy::action_money(float frame_ratio)
   // jump when on ground
   if(dying == DYING_NOT && issolid(base.x, base.y+32))
     {
-      physic.set_velocity(physic.get_velocity_x(), JUMPV);
+      physic.set_velocity_y(JUMPV);
       physic.enable_gravity(true);
 
       mode = MONEY_JUMP;
@@ -571,7 +596,7 @@ BadGuy::action_bouncingsnowball(float frame_ratio)
   // jump when on ground
   if(dying == DYING_NOT && issolid(base.x, base.y+32))
     {
-      physic.set_velocity(physic.get_velocity_x(), JUMPV);
+      physic.set_velocity_y(JUMPV);
       physic.enable_gravity(true);
     }                                                     
   else
@@ -604,17 +629,17 @@ BadGuy::action_flyingsnowball(float frame_ratio)
   // go into flyup mode if none specified yet
   if(dying == DYING_NOT && mode == NORMAL) {
     mode = FLY_UP;
-    physic.set_velocity(physic.get_velocity_x(), FLYINGSPEED);
+    physic.set_velocity_y(FLYINGSPEED);
     timer.start(DIRCHANGETIME/2);
   }
 
   if(dying == DYING_NOT && !timer.check()) {
     if(mode == FLY_UP) {
       mode = FLY_DOWN;
-      physic.set_velocity(physic.get_velocity_x(), -FLYINGSPEED);
+      physic.set_velocity_y(-FLYINGSPEED);
     } else if(mode == FLY_DOWN) {
       mode = FLY_UP;
-      physic.set_velocity(physic.get_velocity_x(), FLYINGSPEED);
+      physic.set_velocity_y(FLYINGSPEED);
     }
     timer.start(DIRCHANGETIME);
   }
@@ -647,7 +672,7 @@ BadGuy::action_spiky(float frame_ratio)
   if (physic.get_velocity_y() == 0 && 
           !issolid(base.x+base.width/2, base.y + base.height)) {
     physic.enable_gravity(true);
-    physic.set_velocity(physic.get_velocity_x(), 2);
+    physic.set_velocity_y(2);
   }
 #endif
 
@@ -816,7 +841,7 @@ BadGuy::bump()
 void
 BadGuy::make_player_jump(Player* player)
 {
-  player->physic.set_velocity(player->physic.get_velocity_x(), 2);
+  player->physic.set_velocity_y(2);
   player->base.y = base.y - player->base.height - 2;
 }
 
@@ -853,7 +878,7 @@ BadGuy::squish(Player* player)
   } else if(kind == BAD_BSOD) {
     squish_me(player);
     set_sprite(img_bsod_squished_left, img_bsod_squished_right, 1);
-    physic.set_velocity(0, physic.get_velocity_y());
+    physic.set_velocity_x(0);
     return;
       
   } else if (kind == BAD_LAPTOP) {
@@ -863,7 +888,7 @@ BadGuy::squish(Player* player)
         play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
         mode = FLAT;
         set_sprite(img_laptop_flat_left, img_laptop_flat_right, 1);
-        physic.set_velocity(0, physic.get_velocity_y());
+        physic.set_velocity_x(0);
 
         timer.start(4000);
       } else if (mode == FLAT) {
@@ -871,10 +896,10 @@ BadGuy::squish(Player* player)
         play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
 
         if (player->base.x < base.x + (base.width/2)) {
-          physic.set_velocity(5, physic.get_velocity_y());
+          physic.set_velocity_x(5);
           dir = RIGHT;
         } else {
-          physic.set_velocity(-5, physic.get_velocity_y());
+          physic.set_velocity_x(-5);
           dir = LEFT;
         }
 
@@ -928,7 +953,7 @@ BadGuy::kill_me()
     set_sprite(img_bsod_falling_left, img_bsod_falling_right, 1);
   
   physic.enable_gravity(true);
-  physic.set_velocity(physic.get_velocity_x(), 0);
+  physic.set_velocity_y(0);
 
   /* Gain some points: */
   if (kind == BAD_BSOD)
@@ -957,6 +982,7 @@ BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
     return;
   }
 
+  /* COLLISION_NORMAL */
   switch (c_object)
     {
     case CO_BULLET:
@@ -965,15 +991,73 @@ BadGuy::collision(void *p_c_object, int c_object, CollisionType type)
 
     case CO_BADGUY:
       pbad_c = (BadGuy*) p_c_object;
+
+      /* If we're a kicked mriceblock, kill any badguys we hit */
       if(kind == BAD_LAPTOP && mode == KICK &&
             pbad_c->kind != BAD_FLAME && pbad_c->kind != BAD_BOMB)
         {
-          /* We're in kick mode, kill the other guy
-            and yourself(wuahaha) : */
           pbad_c->kill_me();
-          kill_me();
         }
+
+      /* Kill badguys that run into exploding bomb */
+      else if (kind == BAD_BOMB && dying == DYING_NOT)
+      {
+        if (pbad_c->kind == BAD_MRBOMB)
+        {
+          // FIXME: this is where other MrBombs *should* explode istead of dying
+          pbad_c->kill_me(); 
+        }
+        else if (pbad_c->kind != BAD_BOMB)
+        {
+          pbad_c->kill_me();
+        }
+      }
+
+      /* Kill any badguys that get hit by stalactite */
+      else if (kind == BAD_STALACTITE && dying == DYING_NOT)
+      {
+        pbad_c->kill_me();
+      }
+
+      /* When enemies run into eachother, make them change directions */
+      else
+      {
+        // Jumpy is an exception
+        if (pbad_c->kind == BAD_MONEY)
+          break;
+        if (dir == LEFT)
+          dir = RIGHT;
+        else if (dir == RIGHT)
+          dir = LEFT;
+
+        physic.inverse_velocity_x();
+      }
+      
+      break;
+
+    case CO_PLAYER:
+      Player* player = static_cast<Player*>(p_c_object);
+      /* Get kicked if were flat */
+      if (mode == FLAT && !dying)
+      {
+        play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
+
+        // Hit from left side
+        if (player->base.x < base.x) {
+          physic.set_velocity_x(5);
+          dir = RIGHT;
+        }
+        // Hit from right side
+        else {
+          physic.set_velocity_x(-5);
+          dir = LEFT;
+        }
+
+        mode = KICK;
+        set_sprite(img_laptop_flat_left, img_laptop_flat_right, 1);
+      }
       break;
+
     }
 }