-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
104 lines (90 loc) · 3.81 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
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
window.addEventListener("load", function(event) {
// Main functions
var render = function() {
display.fill("#000000" );
//if we're still on the menu, draw it
if(!game){
display.drawMenu();
}
else{ //else, draw the game
display.drawTileMap(game.stage.tileMap);
game.movables.forEach(e => (e == game.player) ? Function.prototype :
(e.tileIndex == -1) ?
display.drawRectangle(e.loc[0], e.loc[1], e.size_x, e.size_y) :
display.drawTileObject(e.loc[0], e.loc[1], e.tileIndex, e.animations)
);
display.drawTileObject(game.player.loc[0], game.player.loc[1], game.player.tileIndex, game.player.animations)
game.ui.forEach(e => display.drawTileObject(e.loc[0], e.loc[1], e.tileIndex, e.animations));
if(game.boss){
game.boss.bossUI.forEach(e => display.drawTileObject(e.loc[0], e.loc[1], e.tileIndex, e.animations));
}
}
display.render();
};
var update = function() {
if(game){
game.update(controller.keyState);
if(game.end){
game = null;
}
}
};
var resize = function(event) {
display.resize(document.documentElement.clientHeight-16, document.documentElement.clientWidth-16, 9/16);
display.render();
}
// Main variables
var size_x = 256*2;
var size_y = 144*2;
var tile_size = 16;
var tileMap_x = 48; //Based on the tilemap image
var tileMap_y = 22; //Based on tilemap image
var controller = new Controller();
var display = new Display(document.getElementById("myCanvas"), size_x, size_y, tile_size, tileMap_x, tileMap_y);
var game //= new Game(size_x, size_y);
// Event Listeners
window.addEventListener ("keydown", function(event) { controller.keyStateSet(event); if(event.keyCode == 32) {menuNext();} } );
window.addEventListener ("keyup", function(event) { controller.keyStateUnset(event); } );
window.addEventListener ("resize", resize);
display.context.canvas.addEventListener ('mousemove', function(event) { controller.mouseEvent(event,
display.context.canvas.width,
display.context.canvas.height); } );
display.context.canvas.addEventListener ('mousedown', function(event) { controller.mouseDownEvent(event); } );
display.context.canvas.addEventListener ('mouseup', function(event) { controller.mouseUpEvent(event); } );
//Initialization (Load tilemap and start running the engine)
display.tileSheet.image.addEventListener ('load', function(event) {
resize();
requestAnimationFrame(run);
}, {once:true});
display.tileSheet.image.src = "imgs/custom_pack.png";
display.menuImg = [new Image(),new Image(),new Image()];
display.menuImg[0].src = "imgs/menu.png";
display.menuImg[1].src = "imgs/howto.png";
display.menuImg[2].src = "imgs/ending.png";
render();
function menuNext(){
if(!game){
display.menuIndex = ((display.menuIndex + 1) % 3);
if(display.menuIndex == 2) {
game = new Game(size_x, size_y);
lastTime = Date.now();
}
}
}
// Game engine code
var step = 1000/30; //30 FPS
var currentTime = Date.now()
var lastTime = Date.now();
var delta = 0;
function run() {
currentTime = Date.now();
delta += currentTime - lastTime;
while(delta > step) {
delta = delta - step;
update();
}
render();
lastTime = Date.now();
requestAnimationFrame(run);
}
});