* Coins inside boxes are now being counted. (We'll have to decide if we want the...
[supertux.git] / src / object / player.cpp
index 89face7..bcde741 100644 (file)
 #include "object/sprite_particle.hpp"
 
 static const int TILES_FOR_BUTTJUMP = 3;
-static const float SHOOTING_TIME = .150;
+static const float SHOOTING_TIME = .150f;
 /// time before idle animation starts
-static const float IDLE_TIME = 2.5;
+static const float IDLE_TIME = 2.5f;
 
 static const float WALK_ACCELERATION_X = 300;
 static const float RUN_ACCELERATION_X = 400;
 static const float SKID_XM = 200;
-static const float SKID_TIME = .3;
+static const float SKID_TIME = .3f;
 static const float MAX_WALK_XM = 230;
 static const float MAX_RUN_XM = 320;
 static const float WALK_SPEED = 100;
 
-static const float KICK_TIME = .3;
-static const float CHEER_TIME = 1;
+static const float KICK_TIME = .3f;
+static const float CHEER_TIME = 1.0f;
 
-static const float UNDUCK_HURT_TIME = 0.25; /**< if Tux cannot unduck for this long, he will get hurt */
+static const float UNDUCK_HURT_TIME = 0.25f; /**< if Tux cannot unduck for this long, he will get hurt */
 
 // growing animation
 Surface* growingtux_left[GROWING_FRAMES];
@@ -141,9 +141,9 @@ void
 Player::init()
 {
   if(is_big())
-    set_size(31.8, 62.8);
+    set_size(31.8f, 62.8f);
   else
-    set_size(31.8, 30.8);
+    set_size(31.8f, 30.8f);
 
   dir = RIGHT;
   old_dir = dir;
@@ -162,11 +162,12 @@ Player::init()
   backflip_direction = 0;
   visible = true;
   swimming = false;
+  speedlimit = 0; //no special limit
 
   on_ground_flag = false;
   grabbed_object = NULL;
 
-  physic = Physic();
+  physic.reset();
 }
 
 void
@@ -187,6 +188,18 @@ Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
   Scripting::unexpose_object(vm, table_idx, name);
 }
 
+float
+Player::get_speedlimit()
+{
+  return speedlimit;
+}
+
+void
+Player::set_speedlimit(float newlimit)
+{
+  speedlimit=newlimit;
+}
+
 void
 Player::set_controller(Controller* controller)
 {
@@ -215,6 +228,12 @@ Player::adjust_height(float new_height)
 }
 
 void
+Player::trigger_sequence(std::string sequence_name)
+{
+  GameSession::current()->start_sequence(sequence_name);
+}
+
+void
 Player::update(float elapsed_time)
 {
   if( no_water ){
@@ -236,17 +255,17 @@ Player::update(float elapsed_time)
 
   // extend/shrink tux collision rectangle so that we fall through/walk over 1
   // tile holes
-  if(fabsf(physic.vx) > MAX_WALK_XM) {
+  if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
     set_width(34);
   } else {
-    set_width(31.8);
+    set_width(31.8f);
   }
 
   // on downward slopes, adjust vertical velocity so tux walks smoothly down
   if (on_ground()) {
     if(floor_normal.y != 0) {
-      if ((floor_normal.x * physic.vx) >= 0) {
-        physic.vy = 250;
+      if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
+        physic.set_velocity_y(250);
       }
     }
   }
@@ -255,7 +274,7 @@ Player::update(float elapsed_time)
   if (backflipping) {
     //prevent player from changing direction when backflipping
     dir = (backflip_direction == 1) ? LEFT : RIGHT;
-    if (backflip_timer.started()) physic.vx = 100 * backflip_direction;
+    if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
   }
 
   // set fall mode...
@@ -341,26 +360,26 @@ Player::is_big()
 void
 Player::apply_friction()
 {
-  if ((on_ground()) && (fabs(physic.vx) < WALK_SPEED)) {
-    physic.vx = 0;
-    physic.ax = 0;
-  } else if(physic.vx < 0) {
-    physic.ax = WALK_ACCELERATION_X * 1.5;
-  } else {
-    physic.ax = WALK_ACCELERATION_X * -1.5;
+  if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
+    physic.set_velocity_x(0);
+    physic.set_acceleration_x(0);
+  } else if(physic.get_velocity_x() < 0) {
+    physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
+    } else if(physic.get_velocity_x() > 0) {
+    physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
   }
 }
 
 void
 Player::handle_horizontal_input()
 {
-  float vx = physic.vx;
-  float vy = physic.vy;
-  float ax = physic.ax;
-  float ay = physic.ay;
+  float vx = physic.get_velocity_x();
+  float vy = physic.get_velocity_y();
+  float ax = physic.get_acceleration_x();
+  float ay = physic.get_acceleration_y();
 
   float dirsign = 0;
-  if(!duck || physic.vy != 0) {
+  if(!duck || physic.get_velocity_y() != 0) {
     if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
       old_dir = dir;
       dir = LEFT;
@@ -373,8 +392,9 @@ Player::handle_horizontal_input()
     }
   }
 
-  // only run if action key is pressed and we're not holding anything
-  if (!(controller->hold(Controller::ACTION) && (!grabbed_object))) {
+  // do not run if action key is pressed or we're holding something
+  // so tux can only walk while shooting
+  if ( controller->hold(Controller::ACTION) || grabbed_object ) {
     ax = dirsign * WALK_ACCELERATION_X;
     // limit speed
     if(vx >= MAX_WALK_XM && dirsign > 0) {
@@ -385,7 +405,11 @@ Player::handle_horizontal_input()
       ax = 0;
     }
   } else {
-    ax = dirsign * RUN_ACCELERATION_X;
+    if( vx * dirsign < MAX_WALK_XM ) {
+      ax = dirsign * WALK_ACCELERATION_X;
+    } else {
+      ax = dirsign * RUN_ACCELERATION_X;
+    }
     // limit speed
     if(vx >= MAX_RUN_XM && dirsign > 0) {
       vx = MAX_RUN_XM;
@@ -401,6 +425,12 @@ Player::handle_horizontal_input()
     vx = dirsign * WALK_SPEED;
   }
 
+  //Check speedlimit.
+  if( speedlimit > 0 &&  vx * dirsign >= speedlimit ) {
+      vx = dirsign * speedlimit;
+      ax = 0;
+  }
+
   // changing directions?
   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
     // let's skid!
@@ -412,7 +442,7 @@ Player::handle_horizontal_input()
         new Particles(
           Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
           dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
-          Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
+          Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
           LAYER_OBJECTS+1));
 
       ax *= 2.5;
@@ -421,10 +451,8 @@ Player::handle_horizontal_input()
     }
   }
 
-  physic.vx = vx;
-  physic.vy = vy;
-  physic.ax = ax;
-  physic.ay = ay;
+  physic.set_velocity(vx, vy);
+  physic.set_acceleration(ax, ay);
 
   // we get slower when not pressing any keys
   if(dirsign == 0) {
@@ -448,12 +476,12 @@ Player::do_duck() {
   if (!is_big())
     return;
 
-  if (physic.vy != 0)
+  if (physic.get_velocity_y() != 0)
     return;
   if (!on_ground())
     return;
 
-  if (adjust_height(31.8)) {
+  if (adjust_height(31.8f)) {
     duck = true;
     unduck_hurt_timer.stop();
   } else {
@@ -470,7 +498,7 @@ Player::do_standup() {
   if (backflipping)
     return;
 
-  if (adjust_height(63.8)) {
+  if (adjust_height(63.8f)) {
     duck = false;
     unduck_hurt_timer.stop();
   } else {
@@ -499,7 +527,7 @@ Player::do_backflip() {
   backflipping = true;
   do_jump(-580);
   sound_manager->play("sounds/flip.wav");
-  backflip_timer.start(0.15);
+  backflip_timer.start(0.15f);
 }
 
 void
@@ -507,7 +535,7 @@ Player::do_jump(float yspeed) {
   if (!on_ground())
     return;
 
-  physic.vy = yspeed;
+  physic.set_velocity_y(yspeed);
   //bbox.move(Vector(0, -1));
   jumping = true;
   on_ground_flag = false;
@@ -528,17 +556,17 @@ Player::handle_vertical_input()
   if(controller->pressed(Controller::JUMP) && (can_jump)) {
     if (duck) {
       // when running, only jump a little bit; else do a backflip
-      if ((physic.vx != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
+      if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
     } else {
       // jump a bit higher if we are running; else do a normal jump
-      if (fabs(physic.vx) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
+      if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
     }
   }
   // Let go of jump key
   else if(!controller->hold(Controller::JUMP)) {
-    if (!backflipping && jumping && physic.vy < 0) {
+    if (!backflipping && jumping && physic.get_velocity_y() < 0) {
       jumping = false;
-      physic.vy = 0;
+      physic.set_velocity_y(0);
     }
   }
 
@@ -553,11 +581,11 @@ Player::handle_vertical_input()
     butt_jump = false;
 
   // swimming
-  physic.ay = 0;
+  physic.set_acceleration_y(0);
   if (swimming) {
     if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
-      physic.ay = -2000;
-    physic.vy = physic.vy * 0.94;
+      physic.set_acceleration_y(-2000);
+    physic.set_velocity_y(physic.get_velocity_y() * 0.94);
   }
 }
 
@@ -598,7 +626,7 @@ Player::handle_input()
     if(Sector::current()->add_bullet(
          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
                       : Vector(32, bbox.get_height()/2)),
-         physic.vx, dir))
+         physic.get_velocity_x(), dir))
       shooting_timer.start(SHOOTING_TIME);
   }
 
@@ -618,7 +646,7 @@ Player::handle_input()
         Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
                 bbox.get_height()*0.66666 - 32);
     Rect dest(pos, pos + Vector(32, 32));
-    if(Sector::current()->is_free_of_statics(dest)) {
+    if(Sector::current()->is_free_of_movingstatics(dest)) {
       MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
       if(moving_object) {
         moving_object->set_pos(pos);
@@ -631,7 +659,7 @@ Player::handle_input()
   }
 }
 
-void 
+void
 Player::try_grab()
 {
   if(controller->hold(Controller::ACTION) && !grabbed_object
@@ -686,10 +714,8 @@ Player::handle_input_ghost()
   if (controller->hold(Controller::ACTION)) {
     set_ghost_mode(false);
   }
-  physic.vx = vx;
-  physic.vy = vy;
-  physic.ax = 0;
-  physic.ay = 0;
+  physic.set_velocity(vx, vy);
+  physic.set_acceleration(0, 0);
 }
 
 void
@@ -751,7 +777,7 @@ bool
 Player::set_bonus(BonusType type, bool animate)
 {
   if(player_status->bonus == NO_BONUS) {
-    if (!adjust_height(62.8)) {
+    if (!adjust_height(62.8f)) {
       printf("can't adjust\n");
       return false;
     }
@@ -880,7 +906,7 @@ Player::draw(DrawingContext& context)
     }
   else
     {
-    if (fabsf(physic.vx) < 1.0f) // standing
+    if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
       {
       if(dir == LEFT)
         tux_body->set_action("stand-left");
@@ -909,7 +935,7 @@ Player::draw(DrawingContext& context)
     }
 
   // Tux is holding something
-  if ((grabbed_object != 0 && physic.vy == 0) ||
+  if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
     {
     if (duck)
@@ -973,18 +999,18 @@ void
 Player::collision_solid(const CollisionHit& hit)
 {
   if(hit.bottom) {
-    if(physic.vy > 0)
-      physic.vy = 0;
+    if(physic.get_velocity_y() > 0)
+      physic.set_velocity_y(0);
 
     on_ground_flag = true;
     floor_normal = hit.slope_normal;
   } else if(hit.top) {
-    if(physic.vy < 0)
-      physic.vy =  .2;
+    if(physic.get_velocity_y() < 0)
+      physic.set_velocity_y(.2f);
   }
 
   if(hit.left || hit.right) {
-    physic.vx = 0;
+    physic.set_velocity_x(0);
   }
 
   // crushed?
@@ -1053,19 +1079,24 @@ Player::kill(bool completely)
 
   sound_manager->play("sounds/hurt.wav");
 
-  physic.vx = 0;
+  physic.set_velocity_x(0);
 
-  if(!completely && is_big()) {
+  if(!completely && (is_big() || growing_timer.started())) {
     if(player_status->bonus == FIRE_BONUS
         || player_status->bonus == ICE_BONUS) {
       safe_timer.start(TUX_SAFE_TIME);
       set_bonus(GROWUP_BONUS, true);
-    } else {
+    } else if(player_status->bonus == GROWUP_BONUS) {
       //growing_timer.start(GROWING_TIME);
       safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
-      adjust_height(30.8);
+      adjust_height(30.8f);
       duck = false;
       set_bonus(NO_BONUS, true);
+    } else if(player_status->bonus == NO_BONUS) {
+      growing_timer.stop();
+      safe_timer.start(TUX_SAFE_TIME);
+      adjust_height(30.8f);
+      duck = false;
     }
   } else {
     for (int i = 0; (i < 5) && (i < player_status->coins); i++)
@@ -1075,11 +1106,9 @@ Player::kill(bool completely)
             Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
             systemRandom.rand(-100,100)));
     }
-    physic.gravity_enabled = true;
-    physic.vx = 0;
-    physic.vy = -700;
-    physic.ax = 0;
-    physic.ay = 0;
+    physic.enable_gravity(true);
+    physic.set_acceleration(0, 0);
+    physic.set_velocity(0, -700);
     player_status->coins -= 25;
     set_bonus(NO_BONUS, true);
     dying = true;
@@ -1100,13 +1129,13 @@ Player::move(const Vector& vector)
 
   // TODO: do we need the following? Seems irrelevant to moving the player
   if(is_big())
-    set_size(31.8, 63.8);
+    set_size(31.8f, 63.8f);
   else
-    set_size(31.8, 31.8);
+    set_size(31.8f, 31.8f);
   duck = false;
   last_ground_y = vector.y;
 
-  physic = Physic();
+  physic.reset();
 }
 
 void
@@ -1140,30 +1169,29 @@ Player::check_bounds(Camera* camera)
 void
 Player::add_velocity(const Vector& velocity)
 {
-  physic.vx += velocity.x;
-  physic.vy += velocity.y;
+  physic.set_velocity(physic.get_velocity() + velocity);
 }
 
 void
 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
 {
   if (end_speed.x > 0)
-    physic.vx = std::min(physic.vx + velocity.x, end_speed.x);
+    physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
   if (end_speed.x < 0)
-    physic.vx = std::max(physic.vx + velocity.x, end_speed.x);
+    physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
   if (end_speed.y > 0)
-    physic.vy = std::min(physic.vy + velocity.y, end_speed.y);
+    physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
   if (end_speed.y < 0)
-    physic.vy = std::max(physic.vy + velocity.y, end_speed.y);
+    physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
 }
 
 void
 Player::bounce(BadGuy& )
 {
   if(controller->hold(Controller::JUMP))
-    physic.vy = -520;
+    physic.set_velocity_y(-520);
   else
-    physic.vy = -300;
+    physic.set_velocity_y(-300);
 }
 
 //Scripting Functions Below
@@ -1174,10 +1202,10 @@ Player::deactivate()
   if (deactivated)
     return;
   deactivated = true;
-  physic.vx = 0;
-  physic.vy = 0;
-  physic.ax = 0;
-  physic.ay = 0;
+  physic.set_velocity_x(0);
+  physic.set_velocity_y(0);
+  physic.set_acceleration_x(0);
+  physic.set_acceleration_y(0);
 }
 
 void
@@ -1190,7 +1218,7 @@ Player::activate()
 
 void Player::walk(float speed)
 {
-  physic.vx = speed;
+  physic.set_velocity_x(speed);
 }
 
 void
@@ -1202,12 +1230,12 @@ Player::set_ghost_mode(bool enable)
   if (enable) {
     ghost_mode = true;
     set_group(COLGROUP_DISABLED);
-    physic.gravity_enabled = false;
+    physic.enable_gravity(false);
     log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
   } else {
     ghost_mode = false;
     set_group(COLGROUP_MOVING);
-    physic.gravity_enabled = true;
+    physic.enable_gravity(true);
     log_debug << "You feel solid again." << std::endl;
   }
 }