-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_actuator.ts
More file actions
193 lines (166 loc) · 5.14 KB
/
html_actuator.ts
File metadata and controls
193 lines (166 loc) · 5.14 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Grid } from './grid.js'
import type { Tile } from './tile.js'
import type { GameState, Position } from './types.js'
/**
* HTML Actuator
*
* Singleton class that manages HTML updates.
*/
export class HTMLActuator {
/** Best Score Container */
static bestContainer = document.querySelector('.best-container') as Element
/** Message Container */
static messageContainer = document.querySelector('.game-message') as Element
/** Score */
static score: number = 0
/** Score Container */
static scoreContainer = document.querySelector('.score-container') as Element
/** Tile Container */
static tileContainer = document.querySelector('.tile-container') as Element
constructor() {}
/**
* Updates the UI based on game state changes.
*
* @param metadata Game Metadata
*/
static actuate(metadata: GameState): void {
window.requestAnimationFrame(function () {
HTMLActuator.clearContainer(HTMLActuator.tileContainer)
Grid.cells.forEach(function (column) {
column.forEach(function (cell) {
if (cell) HTMLActuator.addTile(cell)
})
})
HTMLActuator.updateScore(metadata.score)
HTMLActuator.updateBestScore(metadata.bestScore)
if (metadata.terminated && metadata.gameOver) HTMLActuator.message(false)
else if (metadata.terminated && metadata.won) HTMLActuator.message(true)
})
}
/**
* Continues the game.
*
* Affects both restarts and keep playing.
*/
static continueGame(): void {
HTMLActuator.clearMessage()
}
/**
* Clears the tile container.
*
* @param container Container
*/
static clearContainer(container: Element): void {
while (container.firstChild) container.removeChild(container.firstChild)
}
/**
* Adds a tile.
*
* @param tile Tile
*/
static addTile(tile: Tile): void {
const wrapper = document.createElement('div')
const inner = document.createElement('div')
const position = tile.previousPosition || tile.position
const positionClass = HTMLActuator.positionClass(position)
const classes = ['tile', `tile-${tile.value}`, positionClass]
if (tile.value > 2048) classes.push('tile-super')
HTMLActuator.applyClasses(wrapper, classes)
inner.classList.add('tile-inner')
inner.textContent = tile.value.toString()
if (tile.previousPosition) {
// Make sure that the tile gets rendered in the previous position first
window.requestAnimationFrame(function () {
classes[2] = HTMLActuator.positionClass(tile.position)
HTMLActuator.applyClasses(wrapper, classes)
})
} else if (tile.mergedFrom) {
classes.push('tile-merged')
HTMLActuator.applyClasses(wrapper, classes)
// Render the tiles that merged
tile.mergedFrom.forEach(function (merged) {
HTMLActuator.addTile(merged)
})
} else {
classes.push('tile-new')
HTMLActuator.applyClasses(wrapper, classes)
}
// Add the inner part of the tile to the wrapper
wrapper.appendChild(inner)
// Put the tile on the board
HTMLActuator.tileContainer.appendChild(wrapper)
}
/**
* Applies classes to an element.
*
* @param element Element to Apply Classes
* @param classes Classes to Apply
*/
static applyClasses(element: any, classes: string[]): void {
element.setAttribute('class', classes.join(' '))
}
/**
* Normalizes the position.
*
* @param position Position
* @returns Normalized Position
*/
static normalizePosition(position: Position): Position {
return { x: position.x + 1, y: position.y + 1 }
}
/**
* Creates a class name for a position.
*
* @param position Position
* @returns Class Name
*/
static positionClass(position: Position): string {
position = HTMLActuator.normalizePosition(position)
return `tile-position-${position.x}-${position.y}`
}
/**
* Updates the Score
*
* @todo The score update should be animated
*
* @param score Score
*/
static updateScore(score: number): void {
// Clear the current score
HTMLActuator.clearContainer(HTMLActuator.scoreContainer)
// Get the difference between the current score and the new score
// @ts-expect-error This will be used in the future
const difference = score - HTMLActuator.score
// Set the new score
HTMLActuator.score = score
// Update the score
HTMLActuator.scoreContainer.textContent = HTMLActuator.score.toString()
// Lab 4: Animate the score update
}
/**
* Updates the best score.
*
* @param bestScore Best Score
*/
static updateBestScore(bestScore: number): void {
HTMLActuator.bestContainer.textContent = bestScore.toString()
}
/**
* Sets the message.
*
* @param won Won Status
*/
static message(won: boolean): void {
HTMLActuator.messageContainer.classList.add(won ? 'game-won' : 'game-over')
HTMLActuator.messageContainer.getElementsByTagName('p')[0].textContent = won
? 'You win!'
: 'Game over!'
}
/**
* Clears the message.
*/
static clearMessage(): void {
HTMLActuator.messageContainer.classList.remove('game-won')
HTMLActuator.messageContainer.classList.remove('game-over')
}
}