Game Dev: Smarter enemies and other tasty features
|
Hi! This is the last week of development for Mole Munch, which means this is the last Mole Munch blogpost! Technically we do have another week after this one, but most of that will be spent obsessing over project reports and finishing work from the graphics course. Even this week the art side of Mole Munch decided to cut down on the hours in order to dedicate more hours to the 2D course. I have been working on getting those last tasty features in before we have to abandon the project. With that comes credits, hi-scores, implementing assets, and I finally fixed the infamous sponge bug! The most important of these features however, is in my opinion the updated enemy AI. Last week during a large playtest arranged by the school, it became increasingly clear that the game needed tweaking in the gameplay department. Most testers did have a lot of fun with the game; switching from dig to surface mode felt great, and avoiding gardeners while grabbing carrots was exciting. However, gardeners did not care about or even notice the player, they would only continue in a straight line and flood any tunnels they collided with. Several testers discovered that with this simple AI, it was easy to stay on the surface, avoid gardeners, and slowly but surely collect carrots. Some players even felt punished for going underground, since there was always a risk of your tunnel getting flooded. So! The problem was that our biggest and most central gameplay mechanic was not really needed since the players got by with half of it. The increased speed or protection from gardeners digmode provided was never neccesairy for these sneaky players since the AI could be easily avoided. How do we solve this? Luckily both playtesters (thanks Simon!) and our design document pointed us in a clear direction; we make the gardeners smarter! The above chart is not quite the current AI; the current version will also stop chasing the player if they get out of their sight; not just when entering digmode. But we did have it documented and planned out so all I had to think about was how to implement it; yay! Not like last week where half the work of the artifact was design. Ok! So how do we code this? First off, when detecting the player (detection happens when the player collides with a large detection hitbox that is attached to each gardener), the gardener must know which direction to walk in order to get to the player. How is the direction determined? To answer that question, allow me to get into some lovely trigonometry! Some searches on google later and I find myself apparently needing a directional vector. A directional vector is a vector which is no longer than 1, making the x and y values of that vector always be less than or equal to zero. This means that the vector is in fact no more than pointing in a direction, which is very useful in my case! If I have the X and Y value of a directional vector pointing towards the player, I can simply multiply the x and y value of the vector with a fitting number to get how far in the x and y axis the gardener should move! So, how do I turn our data (the positions of gardener and player) into this magical vector? I will not go into why the math itself works, but this picture at least describes all of the steps required so that you can code it yourself if needed; This process is repeated every frame of which the gardener is chasing the player, so that the gardener direction is constantly updated to be aimed at the player position, in case the player moves. By only using the first three steps, one can also obtain the angle between objects. This is very useful when you need something to turn towards someting else. Just remember that the rotate functions wants your angle in degrees rather than radians. You will need to convert the angle to degrees by multiplying the angle by 180 divided by pi. C(degrees) = C(radians)*(180/pi) This piece of math is not only used for the gardener AI in Mole Munch. I have also used it for player knockback when taking damage so that you will always be knocked away from the source. It is also used when gardeners are hit with rocks so that they too will be knocked back in believable directions. Math is amazing! End math stuff ~ With the gardener AI completed, it was time to ask our testers what they thought. Overall the reactions were very positive, and several testers felt pretty intimidated by the new AI. Not a single tester was able to stay in surface mode; all players attempting to do so were promptly kicked by an angry gardener who had spotted the player. Mission accomplished! And with that addition, the gameplay of Mole Munch is more or less finished. It feels pretty weird stopping work on something you have spent 200% of your time on for several months. I guess I better go make other games to fill up the time! Thanks for reading, until next time! |




