import Events , { EventName } from "../../common/util/Events" ; import Tool from "../../common/util/Tool" ; import { TransitionData } from "../../constant/BaseConst" ; import ParamItem from "../parameters/ParamItem" ; import StateMachine from "./StateMachine" ; import Transition from "./Transition" ; /**
 * 管理运行时状态数据
 */ export default class State { /** 记录除AnyState外所有状态数据 */ private static _allStates : Set < State > = new Set ( ) ; public static getAllStates ( ) { return this . _allStates ; } private static add ( s ) { this . _allStates . add ( s ) ; } private static delete ( s ) { this . _allStates . delete ( s ) ; } param state param name private static getUniqueName ( state , name = 'State' ) { let index = 0 ; let findName = false ; while ( ! findName ) { findName = true ; let values = this . _allStates . values ( ) ; for ( let i = 0 ; i < this . _allStates . size ; i ++ ) { let s = values . next ( ) . value ; if ( s === state ) { continue ; } if ( s . _name === `${ name }${ index > 0 ? index : '' }` ) { index ++ ; findName = false ; break ; } } } return `${ name }${ index > 0 ? index : '' }` ; } /**
     * 获取State数量（不包括AnyState）
     */ public static getStateNum ( ) { return this . _allStates . size ; } /**
     * 随机获取一个State
     */ public static getRandState ( ) { if ( this . _allStates . size === 0 ) { return null ; } let values = this . _allStates . values ( ) ; return values . next ( ) . value ; } private _name : string = '' ; /** 状态名（唯一） */ public get name ( ) { return this . _isAnyState ? 'AnyState' : this . _name ; } public set name ( v ) { if ( this . _isAnyState || this . _name === v ) { return ; } this . _name = State . getUniqueName ( this , v ) ; Events . emit ( EventName . STATE_NAME_CHANGED , this ) ; } /** 动画名 */ public motion : string = '' ; private _speed : number = 1 ; /** 动画播放速度 */ public get speed ( ) { return this . _speed ; } public set speed ( v ) { this . _speed = v ; } /** 动画播放速度混合的number类型参数 */ public multiplierParam : ParamItem = null ; /** 动画是否循环播放 */ public loop : boolean = false ; /** 转向别的状态的转换数据 */ private _transitions : Transition [ ] = [ ] ; private _position : cc . Vec2 = cc . v2 ( 0 , 0 ) ; /** 此节点在父状态机中的坐标 */ public get position ( ) { return this . _position ; } private _upStateMachine : StateMachine = null ; /** 父状态机 */ public get upStateMachine ( ) { return this . _upStateMachine ; } private _isAnyState : boolean = false ; /** 是否为AnyState */ public get isAnyState ( ) { return this . _isAnyState ; } constructor ( upStateMachine , isAnyState ) { this . _isAnyState = isAnyState ; if ( ! this . _isAnyState ) { this . _upStateMachine = upStateMachine ; this . _upStateMachine . add ( this ) ; this . _name = State . getUniqueName ( this ) ; State . add ( this ) ; } } /**
     * 销毁
     */ public destroy ( ) { if ( ! this . _isAnyState ) { State . delete ( this ) ; } this . _transitions . forEach ( ( e ) => { e . destroy ( ) ; } ) ; this . _transitions . length = 0 ; } public changeUpStateMachine ( upStateMachine ) { if ( ! this . _isAnyState ) { this . _upStateMachine . delete ( this , false ) ; this . _upStateMachine = upStateMachine ; this . _upStateMachine . add ( this ) ; } } public addTransition ( toState ) { let transition = new Transition ( this , toState ) ; Tool . arrayAdd ( this . _transitions , transition ) ; return transition ; } public deleteTransition ( transition ) { if ( Tool . arrayDelete ( this . _transitions , transition ) ) { transition . destroy ( ) ; } } param to param cur public getTransitions ( to = null , cur = null ) { let transitionArr = [ ] ; if ( to instanceof State ) { this . _transitions . forEach ( ( e ) => { if ( to === e . toState ) { transitionArr . push ( e ) ; } } ) ; } else if ( to instanceof StateMachine ) { if ( to . has ( this ) ) { if ( ! cur ) { cc . error ( `[State.getTransitions] error: cur is null` ) ; return transitionArr ; } this . _transitions . forEach ( ( e ) => { if ( ! cur . has ( e . toState ) ) { transitionArr . push ( e ) ; } } ) ; } else { this . _transitions . forEach ( ( e ) => { if ( to . has ( e . toState ) ) { transitionArr . push ( e ) ; } } ) ; } } else { transitionArr = this . _transitions ; } return transitionArr ; } /**
     * 根据下标交换transition数组中元素
     */ public swapTransition ( idx1 , idx2 ) { Tool . arraySwap ( this . _transitions , idx1 , idx2 ) ; } /**
     * 将元素移动到目标下标的位置，其余元素相对位置不变
     */ public moveTransition ( fromIdx , toIdx ) { Tool . arrayMove ( this . _transitions , fromIdx , toIdx ) ; } public getMultiplierName ( ) { return this . multiplierParam ? this . multiplierParam . paramName : '' ; } public setPosition ( x , y = 0 ) { this . _position = cc . v2 ( x , y ) ; } public getAllTransitionData ( ) { let arr = [ ] ; this . getTransitions ( ) . forEach ( ( e ) => { arr . push ( e . getTransitionData ( ) ) ; } ) ; return arr ; } }