- Practical Game AI Programming
- Micael DaGra?a
- 455字
- 2021-07-02 20:43:04
Defining the states
Let's define what triggers each state and how the AI should choose the correct state in different scenarios. The PASSIVE state will be the default state, and the game will start in that position until the player comes across our character. The DEFENSIVE state will be used in two different situations–if the player comes from the right side and if he has already confronted the player and has low HP. Finally, the AGGRESSIVE state will be activated if the player comes from the left side or has already arrived at the middle area:
public class Enemy : MonoBehaviour {
private int Health = 100;
private bool statePassive;
private bool stateAggressive;
private bool stateDefensive;
private bool triggerL;
private bool triggerR;
private bool triggerM;
// Use this for initialisation
void Start () {
statePassive = true;
}
// Update is called once per frame
void Update () {
// The AI will remain passive until an interaction with the player occurs
if(Health == 100 && triggerL == false && triggerR == false && triggerM
== false)
{
statePassive = true;
stateAggressive = false;
stateDefensive = false;
}
// The AI will shift to the defensive mode if player comes from the
right side or if the AI is below 20 HP
if(Health<= 100 && triggerR == true || Health<= 20)
{
statePassive = false;
stateAggressive = false;
stateDefensive = true;
}
// The AI will shift to the aggressive mode if player comes from the
left side or it's on the middle and AI is above 20HP
if(Health> 20 && triggerL == true || Health> 20 && triggerM == true)
{
statePassive = false;
stateAggressive = true;
stateDefensive = false;
}
}
}
We added the trigger variables triggerL, trigger, and triggerM and also defined when the AI should change from one behavior state to another. At this point, our enemy already knows what to do in different situations that may occur during gameplay according to the player's position.
Now we just need to determine what will happen on each state, because that is what differentiates a DEFENSIVE state from an AGGRESSIVE one. For this specific enemy, where his main function is to protect the entrance of the building, we want him to stay put at all times and to never run after the player. This is because the AI doesn't know that it is just one person and can't take the risk of going after just one soldier if there's a possibility of being greeted by several. This will help to give a little realism to the actions of the enemy. We'll also use the defensive behavior state for the moment where the enemy feels that it is losing the battle and is about to die, or when it has the advantage of using the building to protect itself while the player doesn't. Finally, the aggressive state will be used when the AI sees a clear advantage to kill or if it doesn't have any other options.
- Flask Web全棧開發(fā)實(shí)戰(zhàn)
- FuelPHP Application Development Blueprints
- Windows系統(tǒng)管理與服務(wù)配置
- Vue.js快速入門與深入實(shí)戰(zhàn)
- Java加密與解密的藝術(shù)(第2版)
- SQL for Data Analytics
- Network Automation Cookbook
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實(shí)訓(xùn)版)
- INSTANT Sinatra Starter
- 圖數(shù)據(jù)庫實(shí)戰(zhàn)
- 智能搜索和推薦系統(tǒng):原理、算法與應(yīng)用
- Zabbix Performance Tuning
- 多媒體技術(shù)及應(yīng)用
- Drupal 8 Development:Beginner's Guide(Second Edition)
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)