Random Respawning of Enemies
|
The artifact I’ve been working on this time is the random respawing of enemies, set to occur whenever the player returns back to the start. Design-wise it allows for the game to move along many consecutive turns with increasing difficulty. A great disadvantage with this random spawning is that it takes a lot of processing; a fxed spawn-pattern might have been more cost-effective (but, of course, more predictable). To allow enemies to spawn at different locations during the course of the game I’ve had to go back and revise the old code for spawning them: Before and during the Beta phase of the project, enemies spawned only once during the game, at the beginning. They were (supposedly) given random x and y coordinates that were limited to the size of the map, and not directly at the players start location. This was not working correctly due to a small error, and hard to spot. Simply put, the enemies spawned using only the randomized x-coordinate, in both their x and y coordinates. As the enemies kept moving around the map correctly I did not notice this error until this revision of the code. I decided to refine this random code to include a check to see if the position would be inside a radius around the home base, making a new random position if that is the case (a brute-force method that might take some extra time, but at least it works). Moving on from that, the spawning code works, so the next step would be to apply it when enemies need to respawn. I therefore made a ”respawnEnemies” function that would handle all the necessary code specific for their spawning. First it iterates through enemies to fin out how many enemies have been taken out, and if not, how many of each type of enemy there are. Enemies that are dead are deleted from the enemy vector. Secondly, it checks the current size the vector versus the size before iteration to check how many casualties there have been. Thirdly, a for-loop will restock enemies depending on how many casualties there were, adding an extra amount for high casualties and the prescence of zeppelins. In the loop, a random number between 0 and 100 is generated to see what type of enemy is spawned, with an decreasing chance for AAA-cannons and Zeppelins to spawn depending on how many there are alive. ![]()
|
