- Cleanups
[supertux.git] / src / player.cpp
index 32c70bb..84d2f02 100644 (file)
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#include <math.h>
+#include <cmath>
 #include <iostream>
 #include <cassert>
+
 #include "gameloop.h"
-#include "globals.h"
+#include "app/globals.h"
 #include "player.h"
 #include "defines.h"
 #include "scene.h"
 #include "tile.h"
-#include "sprite.h"
+#include "special/sprite.h"
 #include "sector.h"
 #include "tilemap.h"
 #include "camera.h"
 #include "gameobjs.h"
 #include "resources.h"
 #include "interactive_object.h"
-#include "screen/screen.h"
+#include "video/screen.h"
 
 // behavior definitions:
 #define TILES_FOR_BUTTJUMP 3
 // animation times (in ms):
 #define SHOOTING_TIME 320
+#define STOMP_TIME 250
 // others stuff:
 #define AUTOSCROLL_DEAD_INTERVAL 300
 
+// growing animation
+Surface* growingtux_left[GROWING_FRAMES];
+Surface* growingtux_right[GROWING_FRAMES];
+
 Surface* tux_life;
 
 Sprite* smalltux_gameover;
 Sprite* smalltux_star;
 Sprite* largetux_star;
-Sprite* growingtux_left;
-Sprite* growingtux_right;
 
 PlayerSprite smalltux;
 PlayerSprite largetux;
@@ -113,10 +117,11 @@ Player::init()
   jumping = false;
   can_jump = true;
   butt_jump = false;
-
+  
+  stomp_pos = Vector(0,0);
   frame_main = 0;
   frame_ = 0;
-  
+
   player_input_init(&input);
 
   invincible_timer.init(true);
@@ -222,7 +227,7 @@ Player::action(float elapsed_time)
   if(dying == DYING_NOT)
     handle_input();
 
-  physic.apply(elapsed_time, base.x, base.y);
+  physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
 
   if(dying == DYING_NOT) 
     {
@@ -285,7 +290,7 @@ Player::action(float elapsed_time)
           if (isbrick(base.x, base.y) ||
               isfullbox(base.x, base.y))
             {
-              Sector::current()->trygrabdistro(
+             Sector::current()->trygrabdistro(
                   Vector(base.x, base.y - 32), BOUNCE);
               Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
 
@@ -299,7 +304,7 @@ Player::action(float elapsed_time)
           if (isbrick(base.x+ 31, base.y) ||
               isfullbox(base.x+ 31, base.y))
             {
-              Sector::current()->trygrabdistro(
+             Sector::current()->trygrabdistro(
                   Vector(base.x+ 31, base.y - 32), BOUNCE);
               Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
 
@@ -414,7 +419,7 @@ Player::handle_horizontal_input()
   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
       if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
           skidding_timer.start(SKID_TIME);
-          sound_manager->play_sound(sounds[SND_SKID]);
+          SoundManager::get()->play_sound(IDToSound(SND_SKID));
           ax *= 2.5;
       } else {
           ax *= 2;
@@ -481,9 +486,9 @@ Player::handle_vertical_input()
       jumping = true;
       can_jump = false;
       if (size == SMALL)
-        sound_manager->play_sound(sounds[SND_JUMP]);
+        SoundManager::get()->play_sound(IDToSound(SND_JUMP));
       else
-        sound_manager->play_sound(sounds[SND_BIGJUMP]);
+        SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
     }
   // Let go of jump key
   else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
@@ -494,7 +499,7 @@ Player::handle_vertical_input()
 
    /* In case the player has pressed Down while in a certain range of air,
       enable butt jump action */
-  if (input.down == DOWN && !butt_jump)
+  if (input.down == DOWN && !butt_jump && !duck)
     if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
       butt_jump = true;
 
@@ -505,6 +510,13 @@ Player::handle_vertical_input()
   // Do butt jump
   if (butt_jump && on_ground() && size == BIG)
   {
+    
+    if (duck) 
+      stomp_pos = Vector(base.x - 32, base.y);
+    else 
+      stomp_pos = Vector(base.x - 32, base.y + 32);    
+    stomp_timer.start(STOMP_TIME);
+    
     butt_jump = false;
 
     // Break bricks beneath Tux
@@ -526,8 +538,9 @@ Player::handle_vertical_input()
       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
       if(badguy)
       {
-        if (fabsf(base.x - badguy->base.x) < 300 &&
-            fabsf(base.y - badguy->base.y) < 300 &&
+        
+       if (fabsf(base.x - badguy->base.x) < 150 &&
+            fabsf(base.y - badguy->base.y) < 60 &&
             (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
               issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
           badguy->kill_me(25);
@@ -633,7 +646,7 @@ Player::grow(bool animate)
   base.y -= 32;
 
   if(animate)
-    growing_timer.start((int)((growingtux_left->get_frames() / growingtux_left->get_fps()) * 1000));
+    growing_timer.start(GROWING_TIME);
 
   old_base = previous_base = base;
 }
@@ -646,7 +659,6 @@ Player::grabdistros()
     {
       Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
       Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
-
       Sector::current()->trygrabdistro(
           Vector(base.x, base.y + base.height), NO_BOUNCE);
       Sector::current()->trygrabdistro(
@@ -654,7 +666,7 @@ Player::grabdistros()
 
       if(size == BIG)
         {
-          Sector::current()->trygrabdistro(
+         Sector::current()->trygrabdistro(
               Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
           Sector::current()->trygrabdistro(
               Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
@@ -669,7 +681,7 @@ Player::grabdistros()
       if(player_status.lives < MAX_LIVES)
         ++player_status.lives;
       /*We want to hear the sound even, if MAX_LIVES is reached*/
-      sound_manager->play_sound(sounds[SND_LIFEUP]);
+      SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
     }
 }
 
@@ -694,16 +706,16 @@ Player::draw(DrawingContext& context)
     {
       if (dying == DYING_SQUISHED)
         {
-          smalltux_gameover->draw(context, pos, LAYER_OBJECTS+1);
+          smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
         }
       else
         {
           if(growing_timer.check())
             {
               if (dir == RIGHT)
-                growingtux_right->draw(context, pos, layer);
+                context.draw_surface(growingtux_right[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
               else 
-                growingtux_left->draw(context, pos, layer);
+                context.draw_surface(growingtux_left[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
             }
           else if (duck && size != SMALL)
             {
@@ -761,6 +773,10 @@ Player::draw(DrawingContext& context)
     else
       sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
   }
+  
+  // Draw stomp clouds when doing a butt jump
+  if (stomp_timer.check())
+      sprite->stomp->draw(context, stomp_pos, LAYER_OBJECTS + 1);
 
   // Draw blinking star overlay
   if (invincible_timer.started() &&
@@ -907,7 +923,7 @@ Player::kill(HurtMode mode)
   if(dying)
     return;
   
-  sound_manager->play_sound(sounds[SND_HURT]);
+  SoundManager::get()->play_sound(IDToSound(SND_HURT));
 
   physic.set_velocity_x(0);
 
@@ -990,3 +1006,18 @@ Player::check_bounds(Camera* camera)
   }
 }
 
+void
+Player::bounce(BadGuy* badguy)
+{
+  if (input.up)
+    physic.set_velocity_y(5.2);
+  else
+    physic.set_velocity_y(2);
+
+  // Move the player a little bit above the badguy to avoid collision
+  // between badguy and player directly after the bounce has happend
+  base.y = badguy->base.y - base.height - 2;
+}
+
+/* EOF */
+