- memleak fix and menu fix from MatzeB
[supertux.git] / src / player.cpp
index 23361f7..8e50c7e 100644 (file)
 #include "screen.h"
 
 Surface* tux_life;
-std::vector<Surface*> tux_right;
-std::vector<Surface*> tux_left;
-Surface* smalltux_jump_left;
-Surface* smalltux_jump_right;
-Surface* smalltux_stand_left;
-Surface* smalltux_stand_right;
-Sprite*  smalltux_gameover;
-Sprite*  smalltux_skid_left;
-Sprite*  smalltux_skid_right;
-
-Sprite*  smalltux_kick_left;
-Sprite*  smalltux_kick_right;
-Sprite*  smalltux_grab_left;
-Sprite*  smalltux_grab_right;
-
-Sprite*  largetux_kick_left;
-Sprite*  largetux_kick_right;
-Sprite*  largetux_grab_left;
-Sprite*  largetux_grab_right;
-
-Sprite*  largetux_stand_left;
-Sprite*  largetux_stand_right;
-
-Sprite* bigtux_right;
-Sprite* bigtux_left;
-Sprite* bigtux_right_jump;
-Sprite* bigtux_left_jump;
-Sprite* ducktux_right;
-Sprite* ducktux_left;
-Surface* skidtux_right;
-Surface* skidtux_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;
-Surface* cape_right[2];
-Surface* cape_left[2];
-Surface* bigcape_right[2];
-Surface* bigcape_left[2];
+
+Sprite* smalltux_gameover;
+Sprite* smalltux_star;
+Sprite* largetux_star;
+
+PlayerSprite smalltux;
+PlayerSprite largetux;
+PlayerSprite firetux;
 
 PlayerKeymap keymap;
 
@@ -482,7 +445,7 @@ Player::handle_input()
       // changing base size confuses collision otherwise
       old_base = previous_base = base;
     }
-  else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0)
+  else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
     {
       duck = false;
       base.y -= 32;
@@ -533,173 +496,80 @@ Player::draw()
         }
       else
         {
+          PlayerSprite* sprite;
+          
           if (size == SMALL)
+            sprite = &smalltux;
+          else if (got_coffee)
+            sprite = &firetux;
+          else
+            sprite = &largetux;
+          
+          if (skidding_timer.started())
             {
-              if (invincible_timer.started())
-                {
-                  if (dir == RIGHT)
-                    cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
-                  else
-                    cape_left[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
-                }
-
-              if (!skidding_timer.started())
-                {
-                  if (physic.get_velocity_y() != 0)
-                    {
-                      if (dir == RIGHT)
-                        smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
-                      else
-                        smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);                   
-                    }
-                  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)
-                            tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
-                          else
-                            tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
-                        }
-                    }
-                }
+              if (dir == RIGHT)
+                sprite->skid_right->draw(base.x - scroll_x, base.y);
               else
-                {
-                  if (dir == RIGHT)
-                    smalltux_skid_right->draw(base.x - scroll_x, base.y);
-                  else
-                    smalltux_skid_left->draw(base.x - scroll_x, base.y); 
-                }
-
-              if (holding_something && physic.get_velocity_y() == 0)
-                {
-                  if (dir == RIGHT)
-                    smalltux_grab_right->draw(base.x - scroll_x, base.y);
-                  else
-                    smalltux_grab_left->draw(base.x - scroll_x, base.y);
-                }
+                sprite->skid_left->draw(base.x - scroll_x, base.y); 
             }
-          else // Large Tux
+          else
             {
-              if (invincible_timer.started())
+              if (duck)
                 {
-                  float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
-                  capex -= scroll_x;
-                  float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
-                
-                  /* Draw cape (just not in ducked mode since that looks silly): */
                   if (dir == RIGHT)
-                    bigcape_right[global_frame_counter % 2]->draw(capex, capey);
-                  else
-                    bigcape_left[global_frame_counter % 2]->draw(capex, capey);
-                }
-
-              if (!got_coffee)
+                    sprite->duck_right->draw(base.x - scroll_x, base.y);
+                  else 
+                    sprite->duck_left->draw(base.x - scroll_x, base.y);
+                }                    
+              else if (physic.get_velocity_y() != 0)
                 {
-                  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)
-                                    bigtux_right->draw(base.x - scroll_x, base.y);
-                                  else
-                                    bigtux_left->draw(base.x - scroll_x, base.y);
-                                }
-                            }
-                          else
-                            {
-                              if (dir == RIGHT)
-                                bigtux_right_jump->draw(base.x - scroll_x, base.y);
-                              else
-                                bigtux_left_jump->draw(base.x - scroll_x, base.y);
-                            }
-                        }
-                      else
-                        {
-                          if (dir == RIGHT)
-                            skidtux_right->draw(base.x - scroll_x - 8, base.y);
-                          else
-                            skidtux_left->draw(base.x - scroll_x - 8, base.y);
-                        }
-                    }
+                  if (dir == RIGHT)
+                    sprite->jump_right->draw(base.x - scroll_x, base.y);
                   else
-                    {
-                      if (dir == RIGHT)
-                        ducktux_right->draw(base.x - scroll_x, base.y);
-                      else
-                        ducktux_left->draw(base.x - scroll_x, base.y);
-                    }
+                    sprite->jump_left->draw(base.x - scroll_x, base.y);                   
                 }
               else
                 {
-                  /* Tux has coffee! */
-                  if (!duck)
+                  if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
                     {
-                      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);
-                            }
-                        }
+                      if (dir == RIGHT)
+                        sprite->stand_right->draw( base.x - scroll_x, 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);
-                        }
+                        sprite->stand_left->draw( base.x - scroll_x, base.y);
                     }
-                  else
+                  else // moving
                     {
                       if (dir == RIGHT)
-                        duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
+                        sprite->walk_right->draw(base.x - scroll_x, base.y);
                       else
-                        duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
+                        sprite->walk_left->draw(base.x - scroll_x, base.y);
                     }
                 }
-
-              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);
-                }
             }
-        }     
-    }
+          
+          // Draw arm overlay graphics when Tux is holding something
+          if (holding_something && physic.get_velocity_y() == 0)
+            {
+              if (dir == RIGHT)
+                sprite->grab_right->draw(base.x - scroll_x, base.y);
+              else
+                sprite->grab_left->draw(base.x - scroll_x, base.y);
+            }
 
+          // 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