-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartAction.js
174 lines (147 loc) · 5.76 KB
/
startAction.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
172
173
174
import Tile from './Tile.js'
import handleLose from './handleLose.js';
import { moveUp, moveDown, moveLeft, moveRight, canMoveUp, canMoveDown, canMoveLeft, canMoveRight } from './Actions.js'
function StartAction(grid, gameBoard) {
const score = document.getElementById('number-score')
var numberScore = 0
var numberHighScore = 0
let xStartPoint = null
let yStartPoint = null
setupInput()
// Lắng nghe các Action Event
function setupInput() {
window.addEventListener("keydown", handleInputPC, { once: true })
document.addEventListener('touchstart', handleTouchStart, { once: true })
document.addEventListener('touchend', handleTouchEndMobile, { once: true })
}
// Xử lý các chuyển động trên PC dùng KeyDown
async function handleInputPC(e) {
switch (e.key) {
case "ArrowUp":
if (!canMoveUp(grid)) {
setupInput()
return
}
await moveUp(grid)
break
case "ArrowDown":
if (!canMoveDown(grid)) {
setupInput()
return
}
await moveDown(grid)
break
case "ArrowLeft":
if (!canMoveLeft(grid)) {
setupInput()
return
}
await moveLeft(grid)
break
case "ArrowRight":
if (!canMoveRight(grid)) {
setupInput()
return
}
await moveRight(grid)
break
default:
setupInput()
return
}
//Tiến hành gộp Tile và cộng điểm Score cho người chơi
grid.cells.forEach(cell => {
let scoreMerge = cell.mergeTiles()
numberScore = scoreMerge ? scoreMerge + numberScore : numberScore
if (numberScore) score.textContent = numberScore.toString()
})
//Kiểm tra và gán điểm cho HighScore để dùng thống kê khi người chơi thua
numberHighScore = numberHighScore > numberScore ? numberHighScore : numberScore
//Sau mỗi lần di chuyển thì luôn random ra một Tile mới ở vị trí bất kì
const newTile = new Tile(gameBoard)
grid.randomEmptyCell().tile = newTile
//Check xem người dùng đã thua hay chưa (Thua là khi không còn ô trống để di chuyển)
if (!canMoveUp(grid) && !canMoveDown(grid) && !canMoveLeft(grid) && !canMoveRight(grid)) {
newTile.waitForTransition(true).then(() => {
handleLose(grid, gameBoard, numberScore, numberHighScore, setupInput)
numberScore = 0;
}
)
return
}
setupInput()
}
// Xử lý các chuyển động trên Mobile dùng Touch Event
function handleTouchStart(e) {
xStartPoint = Math.abs(e.touches[0].clientX)
yStartPoint = Math.abs(e.touches[0].clientY)
}
// Xử lý các chuyển động trên Mobile dùng Touch Event
async function handleTouchEndMobile(e) {
let move = null
let xEndPoint = Math.abs(e.changedTouches[0].clientX)
let yEndPoint = Math.abs(e.changedTouches[0].clientY)
const xDiff = xStartPoint - xEndPoint
const yDiff = yStartPoint - yEndPoint
if (Math.abs(xDiff) > Math.abs(yDiff)) {
move = xDiff > 0 ? 'ArrowLeft' : 'ArrowRight'
} else if (Math.abs(yDiff) > Math.abs(xDiff)) {
move = yDiff > 0 ? 'ArrowUp' : 'ArrowDown'
}
switch (move) {
case "ArrowUp":
if (!canMoveUp(grid)) {
setupInput()
return
}
await moveUp(grid)
break
case "ArrowDown":
if (!canMoveDown(grid)) {
setupInput()
return
}
await moveDown(grid)
break
case "ArrowLeft":
if (!canMoveLeft(grid)) {
setupInput()
return
}
await moveLeft(grid)
break
case "ArrowRight":
if (!canMoveRight(grid)) {
setupInput()
return
}
await moveRight(grid)
break
default:
setupInput()
return
}
//Tiến hành gộp Tile và cộng điểm Score cho người chơi
grid.cells.forEach(cell => {
let scoreMerge = cell.mergeTiles()
numberScore = scoreMerge ? scoreMerge + numberScore : numberScore
if (numberScore) score.textContent = numberScore.toString()
})
//Kiểm tra và gán điểm cho HighScore để dùng thống kê khi người chơi thua
numberHighScore = numberHighScore > numberScore ? numberHighScore : numberScore
//Sau mỗi lần di chuyển thì luôn random ra một Tile mới ở vị trí bất kì
const newTile = new Tile(gameBoard)
grid.randomEmptyCell().tile = newTile
//Check xem người dùng đã thua hay chưa (Thua là khi không còn ô trống để di chuyển)
if (!canMoveUp(grid) && !canMoveDown(grid) && !canMoveLeft(grid) && !canMoveRight(grid)) {
newTile.waitForTransition(true).then(() => {
handleLose(grid, gameBoard, numberScore, numberHighScore, setupInput)
numberScore = 0
}
)
return
}
setupInput()
}
}
export default StartAction