Fancy Mansion – Artificial Intelligence

Last week I did some pre-work for making path-finding work in our game, and this week’s work took off right after last week’s work ended. This week I have been working with getting the AI for Mr. Fancy implemented, making him move to the location where the player made a sound (i.e. when the player is running). Something I have also implemented is a kind of a field of view for Mr. Fancy, which makes him chase the player if they are near and in vision of Mr. Fancy.

As I mentioned last week we were given some code on path-finding and I have used this code to implement the searching mechanism for Mr. Fancy. To begin with Mr. Fancy has a patrol route, which is a couple of tiles in the map where he path-finds between them. When the player is running (pressing down SHIFT and a directional button) and if Mr. Fancy is within hearing range, a function then searches the shortest way to the tile that the player stepped on. When the way is found I store the coordinates for each of the tiles in this path in a vector, which is then past to the Enemy class where he moves to each of these tiles. When Mr. Fancy reaches his goal he then gets into a idle state where he waits for a couple of seconds, and then he searches for and moves to the tile in his patrol route that he left when he started searching for the player. If the player would make a new sound somewhere else, while Mr. Fancy is searching for the player, he would then search to this position instead. This is how the path-finding basically works in our game.

To make Mr. Fancy chase the player when the player are near him, I have made a way for Mr. Fancy to be able to ”see” the player. Mr. Fancy has a set distance which he can see and he should not be able to see the player through the walls. To make this work I check all the colliders within a rectangle (where Mr. Fancy is in the middle and half the width is his sight range) and then check if a line between the player and Mr. Fancy intersects with any of the lines in the colliders. If an intersection happens then he does not see the player, but if no intersection exists then he will spot the player and then chase the player down. The following pictures will show a bit how this works.

FancyMansion_Blogpost_5_printscreen_1

In this picture he does not see the player.

FancyMansion_Blogpost_5_printscreen_2

In this picture he does see the player and will likely shoot in any moment.

FancyMansion_Blogpost_5_printscreen_3

In this picture we can see a rough estimate of the outline (yellow) of the collider for that wall and also a line between the player and Mr. Fancy (blue), which does intersect with the collider thus making the player unseen by the enemy.

There are also some special cases, i.e. when the player is wearing the armor Mr. Fancy will hear the player even when the player is only walking, but Mr. Fancy won’t shoot if the player stands still when he finds the player.

Next up I will be implementing some more of the pickups and polishing on the AI, and also implement a HUD to make the game a bit easier to understand.

About Sven Almberg

2014  Programming