Beispiel #1
0
void GameState::createDollar()
{
  auto& dollarList = entityManager->getAll<Dollar>();
  Config& config = Config::getInstance();
  while (dollarList.size() < unsigned(Player::getAlivePlayerCount()))
  {
    Dollar* newDollar = entityManager->create<Dollar>(atlas->findRegion(config.getDollarRegion()),
                                                      0.f,
                                                      0.f,
                                                      config.getDollarWidth(),
                                                      config.getDollarHeight());
    int newDollar_xPos = 0;
    int newDollar_yPos = 0;
    bool valid = false;
    while (!valid)
    { //Loop until valid pos is found
      valid = true;
      newDollar_xPos = rand() % (config.getGameWidth() - (int)newDollar->getWidth());
      newDollar_yPos = rand() % (config.getGameHeight() - (int)newDollar->getHeight());
      newDollar->setX((float)newDollar_xPos);
      newDollar->setY((float)newDollar_yPos);

      for (auto& e : entityManager->getAll<Player>())
      {
        Player* p = reinterpret_cast<Player*>(e);
        if (AbstractEntity::collides(p, newDollar))
        {
          valid = false;
          break;
        }
      }
    }

    newDollar->setX((float)newDollar_xPos);
    newDollar->setY((float)newDollar_yPos);
    newDollar->copyDataForInterpolation();
  }
}