-optimized and cleaned up collision_object_map
[supertux.git] / src / player.cpp
index 9186fed..afc4eb5 100644 (file)
@@ -34,44 +34,9 @@ Sprite* smalltux_gameover;
 Sprite* smalltux_star;
 Sprite* largetux_star;
 
-Sprite* smalltux_stand_left;
-Sprite* smalltux_stand_right;
-Sprite* smalltux_walk_right;
-Sprite* smalltux_walk_left;
-Sprite* smalltux_jump_left;
-Sprite* smalltux_jump_right;
-Sprite* smalltux_kick_left;
-Sprite* smalltux_kick_right;
-Sprite* smalltux_skid_left;
-Sprite* smalltux_skid_right;
-Sprite* smalltux_grab_left;
-Sprite* smalltux_grab_right;
-
-Sprite* largetux_stand_left;
-Sprite* largetux_stand_right;
-Sprite* largetux_walk_right;
-Sprite* largetux_walk_left;
-Sprite* largetux_jump_right;
-Sprite* largetux_jump_left;
-Sprite* largetux_kick_left;
-Sprite* largetux_kick_right;
-Sprite* largetux_skid_right;
-Sprite* largetux_skid_left;
-Sprite* largetux_grab_left;
-Sprite* largetux_grab_right;
-Sprite* largetux_duck_right;
-Sprite* largetux_duck_left;
-
-Surface* firetux_right[3];
-Surface* firetux_left[3];
-Surface* bigfiretux_right[3];
-Surface* bigfiretux_left[3];
-Surface* bigfiretux_right_jump;
-Surface* bigfiretux_left_jump;
-Surface* duckfiretux_right;
-Surface* duckfiretux_left;
-Surface* skidfiretux_right;
-Surface* skidfiretux_left;
+PlayerSprite smalltux;
+PlayerSprite largetux;
+PlayerSprite firetux;
 
 PlayerKeymap keymap;
 
@@ -127,6 +92,7 @@ Player::init()
   skidding_timer.init(true);
   safe_timer.init(true);
   frame_timer.init(true);
+  kick_timer.init(true);
 
   physic.reset();
 }
@@ -156,6 +122,8 @@ Player::key_event(SDLKey key, int state)
     }
   else if(key == keymap.fire)
     {
+      if (state == UP)
+        input.old_fire = UP;
       input.fire = state;
       return true;
     }
@@ -297,6 +265,7 @@ Player::action(double frame_ratio)
   skidding_timer.check();
   invincible_timer.check();
   safe_timer.check();
+  kick_timer.check();
 }
 
 bool
@@ -402,7 +371,7 @@ Player::handle_horizontal_input()
 void
 Player::handle_vertical_input()
 {
-  if(input.up == DOWN)
+  if(input.up == DOWN && input.old_up == UP)
     {
       if (on_ground())
         {
@@ -441,12 +410,14 @@ Player::handle_input()
     {
       handle_vertical_input();
     }
+  input.old_up = input.up;
 
   /* Shoot! */
 
   if (input.fire == DOWN && input.old_fire == UP && got_coffee)
     {
       World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
+      input.old_fire = DOWN;
     }
 
   /* tux animations: */
@@ -472,7 +443,7 @@ Player::handle_input()
     }
 
   /* Duck! */
-  if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
+  if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
     {
       duck = true;
       base.height = 32;                             
@@ -485,11 +456,25 @@ Player::handle_input()
       duck = false;
       base.y -= 32;
       base.height = 64;
-      old_base = previous_base = base;
+      // changing base size confuses collision otherwise
+      old_base = previous_base = base;                        
     }
 }
 
 void
+Player::grow()
+{
+  if(size == BIG)
+    return;
+  
+  size = BIG;
+  base.height = 64;
+  base.y -= 32;
+
+  old_base = previous_base = base;
+}
+
+void
 Player::grabdistros()
 {
   /* Grab distros: */
@@ -531,158 +516,84 @@ Player::draw()
         }
       else
         {
+          PlayerSprite* sprite;
+          
           if (size == SMALL)
+            sprite = &smalltux;
+          else if (got_coffee)
+            sprite = &firetux;
+          else
+            sprite = &largetux;
+          
+          if (duck && size != SMALL)
             {
-              if (!skidding_timer.started())
-                {
-                  if (physic.get_velocity_y() != 0)
-                    {
-                      if (dir == RIGHT)
-                        smalltux_jump_right->draw(base.x - scroll_x, base.y);
-                      else
-                        smalltux_jump_left->draw(base.x - scroll_x, base.y);                   
-                    }
-                  else
-                    {
-                      if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
-                        {
-                          if (dir == RIGHT)
-                            smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
-                          else
-                            smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
-                        }
-                      else // moving
-                        {
-                          if (dir == RIGHT)
-                            smalltux_walk_right->draw(base.x - scroll_x, base.y);
-                          else
-                            smalltux_walk_left->draw(base.x - scroll_x, base.y);
-                        }
-                    }
-                }
+              if (dir == RIGHT)
+                sprite->duck_right->draw(base.x - scroll_x, base.y);
+              else 
+                sprite->duck_left->draw(base.x - scroll_x, base.y);
+            }
+          else if (skidding_timer.started())
+            {
+              if (dir == RIGHT)
+                sprite->skid_right->draw(base.x - scroll_x, base.y);
+              else
+                sprite->skid_left->draw(base.x - scroll_x, base.y); 
+            }
+          else if (kick_timer.started())
+            {
+              if (dir == RIGHT)
+                sprite->kick_right->draw(base.x - scroll_x, base.y);
+              else
+                sprite->kick_left->draw(base.x - scroll_x, base.y); 
+            }
+          else if (physic.get_velocity_y() != 0)
+            {
+              if (dir == RIGHT)
+                sprite->jump_right->draw(base.x - scroll_x, base.y);
               else
+                sprite->jump_left->draw(base.x - scroll_x, base.y);                   
+            }
+          else
+            {
+              if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
                 {
                   if (dir == RIGHT)
-                    smalltux_skid_right->draw(base.x - scroll_x, base.y);
+                    sprite->stand_right->draw( base.x - scroll_x, base.y);
                   else
-                    smalltux_skid_left->draw(base.x - scroll_x, base.y); 
+                    sprite->stand_left->draw( base.x - scroll_x, base.y);
                 }
-
-              if (holding_something && physic.get_velocity_y() == 0)
+              else // moving
                 {
                   if (dir == RIGHT)
-                    smalltux_grab_right->draw(base.x - scroll_x, base.y);
+                    sprite->walk_right->draw(base.x - scroll_x, base.y);
                   else
-                    smalltux_grab_left->draw(base.x - scroll_x, base.y);
+                    sprite->walk_left->draw(base.x - scroll_x, base.y);
                 }
-
-              if (invincible_timer.started())
-                smalltux_star->draw(base.x - scroll_x, base.y);
             }
-          else // Large Tux
+                      
+          // Draw arm overlay graphics when Tux is holding something
+          if (holding_something && physic.get_velocity_y() == 0)
             {
-              if (!got_coffee)
-                {
-                  if (!duck)
-                    {
-                      if (!skidding_timer.started())
-                        {
-                          if (physic.get_velocity_y() == 0)
-                            {
-                              if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
-                                {
-                                  if (dir == RIGHT)
-                                    largetux_stand_right->draw(base.x - scroll_x, base.y);
-                                  else
-                                    largetux_stand_left->draw(base.x - scroll_x, base.y);
-                                }
-                              else // walking
-                                {
-                                  if (dir == RIGHT)
-                                    largetux_walk_right->draw(base.x - scroll_x, base.y);
-                                  else
-                                    largetux_walk_left->draw(base.x - scroll_x, base.y);
-                                }
-                            }
-                          else
-                            {
-                              if (dir == RIGHT)
-                                largetux_jump_right->draw(base.x - scroll_x, base.y);
-                              else
-                                largetux_jump_left->draw(base.x - scroll_x, base.y);
-                            }
-                        }
-                      else
-                        {
-                          if (dir == RIGHT)
-                            largetux_skid_right->draw(base.x - scroll_x - 8, base.y);
-                          else
-                            largetux_skid_left->draw(base.x - scroll_x - 8, base.y);
-                        }
-                    }
-                  else
-                    {
-                      if (dir == RIGHT)
-                        largetux_duck_right->draw(base.x - scroll_x, base.y);
-                      else
-                        largetux_duck_left->draw(base.x - scroll_x, base.y);
-                    }
-                }
+              if (dir == RIGHT)
+                sprite->grab_right->draw(base.x - scroll_x, base.y);
               else
-                {
-                  /* Tux has coffee! */
-                  if (!duck)
-                    {
-                      if (!skidding_timer.started())
-                        {
-                          if (!jumping || physic.get_velocity_y() > 0)
-                            {
-                              if (dir == RIGHT)
-                                bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
-                              else
-                                bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
-                            }
-                          else
-                            {
-                              if (dir == RIGHT)
-                                bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
-                              else
-                                bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
-                            }
-                        }
-                      else
-                        {
-                          if (dir == RIGHT)
-                            skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
-                          else
-                            skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
-                        }
-                    }
-                  else
-                    {
-                      if (dir == RIGHT)
-                        duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
-                      else
-                        duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
-                    }
-                }
-
-              if (holding_something && !duck && physic.get_velocity_y() == 0)
-                {
-                  if (dir == RIGHT)
-                    largetux_grab_right->draw(base.x - scroll_x, base.y);
-                  else
-                    largetux_grab_left->draw(base.x - scroll_x, base.y);
-                }
+                sprite->grab_left->draw(base.x - scroll_x, base.y);
+            }
 
-              if (invincible_timer.started())
+          // Draw blinking star overlay
+          if (invincible_timer.started())
+            {
+              if (size == SMALL || duck)
+                smalltux_star->draw(base.x - scroll_x, base.y);
+              else
                 largetux_star->draw(base.x - scroll_x, base.y);
             }
-        }     
-    }
-
+        }
+    }     
+  
   if (debug_mode)
-    fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
+    fillrect(base.x - scroll_x, base.y, 
+             base.width, base.height, 75,75,75, 150);
 }
 
 void
@@ -700,7 +611,8 @@ Player::collision(void* p_c_object, int c_object)
           !safe_timer.started() &&
           pbad_c->mode != BadGuy::HELD)
         {
-          if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN)
+          if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
+               && !holding_something)
             {
               holding_something = true;
               pbad_c->mode = BadGuy::HELD;
@@ -734,7 +646,7 @@ Player::collision(void* p_c_object, int c_object)
                 }
               else
                 {
-                  pbad_c->kill_me();
+                  pbad_c->kill_me(25);
                 }
             }
           player_status.score_multiplier++;