diff --git a/assets/resources/scenes/drag.scene b/assets/resources/scenes/drag.scene index cd90c06..a3918d8 100644 --- a/assets/resources/scenes/drag.scene +++ b/assets/resources/scenes/drag.scene @@ -290,7 +290,7 @@ "_priority": 1073741824, "_fov": 45, "_fovAxis": 0, - "_orthoHeight": 459.1304347826087, + "_orthoHeight": 422.9685807150596, "_near": 1, "_far": 2000, "_color": { diff --git a/assets/scripts/components/GPDrag.ts b/assets/scripts/components/GPDrag.ts index 15f6be2..0c0bb98 100644 --- a/assets/scripts/components/GPDrag.ts +++ b/assets/scripts/components/GPDrag.ts @@ -1,4 +1,5 @@ import { _decorator, CCBoolean, TweenEasing , CCFloat, CCString, Component, easing, Enum, EventTouch, isValid, Node, NodeEventType, Tween, tween, Vec2, Vec3 } from 'cc'; +import { GPWorkFlow, GPWorkFlowNode } from './GPWorkFlow'; const { ccclass, property } = _decorator; export enum FloatType { None, @@ -67,12 +68,40 @@ export class GPDrag extends Component { public succeedCallback:Function = null; private backTween:Tween - + public backHomeWorkFlow = new GPWorkFlow(); start() { this.node.on(NodeEventType.TOUCH_START, this.OnDragStart, this) this.node.on(NodeEventType.TOUCH_MOVE, this.OnDragMove, this) this.node.on(NodeEventType.TOUCH_END, this.OnDragEnd, this) this.node.on(NodeEventType.TOUCH_CANCEL, this.OnDragEnd, this) + + let moveNode = new GPWorkFlowNode(); + this.backHomeWorkFlow.headNode = moveNode; + let self = this; + moveNode.OnStart = (wfNode:GPWorkFlowNode)=>{ + let p = new Vec3(self.dragStartPos.x, self.dragStartPos.y, 0); + switch(this.backHomeType) { + case DragBackHomeType.SetPosition: + self.node.setPosition(p) + wfNode.done(); + break; + case DragBackHomeType.Tween: + self.backTween && self.backTween.stop() + let easing:TweenEasing = EasingString[self.backTweenEasing] as TweenEasing + self.backTween = tween(self.node) + .to(self.backTweenTime, {position:p}, {easing:easing}).call(()=>{ + wfNode.done(); + }).start() + break; + } + } + + let arrivedNode = new GPWorkFlowNode(); + moveNode.nextNode = arrivedNode; + arrivedNode.OnStart = (wfNode:GPWorkFlowNode)=>{ + self.OnArrivedHome(); + wfNode.done(); + } } private OnDragStart(e:EventTouch) { @@ -106,19 +135,8 @@ export class GPDrag extends Component { if (this.succeedCheck && this.succeedCheck(e)) { this.succeedCallback && this.succeedCallback(e) } else if(this.backHomeWhenFailed){ - let p = new Vec3(this.dragStartPos.x, this.dragStartPos.y, 0); - switch(this.backHomeType) { - case DragBackHomeType.SetPosition: - this.node.setPosition(p) - this.OnArrivedHome(); - break; - case DragBackHomeType.Tween: - this.backTween && this.backTween.stop() - let easing:TweenEasing = EasingString[this.backTweenEasing] as TweenEasing - this.backTween = tween(this.node) - .to(this.backTweenTime, {position:p}, {easing:easing}).call(this.OnArrivedHome.bind(this)).start() - break; - } + this.backHomeWorkFlow.start(); + } } @@ -141,4 +159,7 @@ export class GPDrag extends Component { this.node.off(NodeEventType.TOUCH_END, this.OnDragEnd, this) this.node.off(NodeEventType.TOUCH_CANCEL, this.OnDragEnd, this) } + protected update(dt: number): void { + this.backHomeWorkFlow.update(dt) + } } \ No newline at end of file diff --git a/assets/scripts/components/GPWorkFlow.ts b/assets/scripts/components/GPWorkFlow.ts new file mode 100644 index 0000000..de0b3c4 --- /dev/null +++ b/assets/scripts/components/GPWorkFlow.ts @@ -0,0 +1,80 @@ +import { _decorator, Component, Node } from 'cc'; +const { ccclass, property } = _decorator; + +export class GPWorkFlowNode { + public OnStart:Function; + public OnUpdate:Function; + public OnEnd:Function; + public nextNodes:GPWorkFlowNode[] = new Array(); + public get nextNode():GPWorkFlowNode { + return this.nextNodes.length > 0 ? this.nextNodes[0] : null; + } + public set nextNode(_nextNode:GPWorkFlowNode) { + if (this.nextNodes.length == 0) { + this.nextNodes.push(_nextNode); + } else { + this.nextNodes[0] = _nextNode; + } + } + private _nextIdx = -1; + public get nextIdx() { + return this._nextIdx; + } + public done(nexIdx = 0) { + this._nextIdx = nexIdx; + if (this.OnEnd) + this.OnEnd(this); + } + public isDone() { + return this._nextIdx >= 0; + } + public refresh() { + this._nextIdx = -1; + for (let i = 0; i < this.nextNodes.length; i++) { + this.nextNodes[i].refresh(); + } + } + public start() { + if (this.OnStart) + this.OnStart(this); + } + public update(deltaTime) { + if (this.OnUpdate) + this.OnUpdate(this, deltaTime); + } + public hasNext() { + return this.nextNodes.length > 0; + } +} +export class GPWorkFlow { + public headNode:GPWorkFlowNode; + private curNode:GPWorkFlowNode; + start() { + if (this.headNode) { + this.headNode.refresh(); + } + this.curNode = this.headNode; + this.curNode.start(); + } + + update(deltaTime: number) { + if (this.curNode != null) { + this.curNode.update(deltaTime) + if (this.curNode.isDone()) { + if (this.curNode.hasNext()) { + this.curNode = this.curNode.nextNodes[this.curNode.nextIdx]; + this.curNode.start() + } else { + this.curNode = null + } + } + } + } + insertHeadNode(wfNode:GPWorkFlowNode) { + let tmp = this.headNode; + this.headNode = wfNode; + wfNode.nextNode = tmp; + } +} + + diff --git a/assets/scripts/components/GPWorkFlow.ts.meta b/assets/scripts/components/GPWorkFlow.ts.meta new file mode 100644 index 0000000..c968644 --- /dev/null +++ b/assets/scripts/components/GPWorkFlow.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "b1a29b57-56ec-43b6-a601-47be8b4e6f2a", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/scripts/testComponents.meta b/assets/scripts/testComponents.meta new file mode 100644 index 0000000..2834f28 --- /dev/null +++ b/assets/scripts/testComponents.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.2.0", + "importer": "directory", + "imported": true, + "uuid": "c1189113-47a8-4cb4-8133-e249e6b58188", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/scripts/testComponents/testWorkFlow.ts b/assets/scripts/testComponents/testWorkFlow.ts new file mode 100644 index 0000000..70174ba --- /dev/null +++ b/assets/scripts/testComponents/testWorkFlow.ts @@ -0,0 +1,28 @@ +import { _decorator, Component, Node, tween, v3 } from 'cc'; +import { GPDrag } from '../components/GPDrag'; +import { GPWorkFlowNode } from '../components/GPWorkFlow'; +const { ccclass, property } = _decorator; + +@ccclass('testWorkFlow') +export class testWorkFlow extends Component { + start() { + let drag = this.getComponent(GPDrag) + let testNode = new GPWorkFlowNode(); + let self = this; + testNode.OnStart = (wfNode:GPWorkFlowNode)=>{ + tween(self.node).by(0.05, {position:v3(10, 0, 0)}) + .by(0.1, {position:v3(-20, 0, 0)}) + .by(0.05, {position:v3(10, 0, 0)}) + .call(()=>{ + wfNode.done() + }).start() + } + drag.backHomeWorkFlow.insertHeadNode(testNode); + } + + update(deltaTime: number) { + + } +} + + diff --git a/assets/scripts/testComponents/testWorkFlow.ts.meta b/assets/scripts/testComponents/testWorkFlow.ts.meta new file mode 100644 index 0000000..b9e3081 --- /dev/null +++ b/assets/scripts/testComponents/testWorkFlow.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "e29b8017-2918-4c92-abc1-420a6c0834d7", + "files": [], + "subMetas": {}, + "userData": {} +}