Example #1
0
void BaseCreatureEntity::animatePhysics(float delay)
{
	velocity.x *= viscosity;
	velocity.y *= viscosity;

	float velx = velocity.x;
	float vely = velocity.y;

	if (specialState[SpecialStateSlow].active)
  {
    velx *= specialState[SpecialStateSlow].param1;
    vely *= specialState[SpecialStateSlow].param1;
  }

	if (recoil.active)
  {
    if (recoil.stun)
    {
      velx = 0.0f;
      vely = 0.0f;
    }
    velx += recoil.velocity.x;
    vely += recoil.velocity.y;
  }

  spin *= viscosity;
	angle += spin * delay;

	if (isCollidingWithMap())
  {
    stuck();
  }
  else
  {
    if ((int)velx > 0)
    {
        x += velx * delay;

        if (collideWithMap(DIRECTION_LEFT))
        {
            x = (float)((int)x);
            while (collideWithMap(DIRECTION_LEFT))
                x--;
            collideMapRight();
        }
        else if (x > map->getWidth() * tileWidth + offsetX)
        {
            exitMap(DIRECTION_RIGHT);
        }
    }
    else if ((int)velx < 0)
    {
        x += velx * delay;

        if (collideWithMap(DIRECTION_RIGHT))
        {
            x = (float)((int)x);
            while (collideWithMap(DIRECTION_RIGHT))
                x++;
            collideMapLeft();
        }
        else if (x < offsetX)
        {
            exitMap(DIRECTION_LEFT);
        }
    }
  }

    vely += weight * delay;
    if ( vely > maxY) vely = maxY;

    if ((int)vely > 0)
    {
        y += vely * delay;

        if (collideWithMap(DIRECTION_BOTTOM))
        {
            y = (float)((int)y);
            while (collideWithMap(DIRECTION_BOTTOM))
                y--;
            collideMapBottom();
        }
    }
    else if ((int)vely < 0)
    {
        y += vely * delay;

        if (collideWithMap(DIRECTION_TOP))
        {
            y = (float)((int)y);
            while (collideWithMap(DIRECTION_TOP))
                y++;
            collideMapTop();
        }
    }

    if (lifetime > 0)
    {
        if (age >= lifetime) dyingFromAge();
    }
    age += delay;
}
void CollidingSpriteEntity::animate(float delay)
{
  velocity.x *= viscosity;
  velocity.y *= viscosity;

  spin *= viscosity;
  angle += spin * delay;

  velocity.y += weight * delay;

  if ((int)velocity.x > 0)
  {
    x += velocity.x * delay;

    if (collideWithMap(DIRECTION_LEFT))
    {
      x = (float)((int)x);
      while (collideWithMap(DIRECTION_LEFT))
        x--;
      collideMapRight();
    }
    else if (x > map->getWidth() * tileWidth + offsetX)
    {
      exitMap(DIRECTION_RIGHT);
    }
  }
  else if ((int)velocity.x < 0)
  {
    x += velocity.x * delay;

    if (collideWithMap(DIRECTION_RIGHT))
    {
      x = (float)((int)x);
      while (collideWithMap(DIRECTION_RIGHT))
        x++;
      collideMapLeft();
    }
    else if (x < offsetX)
    {
      exitMap(DIRECTION_LEFT);
    }
  }

  velocity.y += weight * delay;
  if ( velocity.y > maxY) velocity.y = maxY;

  if (isCollidingWithMap())
  {
    stuck();
  }
  else
  {
    if ((int)velocity.y > 0)
    {
      y += velocity.y * delay;

      if (collideWithMap(DIRECTION_BOTTOM))
      {
        y = (float)((int)y);
        while (collideWithMap(DIRECTION_BOTTOM))
          y--;
        collideMapBottom();
      }
    }
    else if ((int)velocity.y < 0)
    {
      y += velocity.y * delay;

      if (collideWithMap(DIRECTION_TOP))
      {
        y = (float)((int)y);
        while (collideWithMap(DIRECTION_TOP))
          y++;
        collideMapTop();
      }
    }
  }

  // for platforming
  /*   if ((int)weight == 0)
     {
         if (!(isOnGround()))
         {
             makeFall();
         }
     }*/

  if (lifetime > 0)
  {
    if (age >= lifetime) isDying = true;
  }
  age += delay;
}
Example #3
0
void ItemEntity::animate(float delay)
{
  // Has been identified ?
  if (itemType >= ItemPotion01 && itemType < ItemPotion01 + NUMBER_UNIDENTIFIED)
  {
    if (game().potionEffectKnown(itemType))
    {
      itemType = game().getPotion(itemType);
      frame = itemType;
    }
  }
  // Has been forgotten
  else if (itemType >= ItemPotion01 + NUMBER_UNIDENTIFIED && itemType < FirstEquipItem)
  {
    if (!game().potionEffectKnown(itemType))
    {
      itemType = game().getPotion(itemType);
      frame = itemType;
    }
  }

  if (isMerchandise)
  {
    testSpriteCollisions();
    return;
  }

  if (isFlying)
  {
    if (!isCollidingWithMap() && isOnMap())
    {
      isFlying = false;
      viscosity = 0.96f;
    }
    else
    {
      if (velocity.x < -10 || velocity.x > 10 || velocity.y < -10 || velocity.y > 10) // moving
      {
        // stay in the map
        if (velocity.x < 40 && x < TILE_WIDTH) velocity.x = 220.0f;
        else if (velocity.x > -40 && x > (TILE_WIDTH - 1) * MAP_WIDTH) velocity.x = -220.0f;
        if (velocity.y < 40 && y < TILE_HEIGHT) velocity.y = 220.0f;
        else if (velocity.y > -40 && y > (TILE_HEIGHT - 1) * MAP_HEIGHT) velocity.y = -220.0f;
      }
      else // not moving
      {
        setVelocity(Vector2D(100.0f + rand()% 250));
      }
      // make move
      x += velocity.x * delay;
      y += velocity.y * delay;
      age += delay;
    }
  }
  else // not flying
  {
    if (isCollidingWithMap() || !isOnMap())
    {
      isFlying = true;
    }
    else
    {
      CollidingSpriteEntity::animate(delay);
      if (firstJump || (canBePickedUp() && !isMerchandise))
      {
        jumpTimer -= delay;
        firstJump = false;
        if (jumpTimer <= 0.0f)
        {
          jumpTimer = 2.0f + 0.1f * (rand() % 40);
          h = 0.1f;
          hVelocity = 150.0f;
        }
      }
    }
  }

  if (h > 0.0f)
  {
    h += hVelocity * delay;
    hVelocity -= 1200.0f * delay;
  }

  if (age > 0.7f) testSpriteCollisions();

  if (isBeating)
  {
    timer -= delay;
    if (timer <= 0.0f)
    {
      timer = HEART_BEAT_DELAY;
      SoundManager::getInstance().playSound(SOUND_HEART);
    }
    float sc;
    if (timer > HEART_BEAT_DELAY - 0.25f)
    {
      sc = timer - HEART_BEAT_DELAY + 1.25f;
    }
    else
      sc = 1.0f;
    sprite.setScale(sc, sc);
  }
  if (itemType == ItemBossHeart && !isBeating && game().getCurrentMap()->isCleared())
  {
    // start beating
    isBeating = true;
    timer = HEART_BEAT_DELAY;
  }

  z = y + height / 2;
}