源码地址:http://pan.baidu.com/s/1dFwzmfB

这篇我们使用上篇文章写的GOAP框架来完成一个实例:

实例内容:

AI有10HP, 需要去站岗,站岗完成扣5HP

当HP<=5必须去补充HP,找到HP球补充HP,每个HP球补充5HP

根据GOAP框架逻辑推断出需要:AI数据提供者,站岗Action,补充HPAction,HP点脚本,站岗点脚本, AI属性脚本

主要脚本:

AI数据提供者

[csharp] view
plain
 copy

  1. public class Worker : MonoBehaviour,IGoap{
  2. private PropertyComponent property;    //属性脚本
  3. public float moveSpeed = 3;            //移动速度
  4. void Start()
  5. {
  6. property = GetComponent<PropertyComponent>();
  7. }
  8. public System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> GetState()
  9. {
  10. HashSet<KeyValuePair<string, object>> state = new HashSet<KeyValuePair<string, object>>();
  11. //当前状态HP是否足够
  12. state.Add(new KeyValuePair<string, object>("EnoughHP", property.HP > 5));
  13. return state;
  14. }
  15. public System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> CreateGoalState()
  16. {
  17. HashSet<KeyValuePair<string, object>> goal = new HashSet<KeyValuePair<string, object>>();
  18. //站岗完成目标
  19. goal.Add(new KeyValuePair<string, object>("SentryComplete", true));
  20. return goal;
  21. }
  22. public void PlanFailed(System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> failedGoal)
  23. {
  24. }
  25. public void PlanFound(System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<string, object>> goal, System.Collections.Generic.Queue<Action> actions)
  26. {
  27. }
  28. public void ActionsFinished()
  29. {
  30. Debug.LogError("FinishedSentry");
  31. }
  32. public void PlanAborted(Action aborterAction)
  33. {
  34. }
  35. public bool MoveAgent(Action tagetAction)
  36. {
  37. //移动
  38. float step = moveSpeed * Time.deltaTime;
  39. gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, tagetAction.target.transform.position, step);
  40. if (gameObject.transform.position.Equals(tagetAction.target.transform.position))
  41. {
  42. tagetAction.IsInRange = true;
  43. return true;
  44. }
  45. else
  46. return false;
  47. }
  48. }

站岗Action

[csharp] view
plain
 copy

  1. public class SentryAction : Action {
  2. private SentryComponent targetSentry;      //站岗目标脚本
  3. private float SentryTimer = 0;             //站岗计时
  4. public float SentryTime = 3;
  5. bool isDone = false;                       //是否完成
  6. public SentryAction()
  7. {
  8. AddPrecondition("EnoughHP", true);  //前置条件:必须HP足够
  9. AddEffect("SentryComplete", true);  //完成效果:站岗完成
  10. }
  11. public override void Reset()
  12. {
  13. targetSentry = null;
  14. SentryTimer = 0;
  15. isDone = false;
  16. }
  17. public override bool IsDone()
  18. {
  19. return isDone;
  20. }
  21. public override bool CheckProcedualPrecondition(GameObject agent)
  22. {
  23. //得到最近的站岗目标
  24. SentryComponent[] sentryComponents = GameObject.FindObjectsOfType<SentryComponent>();
  25. SentryComponent temp = null;
  26. foreach(var v in sentryComponents)
  27. {
  28. if (temp == null)
  29. {
  30. temp = v;
  31. continue;
  32. }
  33. if (Vector3.Distance(agent.transform.position, v.transform.position) < Vector3.Distance(agent.transform.position, temp.transform.position))
  34. temp = v;
  35. }
  36. targetSentry = temp;
  37. target = temp.gameObject;
  38. return temp != null;
  39. }
  40. public override bool Perform(GameObject agent)
  41. {
  42. //站岗执行
  43. SentryTimer += Time.deltaTime;
  44. if(SentryTimer > SentryTime)
  45. {
  46. //站岗完成消耗HP
  47. agent.GetComponent<PropertyComponent>().HP -= 5;
  48. isDone = true;
  49. }
  50. return true;
  51. }
  52. public override bool RequiresInRange()
  53. {
  54. return true;
  55. }
  56. }

补充HPAction

[csharp] view
plain
 copy

  1. public class GetHPAction : Action {
  2. private HPPointComponent targetHPPoint;     //HP点目标脚本
  3. bool isDone = false;
  4. void Start()
  5. {
  6. AddEffect("EnoughHP", true);   //完成效果:HP补充到足够
  7. }
  8. public override void Reset()
  9. {
  10. targetHPPoint = null;
  11. isDone = false;
  12. }
  13. public override bool IsDone()
  14. {
  15. return isDone;
  16. }
  17. public override bool CheckProcedualPrecondition(GameObject agent)
  18. {
  19. HPPointComponent[] tempComponents = GameObject.FindObjectsOfType<HPPointComponent>();
  20. HPPointComponent temp = null;
  21. foreach (var v in tempComponents)
  22. {
  23. if (temp == null)
  24. {
  25. temp = v;
  26. continue;
  27. }
  28. if (Vector3.Distance(agent.transform.position, v.transform.position) < Vector3.Distance(agent.transform.position, temp.transform.position))
  29. temp = v;
  30. }
  31. targetHPPoint = temp;
  32. target = temp.gameObject;
  33. return temp != null;
  34. }
  35. public override bool Perform(GameObject agent)
  36. {
  37. DestroyImmediate(targetHPPoint.gameObject);
  38. isDone = true;
  39. agent.GetComponent<PropertyComponent>().HP += 5;
  40. return true;
  41. }
  42. public override bool RequiresInRange()
  43. {
  44. return true;
  45. }
  46. }

AI决策算法 之 GOAP (三)的更多相关文章

  1. AI决策算法 之 GOAP (一)

    http://blog.csdn.net/lovethrain/article/details/67632033 本系列文章内容部分参考自:http://gamerboom.com/archives/ ...

  2. AI决策算法 之 GOAP (二)

    http://blog.csdn.net/lovethRain/article/details/67634803 GOAP 的主要逻辑: 1.Agent的状态机初始化Idle状态 2.Idel状态根据 ...

  3. 贝叶斯公式由浅入深大讲解—AI基础算法入门

    1 贝叶斯方法 长久以来,人们对一件事情发生或不发生的概率,只有固定的0和1,即要么发生,要么不发生,从来不会去考虑某件事情发生的概率有多大,不发生的概率又是多大.而且概率虽然未知,但最起码是一个确定 ...

  4. 贝叶斯公式由浅入深大讲解—AI基础算法入门【转】

    本文转载自:https://www.cnblogs.com/zhoulujun/p/8893393.html 1 贝叶斯方法 长久以来,人们对一件事情发生或不发生的概率,只有固定的0和1,即要么发生, ...

  5. 2018科大讯飞AI营销算法大赛全面来袭,等你来战!

    AI技术已成为推动营销迭代的重要驱动力.AI营销高速发展的同时,积累了海量的广告数据和用户数据.如何有效应用这些数据,是大数据技术落地营销领域的关键,也是检测智能营销平台竞争力的标准. 讯飞AI营销云 ...

  6. 多维算法思考(三):AB组合问题

    多维算法思考(三):AB组合问题 题目:x个A,y个B可以组合成多少个不同排列的问题. 首先,我们用数学的方式思考,这个问题属于<组合数学>的问题,我们的第一种方法可以用组合思路来求解. ...

  7. 算法 排序lowB三人组 冒泡排序 选择排序 插入排序

    参考博客:基于python的七种经典排序算法   [经典排序算法][集锦]     经典排序算法及python实现 首先明确,算法的实质 是 列表排序.具体就是操作的列表,将无序列表变成有序列表! 一 ...

  8. 实践案例丨基于ModelArts AI市场算法MobileNet_v2实现花卉分类

    概述 MobileNetsV2是基于一个流线型的架构,它使用深度可分离的卷积来构建轻量级的深层神经网,此模型基于 MobileNetV2: Inverted Residuals and Linear ...

  9. 五子棋 AI(AIpha-beta算法)

    博弈树 下过五子棋的人都应该知道,越厉害的人,对棋面的预测程度越深.换句话讲,就是当你下完一步棋,我就能在我的脑海里假设把我所有可能下的地方都下一遍,然后考虑我下完之后你又会下在哪里,最后我根据每次预 ...

随机推荐

  1. LeetCode 017 4Sum

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  2. CountDownLatch,CyclicBarrier,Semaphore的使用

    什么时候使用CountDownLatch CountDownLatch原理和示例 Semaphore信号量的原理和示例 CyclicBarrier的用法 CyclicBarrier 和 CountDo ...

  3. C ~ 指针零散记录

    2016.10.11 一个记录 void MB_float_u16(float f,uint16_t *a,uint16_t *b) { uint8_t *fp; ① uint8_t *ap; ② a ...

  4. web 全栈 学习 1 工程师成长思路图

    第一部分: 技术的三个阶段 实现 ---> 借鉴 ---> 优化 实现:为了实现功能,不考虑可读性.借鉴:阅读开源代码,开源程序,学到编程思想.优化:可读性,可执行. 阶段一:实现多做事, ...

  5. c# wpf ComboBox 动态下拉框 及 动态默认值设定

    1.下拉框声明 <ComboBox x:Name="DirComboBox" Width="150" Height="18" Marg ...

  6. 算法(Algorithms)第4版 练习 1.3.219

    方法实现: //1.3.19 /** * remove the last node in the linked list whose first node is first * * @return r ...

  7. ps炫光素材

    炫光闪电笔刷,炫光闪电笔刷,雷电笔刷,自然闪电Photoshop笔刷下载ps炫光素材 素材下载:http://www.huiyi8.com/sc/8695.html

  8. zoj 3813 Alternating Sum(2014ACMICPC Regional 牡丹江站网络赛 E)

    1.http://blog.csdn.net/dyx404514/article/details/39122743 思路:题目意思很清楚了,这里只说思路. 设区间[L,R],区间长度为len=(R-L ...

  9. 插件_热部署_JRebel

    一.License Server 1.运行反向代理 GitHub地址:https://github.com/ilanyu/ReverseProxy 下载地址:ReverseProxy_windows_ ...

  10. usg6500