nolok dies properly now :)
[supertux.git] / src / player.cpp
index 9200ae6..7dd8084 100644 (file)
@@ -16,7 +16,6 @@
 //  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 <config.h>
 
 #include <typeinfo>
@@ -74,7 +73,7 @@ PlayerKeymap::PlayerKeymap()
   keymap.right = SDLK_RIGHT;
 
   keymap.power = SDLK_LCTRL;
-  keymap.jump  = SDLK_LALT;
+  keymap.jump  = SDLK_SPACE;
 }
 
 void player_input_init(player_input_type* pplayer_input)
@@ -92,29 +91,16 @@ void player_input_init(player_input_type* pplayer_input)
 }
 
 void
-TuxBodyParts::set_action(std::string action)
-{
-  if(head != NULL)
-    head->set_action(action);
-  if(body != NULL)
-    body->set_action(action);
-  if(arms != NULL)
-    arms->set_action(action);
-  if(feet != NULL)
-    feet->set_action(action);
-}
-
-void
-TuxBodyParts::one_time_animation()
+TuxBodyParts::set_action(std::string action, int loops)
 {
   if(head != NULL)
-    head->start_animation(1);
+    head->set_action(action, loops);
   if(body != NULL)
-    body->start_animation(1);
+    body->set_action(action, loops);
   if(arms != NULL)
-    arms->start_animation(1);
+    arms->set_action(action, loops);
   if(feet != NULL)
-    feet->start_animation(1);
+    feet->set_action(action, loops);
 }
 
 void
@@ -176,13 +162,8 @@ Player::init()
 
   on_ground_flag = false;
 
-  frame_main = 0;
-  frame_ = 0;
-
   player_input_init(&input);
 
-  frame_timer.start(.025, true);
-
   physic.reset();
 }
 
@@ -266,31 +247,12 @@ Player::action(float elapsed_time)
   if (input.fire == UP)
     holding_something = false;
 
-  /* --- HANDLE TUX! --- */
   if(dying == DYING_NOT)
     handle_input();
 
   movement = physic.get_movement(elapsed_time);
 
-  if(dying == DYING_NOT) 
-    {
 #if 0
-      collision_swept_object_map(&old_base, &base);
-
-      if ((!invincible_timer.started() && !safe_timer.started())
-          && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
-          ||  isspike(base.x, base.y + base.height)
-          ||  isspike(base.x + base.width, base.y + base.height)))
-      {
-         kill(SHRINK);
-      }
-
-      // Don't accelerate Tux if he is running against a wall
-      if (target.x != base.x)
-      {
-        physic.set_velocity_x(0);
-      }
-
       // special exception for cases where we're stuck under tiles after
       // being ducked. In this case we drift out
       if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
@@ -300,31 +262,8 @@ Player::action(float elapsed_time)
           previous_base = old_base = base;
         }
 
-      // Land:
-      if (!on_ground())
-        {
-          physic.enable_gravity(true);
-          if(under_solid())
-            {
-              // fall down
-              physic.set_velocity_y(0);
-              jumped_in_solid = true;
-              jumping = false;
-              flapping = false;
-            }
-        }
-      else
-        {
-          /* Land: */
-          if (physic.get_velocity_y() < 0)
-            {
-              base.y = (int)(((int)base.y / 32) * 32);
-              physic.set_velocity_y(0);
-            }
-
-          physic.enable_gravity(false);
-          /* Reset score multiplier (for multi-hits): */
-          if (!invincible_timer.started())
+      /* Reset score multiplier (for multi-hits): */
+      if (!invincible_timer.started())
             {
             if(player_status.score_multiplier > player_status.max_score_multiplier)
               {
@@ -339,55 +278,8 @@ Player::action(float elapsed_time)
             }
         }
 
-      if(jumped_in_solid)
-        {
-          if (isbrick(base.x, base.y) ||
-              isfullbox(base.x, base.y))
-            {
-             Sector::current()->trygrabdistro(
-                  Vector(base.x, base.y - 32), BOUNCE);
-              Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
-
-              Sector::current()->trybreakbrick(
-                  Vector(base.x, base.y), size == SMALL);
-
-              bumpbrick(base.x, base.y);
-              Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
-            }
-
-          if (isbrick(base.x+ 31, base.y) ||
-              isfullbox(base.x+ 31, base.y))
-            {
-             Sector::current()->trygrabdistro(
-                  Vector(base.x+ 31, base.y - 32), BOUNCE);
-              Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
-
-              if(size == BIG)
-                Sector::current()->trybreakbrick(
-                    Vector(base.x+ 31, base.y), size == SMALL);
-
-              bumpbrick(base.x+ 31, base.y);
-              Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
-            }
-        }
-#endif
-
-      grabdistros();
-
-#if 0
-      if (jumped_in_solid)
-        {
-          ++base.y;
-          ++old_base.y;
-          if(on_ground())
-            {
-              /* Make sure jumping is off. */
-              jumping = false;
-              flapping = false;
-            }
-        }
-#endif
     }
+#endif
 
   on_ground_flag = false;
 }
@@ -453,12 +345,13 @@ Player::handle_horizontal_input()
           skidding_timer.start(SKID_TIME);
           SoundManager::get()->play_sound(IDToSound(SND_SKID));
           // dust some partcles
-          Sector::current()->add_particles(
-              Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
+          Sector::current()->add_object(
+              new Particles(
+                Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
                 bbox.p2.y),
               dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
-              Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, 800,
-              LAYER_OBJECTS+1);
+              Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
+              LAYER_OBJECTS+1));
 
           ax *= 2.5;
         }
@@ -513,6 +406,17 @@ Player::handle_vertical_input()
       fall_mode = JUMPING;
   }
 
+  if(on_ground()) { /* Make sure jumping is off. */
+    jumping = false;
+    flapping = false;
+    falling_from_flap = false;
+    if (flapping_timer.started()) {
+      flapping_timer.start(0);
+    }
+
+    physic.set_acceleration_y(0); //for flapping
+  }
+
   // Press jump key
   if(input.jump == DOWN && can_jump && on_ground())
     {
@@ -727,18 +631,6 @@ Player::handle_vertical_input()
     }
 #endif
 
-  if(on_ground())   /* Make sure jumping is off. */
-    {
-      jumping = false;
-      flapping = false;
-      falling_from_flap = false;
-      if (flapping_timer.started()) {
-        flapping_timer.start(0);
-      }
-
-      physic.set_acceleration_y(0); //for flapping
-    }
-
   input.old_jump = input.jump;
 }
 
@@ -746,42 +638,20 @@ void
 Player::handle_input()
 {
   /* Handle horizontal movement: */
-    handle_horizontal_input();
+  handle_horizontal_input();
 
   /* Jump/jumping? */
-
   if (on_ground() && input.jump == UP)
     can_jump = true;
   handle_vertical_input();
 
   /* Shoot! */
-  if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
-    {
-      if(Sector::current()->add_bullet(
-            get_pos() + Vector(0, bbox.get_height()/2),
+  if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER) {
+    if(Sector::current()->add_bullet(
+          get_pos() + Vector(0, bbox.get_height()/2),
           physic.get_velocity_x(), dir))
-        shooting_timer.start(SHOOTING_TIME);
-      input.old_fire = DOWN;
-    }
-
-  /* tux animations: */
-  if(frame_timer.check()) {
-    if (input.right == UP && input.left == UP)
-    {
-      frame_main = 1;
-      frame_ = 1;
-    }
-    else
-    {
-      if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
-          (global_frame_counter % 4) == 0)
-        frame_main = (frame_main + 1) % 4;
-
-      frame_ = frame_main;
-
-      if (frame_ == 3)
-        frame_ = 1;
-    }
+      shooting_timer.start(SHOOTING_TIME);
+    input.old_fire = DOWN;
   }
 
   /* Duck! */
@@ -845,7 +715,7 @@ Player::draw(DrawingContext& context)
   else
     tux_body = big_tux;
 
-  int layer = LAYER_OBJECTS - 1;
+  int layer = LAYER_OBJECTS + 10;
 
   /* Set Tux sprite action */
   if (duck && size == BIG)
@@ -906,11 +776,9 @@ Player::draw(DrawingContext& context)
     if(size == BIG)
       {
       if(dir == LEFT)
-        tux_body->head->set_action("idle-left");
+        tux_body->head->set_action("idle-left", 1);
       else // dir == RIGHT
-        tux_body->head->set_action("idle-right");
-
-      tux_body->head->start_animation(1);
+        tux_body->head->set_action("idle-right", 1);
       }
 
     }
@@ -963,7 +831,7 @@ Player::draw(DrawingContext& context)
             get_pos(), layer);
       }
     }
-  else if (safe_timer.started() && global_frame_counter%2)
+  else if (safe_timer.started() && size_t(global_time*40)%2)
     ;  // don't draw Tux
   else
     tux_body->draw(context, get_pos(), layer);
@@ -971,19 +839,19 @@ Player::draw(DrawingContext& context)
   // Draw blinking star overlay
   if (invincible_timer.started() &&
      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
-      || global_frame_counter % 3)
+      || size_t(global_time*20)%2)
      && !dying)
   {
     if (size == SMALL || duck)
-      smalltux_star->draw(context, get_pos(), LAYER_OBJECTS + 2);
+      smalltux_star->draw(context, get_pos(), layer + 5);
     else
-      bigtux_star->draw(context, get_pos(), LAYER_OBJECTS + 2);
+      bigtux_star->draw(context, get_pos(), layer + 5);
   }
  
   if (debug_mode)
     context.draw_filled_rect(get_pos(),
         Vector(bbox.get_width(), bbox.get_height()),
-        Color(75,75,75, 150), LAYER_OBJECTS+1);
+        Color(75,75,75, 150), LAYER_OBJECTS+20);
 }
 
 HitResponse
@@ -999,12 +867,11 @@ Player::collision(GameObject& other, const CollisionHit& hit)
         physic.set_velocity_y(0);
       on_ground_flag = true;
     } else if(hit.normal.y > 0) { // bumped against the roof
-      physic.set_velocity_y(0);
+      physic.set_velocity_y(.1);
     }
     
-    if(hit.normal.x != 0) { // hit on the side?
-      if(hit.normal.y > 0.6) // limits the slopes we can move up...
-        physic.set_velocity_x(0);
+    if(fabsf(hit.normal.x) > .9) { // hit on the side?
+      physic.set_velocity_x(0);
     }
 
     return CONTINUE;
@@ -1019,122 +886,6 @@ Player::collision(GameObject& other, const CollisionHit& hit)
   return FORCE_MOVE;
 }
 
-#if 0
-void
-Player::collision(void* p_c_object, int c_object)
-{
-  //BadGuy* pbad_c = NULL;
-  //Trampoline* ptramp_c = NULL;
-  //FlyingPlatform* pplatform_c = NULL;
-
-  switch (c_object)
-    {
-    case CO_BADGUY:
-      pbad_c = (BadGuy*) p_c_object;
-
-     /* Hurt player if he touches a badguy */
-      if (!pbad_c->dying && !dying &&
-          !safe_timer.started() &&
-          pbad_c->mode != BadGuy::HELD)
-        {
-          if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
-               && !holding_something)
-            {
-              holding_something = true;
-              pbad_c->mode = BadGuy::HELD;
-              pbad_c->base.y-=8;
-            }
-          else if (pbad_c->mode == BadGuy::FLAT)
-            {
-              // Don't get hurt if we're kicking a flat badguy!
-            }
-          else if (pbad_c->mode == BadGuy::KICK)
-            {
-              /* Hurt if you get hit by kicked laptop: */
-              if (!invincible_timer.started())
-                {
-                  kill(SHRINK);
-                }
-              else
-                pbad_c->kill_me(20);
-            }
-          else if (!pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
-              || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
-              || pbad_c->kind == BAD_SPIKY))
-                pbad_c->kill_me(20);
-          else
-            {
-              if (!invincible_timer.started())
-                {
-                  kill(SHRINK);
-                }
-              else
-                {
-                  pbad_c->kill_me(25);
-                }
-            }
-          player_status.score_multiplier++;
-        }
-      break;
-
-    case CO_TRAMPOLINE:
-      ptramp_c = (Trampoline*) p_c_object;
-      
-      // Pick up trampoline
-      if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
-      {
-        holding_something = true;
-        ptramp_c->mode = Trampoline::M_HELD;
-        ptramp_c->base.y -= 8;
-      }
-      // Set down trampoline
-      else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
-      {
-        holding_something = false;
-        ptramp_c->mode = Trampoline::M_NORMAL;
-        ptramp_c->base.y += 8;
-        ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
-
-        //if (dir == RIGHT)
-        //  ptramp_c->base.x = base.x + base.width+1;
-        //else /* LEFT */
-        //  ptramp_c->base.x = base.x - base.width-1;
-      }
-/*
-      // Don't let tux walk through trampoline
-      else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
-      {
-        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;
-        }
-      }
-*/
-      break;
-    case CO_FLYING_PLATFORM:
-      pplatform_c = (FlyingPlatform*) p_c_object;
-      
-      base.y = pplatform_c->base.y - base.height;
-      physic.set_velocity_x(pplatform_c->get_vel_x());
-      
-      physic.enable_gravity(false);
-      can_jump = true;
-      fall_mode = ON_GROUND;
-      break;
-
-    default:
-      break;
-    }
-
-}
-#endif
-
 void
 Player::make_invincible()
 {
@@ -1243,7 +994,7 @@ Player::check_bounds(Camera* camera)
 }
 
 void
-Player::bounce(BadGuy& badguy)
+Player::bounce(BadGuy& )
 {
   //Make sure we stopped flapping
   flapping = false;