|
@@ -1,54 +1,73 @@
|
1
|
1
|
// 辅助逻辑的颜色,
|
2
|
|
-const colorList = ['#f5222d', '#d4380d', '#d46b08', '#d48806', '#d4b106', '#7cb305', '#389e0d', '#08979c', '#096dd9', '#531dab']
|
3
|
|
-import { desginRadius, desginCageRadius, cageNum, scale, primaryColor } from './design';
|
|
2
|
+const colorList = [
|
|
3
|
+ "#f5222d",
|
|
4
|
+ "#d4380d",
|
|
5
|
+ "#d46b08",
|
|
6
|
+ "#d48806",
|
|
7
|
+ "#d4b106",
|
|
8
|
+ "#7cb305",
|
|
9
|
+ "#389e0d",
|
|
10
|
+ "#08979c",
|
|
11
|
+ "#096dd9",
|
|
12
|
+ "#531dab",
|
|
13
|
+];
|
|
14
|
+import {
|
|
15
|
+ desginRadius,
|
|
16
|
+ desginCageRadius,
|
|
17
|
+ cageNum,
|
|
18
|
+ scale,
|
|
19
|
+ primaryColor,
|
|
20
|
+} from "./design";
|
4
|
21
|
|
5
|
22
|
// 绘制目标轿厢
|
6
|
23
|
export function drawTargeCages({ two, center }) {
|
7
|
24
|
// 摩天轮半径
|
8
|
25
|
const wheelRaduis = desginRadius * scale;
|
9
|
|
- const perAngle = Math.PI / cageNum // 2 * Math.PI / ( cageNum * 2 ) 分成 轿厢 * 2 的分数, 最终效果是每隔一个空白,一个轿厢
|
|
26
|
+ const perAngle = Math.PI / cageNum; // 2 * Math.PI / ( cageNum * 2 ) 分成 轿厢 * 2 的分数, 最终效果是每隔一个空白,一个轿厢
|
10
|
27
|
const offsetAngle = perAngle / 2;
|
11
|
28
|
// 轿厢半径
|
12
|
|
- const cageRadius = desginCageRadius * scale;
|
13
|
|
-
|
|
29
|
+ const cageRadius = desginCageRadius * scale - 1;
|
|
30
|
+
|
14
|
31
|
// 绘制
|
15
|
|
- const list = Array(cageNum).fill().map((_, inx) => {
|
16
|
|
- const startAngle = 2 * inx * perAngle - offsetAngle; // 实际效果图是圆形有固定大小的, 不能直接从 0° 开始, 需要从 0 - offset 开始
|
17
|
|
- // const endAngle = startAngle + perAngle
|
18
|
|
-
|
19
|
|
- // 1、计算扇形边上的中心坐标
|
20
|
|
- const cx = center.x + Math.cos(startAngle + perAngle / 2) * wheelRaduis
|
21
|
|
- const cy = center.y + Math.sin(startAngle + perAngle / 2) * wheelRaduis
|
22
|
|
-
|
23
|
|
- // 2、轿厢
|
24
|
|
- const rect = two.makeCircle(cx, cy, cageRadius);
|
25
|
|
- rect.id = `mnt-${inx}`
|
26
|
|
- // rect.fill = colorList[inx] // 取消这个注释, 有助于梳理逻辑
|
27
|
|
- rect.noFill()
|
28
|
|
- rect.stroke = primaryColor
|
29
|
|
- rect.linewidth = 2
|
30
|
|
- rect.dashes = [4]
|
31
|
|
- rect.__$angle = startAngle; // 轿厢的初始弧度
|
32
|
|
- rect.__$mnt = null; // 当前实体轿厢 也就是已经被击中的轿厢
|
33
|
|
-
|
34
|
|
- return rect
|
35
|
|
- })
|
|
32
|
+ const list = Array(cageNum)
|
|
33
|
+ .fill()
|
|
34
|
+ .map((_, inx) => {
|
|
35
|
+ const startAngle = 2 * inx * perAngle - offsetAngle; // 实际效果图是圆形有固定大小的, 不能直接从 0° 开始, 需要从 0 - offset 开始
|
|
36
|
+ // const endAngle = startAngle + perAngle
|
|
37
|
+
|
|
38
|
+ // 1、计算扇形边上的中心坐标
|
|
39
|
+ const cx = center.x + Math.cos(startAngle + perAngle / 2) * wheelRaduis;
|
|
40
|
+ const cy = center.y + Math.sin(startAngle + perAngle / 2) * wheelRaduis;
|
|
41
|
+
|
|
42
|
+ // 2、轿厢
|
|
43
|
+ const rect = two.makeCircle(cx, cy, cageRadius);
|
|
44
|
+ rect.id = `mnt-${inx}`;
|
|
45
|
+ // rect.fill = colorList[inx] // 取消这个注释, 有助于梳理逻辑
|
|
46
|
+ rect.noFill();
|
|
47
|
+ rect.stroke = primaryColor;
|
|
48
|
+ rect.linewidth = 2;
|
|
49
|
+ rect.dashes = [4];
|
|
50
|
+ rect.__$angle = startAngle; // 轿厢的初始弧度
|
|
51
|
+ rect.__$mnt = null; // 当前实体轿厢 也就是已经被击中的轿厢
|
|
52
|
+
|
|
53
|
+ return rect;
|
|
54
|
+ });
|
36
|
55
|
|
37
|
56
|
// 是否击中
|
38
|
57
|
const checkHit = (bullet) => {
|
39
|
58
|
// next 是否继续下一轮验证
|
40
|
59
|
// error 是否发生了错误
|
41
|
|
- let res = { next: true, error: false }
|
|
60
|
+ let res = { next: true, error: false };
|
42
|
61
|
|
43
|
62
|
// 可接受校验偏差范围
|
44
|
63
|
const offset = cageRadius / 3;
|
45
|
|
-
|
|
64
|
+
|
46
|
65
|
const rect2 = bullet.getBoundingClientRect();
|
47
|
|
- const x = rect2.left + rect2.width / 2
|
48
|
|
- const y = rect2.top + rect2.height / 2
|
|
66
|
+ const x = rect2.left + rect2.width / 2;
|
|
67
|
+ const y = rect2.top + rect2.height / 2;
|
49
|
68
|
|
50
|
69
|
// 如果子弹 y 坐标超过警戒线
|
51
|
|
- if (y < (center.y + wheelRaduis + offset)) {
|
|
70
|
+ if (y < center.y + wheelRaduis + offset) {
|
52
|
71
|
res.next = false;
|
53
|
72
|
res.error = true;
|
54
|
73
|
return res;
|
|
@@ -58,24 +77,23 @@ export function drawTargeCages({ two, center }) {
|
58
|
77
|
const cage = list.filter((it) => {
|
59
|
78
|
const rect = it.getBoundingClientRect();
|
60
|
79
|
|
61
|
|
- const left = rect.left + offset
|
62
|
|
- const right = rect.right - offset
|
63
|
|
- const top = rect.top + offset
|
64
|
|
- const bottom = rect.bottom - offset
|
|
80
|
+ const left = rect.left + offset;
|
|
81
|
+ const right = rect.right - offset;
|
|
82
|
+ const top = rect.top + offset;
|
|
83
|
+ const bottom = rect.bottom - offset;
|
65
|
84
|
|
66
|
85
|
// 如果当前子弹的中心点位于目标轿厢矩形范围内
|
67
|
86
|
// 则代表击中
|
68
|
|
- return x >= left && x <= right &&
|
69
|
|
- y >= top && y <= bottom;
|
70
|
|
- })[0]
|
|
87
|
+ return x >= left && x <= right && y >= top && y <= bottom;
|
|
88
|
+ })[0];
|
71
|
89
|
|
72
|
90
|
// 未找到对应的目标轿厢
|
73
|
91
|
if (!cage) {
|
74
|
92
|
res.next = true;
|
75
|
93
|
res.error = false;
|
76
|
94
|
return res;
|
77
|
|
- };
|
78
|
|
-
|
|
95
|
+ }
|
|
96
|
+
|
79
|
97
|
// 如果目标轿厢已经挂载了一个
|
80
|
98
|
if (cage.__$mnt) {
|
81
|
99
|
res.next = false;
|
|
@@ -90,11 +108,11 @@ export function drawTargeCages({ two, center }) {
|
90
|
108
|
res.next = false;
|
91
|
109
|
res.error = false;
|
92
|
110
|
return res;
|
93
|
|
- }
|
|
111
|
+ };
|
94
|
112
|
|
95
|
113
|
return {
|
96
|
114
|
list,
|
97
|
115
|
cageRadius,
|
98
|
116
|
checkHit,
|
99
|
|
- }
|
|
117
|
+ };
|
100
|
118
|
}
|