-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
172 lines (154 loc) · 4.25 KB
/
main.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 单独一行页面数据
const templateRow = (line, x) => {
let t = ``
for (let i = 0; i < line.length; i++) {
let y = i
// n 为主题颜色的索引
let n = line[i]
let color = window.schemeColors[n]
t += ` <td class="padding-none color-${x}-${y}" data-x=${x} data-y=${y} style="background-image: url(images/${color}.png);"></td>`
}
let html = ` <tr> ${t} </tr>`
return html
}
// 所有行
const templateRows = (colors) => {
let t = ''
for (let i = 0; i < colors.length; i++) {
t += templateRow(colors[i], i)
}
return t
}
const templateschemeColors = () => {
let colors = window.schemeColors.slice(0, window.schemeColorsNum)
let t = ''
for (let i = 0; i < colors.length; i++) {
let color = colors[i]
t += `<button class="btn" data-index="${i}" style="background-image: url(images/${color}.png);"></button>`
}
return t
}
// 地图数据渲染 和 主题颜色数据渲染
const renderColors = (colors) => {
// 清空页面容器数据
e('#board').innerHTML = ''
//更新页面数据
e('#board').insertAdjacentHTML('beforeend', templateRows(colors))
e('#controls').innerHTML = ''
e('#controls').insertAdjacentHTML('beforeend', templateschemeColors())
}
const initTime = () => {
clearTimeout(window.timeId)
window.total_time = 0
window.timeId = setInterval(() => {
window.total_time += 1
let time = formatTime(window.total_time)
e('#counter').innerHTML = time
}, 1000)
}
const init = () => {
initTime()
// 通过配置的数据(那个主题、地图的宽高) 生成颜色数据
let cs = userDefinedMap(row, col, schemeColorsNum)
// 渲染数据
log('cs', cs)
window.currentColors = JSON.parse(JSON.stringify(cs))
renderColors(cs)
}
const bindEventNav = () => {
// 菜单配置开关
e('#options-btn').addEventListener('click', (event) => {
let menu = e('#options-menu')
let cls = 'show'
if (menu.classList.contains(cls)) {
menu.classList.remove(cls)
} else {
menu.classList.add(cls)
}
})
e('#new-game-btn').addEventListener('click', (event) => {
init()
})
}
const bindEventOption = () => {
bindAll(es('.grammy-options'), 'change', event => {
let option = event.target
let type = option.dataset.type
let v = option.value
// 主题
if (type === 'scheme') {
defineSchemeColors(v)
}
// 颜色数量
if (type === 'number') {
let nums = {
0: 3,
1: 4,
2: 5,
}
window.schemeColorsNum = nums[v]
}
// 设置row col
if (type === 'size') {
let size = {
0: 5,
1: 10,
2: 15,
3: 20,
}
window.row = window.col = size[v]
}
init()
// log('type', type, 'v', v)
})
}
const bindEventControls = () => {
e('#controls').addEventListener('click', (event) => {
let btn = event.target
let index = Number(btn.dataset.index)
// log('开始', currentColors)
handleColor(index)
// log('indx', index, '之后currentColors', currentColors)
renderColors(window.currentColors)
win()
})
}
const bindEvents = () => {
log('开始事件')
// 选择颜色
bindEventControls()
// 开始游戏和打开配置菜单栏
bindEventNav()
// 选择配置数据
bindEventOption()
}
const defineSchemeColors = (index = 0) => {
// 选择主题 默认选择第一个
const colorScheme = [
[1, 2, 3,4, 5],
[6, 7, 8, 9, 10],
[1, 5, 10, 8, 5],
[11, 12, 8, 9, 10],
]
// 主题的颜色
window.schemeColors = colorScheme[index]
}
const win = () => {
if (isWin()) {
modal('Good ', () => {
clearTimeout(window.timeId)
})
}
}
const __main = () => {
window.schemeColors = []
// 地图数据
window.currentColors = []
defineSchemeColors()
window.row = 5
window.col = 5
window.schemeColorsNum = 5
init()
bindEvents()
}
__main()