-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
142 lines (126 loc) · 2.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
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
var wad;
var shownElements = ["wad-selection", "loading", "game"];
var fps = 35;
var ms = 1000/fps;
var gametic = 0;
var dt_fps = 0;
var dt_ms = 0;
var fast = false;
var drawing = true;
var showFps = true;
var quitting = false;
var gamemode = "indetermined";
function loadFile()
{
decoder = new TextDecoder("utf-8");
setShown("game");
var canvas = document.getElementById("jsdoom-canvas");
graphics.init(canvas);
var reader = new FileReader();
var i = 0;
reader.onload = function()
{
console.log("Loading WAD data...");
var data = reader.result;
wad = new WAD(data);
if(wad.getFirstLump("MAP02")) //map01 may be missing
{
gamemode = "commercial";
}
else if(!wad.getFirstLump("E2M1"))
{
gamemode = "shareware";
}
else if(!wad.getFirstLump("E4M1"))
{
gamemode = "registered";
}
else
{
gamemode = "retail";
}
if(document.getElementsByName("pwad")[0].files.length > 0)
{
reader.onload = function()
{
console.log("Loading PWAD " + i + "'s data...");
var data = reader.result;
wad.loadPwad(new WAD(data));
i++;
if(i < document.getElementsByName("pwad")[0].files.length) reader.readAsArrayBuffer(document.getElementsByName("pwad")[0].files[i]);
else init();
}
reader.readAsArrayBuffer(document.getElementsByName("pwad")[0].files[i]);
}
else init();
}
console.log("Reading file");
reader.readAsArrayBuffer(document.getElementsByName("iwad")[0].files[0]);
}
function init()
{
setTimeout(run, 0, performance.now());
console.log("Started loop");
window.addEventListener("keydown", onKeyDown, true);
window.addEventListener("keyup", onKeyUp, true);
gamestates["title"].init();
menu.init();
}
function setShown(e, loadMsg)
{
for(var s in shownElements)
{
if(shownElements[s] == "loading")
document.getElementById("loading-p").textContent = loadMsg;
if(e == shownElements[s])
document.getElementById(shownElements[s]).classList.remove('hidden');
else
document.getElementById(shownElements[s]).classList.add('hidden');
}
}
function run(dt)
{
var dt_now = performance.now();
dt_ms = dt_now - dt;
dt_fps = 1/(dt_ms/1000);
if(!wipe.gonnaWipe && !quitting)
{
update();
if(drawing) draw();
}
if(wipe.gonnaWipe)
{
wipe.wipe();
}
if(showFps) font.drawText("fps: " + dt_fps.toFixed(2) + "\nms: " + dt_ms.toFixed(2), 0, 186);
if(drawing) graphics.applyBuffer();
var dt_later = performance.now();
setTimeout(run, fast ? 0 : ms - (dt_later-dt_now), [dt_now]);
}
function update()
{
gametic++;
graphics.update();
menu.update();
gamestates[gamestate].update();
input.removeKeysDown();
}
function draw()
{
gamestates[gamestate].draw();
menu.draw();
}
function onKeyDown(e)
{
input.onKeyDown(e);
e.preventDefault();
}
function onKeyUp(e)
{
input.onKeyUp(e);
e.preventDefault();
}
function quit()
{
document.location.reload();
}