- 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.
- Learn ECMAScript(Second Edition)
- Software Defined Networking with OpenFlow
- 精通軟件性能測試與LoadRunner實戰(第2版)
- Animate CC二維動畫設計與制作(微課版)
- INSTANT Mercurial SCM Essentials How-to
- The HTML and CSS Workshop
- RISC-V體系結構編程與實踐(第2版)
- Quantum Computing and Blockchain in Business
- Odoo 10 Implementation Cookbook
- 大話Java:程序設計從入門到精通
- R數據科學實戰:工具詳解與案例分析
- Learning VMware vSphere
- Practical Microservices
- Django Design Patterns and Best Practices
- Android智能手機APP界面設計實戰教程