Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/main.js
1036 views
1
/*jshint bitwise: false*/
2
/* globals requirejs, define*/
3
requirejs.config({
4
shim: {
5
box2d: {
6
exports: 'Box2D'
7
}
8
},
9
paths: {
10
box2d: 'Box2dWeb/Box2dWeb-2.1.a.3.min'
11
}
12
});
13
14
define(function( require ) {
15
'use strict';
16
17
var Game = require( 'game' );
18
var Player = require( 'entities/player' );
19
20
var Material = require( 'config/material' );
21
22
var game = Game.instance = new Game();
23
game.setPlayer( new Player() );
24
game.camera.target = game.player;
25
26
// Add game element to body.
27
game.element.classList.add( 'game' );
28
document.body.insertBefore( game.element, document.body.firstChild );
29
30
// Setup input.
31
var input = game.input;
32
33
document.addEventListener( 'keydown', input.onKeyDown.bind( input ) );
34
document.addEventListener( 'keyup', input.onKeyUp.bind( input ) );
35
36
if ( typeof window.ontouchstart !== 'undefined' ) {
37
game.canvas.addEventListener( 'touchstart', input.onTouchStart.bind( input ) );
38
game.canvas.addEventListener( 'touchmove', input.onTouchMove.bind( input ) );
39
game.canvas.addEventListener( 'touchend', input.onTouchEnd.bind( input ) );
40
}
41
42
// Toggle player material.
43
var materialBtn = document.getElementById( 'material-btn' );
44
function togglePlayerMaterial() {
45
game.player.toggleMaterial();
46
47
if ( game.player.material & Material.MATTER ) {
48
materialBtn.innerHTML = 'matter';
49
materialBtn.classList.add( 'matter' );
50
materialBtn.classList.remove( 'antimatter' );
51
} else if ( game.player.material & Material.ANTIMATTER ) {
52
materialBtn.innerHTML = 'antimatter';
53
materialBtn.classList.add( 'antimatter' );
54
materialBtn.classList.remove( 'matter' );
55
}
56
}
57
58
togglePlayerMaterial();
59
materialBtn.addEventListener( 'click', togglePlayerMaterial );
60
61
document.addEventListener( 'keydown', function( event ) {
62
// Space.
63
if ( event.which === 32 ) {
64
if ( game && game.player ) {
65
event.preventDefault();
66
togglePlayerMaterial();
67
}
68
}
69
});
70
71
window.addEventListener( 'blur', function() {
72
game.running = false;
73
74
// Disable all inputs.
75
Object.keys( game.input.keys ).forEach(function( key ) {
76
game.input.keys[ key ] = false;
77
});
78
79
Object.keys( game.input.controls ).forEach(function( control ) {
80
game.input.controls[ control ] = false;
81
});
82
});
83
84
window.addEventListener( 'focus', function() {
85
game.running = true;
86
game.tick();
87
});
88
89
// Title view.
90
var TitleView = require( 'views/title-view' );
91
var titleView = new TitleView();
92
document.body.insertBefore( titleView.el, document.body.firstChild );
93
94
var SettingsView = require( 'views/settings-view' );
95
var settingsView = new SettingsView();
96
document.body.insertBefore( settingsView.el, document.body.firstChild );
97
98
// Start on click.
99
var startBtn = titleView.el.querySelector( '#start-btn' );
100
function start() {
101
titleView.remove();
102
startBtn.removeEventListener( 'click', start );
103
104
// Start game.
105
game.tick();
106
107
var level = require( 'levels/level-01' );
108
level( game );
109
}
110
111
startBtn.addEventListener( 'click', start );
112
113
// For debugging purposes.
114
if ( window.localStorage.getItem( 'debug' ) ) {
115
start();
116
var level = require( 'levels/level-03' );
117
level( game );
118
}
119
});
120
121