void
WalkingBadguy::active_update(float elapsed_time, float dest_x_velocity)
{
  BadGuy::active_update(elapsed_time);

  float current_x_velocity = physic.get_velocity_x ();

  if (frozen)
  {
    physic.set_velocity_x (0.0);
    physic.set_acceleration_x (0.0);
  }
  /* We're very close to our target speed. Just set it to avoid oscillation */
  else if ((current_x_velocity > (dest_x_velocity - 5.0))
      && (current_x_velocity < (dest_x_velocity + 5.0)))
  {
    physic.set_velocity_x (dest_x_velocity);
    physic.set_acceleration_x (0.0);
  }
  /* Check if we're going too slow or even in the wrong direction */
  else if (((dest_x_velocity <= 0.0) && (current_x_velocity > dest_x_velocity))
      || ((dest_x_velocity > 0.0) && (current_x_velocity < dest_x_velocity)))
  {
    /* acceleration == walk-speed => it will take one second to get from zero
     * to full speed. */
    physic.set_acceleration_x (dest_x_velocity);
  }
  /* Check if we're going too fast */
  else if (((dest_x_velocity <= 0.0) && (current_x_velocity < dest_x_velocity))
      || ((dest_x_velocity > 0.0) && (current_x_velocity > dest_x_velocity)))
  {
    /* acceleration == walk-speed => it will take one second to get twice the
     * speed to normal speed. */
    physic.set_acceleration_x ((-1.f) * dest_x_velocity);
  }
  else
  {
    /* The above should have covered all cases. */
    assert (23 == 42);
  }

  if (max_drop_height > -1) {
    if (on_ground() && might_fall(max_drop_height+1))
    {
      turn_around();
    }
  }

  if ((dir == LEFT) && (physic.get_velocity_x () > 0.0)) {
    dir = RIGHT;
    set_action (walk_right_action, /* loops = */ -1);
  }
  else if ((dir == RIGHT) && (physic.get_velocity_x () < 0.0)) {
    dir = LEFT;
    set_action (walk_left_action, /* loops = */ -1);
  }
}
Exemple #2
0
void
CaptainSnowball::active_update(float elapsed_time)
{
  if (on_ground() && might_climb(8, 64)) {
    physic.set_velocity_y(-400);
  } else if (on_ground() && might_fall(16)) {
    physic.set_velocity_y(-400);
    walk_speed = BOARDING_SPEED;
    physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed);
  }
  WalkingBadguy::active_update(elapsed_time);
}
void
Spiky::active_update(float elapsed_time)
{
  BadGuy::active_update(elapsed_time);

  if (might_fall(601))
  {
    dir = (dir == LEFT ? RIGHT : LEFT);
    sprite->set_action(dir == LEFT ? "left" : "right");
    physic.set_velocity_x(-physic.get_velocity_x());
  }
}
void
WalkingBadguy::active_update(float elapsed_time)
{
  BadGuy::active_update(elapsed_time);

  if (max_drop_height > -1) {
    if (on_ground() && might_fall(max_drop_height+1))
    {
      turn_around();
    }
  }

}
Exemple #5
0
void
Totem::active_update(float elapsed_time)
{
  BadGuy::active_update(elapsed_time);

  if (!carried_by) {
    if (on_ground() && might_fall())
    {
      dir = (dir == LEFT ? RIGHT : LEFT);
      initialize();
    }

    Sector* s = Sector::current();
    if (s) {
      // jump a bit if we find a suitable totem
      for (std::vector<MovingObject*>::iterator i = s->moving_objects.begin(); i != s->moving_objects.end(); ++i) {
        Totem* t = dynamic_cast<Totem*>(*i);
        if (!t) continue;

        // skip if we are not approaching each other
        if (!((dir == LEFT) && (t->dir == RIGHT))) continue;

        Vector p1 = bbox.p1;
        Vector p2 = t->get_pos();

        // skip if not on same height
        float dy = (p1.y - p2.y);
        if (fabsf(dy - 0) > 2) continue;

        // skip if too far away
        float dx = (p1.x - p2.x);
        if (fabsf(dx - 128) > 2) continue;

        physic.set_velocity_y(JUMP_ON_SPEED_Y);
        p1.y -= 1;
        this->set_pos(p1);
        break;
      }
    }
  }

  if (carried_by) {
    this->synchronize_with(carried_by);
  }

  if (carrying) {
    carrying->synchronize_with(this);
  }

}
Exemple #6
0
void
Totem::active_update(float dt_sec)
{
  BadGuy::active_update(dt_sec);

  if (!carried_by) {
    if (on_ground() && might_fall())
    {
      m_dir = (m_dir == Direction::LEFT ? Direction::RIGHT : Direction::LEFT);
      initialize();
    }

    // jump a bit if we find a suitable totem
    for (auto& obj : Sector::get().get_objects_by_type<MovingObject>()) {
      auto t = dynamic_cast<Totem*>(&obj);
      if (!t) continue;

      // skip if we are not approaching each other
      if (!((m_dir == Direction::LEFT) && (t->m_dir == Direction::RIGHT))) continue;

      Vector p1 = m_col.m_bbox.p1();
      Vector p2 = t->get_pos();

      // skip if not on same height
      float dy = (p1.y - p2.y);
      if (fabsf(dy - 0) > 2) continue;

      // skip if too far away
      float dx = (p1.x - p2.x);
      if (fabsf(dx - 128) > 2) continue;

      m_physic.set_velocity_y(JUMP_ON_SPEED_Y);
      p1.y -= 1;
      set_pos(p1);
      break;
    }
  }

  if (carried_by) {
    synchronize_with(carried_by);
  }

  if (carrying) {
    carrying->synchronize_with(this);
  }

}