-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
79 lines (72 loc) · 1.8 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
/**
* John Conway's Game of Life
*
* SPACE
* pause / unpause
* LEFT/RIGHT
* next step, if paused
* MOUSE CLICK or MOUSE DRAG
* activate cell
*
*/
var gamejs = require('gamejs');
var Map = require('./gof').Map;
gamejs.ready(main);
function addClickHandler(elementId, fn) {
document.getElementById(elementId).addEventListener('click', fn, false);
};
// disable normal browser mouse select
function disableMouseSelect() {
// no text select on drag
document.body.style.webkitUserSelect = 'none';
// non right clickery
document.body.oncontextmenu = function() { return false; };
}
function main() {
var screen = [
Math.max(window.innerWidth - 50, 250),
Math.max(window.innerHeight - 200, 250)
];
disableMouseSelect();
var map = new Map(screen);
var display = gamejs.display.setMode(screen)
gamejs.time.fpsCallback(tick, this, 8);
/**
* Keyboard handling
*/
var isMouseDown = false;
function eventHandler(event) {
if (event.type === gamejs.event.MOUSE_DOWN) {
isMouseDown = true;
map.setAt(event.pos);
} else if (event.type === gamejs.event.MOUSE_UP) {
isMouseDown = false;
} else if (event.type === gamejs.event.MOUSE_MOTION) {
if (isMouseDown) {
map.setAt(event.pos);
}
}
}
addClickHandler('gof-playpause', function() {
map.togglePaused();
});
addClickHandler('gof-step', function() {
map.forceUpdate();
});
addClickHandler('gof-random', function() {
map.random();
});
addClickHandler('gof-clear', function() {
map.clear();
});
/**
* Every game tick...
*/
function tick() {
gamejs.event.get().forEach(eventHandler);
map.update();
display.clear();
map.draw(display);
return;
};
};