fsm demo

博客介绍了如何在Unity3D中使用有限状态机(FSM)来组织AI逻辑,强调将所有状态集中管理,便于理解和维护。
摘要由CSDN通过智能技术生成
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
/**
A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand
 
Written by Roberto Cezar Bianchini, July 2010
 
 
How to use:
1. Place the labels for the transitions and the states of the Finite State System
   in the corresponding enums.
 
2. Write new class(es) inheriting from FSMState and fill each one with pairs (transition-state).
   These pairs represent the state S2 the FSMSystem should be if while being on state S1, a
   transition T is fired and state S1 has a transition from it to S2. Remember this is a Deterministic FSM. 
   You can't have one transition leading to two different states.
 
  Method Reason is used to determine which transition should be fired.
  You can write the code to fire transitions in another place, and leave this method empty if you
  feel it's more appropriate to your project.
 
  Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
  You can write the code for the actions in another place, and leave this method empty if you
  feel it's more appropriate to your project.
 
3. Create an instance of FSMSystem class and add the states to it.
 
4. Call Reason and Act (or whichever methods you have for firing transitions and making the NPCs
    behave in your game) from your Update or FixedUpdate methods.
 
Asynchronous transitions from Unity Engine, like OnTriggerEnter, SendMessage, can also be used, 
just call the Method PerformTransition from your FSMSystem instance with the correct Transition 
when the event occurs.
 
 
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
 
 
/// <summary>
/// Place the labels for the Transitions in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum Transition
{
    NullTransition = 0, // Use this transition to represent a non-existing transition in your system
}
 
/// <summary>
/// Place the labels for the States in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum StateID
{
    NullStateID = 0, // Use this ID to represent a non-existing State in your system
}
 
/// <summary>
/// This class represents the States in the Finite State System.
/// Each state has a Dictionary with pairs (transition-state) showing
/// which state the FSM should be if a transition is fired while this state
/// is the current state.
/// Method Reason is used to determine which transition should be fired .
/// Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
/// </summary>
public abstract class FSMState
{
    protected Diction
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值