Making enemies smarter

We created enemy objects in our example game earlier; these enemies are just falling down the screen and shooting, but now we will make cleverer enemies. The topic of AI is a complex one, and it is probably difficult to cover this topic in this chapter. So, instead of covering AI theories, I'll show you techniques that can make your enemies act smarter than usual.

Enabling enemy patrolling

The first thing we want to do to the enemies is to give them the ability to move around the screen, just like our own units. To do this, we'll first add a Timer behavior to the enemyBase object so that it can do something repeatedly. We also need to add an instance variable to it to record the state of the enemies; we don't want them to move around patrolling when they're shooting our units. Let's name this instance variable patrolling and make it a Boolean; don't forget to change the initial value to true.

We want the enemies to go on a patrol every few seconds, but to make it look more realistic, we will randomize the duration when the timer object fires so that the enemy tanks don't move at the same time. We will only trigger this timer once, because the time duration for the next patrolling time is randomized too, and might not be the same as this one:

Enabling enemy patrolling

When the timer fires, enemyBase will look for a point on the screen; this location is randomized to make each enemy go to different places. Looking for a random point on the layout means we want a random value between 0 and the layout width for the x point and a random value between 0 and the layout height for the y point. Thankfully, Construct 2 has expressions for these: LayoutWidth and LayoutHeight. We can use them to get the value of the layout's width and height:

Enabling enemy patrolling

After this, the enemies will find their patrol location and the path to go there; they just need to traverse it:

Enabling enemy patrolling

Then, after arriving at the location, the enemy will start patrolling again; we do this the same way we started the patrol at the start of the layout:

Enabling enemy patrolling

We also need to consider the situation when the enemy encounters the player's unit while it is patrolling. We want the enemy to stop where they found the player's unit and then shoot, instead of shooting while moving. So, we'll stop the enemy unit here, and we'll also change the patrolling instance variable's value to false, because it's not patrolling right now:

Enabling enemy patrolling

Test the game now and you can see that the enemy units stopped before they shoot.

Creating a scout type of enemy

The enemies can patrol on the game field, but without anyone to guide them, they'll only wander around aimlessly. To make the enemy cleverer, we can create a scout for the enemy; this scout searches for the player and tells all other units when it finds the player.

We'll add a new sprite object to the layout as the scout; for the image, we'll use red_sliderRight from UI elementsBase pack. Just like we did for enemyBase, we'll give a Pathfinding behavior to this object; we'll also add a Turret behavior to this object. This Turret behavior is not to shoot at the player but to recognize the player's units as its target and do something if it finds one. As a result of this, we don't want it to rotate, so let's set the Rotate property value to No.

Creating a scout type of enemy

Now, it's ready to search for the player's units.

The logic for the scout unit is like this: it will wander around at a designated area to look for the player's units; if it finds the player, it will inform all the other units about the player's location so that they can go there.

So, the first thing that we need to do is tell the scout unit to look for the player's tank; this is done by adding the player's unit as its target:

Creating a scout type of enemy

Then, we'll tell the enemyScout object to go to a random point on the layout to look for the player's tank. This is similar to when we want the enemy tanks to wander around, except that we don't want it to search the whole layout. We only want the scouts to search for half of the layout to make it easier and faster for them to look for the player's tanks.

So, we'll add a Timer behavior to the scout, like we did for the other enemy units. And like other enemy units, we'll start the timer to search at the start of layout:

Creating a scout type of enemy

When the timer fires, the scout begins the search. Just like the other enemy units, the scouts will move along the path and start the scout timer again after arriving at the random position it got when the timer started.

Creating a scout type of enemy

We will now determine what will happen if the scout finds the player's tank. We decided that the scout will inform all the other units about its location, but how do we do this? We can use an array to store the two values; the x position will be stored in the index number 0, and index number 1 will be used to store the y position. This way, when we want the other enemy units to go, we can tell them to look at the array.

So, let's add a new array to the project and call it scoutArray; we'll fill this array when the enemy scout has found the player's unit. Thankfully, there's a trigger that we can use for this kind of situation: the turret object on target acquired. This trigger is fired when the object finds a target in range; this is when we want to store the object's x and y positions to the array.

Creating a scout type of enemy

Alright, this will store the position we want the other units to go to, but they won't go to this position yet, because there's still no code that tells them to. To make other units go to this position, we only need to add one more action: the enemyBase find path to scoutArray.At(0), scoutArray.At(1))

Creating a scout type of enemy

This will make the enemy base find a path to where the scout currently is. You can test the game to see how it works, but before that, we want to disable the enemyBase object's start timer action at the start of the layout so that the other enemy units don't move until the scout finds the player.

Additional reading

There are a lot of new things that you learned; all of this information can't be contained in a single chapter and still manage to make a game. This is why I will leave you with a list of references for further reading if you want to:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset