Graphics Reference
In-Depth Information
Data manager is used to store player information (see Chapter 3 for a full breakdown
on t his):
// if a player manager is not set in the editor, let's try
// to find one
if(myPlayerManager==null)
myPlayerManager= myGO.GetComponent<BasePlayerManager>();
// cache ref to data manager
myDataManager= myPlayerManager.DataManager;
This code sets up the player name and default health amount from the variable
startHealthAmount. The player name is not used in this game, but we set it here as a mat-
ter of consistency across the examples:
// set default data
myDataManager.SetName("Player");
myDataManager.SetHealth(startHealthAmount);
When the isAIControlled Boolean is set to true, the player's name is set to AIPlayer
and the script needs to run an InitAI() function to get the AI controller all ready to go:
if(isAIControlled)
{
// set our name to an AI player
myDataManager.SetName("AIPlayer");
// set up AI
InitAI();
}
}
AI players need a little more initialization setup than users. The InitAI() function starts
out by using GameObject.GetComponent() to find the BaseAIController component:
void InitAI()
{
// cache a reference to the AI controller
AIController= myGO.GetComponent<BaseAIController>();
For the AI control to work, it is essential that a BaseAIController component is
added to the player. Rather than error out, the script goes ahead and uses GameObject.
AddComponent() to add a BaseAIController if one is not already there:
// check to see if we found an AI controller component, if
// not we add one here
if(AIController==null)
AIController= myGO.AddComponent<BaseAIController>();
Call Init() on the controller (for more on what actually happens during Init() of the
AIController, take a look at the AI system in Chapter 9):
// initalize the AI controller
AIController.Init();
Search WWH ::




Custom Search