-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·74 lines (61 loc) · 1.84 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
import Timer from "./utils/Timer.js";
import Mouse from "./utils/Mouse.js";
import KeyboardState from "./utils/KeyboardState.js";
import Camera from "./utils/Camera.js";
import {SetupMouse} from "./mouse.js";
import {SetupKeyboard} from "./keyboard.js";
import TerrainGenerator from "./TerrainGenerator.js";
// HTML container references
const HtmlContainer = document.getElementById('scene');
const HtmlSeeder = document.getElementById('seed');
// Config options
let FPS = 60;
let WIDTH = 1920 / 2;
let HEIGHT = 1080 / 2;
let SEED = HtmlSeeder.value;
// Instantiation
const timer = new Timer(1 / FPS);
const camera = new Camera(WIDTH, HEIGHT);
const mouse = new Mouse(WIDTH, HEIGHT);
const keyboard = new KeyboardState();
const scene = new TerrainGenerator(WIDTH, HEIGHT, SEED, camera, keyboard);
// When seed changes
['keyup', 'change'].forEach(e => {
HtmlSeeder.addEventListener(e, () => {
scene.seed = HtmlSeeder.value;
scene.refresh();
});
})
// Load the scene first and then start the timer
scene.load(() => {
HtmlContainer.style.width = WIDTH;
HtmlContainer.style.height = HEIGHT;
HtmlContainer.append(scene.canvas);
SetupKeyboard(keyboard, camera);
SetupMouse(mouse, camera);
mouse.listenTo(scene.canvas);
timer.start();
});
// Update values
timer.update = function(deltaTime, tick, time) {
// We don't have to rely on timing for this. We save the resources.
}
// FPS counter
let times = [];
let fps;
// Render the scene
timer.render = function(deltaTime, tick, time) {
// Since we don't have to rely on timing for this demo
camera.update(deltaTime);
scene.update(deltaTime, tick, time);
// The actual rendering
scene.render(deltaTime, tick, time);
// The FPS counter
const now = performance.now();
while(times.length > 0 && times[0] <= now - 1000) {
times.shift();
}
times.push(now);
fps = times.length;
document.title = 'FPS: ' + fps;
}