SurroundMotionStreakAction.ts
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
const { ccclass, property } = cc._decorator;
enum MoveDirection {
Clockwise,
Anticlockwise
}
@ccclass
export default class SurroundMotionStreakAction extends cc.Component {
// LIFE-CYCLE CALLBACKS:
@property
moveSpeed: number = 1;
@property
fadeTime: number = 1;
@property
minSeg: number = 1;
@property
stroke: number = 10;
@property
direction: MoveDirection = MoveDirection.Clockwise;
@property({ type: cc.Node })
actNode: cc.Node = new cc.Node;
@property({ type: cc.Texture2D })
motionStreakTexture: cc.Texture2D = new cc.Texture2D;
private readonly _baseTime: number = 1;
onLoad() { }
start() {
}
onEnable() {
let lt = cc.v2(this.node.width * (-1) / 2, this.node.height / 2);
let rt = cc.v2(this.node.width / 2, this.node.height / 2);
let rb = cc.v2(this.node.width / 2, this.node.height * (-1) / 2);
let lb = cc.v2(this.node.width * (-1) / 2, this.node.height * (-1) / 2);
let actTime: number = this._baseTime / this.moveSpeed;
let max = Math.max(this.node.width, this.node.height);
let wTime = this.node.width / max * actTime;
let hTime = this.node.height / max * actTime;
let ltAct = cc.moveTo(hTime, lt);
let rtAct = cc.moveTo(wTime, rt);
let rbAct = cc.moveTo(hTime, rb);
let lbAct = cc.moveTo(wTime, lb);
if (this.direction === MoveDirection.Anticlockwise) {
ltAct = cc.moveTo(wTime, lt);
rtAct = cc.moveTo(hTime, rt);
rbAct = cc.moveTo(wTime, rb);
lbAct = cc.moveTo(hTime, lb);
}
let seqClockwise = cc.sequence(ltAct, rtAct, rbAct, lbAct);
let seqAnticlockwise = cc.sequence(rtAct, ltAct, lbAct, rbAct);
let motionStreak = this.actNode.getComponent(cc.MotionStreak);
if (!motionStreak) {
motionStreak = this.actNode.addComponent(cc.MotionStreak);
motionStreak.texture = this.motionStreakTexture;
motionStreak.fadeTime = this.fadeTime;
motionStreak.minSeg = this.minSeg;
motionStreak.stroke = this.stroke;
}
motionStreak.enabled = true;
this.actNode.stopAllActions();
if (this.direction === MoveDirection.Clockwise) {
this.actNode.x = this.node.width * (-1) / 2;
this.actNode.y = this.node.height / 2;
this.actNode.runAction(cc.repeatForever(seqClockwise));
} else if (this.direction === MoveDirection.Anticlockwise) {
this.actNode.x = this.node.width / 2;
this.actNode.y = this.node.height / 2;
this.actNode.runAction(cc.repeatForever(seqAnticlockwise));
}
}
onDisable() {
let motionStreak = this.actNode.getComponent(cc.MotionStreak);
if (motionStreak) {
motionStreak.enabled = false;
}
this.node.stopAllActions();
}
}