forked from Binaryify/vue-tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (127 loc) · 3.32 KB
/
app.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
import Decorate from './components/decorate/index.vue'
import Guide from './components/guide/index.vue'
import Next from './components/next/index.vue'
import Music from './components/music/index.vue'
import Pause from './components/pause/index.vue'
import Number from './components/number/index.vue'
import Point from './components/point/index.vue'
import Keyboard from './components/keyboard/index.vue'
import Logo from './components/logo/index.vue'
import Matrix from './components/matrix/index.vue'
import { mapState } from 'vuex'
import { transform, lastRecord, speeds, i18n, lan } from './unit/const'
import { visibilityChangeEvent, isFocus } from './unit/'
import states from './control/states'
export default {
mounted() {
this.render()
window.addEventListener('resize', this.resize.bind(this), true)
},
data() {
return {
size: {},
w: document.documentElement.clientWidth,
h: document.documentElement.clientHeight,
filling: ''
}
},
components: {
Decorate,
Guide,
Next,
Music,
Pause,
Number,
Point,
Logo,
Keyboard,
Matrix
},
computed: {
pContent() {
return this.cur ? i18n.cleans[lan] : i18n.startLine[lan]
},
level: () => i18n.level[lan],
nextText: () => i18n.next[lan],
...mapState([
'matrix',
'keyboard',
'music',
'pause',
'next',
'cur',
'speedStart',
'speedRun',
'startLines',
'clearLines',
'points',
'max',
'reset',
'drop'
])
},
methods: {
render() {
let filling = 0
const size = (() => {
const w = this.w
const h = this.h
const ratio = h / w
let scale
let css = {}
if (ratio < 1.5) {
scale = h / 960
} else {
scale = w / 640
filling = (h - 960 * scale) / scale / 3
css = {
'padding-top': Math.floor(filling) + 42 + 'px',
'padding-bottom': Math.floor(filling) + 'px',
'margin-top': Math.floor(-480 - filling * 1.5) + 'px'
}
}
css[transform] = `scale(${scale})`
return css
})()
this.size = size
this.start()
this.filling = filling
},
resize() {
this.w = document.documentElement.clientWidth
this.h = document.documentElement.clientHeight
this.render()
},
start() {
if (visibilityChangeEvent) {
// 将页面的焦点变换写入store
document.addEventListener(
visibilityChangeEvent,
() => {
states.focus(isFocus())
},
false
)
}
if (lastRecord) {
// 读取记录
if (lastRecord.cur && !lastRecord.pause) {
// 拿到上一次游戏的状态, 如果在游戏中且没有暂停, 游戏继续
const speedRun = this.$store.state.speedRun
let timeout = speeds[speedRun - 1] / 2 // 继续时, 给予当前下落速度一半的停留时间
// 停留时间不小于最快速的速度
timeout =
speedRun < speeds[speeds.length - 1]
? speeds[speeds.length - 1]
: speedRun
states.auto(timeout)
}
if (!lastRecord.cur) {
states.overStart()
}
} else {
states.overStart()
}
}
}
}