-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
72 lines (70 loc) · 2.24 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
var Game = {
_display: null,
_currentScreen: null,
_screenWidth: 60,
_screenHeight: 24,
_level: 1,
init: function() {
// Any necessary initialization will go here.
this._display = new ROT.Display({width: this._screenWidth + 24, height: this._screenHeight + 5, fontSize: 24, fontFamily: "'Ubuntu Mono', monospace", spacing: 1.2, fg: "#FFFFFF"});
// Create a helper function for binding to an event
// and making it send it to the screen
let game = this; // So that we don't lose this
let bindEventToScreen = function(event) {
window.addEventListener(event, function(e) {
// When an event is received, send it to the
// screen if there is one
if (game._currentScreen !== null) {
// Send the event type and data to the screen
game._currentScreen.handleInput(event, e, true);
}
});
}
// Bind keyboard input events
bindEventToScreen('keydown');
bindEventToScreen('keypress');
},
refresh: function() {
// Clear the screen
this._display.clear();
// Render the screen
this._currentScreen.render(this._display);
},
getDisplay: function() {
return this._display;
},
getScreenWidth: function(){
return this._screenWidth;
},
getScreenHeight: function(){
return this._screenHeight;
},
getLevel: function(){
return this._level;
},
incrementLevel: function(){
this._level += 1;
},
switchScreen: function(screen) {
// If we had a screen before, notify it that we exited
if (this._currentScreen !== null) {
this._currentScreen.exit();
}
// Clear the display
this.getDisplay().clear();
// Update our current screen, notify it we entered
// and then render it
this._currentScreen = screen;
if (!this._currentScreen !== null) {
this._currentScreen.enter();
this.refresh();
}
}
}
window.onload = function(){
Game.init();
let container = Game.getDisplay().getContainer();
document.documentElement.appendChild(container);
//Load the start screen
Game.switchScreen(Game.Screen.startScreen);
}