Click to Play!

Monday, January 20, 2014

DragCore: Spawning Enemies


     Making enemies pop out was a big pile of code in the old DragCore prototype. With the new one it has been simplified to a few lines of code thanks to the greatness of Unity. What used to be an array and tons of code to alter each of the 200 elements became a matter of instantiating up to 20 objects and have them reset themselves. See the code that makes it work below the break.





     Each enemy object has a script attached which is the code that tells them what to do. The stuff I'm showing here is all the outside stuff controlling how, when, and where to appear. The code above is a for loop (code that will repeat itself a set number of times). It's set to create 20 new enemies right now while I'm still testing things. I plan to change it to scale with difficulty later. This loop calls on a method I made called PickEnemy() which chooses which enemy type (spaceship, comet, etc.) and color will appear.

    Instantiate() loads one of the pre-made enemy objects and places it into the scene. Doing this a lot could cause lots of lag, as I learned firsthand when making TriGrid. It's best to only instantiate and delete simple objects that don't respawn instantly. The enemies are complex so I chose to have them set their locations off-screen when they die instead of deleting themselves and loading again.


      Right now I have a method to give the game some basic settings I can alter in order to test whatever I need to. The available types and colors for enemies to pick are listed in true/false arrays that PickEnemy() can scan and choose from.


     When enemies are spawned this function checks to see which enemy variations are available and randomly selects one. As a bonus, I have extra copies of the color and type arrays to make it so that the random generators stop generating enemies they spawned before and reset the unavailable ones once it's run out. It's giving me bugs right now and I'll need to get it sorted out tomorrow. Right now, even with settings allowing all enemy types and colors, only a few are showing up.

    Onward to finishing up DragCore!

No comments:

Post a Comment