-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings_loader.ts
98 lines (94 loc) · 2.42 KB
/
settings_loader.ts
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
/**
* Singleton contains settings values
* @example
* Example usage:
* ```ts
* SettingsLoader.getSettings().FIGURE_COLORS
* ```
*/
export class SettingsLoader {
private static instance: SettingsLoader
public readonly GAME_FIELD_BACKGROUND_COLOR
/**
* Colors for figures (HEX RGB strings). Zero indexed color are game field background color
*/
public readonly FIGURE_COLORS: string[]
/**
* Available game modes. Key - game mode code, value - human readable representation
*/
public readonly GAME_MODES: Object
public readonly GAME_FIELD_HEIGHT_IN_CELLS: number
public readonly GAME_FIELD_WIDTH_IN_CELLS: number
/**
* Rows collapse effect color
*/
public readonly EFFECT_COLOR: string
/**
* Effect color for tetris (4 rows collapse)
*/
public readonly TETRIS_EFFECT_COLOR: string
/**
* Levels speed settings, keys are count of rows, values are clock intervals
*/
public readonly LEVELS: { [key: string]: number }
constructor() {
if (SettingsLoader.instance) {
throw new Error('Use SettingsLoader.getSettings()')
}
this.GAME_FIELD_BACKGROUND_COLOR = '161f27'
this.FIGURE_COLORS = [
this.GAME_FIELD_BACKGROUND_COLOR,
'FFD498',
'047C51',
'FE5825',
'700414',
'DF3D2E',
'FEFEFE',
'668BC4',
'335495'
]
this.GAME_MODES = {
EASY: 'Easy mode',
MEDIUM: 'Medium mode',
HARD: 'Hard mode'
}
this.GAME_FIELD_HEIGHT_IN_CELLS = 20
this.GAME_FIELD_WIDTH_IN_CELLS = 10
this.EFFECT_COLOR = 'ffffff'
this.TETRIS_EFFECT_COLOR = 'ff0000'
this.LEVELS = {
0: 770,
15: 730,
30: 680,
45: 620,
60: 550,
75: 470,
90: 380,
100: 280,
110: 160,
120: 100
}
}
/**
* Loads settings from json file via http
* @param fileURI URI for settings file
* @returns settings object
*/
private readSettingsFromJson(fileURI: string) {
const request = new XMLHttpRequest()
request.open('GET', fileURI, false)
//request.overrideMimeType('application/json');
request.send()
if (request.status == 200 && request.readyState == 4) {
return JSON.parse(request.responseText)
}
}
/**
* Return SettingsLoader instance
* @returns SettingsLoader instance
*/
static getSettings(): SettingsLoader {
SettingsLoader.instance = SettingsLoader.instance || new SettingsLoader()
return SettingsLoader.instance
}
}