Here is how the GameServer spawns enemies randomly.
// Check if its time to attempt to spawn enemies
if (now() >= mTimeForNextSpawn + mLastSpawnTime)
{
// No more enemies are spawned near the end
if (mBattleFieldRect.top > 600.f)
{
std::size_t enemyCount = 1u + randomInt(2);
float spawnCenter = static_cast<float>(randomInt(500) - 250);
// In case only one enemy is being spawned, it appears directly at the spawnCenter
float planeDistance = 0.f;
float nextSpawnPosition = spawnCenter;
// In case there are two enemies being spawned together, each is spawned at each side of the spawnCenter, with a minimum distance
if (enemyCount == 2)
{
planeDistance = static_cast<float>(150 + randomInt(250));
nextSpawnPosition = spawnCenter - planeDistance / 2.f;
}
// Send the spawn orders to all clients
for (std::size_t i = 0; i < enemyCount; ++i)
{
sf::Packet packet;
packet << static_cast<sf::Int32>(Server::SpawnEnemy);
packet << static_cast<sf::Int32>(1 + randomInt(Aircraft::TypeCount-1));
packet << mWorldHeight - mBattleFieldRect.top + 500;
packet << nextSpawnPosition;
nextSpawnPosition += planeDistance / 2.f;
sendToAll(packet);
}
mLastSpawnTime = now();
mTimeForNextSpawn = sf::milliseconds(2000 + randomInt(6000));
}
There a problem with planeDistance where nextSpawnPosition in the loop should be nextSpawnPosition += planeDistance; Otherwise, the plane Distance is halved.
Also, packet << mWorldHeight - mBattleFieldRect.top + 500; seems confusing but means 500.f above the mBattleFieldRect.top after addEnemy because mSpawnPositon.y is equal to mWorldHeight.
void World::addEnemy(Aircraft::Type type, float relX, float relY)
{
SpawnPoint spawn(type, mSpawnPosition.x + relX, mSpawnPosition.y - relY);
mEnemySpawnPoints.push_back(spawn);
}
void World::setCurrentBattleFieldPosition(float lineY)
{
mWorldView.setCenter(mWorldView.getCenter().x, lineY - mWorldView.getSize().y/2);
mSpawnPosition.y = mWorldBounds.height;
}
It would be much safer if there was another method for addEnemy for multiplayer instead of destroying the meaning of mSpawnPosition.
Here is how the GameServer spawns enemies randomly.
There a problem with planeDistance where nextSpawnPosition in the loop should be nextSpawnPosition += planeDistance; Otherwise, the plane Distance is halved.
Also,
packet << mWorldHeight - mBattleFieldRect.top + 500;seems confusing but means 500.f above the mBattleFieldRect.top after addEnemy because mSpawnPositon.y is equal to mWorldHeight.It would be much safer if there was another method for addEnemy for multiplayer instead of destroying the meaning of mSpawnPosition.