-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·117 lines (95 loc) · 2.68 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
// variables
var last, epsilon;
var fps = 30;
// objects
var canvas, ctx;
var player, scene, camera;
var dict = {};
// include js files
include(["point.js", "math.js", "light.js", "polygon.js", "player.js", 'scene.js', "camera.js", "input.js", "collision.js"]);
function include(files) {
// loads all files
for (var i = 0; i < files.length; i++) {
document.write("<script type='text/javascript' src='" + files[i] + "'></script>");
}
}
function init() {
init_canvas();
init_scene();
init_player();
init_camera();
init_event_listeners();
last = Date.now();
setInterval(loop, (1000 / fps));
}
function init_canvas() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext('2d');
rescaleCanvas();
}
function init_event_listeners() {
canvas.addEventListener("mousemove", onMouseMove, false);
canvas.addEventListener("mousedown", onMouseDown, false);
window.addEventListener('keydown', onKeyDown, true);
window.addEventListener("keyup", onKeyReleased, true);
window.addEventListener('resize', rescaleCanvas);
}
function init_player() {
player = new Player(200, 75, 1, 1.70, "andre");
}
function init_scene() {
scene = new Scene();
scene.createObjects();
scene.createLights();
}
function init_camera() {
camera = new Camera(scene, ctx);
}
function loop() {
var current = Date.now();
epsilon = (current - last) / 20;
player.move(scene);
update_enemies();
update_lights();
camera.render(ctx);
update_gui();
last = current;
}
var evx = 0;
var evy = 0;
function update_enemies() {
evx += Math.random() - 0.5;
evy += Math.random() - 0.5;
evx = restrictRange(evx, -2, 2);
evy = restrictRange(evy, -2, 2);
dict.enemy1.move(evx, evy);
respond_to_collisions(dict.enemy1, scene.objects, 10);
}
function update_lights() {
dict.playerLight.position.x = player.obj.center.x || 0;
dict.playerLight.position.y = player.obj.center.y || 0;
// dict.siren1.direction += 0.2;
// dict.siren2.direction += 0.2;
}
function update_gui() {
ctx.globalCompositeOperation = "source-over";
render_cursor(2);
}
function render_cursor(size) {
ctx.beginPath();
ctx.arc(mouse.x, mouse.y, size * 1.75, Math.PI * 2, 0, true);
ctx.fillStyle = "#000000";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.arc(mouse.x, mouse.y, size, Math.PI * 2, 0, true);
ctx.fillStyle = "#ffffff";
ctx.fill();
ctx.closePath();
}
function rescaleCanvas() {
canvas.height = window.innerHeight;
canvas.style.height = window.innerHeight + 'px';
canvas.width = window.innerWidth;
canvas.style.width = window.innerWidth + 'px';
}