C # development Wpf/silverlight animation and games series Tutorials (Game Course): (29) AI Tracker
Through the continuous improvement of the previous 28 sections, the protagonist already has most of the functions of the MMORPG game, but other elves such as monsters are temporarily unable to act, in the last section they are innocent of the protagonist's meat target, they began to shout: "God, please give us soul and wisdom!" In fact, the soul has long been, is the lifeline of the elves. So how do you give the elves wisdom?
The realization of Elves ' wisdom is to give AI (AI) to the elves. Full game engine must have more or less a certain AI, such as chess type game has their unique licensing AI, shooting type game also has the corresponding calculation AI and so on. AI types in different types of games have different embodiment and positioning, can be deep shallow, or even unlimited expansion of the possibility. In the MMORPG type of game is the "tracker" as the most basic elf with AI, but also in the game with a rate of more than 80% of the AI type. What is a "stalker"? Simply put, when a monster discovers a hostile player or other hostile elf, it locks the target and moves to its location. Once the other person enters his or her range of attack, it immediately initiates a physical or magical attack, and if the other person runs away, he or she will continue to pursue and cycle through the previous process, just like a ghost haunting you. Does this describe whether you have a similar feeling in four? Yes, the "tracker" AI is actually the most direct embodiment of the main character combat function module in the previous section. Below I will explain how to implement the Monster's "tracker" AI through code.
Game AI Design is also an important test of the rationality of the game engine framework, we recall that the protagonist and monsters are Qxspirit type control, through the previous chapters we have achieved most of the features of the protagonist, such as mobile, attack, pursuit, injury and so on; Can we use the methods used to implement these functions for all other sprites? That's for sure. For example, the method of the obstacle prediction is willcollide (), the line moving Method Straightmoveto (), the path-seeking Move Method Astarmoveto (), the method of the moving Arrival destination Arrivetarget (), etc. These methods are all without parameters in the previous chapters, the processing of which is based on the leader of the main elements of the corresponding property settings and modifications. At this point we might add a qxspirit spirit parameter to each of these methods and then change all the leader in the method to spirit, so that all the wizard objects can reuse these methods after the division AI:
For example, the original Arrivetarget () method was written as follows:
private bool ArriveTarget() {
return (storyboard != null && storyboard.GetCurrentProgress(Leader) == 1) ? true : false;
}
To be converted to AI General:
private bool ArriveTarget(QXSpirit spirit) {
return (Super.storyboard.ContainsKey(spirit.Name) && SpiritStoryBoard(spirit.Name).GetCurrentProgress(spirit) == 1) ? true : false;
}
It's not just a lot of arguments, but have you noticed that I've defined storyboard as a dictionary to store all the sprites ' moving animations:
public static dictionary<string, storyboard> Storyboard = new dictionary<string, storyboard> ();
Each sprite is first judged by its own name (spirit) as it moves. Name) is the storyboard key value for the key name exists, if it does not exist, creates a new one, and then executes the Move property animation on this storyboard, for example, the line Move method changes to:
private void StraightMoveTo(QXSpirit spirit, Point p) {
……
if (!Super.storyboard.ContainsKey(spirit.Name)) { Super.storyboard.Add(spirit.Name, new Storyboard()); }
……
}
It is convenient and efficient to use a dictionary to manage all the sprites ' mobile animations, and we need to easily get the storyboard of the specified key name through a lambda:
private Storyboard SpiritStoryBoard(string key) {
return Super.storyboard.Single(X => X.Key == key).Value;
}
You can also create a new storyboard each time you move:
private void NewSpiritStoryboard(string key) {
if (!Super.storyboard.ContainsKey(key)) {
Super.storyboard.Add(key, new Storyboard());
}
}