mrferrys/finitestatemachine

基于《游戏编程宝石1》第三章第1节的一个有限状态机系统,作者Roberto Cezar Bianchini,2010年7月编写。由MrFerrys移植到PHP。

1.0.0 2022-01-30 18:16 UTC

This package is auto-updated.

Last update: 2024-09-20 23:39:02 UTC


README

基于《游戏编程宝石1》第三章第1节的一个有限状态机系统,作者Roberto Cezar Bianchini,2010年7月编写,由MrFerrys移植到PHP。

描述

基于《游戏编程宝石1》第三章第1节的一个有限状态机系统,作者Roberto Cezar Bianchini,2010年7月编写,由MrFerrys移植到PHP。

入门指南

依赖

  • PHP >= 5

安装

composer require mrferrys/finitestatemachine

用法

  • 如何使用。
	use mrferrys\finitestatemachine\finiteStateMachineClass as fsmClass;
	use mrferrys\finitestatemachine\stateClass as stateClass;
	
	class State_1 extends stateClass
	{
		
		function __construct($id)
		{
		   $this->stateID=$id;
		}
		public function DoBeforeEntering(){
			echo "StateID: $this->stateID DoBeforeEntering <br>";
		}

		public function DoBeforeLeaving(){
			echo "StateID: $this->stateID DoBeforeLeaving <br>";
		}
		
		public function Reason(){
			echo "StateID: $this->stateID Reason <br>";
			if($this->fsm!=null)
			{
				$this->fsm->PerformTransition($this->stateID);
			}
		}
		
		public function Act(){
			echo "StateID: $this->stateID ACTION <br>";
		}
	}

	class stateMachine{
	public $fsm;
	public $states=array(
		"nullState"=>0,
		"state_1"=>1,
		"state_2"=>2,
		"finish"=>3
		);
	public $transitions=array(
		"nullTransition"=>0,
		 "state_1_done" => 1,
		 "state_2_done"=> 2,
		 "finishing"=>3,
		);
		function __construct()
		{
		   
		}

		public function buildFSM(){
			$this->fsm =   new fsmClass();
			$s1         =   new State_1($this->states["state_1"]);
			$s1->fsm    =   $this->fsm;
			$s1->AddTransition($this->transitions["state_1_done"],$this->states["state_2"]);

			$s2         =   new State_1($this->states["state_2"]);
			$s2->fsm    =   $this->fsm;
			$s2->AddTransition($this->transitions["state_2_done"],$this->states["finish"]);

			$s3  =   new State_1($this->states["finish"]);
			$s3->fsm    = $this->fsm;
			$this->fsm->AddState($s1);
			$this->fsm->AddState($s2);
			$this->fsm->AddState($s3);
		}

		public function startProcess(){

			while($this->fsm!=null && $this->fsm->getCurrentStateID() != $this->states["finish"] )
			{
				$this->fsm->getCurrentState()->Reason();
				$this->fsm->getCurrentState()->Act();
				$this->fsm->PerformTransition($this->fsm->getCurrentStateID());
			}
		}

	}
	//MAIN
	$machine= new stateMachine();
	$machine->buildFSM();
	$machine->startProcess();

作者

MrFerrys

版本历史

  • 1.0.0
    • 初始发布 (X.Y.Z 主要.次要.修补)

许可证

有限状态机:基于《游戏编程宝石1》第三章第1节的有限状态机系统

作者:Roberto Cezar Bianchini,2010年7月

使用方法:1. 将有限状态系统的转换和状态标签放在相应的枚举中。

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.

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论这些责任是在合同、侵权或其他法律行为中产生的,还是与软件的使用或其他交易有关。由MrFerrys移植到PHP,作者Roberto Cezar Bianchini

有关详细信息,请参阅LICENSE文件

致谢

有限状态机