This commit is contained in:
YipLee
2021-01-19 22:30:12 +08:00
commit c4f716c8e9
165 changed files with 61216 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
import { ConditionData, LogicType } from "../../constant/BaseConst";
import ParamItem from "../parameters/ParamItem";
/**
* 管理运行时单个条件数据
*/
export default class Condition {
private _paramItem: ParamItem = null;
public get paramItem() { return this._paramItem; }
public value: number = 0;
public logic: LogicType = LogicType.EQUAL;
constructor(paramItem: ParamItem) {
this.reset(paramItem);
}
/**
* 销毁
*/
public destroy() {
}
public reset(paramItem: ParamItem) {
this._paramItem = paramItem;
this.value = 0;
this.logic = LogicType.EQUAL;
}
public getConditionData() {
let data: ConditionData = {
param: this._paramItem.paramName,
value: this.value,
logic: this.logic
};
return data;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "2ad76e07-2aca-40af-8bb1-3a90782bf375",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,224 @@
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 {
//#region 静态成员
/** 记录除AnyState外所有状态数据 */
private static _allStates: Set<State> = new Set();
public static getAllStates() {
return this._allStates;
}
private static add(s: State) {
this._allStates.add(s);
}
private static delete(s: State) {
this._allStates.delete(s);
}
/**
* 获取唯一的状态名
* @param state 需要命名的state
* @param name 传入的命名
*/
private static getUniqueName(state: State, name: string = '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: State = 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(): number {
return this._allStates.size;
}
/**
* 随机获取一个State
*/
public static getRandState(): State {
if (this._allStates.size === 0) {
return null;
}
let values = this._allStates.values();
return values.next().value;
}
//#endregion
private _name: string = '';
/** 状态名(唯一) */
public get name() { return this._isAnyState ? 'AnyState' : this._name; }
public set name(v: string) {
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: number) {
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: StateMachine, isAnyState: boolean) {
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: StateMachine) {
if (!this._isAnyState) {
this._upStateMachine.delete(this, false);
this._upStateMachine = upStateMachine;
this._upStateMachine.add(this);
}
}
public addTransition(toState: State): Transition {
let transition = new Transition(this, toState);
Tool.arrayAdd(this._transitions, transition);
return transition;
}
public deleteTransition(transition: Transition) {
if (Tool.arrayDelete(this._transitions, transition)) {
transition.destroy();
}
}
/**
* 获取指向目标的Transition不传参则返回全部
* @param to
* @param cur 当前编辑器视图所在状态机
*/
public getTransitions(to: State | StateMachine = null, cur: StateMachine = null): Transition[] {
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: number, idx2: number) {
Tool.arraySwap(this._transitions, idx1, idx2);
}
/**
* 将元素移动到目标下标的位置,其余元素相对位置不变
*/
public moveTransition(fromIdx: number, toIdx: number) {
Tool.arrayMove(this._transitions, fromIdx, toIdx);
}
public getMultiplierName() {
return this.multiplierParam ? this.multiplierParam.paramName : '';
}
public setPosition(x: number | cc.Vec2 | cc.Vec3, y: number = 0) {
this._position = cc.v2(x, y);
}
public getAllTransitionData(): TransitionData[] {
let arr: TransitionData[] = [];
this.getTransitions().forEach((e) => {
arr.push(e.getTransitionData());
});
return arr;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "9cc65592-425e-4040-b1a4-6d7c08e12876",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,315 @@
import Events, { EventName } from "../../common/util/Events";
import State from "./State";
import Transition from "./Transition";
/**
* 管理运行时状态机数据
*/
export default class StateMachine {
//#region 静态成员
/** 记录子状态机数据 */
private static _allSubStateMachines: Set<StateMachine> = new Set();
private static add(s: StateMachine) {
this._allSubStateMachines.add(s);
}
private static delete(s: StateMachine) {
this._allSubStateMachines.delete(s);
}
/**
* 获取唯一的状态机名
* @param stateMachine 需要命名的StateMachine
* @param name 传入的命名
*/
private static getUniqueName(stateMachine: StateMachine, name: string = 'StateMachine') {
let index = 0;
let findName = false;
while (!findName) {
findName = true;
let values = this._allSubStateMachines.values();
for (let i = 0; i < this._allSubStateMachines.size; i++) {
let s: StateMachine = values.next().value;
if (s === stateMachine) {
continue;
}
if (s._name === `${name}${index > 0 ? index : ''}`) {
index++;
findName = false;
break;
}
}
}
return `${name}${index > 0 ? index : ''}`;
}
//#endregion
private _name: string = '';
/** 状态机名(唯一) */
public get name() { return this.isMain ? 'BaseLayer' : this._name; }
public set name(v: string) {
if (this._name === v || this.isMain) {
return;
}
this._name = StateMachine.getUniqueName(this, v);
Events.emit(EventName.STATE_MACHINE_NAME_CHANGED, this);
}
private _position: cc.Vec2 = cc.v2(0, 0);
/** 此节点在父状态机中的坐标 */
public get position() { return this._position; }
private _layerPos: cc.Vec2 = cc.v2(0, 0);
/** 此状态机视图坐标 */
public get layerPos() { return this._layerPos; }
private _layerScale: number = 1;
/** 此状态机视图缩放 */
public get layerScale() { return this._layerScale; }
private _anyStatePos: cc.Vec2 = cc.v2(-360, 300);
/** AnyState节点在此状态机视图中的坐标 */
public get anyStatePos() { return this._anyStatePos; }
private _upStateMachinePos: cc.Vec2 = cc.v2(360, 300);
/** 父状态机节点在此状态机视图中的坐标 */
public get upStateMachinePos() { return this._upStateMachinePos; }
private _upStateMachine: StateMachine = null;
/** 父状态机 */
public get upStateMachine() { return this._upStateMachine; }
private _subStateMachines: Set<StateMachine> = new Set();
/** 内部子状态机 */
public get subStateMachines() { return this._subStateMachines; }
private _subStates: Set<State> = new Set();
/** 内部状态 */
public get subStates() { return this._subStates; }
/** 是否为主状态机 */
public get isMain() { return this._upStateMachine === null; }
constructor(upStateMachine: StateMachine) {
this._upStateMachine = upStateMachine;
if (!this.isMain) {
this._upStateMachine.add(this);
this._name = StateMachine.getUniqueName(this);
StateMachine.add(this);
}
}
/**
* 销毁
*/
public destroy() {
if (!this.isMain) {
StateMachine.delete(this);
}
this._subStateMachines.forEach((e) => {
e.destroy();
});
this._subStateMachines.clear();
this._subStates.forEach((e) => {
e.destroy();
});
this._subStates.clear();
}
/**
* 更改父状态机
*/
public changeUpStateMachine(upStateMachine: StateMachine) {
if (!this.isMain) {
this._upStateMachine.delete(this, false);
this._upStateMachine = upStateMachine;
this._upStateMachine.add(this);
}
}
/**
* 判断某个状态或状态机是否在当前状态机内部(默认递归)
* @param sub
* @param recursive 是否递归查找内部
*/
public has(sub: State | StateMachine, recursive: boolean = true): boolean {
if (this.isMain && recursive) {
return true;
}
if (sub instanceof State) {
if (!recursive) {
return this._subStates.has(sub);
} else {
if (this._subStates.has(sub)) {
return true;
}
let values = this._subStateMachines.values();
for (let i = 0; i < this._subStateMachines.size; i++) {
let stateMachine: StateMachine = values.next().value;
if (stateMachine.has(sub)) {
return true;
}
}
}
} else if (sub instanceof StateMachine) {
if (!recursive) {
return this._subStateMachines.has(sub);
} else {
if (this._subStateMachines.has(sub)) {
return true;
}
let values = this._subStateMachines.values();
for (let i = 0; i < this._subStateMachines.size; i++) {
let stateMachine: StateMachine = values.next().value;
if (stateMachine.has(sub)) {
return true;
}
}
}
}
return false;
}
public add(sub: State | StateMachine) {
if (sub instanceof State) {
if (sub.isAnyState) {
return;
}
this._subStates.add(sub);
} else if (sub instanceof StateMachine) {
this._subStateMachines.add(sub);
}
}
public delete(sub: State | StateMachine, destroy: boolean = true) {
if (destroy) {
sub.destroy();
}
if (sub instanceof State) {
this._subStates.delete(sub);
} else if (sub instanceof StateMachine) {
this._subStateMachines.delete(sub);
}
}
/**
* 将目标从别的状态机移入当前状态机内
* @param target 目标状态或状态机
* @returns 是否成功进行移入操作
*/
public moveTargetIn(target: State | StateMachine): boolean {
if (target instanceof State) {
if (this._subStates.has(target)) {
return false;
}
target.changeUpStateMachine(this);
return true;
} else if (target instanceof StateMachine) {
if (this === target || this._subStateMachines.has(target) || target.has(this)) {
return false;
}
target.changeUpStateMachine(this);
return true;
}
return false;
}
/**
* 递归查找内部所有子状态
*/
public getAllSubStates(states: Set<State> = new Set()): Set<State> {
this._subStates.forEach((e) => {
states.add(e);
});
this._subStateMachines.forEach((e) => {
e.getAllSubStates(states);
});
return states;
}
/**
* 查找所有外部状态
*/
public getAllOutStates(): Set<State> {
let states: Set<State> = new Set()
let allSub = this.getAllSubStates();
State.getAllStates().forEach((e) => {
if (!allSub.has(e)) {
states.add(e);
}
});
return states;
}
/**
* 获取所有子状态指向目标的Transition递归查找
* @param cur 当前编辑器视图所在状态机
*/
private getSubTransitions(to: State | StateMachine, transitionArr: Transition[], cur: StateMachine) {
this._subStates.forEach((e) => {
transitionArr = transitionArr.concat(e.getTransitions(to, cur));
});
this._subStateMachines.forEach((e) => {
transitionArr = e.getSubTransitions(to, transitionArr, cur);
});
return transitionArr;
}
/**
* 获取所有外部状态指向目标的Transition
* @param exclude 遍历子状态机时过滤的状态机
* @param cur 当前编辑器视图所在状态机
*/
private getOutTransitions(to: State | StateMachine, transitionArr: Transition[], exclude: StateMachine, cur: StateMachine) {
this._subStates.forEach((e) => {
transitionArr = transitionArr.concat(e.getTransitions(to, cur));
});
this._subStateMachines.forEach((e) => {
if (e !== exclude)
transitionArr = e.getSubTransitions(to, transitionArr, cur);
});
if (this.isMain) {
return transitionArr;
}
transitionArr = this._upStateMachine.getOutTransitions(to, transitionArr, this, cur);
return transitionArr;
}
/**
* 获取指向目标的Transition
* @param to
*/
public getTransitions(to: State | StateMachine): Transition[] {
let transitionArr = [];
if (this.has(to)) {
transitionArr = this.getOutTransitions(to, transitionArr, to.upStateMachine, to.upStateMachine);
} else {
transitionArr = this.getSubTransitions(to, transitionArr, this._upStateMachine);
}
return transitionArr;
}
public setPosition(x: number | cc.Vec2 | cc.Vec3, y: number = 0) {
this._position = cc.v2(x, y);
}
public setLayerPos(x: number | cc.Vec2 | cc.Vec3, y: number = 0) {
this._layerPos = cc.v2(x, y);
}
public setLayerScale(scale: number) {
this._layerScale = scale;
}
public setAnyStatePos(x: number | cc.Vec2 | cc.Vec3, y: number = 0) {
this._anyStatePos = cc.v2(x, y);
}
public setUpStateMachinePos(x: number | cc.Vec2 | cc.Vec3, y: number = 0) {
this._upStateMachinePos = cc.v2(x, y);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "81405035-481c-437c-93ec-8a889596391a",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,89 @@
import Events, { EventName, preloadEvent } from "../../common/util/Events";
import Tool from "../../common/util/Tool";
import { TransitionData } from "../../constant/BaseConst";
import ParamItem from "../parameters/ParamItem";
import Condition from "./Condition";
import State from "./State";
/**
* 管理运行时单个状态转换数据
*/
export default class Transition {
private _fromState: State = null;
public get fromState() { return this._fromState; }
private _toState: State = null;
public get toState() { return this._toState; }
/** 状态转换的条件 */
private _conditions: Condition[] = [];
public get conditions() { return this._conditions; }
/** 状态转换是否需要满足动画播放结束 */
public hasExitTime: boolean = false;
constructor(from: State, to: State) {
this._fromState = from;
this._toState = to;
Events.targetOn(this);
}
/**
* 销毁
*/
public destroy() {
this._conditions.forEach((e) => {
e.destroy();
});
this._conditions.length = 0;
Events.targetOff(this);
}
/**
* 状态转换字符串
*/
public getTransStr() {
return `${this._fromState.name} -> ${this._toState.name}`;
}
public addCondition(paramItem: ParamItem) {
let condition = new Condition(paramItem);
Tool.arrayAdd(this._conditions, condition);
return condition;
}
public deleteCondition(condition: Condition) {
if (Tool.arrayDelete(this._conditions, condition)) {
condition.destroy();
}
}
/**
* 将元素移动到目标下标的位置,其余元素相对位置不变
*/
public moveCondition(fromIdx: number, toIdx: number) {
Tool.arrayMove(this._conditions, fromIdx, toIdx);
}
public getTransitionData() {
let data: TransitionData = {
toState: this.toState.name,
hasExitTime: this.hasExitTime,
conditions: []
};
this._conditions.forEach((e) => {
data.conditions.push(e.getConditionData());
});
return data;
}
@preloadEvent(EventName.PARAM_DELETE)
private onEventParamDelete(paramItem: ParamItem) {
for (let i = this._conditions.length - 1; i >= 0; i--) {
if (this._conditions[i].paramItem === paramItem) {
this._conditions.splice(i, 1);
}
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "c5d54a4e-d4e7-4c79-ab84-4ee81a0633db",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}