关于FSM(有限状态机)的学习2
来源:互联网 发布:java javascript 编辑:程序博客网 时间:2024/05/16 00:43
以下内容参照《unity3d 人工智能编程》--Aung Sithu Kyaw,Clifford Peters ,Thet Naing Swe编著</span>
最近在研究此书,花费几天时间,略有收获,所以写下此编文章,计划是3章,这是第一章,受个人能力限制,这个只是我的粗陋之见。
首先贴下此书源码下载地址,http://hzbook.com/Books/8580.html,请大家尊重知识产权,如果有需要自行购买(非广告)。
本文主要讲述的是有限状态机的框架第二篇是人工智能所要实现的行为,部分代码没有注释,但应该不影响阅读
这个是继承AdvanceFSM的子类,前文,已经说过了,这个是坦克人工智能所实现的行为
using UnityEngine;using System.Collections;public class NPCTankController : AdvancedFSM { //子弹预制 public GameObject bullet; //生命值 private int health; /// <summary> /// 重写初始化方法 /// </summary> protected override void Initialize() { health = 100; elapsedTime = 0.0f; shootRate = 2.0f; GameObject objPlayer = GameObject.FindGameObjectWithTag("Player"); playerTransform = objPlayer.transform; if (!playerTransform) { print("没有添加Player标签"); return; } //获得子类的坦克炮塔以及炮塔下的旋转点,由于每个人的设计不同,可以相应的改变 turret = gameObject.transform.GetChild(0).transform; bulletSpwanPoint = turret.GetChild(0).transform; //开始实现有限状态机 ConstructFSM(); } protected override void FSMUpdate() { //更新计时器 elapsedTime += Time.deltaTime; } /// <summary> /// 有限状态机 /// </summary> private void ConstructFSM() { //获得巡逻点的 pointList = GameObject.FindGameObjectsWithTag("WandarPoint"); //给巡逻点赋值 Transform[] waypoints = new Transform[pointList.Length]; int i = 0; foreach (GameObject obj in pointList ) { waypoints[i] = obj.transform; i++; } //把对应的状态的键和值加入字典 //巡逻 PatrolState patrol = new PatrolState(waypoints); //发现玩家,并追逐玩家 patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing); //死亡 patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead); //追逐 ChaseState chase = new ChaseState(waypoints); //丢失玩家,转为巡逻 chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); //接近玩家并且能攻击玩家 chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking); //死亡 chase.AddTransition(Transition.NoHealth, FSMStateID.Dead); //进攻 AttackState attack = new AttackState(waypoints); //丢失玩家,转为巡逻 attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); //发现玩家,并追逐玩家 attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing); //死亡 attack.AddTransition(Transition.NoHealth, FSMStateID.Dead); //死亡 DeadState dead = new DeadState(); dead.AddTransition(Transition.NoHealth, FSMStateID.Dead); //添加转移和状态 AddFSMState(patrol); AddFSMState(chase); AddFSMState(attack); AddFSMState(dead); } //碰撞检测 void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Bullet") { health -= 30; if (health <= 0) { SetTransition(Transition.NoHealth); } } } /// <summary> /// 设置状态转移 /// </summary> /// <param name="t"></param> public void SetTransition(Transition t) { PerformTransition(t); } public void ShootBullet() { if (elapsedTime >= shootRate) { Instantiate(bullet, bulletSpwanPoint.position, bulletSpwanPoint.rotation); elapsedTime = 0.0f; } }}
巡逻状态
using UnityEngine;using System.Collections;using System.Collections .Generic ;public class PatrolState : FSMState { public PatrolState (Transform [] wp) { wayPoints = wp; stateID = FSMStateID.Patrolling; curSpeed = 1.0f; curRotSpeed = 1.0f; } /// 该方法用于检查当前状态是否需要转移到另外一个状态 public override void Reason(Transform player, Transform npc) { //转换状态 if (Vector3.Distance(npc.position, player.position)<=300.0f) { npc.GetComponent<NPCTankController>().SetTransition(Transition.SawPlayer); } } /// 该方法为currentState变量执行实际的任务 public override void Act(Transform player, Transform npc) { //寻找下一个巡逻点 if (Vector3.Distance(npc.position, destinationPos) <= 100.0f) { FindNextPoint(); } //旋转炮塔 Quaternion targetRotation = Quaternion.LookRotation(destinationPos - npc.position); npc.rotation = Quaternion.Slerp(npc.rotation, targetRotation, Time.deltaTime * curSpeed); //移动 npc.Translate(Vector3.forward * Time.deltaTime * curSpeed); } }
追逐状态
using UnityEngine;using System.Collections;public class ChaseState : FSMState { private Transform[] waypoints; public ChaseState(Transform[] wp) { this.waypoints = wp; stateID = FSMStateID.Chasing; curRotSpeed = 1.0f; curSpeed = 100.0f; FindNextPoint(); } public override void Reason(Transform player, Transform npc) { destinationPos = player.position; float dist = Vector3.Distance(npc.position, destinationPos); if (dist <= 200.0f) { npc.GetComponent<NPCTankController>().SetTransition(Transition.ReachPlayer); } else if (dist >= 300.0f) { npc.GetComponent<NPCTankController>().SetTransition(Transition.LostPlayer); } } public override void Act(Transform player, Transform npc) { destinationPos = player.position; Quaternion targetRotation = Quaternion.LookRotation(destinationPos - npc.position); npc.rotation = Quaternion.Slerp(npc.rotation, targetRotation, Time.deltaTime * curRotSpeed); npc.Translate(Vector3.forward * Time.deltaTime * curSpeed); }}
进攻状态
using UnityEngine;using System.Collections;public class AttackState : FSMState { //初始化相关属性 public AttackState(Transform[] wp) { wayPoints = wp; stateID = FSMStateID.Attacking; curRotSpeed = 1.0f; curSpeed = 100.0f; FindNextPoint(); } public override void Reason(Transform player, Transform npc) { float dist = Vector3.Distance(npc.position, player.position); if (dist >= 200.0f && dist <= 300.0f) { Quaternion targetRotation = Quaternion.LookRotation(destinationPos - npc.position); npc.rotation = Quaternion.Slerp(npc.rotation, targetRotation, Time.deltaTime * curRotSpeed); npc.Translate(Vector3.forward * Time.deltaTime * curSpeed); npc.GetComponent <NPCTankController >().SetTransition (Transition.SawPlayer ); } else if (dist >= 300.0f) { npc.GetComponent<NPCTankController>().SetTransition(Transition.LostPlayer); } } public override void Act(Transform player, Transform npc) { destinationPos = player.position; Transform turret = npc.GetComponent<NPCTankController>().turret; Quaternion turretRotation = Quaternion.LookRotation(destinationPos - turret.position); turret.rotation = Quaternion.Slerp(turret.rotation, turretRotation, Time.deltaTime * curRotSpeed); npc.GetComponent<NPCTankController>().ShootBullet(); }}
using UnityEngine;using System.Collections;public class DeadState : FSMState { public DeadState() { stateID = FSMStateID.Dead; } public override void Reason(Transform player, Transform npc) { } public override void Act(Transform player, Transform npc) { //死亡状态什么都不做,如果有死亡动作,可以写个方法播放 }}
此篇结束,下一文是我就这次学习的总结
0 0
- 关于FSM(有限状态机)的学习2
- 关于FSM(有限状态机)的学习(1)
- 关于FSM(有限状态机)的学习3---总结
- 有限状态机(FSM)学习总结
- 有限状态机(FSM)学习总结
- 有限状态机(FSM)的分类
- 算法学习笔记(九)有限状态机 FSM 的应用
- Unity3D有限状态机(FSM)学习笔记【2】IState
- FSM有限状态机学习
- Unity3D有限状态机(FSM)学习笔记【1】有限状态机总述
- 一个通用的有限状态机(FSM)框架
- 有限状态机FSM的理解
- 有限状态机FSM的理解
- 有限状态机(FSM)初探
- 简单有限状态机(FSM)
- boost 有限状态机(FSM)
- 有限状态机(FSM)
- 简单有限状态机(FSM)的实现
- HOJ 2148&POJ 2680(DP递推,加大数运算)
- inline和define
- 在Spring中配置使用commons-logging的simplelog来输出日志
- 简单的事情复杂做
- 深入理解Android消息处理系统——Looper、Handler、Thread
- 关于FSM(有限状态机)的学习2
- SQL注入与防范
- 手机卫士第七天笔记
- 在web service间传递java bean
- 天声人語 20160306 見えてきた微生物
- 字符串最后一个单词的长度
- ppz飞控代码不同姿态表示之间转换理解
- 搭建可调试的微信公众平台本地测试环境
- JavaWeb开发_Tomcat安装及配置教程