forked from WLDragon/shiguilandan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshake.ts
70 lines (60 loc) · 1.37 KB
/
shake.ts
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
import { DisplayObject } from "pixi.js"
import { remove } from './others';
let running = false
const targetList: ShakeObject[] = []
/**
* 抖动显示对象
* @param target 显示对象
* @param times 抖动次数
*/
export function shake(target: DisplayObject, times: number, size: number = 10) {
if (!targetList.find(so => so.target == target)) {
targetList.push(new ShakeObject(target, times, size))
if (!running) {
running = true
shakeTick()
}
}
}
function shakeTick() {
let removeList: ShakeObject[] = []
targetList.forEach(so => {
so.next()
if (so.times <= 0) {
so.recover()
removeList.push(so)
}
})
removeList.forEach(so => {
remove(so, targetList)
})
if (targetList.length) {
setTimeout(shakeTick, 30)
} else {
running = false
}
}
class ShakeObject {
target: DisplayObject
times: number
size: number
ox: number
oy: number
constructor(target: DisplayObject, times: number, size: number) {
this.target = target
this.times = times
this.size = size
this.ox = target.x
this.oy = target.y
}
next() {
let halfSize = this.size / 2
let x = this.ox + Math.random() * this.size - halfSize
let y = this.oy + Math.random() * this.size - halfSize
this.target.position.set(x, y)
this.times--
}
recover() {
this.target.position.set(this.ox, this.oy)
}
}