| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- type Action = {check:() => boolean,entry:() => void,do:(dt:number)=>void,exit:() => void};
- export type ActionCreat = (...args: any) => Action;
- /**行为机*/
- export class ActionMgr {
- private _list:Action[]=[];
- private _current:Action;
- /**加入一个创建行为的闭包的函数
- * ActionCreat = (...args: any) => Action;
- * Action ={check:() => boolean,entry:() => void,do:(dt:number)=>void}
- */
- public add(actionCreat:ActionCreat,...args: any):void{
- this._list.push(actionCreat(...args));
- }
- /**检测和进入行为*/
- public check_entry():void{
- if(this._current?.check()) return;
- this._current?.exit()
- for(let i=0;i<this._list.length;i++){
- if(this._current!=this._list[i] && this._list[i].check()){
- this._current =this._list[i];
- this._current.entry();
- return;
- }
- }
- this._current=null;
- }
- /**是否有正在执行的行为 */
- public get isAction():boolean{
- return this._current!=null;
- }
- /**执行行为*/
- public do(dt:number):void{
- if(this._current)this._current.do(dt);
- }
- /**清除所有行为*/
- public clean():void{
- this._list.length=0;
- this._current=null;
- }
- /*例子
- public test():void{
- const who={state:0};
- this.add((who:{state:number})=>{
- const w = who;
- let time:number=0;
- return{
- check:()=>{
- if(w.state==0){
- return true;
- }
- return false;
- },
- entry:()=>{
- time=0;
- console.log("entry 行为1",time);
- },
- do:(dt:number)=>{
- time+=dt;
- if(time>=2){
- console.log("do",time);
- w.state=1;
- };
- }
- }
- },who);
- this.add((who:{state:number})=>{
- const w = who;
- let time:number=0;
- return{
- check:()=>{
- if(w.state==1){
- return true;
- }
- return false;
- },
- entry:()=>{
- time=0;
- console.log("entry 行为2",time);
- },
- do:(dt:number)=>{
- time+=dt;
- if(time>=2){
- console.log("do",time);
- w.state=0;
- };
- }
- }
- },who);
- }*/
- }
|