update
This commit is contained in:
@@ -1,22 +1,21 @@
|
||||
import { _decorator, Component } from 'cc';
|
||||
import { EntityStateEnum } from '../Enum';
|
||||
import StateMachine from './StateMachine';
|
||||
import { _decorator, Component } from "cc";
|
||||
import { EntityStateEnum } from "../Enum";
|
||||
import StateMachine from "./StateMachine";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('EntityManager')
|
||||
@ccclass("EntityManager")
|
||||
export abstract class EntityManager extends Component {
|
||||
fsm: StateMachine
|
||||
private _state: EntityStateEnum
|
||||
fsm: StateMachine;
|
||||
private _state: EntityStateEnum;
|
||||
|
||||
get state() {
|
||||
return this._state
|
||||
return this._state;
|
||||
}
|
||||
|
||||
set state(newState) {
|
||||
this._state = newState
|
||||
this.fsm.setParams(newState, true)
|
||||
this._state = newState;
|
||||
this.fsm.setParams(newState, true);
|
||||
}
|
||||
|
||||
abstract init(...args: any[]): void
|
||||
abstract init(...args: any[]): void;
|
||||
}
|
||||
|
||||
|
@@ -1,13 +1,12 @@
|
||||
export default class Singleton {
|
||||
private static _instance: any = null
|
||||
private static _instance: any = null;
|
||||
|
||||
static GetInstance<T>(): T {
|
||||
if (this._instance === null) {
|
||||
this._instance = new this()
|
||||
this._instance = new this();
|
||||
}
|
||||
return this._instance
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
protected constructor() {
|
||||
}
|
||||
}
|
||||
protected constructor() {}
|
||||
}
|
||||
|
@@ -1,48 +1,45 @@
|
||||
import { animation, AnimationClip, Sprite, SpriteFrame } from 'cc'
|
||||
import DataManager from '../Global/DataManager'
|
||||
import { ResourceManager } from '../Global/ResourceManager'
|
||||
import { sortSpriteFrame } from '../Utils'
|
||||
import StateMachine from './StateMachine'
|
||||
import { animation, AnimationClip, Sprite, SpriteFrame } from "cc";
|
||||
import DataManager from "../Global/DataManager";
|
||||
import { ResourceManager } from "../Global/ResourceManager";
|
||||
import { sortSpriteFrame } from "../Utils";
|
||||
import StateMachine from "./StateMachine";
|
||||
|
||||
/***
|
||||
* unit:milisecond
|
||||
*/
|
||||
export const ANIMATION_SPEED = 1 / 10
|
||||
export const ANIMATION_SPEED = 1 / 10;
|
||||
|
||||
/***
|
||||
* 状态(每组动画的承载容器,持有SpriteAnimation组件执行播放)
|
||||
*/
|
||||
export default class State {
|
||||
private animationClip: AnimationClip
|
||||
private animationClip: AnimationClip;
|
||||
constructor(
|
||||
private fsm: StateMachine,
|
||||
private path: string,
|
||||
private wrapMode: AnimationClip.WrapMode = AnimationClip.WrapMode.Normal,
|
||||
private force: boolean = false,
|
||||
private force: boolean = false
|
||||
) {
|
||||
//生成动画轨道属性
|
||||
const track = new animation.ObjectTrack()
|
||||
track.path = new animation.TrackPath().toComponent(Sprite).toProperty('spriteFrame')
|
||||
const spriteFrames = DataManager.Instance.textureMap.get(this.path)
|
||||
const frames: Array<[number, SpriteFrame]> = sortSpriteFrame(spriteFrames).map((item, index) => [
|
||||
index * ANIMATION_SPEED,
|
||||
item,
|
||||
])
|
||||
track.channel.curve.assignSorted(frames)
|
||||
const track = new animation.ObjectTrack();
|
||||
track.path = new animation.TrackPath().toComponent(Sprite).toProperty("spriteFrame");
|
||||
const spriteFrames = DataManager.Instance.textureMap.get(this.path);
|
||||
const frames: Array<[number, SpriteFrame]> = sortSpriteFrame(spriteFrames).map((item, index) => [index * ANIMATION_SPEED, item]);
|
||||
track.channel.curve.assignSorted(frames);
|
||||
|
||||
//动画添加轨道
|
||||
this.animationClip = new AnimationClip()
|
||||
this.animationClip.name = this.path
|
||||
this.animationClip.duration = frames.length * ANIMATION_SPEED
|
||||
this.animationClip.addTrack(track)
|
||||
this.animationClip.wrapMode = this.wrapMode
|
||||
this.animationClip = new AnimationClip();
|
||||
this.animationClip.name = this.path;
|
||||
this.animationClip.duration = frames.length * ANIMATION_SPEED;
|
||||
this.animationClip.addTrack(track);
|
||||
this.animationClip.wrapMode = this.wrapMode;
|
||||
}
|
||||
|
||||
run() {
|
||||
if (this.fsm.animationComponent.defaultClip?.name === this.animationClip.name && !this.force) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
this.fsm.animationComponent.defaultClip = this.animationClip
|
||||
this.fsm.animationComponent.play()
|
||||
this.fsm.animationComponent.defaultClip = this.animationClip;
|
||||
this.fsm.animationComponent.play();
|
||||
}
|
||||
}
|
||||
|
@@ -1,30 +1,30 @@
|
||||
import { _decorator, Animation, Component } from 'cc'
|
||||
import { EntityTypeEnum } from '../Common'
|
||||
import { FsmParamTypeEnum } from '../Enum'
|
||||
const { ccclass } = _decorator
|
||||
import State from './State'
|
||||
import SubStateMachine from './SubStateMachine'
|
||||
import { _decorator, Animation, Component } from "cc";
|
||||
import { EntityTypeEnum } from "../Common";
|
||||
import { FsmParamTypeEnum } from "../Enum";
|
||||
const { ccclass } = _decorator;
|
||||
import State from "./State";
|
||||
import SubStateMachine from "./SubStateMachine";
|
||||
|
||||
type ParamsValueType = boolean | number
|
||||
type ParamsValueType = boolean | number;
|
||||
|
||||
export interface IParamsValue {
|
||||
type: FsmParamTypeEnum
|
||||
value: ParamsValueType
|
||||
type: FsmParamTypeEnum;
|
||||
value: ParamsValueType;
|
||||
}
|
||||
|
||||
export const getInitParamsTrigger = () => {
|
||||
return {
|
||||
type: FsmParamTypeEnum.Trigger,
|
||||
value: false,
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const getInitParamsNumber = () => {
|
||||
return {
|
||||
type: FsmParamTypeEnum.Number,
|
||||
value: 0,
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/***
|
||||
* 流动图
|
||||
@@ -39,47 +39,47 @@ export const getInitParamsNumber = () => {
|
||||
/***
|
||||
* 有限状态机基类
|
||||
*/
|
||||
@ccclass('StateMachine')
|
||||
@ccclass("StateMachine")
|
||||
export default abstract class StateMachine extends Component {
|
||||
private _currentState: State | SubStateMachine = null
|
||||
params: Map<string, IParamsValue> = new Map()
|
||||
stateMachines: Map<string, SubStateMachine | State> = new Map()
|
||||
animationComponent: Animation
|
||||
type: EntityTypeEnum
|
||||
private _currentState: State | SubStateMachine = null;
|
||||
params: Map<string, IParamsValue> = new Map();
|
||||
stateMachines: Map<string, SubStateMachine | State> = new Map();
|
||||
animationComponent: Animation;
|
||||
type: EntityTypeEnum;
|
||||
|
||||
getParams(paramName: string) {
|
||||
if (this.params.has(paramName)) {
|
||||
return this.params.get(paramName).value
|
||||
return this.params.get(paramName).value;
|
||||
}
|
||||
}
|
||||
|
||||
setParams(paramName: string, value: ParamsValueType) {
|
||||
if (this.params.has(paramName)) {
|
||||
this.params.get(paramName).value = value
|
||||
this.run()
|
||||
this.resetTrigger()
|
||||
this.params.get(paramName).value = value;
|
||||
this.run();
|
||||
this.resetTrigger();
|
||||
}
|
||||
}
|
||||
|
||||
get currentState() {
|
||||
return this._currentState
|
||||
return this._currentState;
|
||||
}
|
||||
|
||||
set currentState(newState) {
|
||||
if (!newState) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
this._currentState = newState
|
||||
this._currentState.run()
|
||||
this._currentState = newState;
|
||||
this._currentState.run();
|
||||
}
|
||||
|
||||
/***
|
||||
* 清空所有trigger
|
||||
*/
|
||||
* 清空所有trigger
|
||||
*/
|
||||
resetTrigger() {
|
||||
for (const [, value] of this.params) {
|
||||
if (value.type === FsmParamTypeEnum.Trigger) {
|
||||
value.value = false
|
||||
value.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,6 @@ export default abstract class StateMachine extends Component {
|
||||
/***
|
||||
* 由子类重写,方法目标是根据当前状态和参数修改currentState
|
||||
*/
|
||||
abstract init(...args: any[]): void
|
||||
abstract run(): void
|
||||
abstract init(...args: any[]): void;
|
||||
abstract run(): void;
|
||||
}
|
||||
|
@@ -1,30 +1,30 @@
|
||||
import State from './State'
|
||||
import StateMachine from './StateMachine'
|
||||
import State from "./State";
|
||||
import StateMachine from "./StateMachine";
|
||||
|
||||
/***
|
||||
* 子有限状态机基类
|
||||
* 用处:例如有个idle的state,但是有多个方向,为了让主状态机更整洁,可以把同类型的但具体不同的state都封装在子状态机中
|
||||
*/
|
||||
export default abstract class SubStateMachine {
|
||||
private _currentState: State = null
|
||||
stateMachines: Map<string, State> = new Map()
|
||||
private _currentState: State = null;
|
||||
stateMachines: Map<string, State> = new Map();
|
||||
|
||||
constructor(public fsm: StateMachine) {}
|
||||
|
||||
get currentState() {
|
||||
return this._currentState
|
||||
return this._currentState;
|
||||
}
|
||||
|
||||
set currentState(newState) {
|
||||
if (!newState) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
this._currentState = newState
|
||||
this._currentState.run()
|
||||
this._currentState = newState;
|
||||
this._currentState.run();
|
||||
}
|
||||
|
||||
/***
|
||||
* 具体类实现
|
||||
*/
|
||||
abstract run(): void
|
||||
abstract run(): void;
|
||||
}
|
||||
|
Reference in New Issue
Block a user