-
Notifications
You must be signed in to change notification settings - Fork 1
/
game-template.html
176 lines (151 loc) · 6 KB
/
game-template.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html>
<head>
<!-- This template provides a starting point for implementing a game that takes full advantage of gameutils.js -->
<title>Gameutils.js game</title>
<meta charset="UTF-8">
<!-- output -->
<script src="src/lib/howler.core.js"></script>
<script src="src/lib/tween.min.js"></script>
<!-- dev mode helpers -->
<script src="src/lib/dat.gui.js"></script>
<script src="src/lib/FileSaver.js"></script>
<!-- input -->
<script src="src/lib/mousetrap.js"></script>
<script src="src/lib/mousetrap-global-bind.js"></script>
<script type="module">
import { querystringUtil } from "./src/gjs/utiljs.js";
import { Audio } from "./src/gjs/legacy/audio.js";
import { Sprite } from "./src/gjs/sprite.js";
import { LoadingBar } from "./src/gjs/loadingbar.js";
import { CanvasUI } from "./src/gjs/canvasui.js";
import { CanvasResizer } from "./src/gjs/canvasresizer.js";
import { commonUI } from "./src/gjs/commonui.js";
import { GameParameters } from "./src/gjs/gameparameters.js";
import { Gamepad } from "./src/gjs/gamepad.js";
import { InputMapper } from "./src/gjs/inputmapper.js";
import { startMainLoop } from "./src/gjs/mainloop.js";
import { cssUtil } from "./src/gjs/utilcolor.js";
import { seedrandom, random } from "./src/lib/seedrandom.js";
var Game = function(resizer) {
this.canvas = resizer.getCanvas();
this.realCtx = this.canvas.getContext('2d');
this.canvasUI = new CanvasUI({
element: this.canvas,
getCanvasPositionFromEvent: function(event) {
return resizer.getCanvasPosition(event);
}
});
this.time = 0;
var numPlayers = 1;
this.input = new InputMapper(this, numPlayers);
// Example usage of InputMapper
//this.input.addListener(Gamepad.BUTTONS.UP_OR_ANALOG_UP, ['up', 'w'], this.upPress, this.upRelease);
if (DEV_MODE) {
this.input.addListener(undefined, ['0'], this.devModeTakeScreenshot);
}
this.takeScreenshot = false;
};
Game.prototype.render = function(ctx) {
// CanvasResizer passes a wrapped 2D context to use here when run in FIXED_COORDINATE_SYSTEM mode,
// where ctx.canvas.width/height are set to the coordinate system width/height.
// Otherwise the context initialized here is used.
if (ctx === undefined) {
ctx = this.realCtx;
}
ctx.fillStyle = cssUtil.rgbString([0, 0, (Math.sin(this.time) * 0.5 + 0.5) * 255]);
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillStyle = '#fff';
ctx.textAlign = 'center';
ctx.fillText("Add ?devMode=1 to the URL to use developer mode!", ctx.canvas.width * 0.5, 20);
var that = this;
if (this.takeScreenshot) {
ctx.canvas.toBlob(function(blob) {
saveAs(blob, 'screenshot.png');
that.takeScreenshot = false;
});
this.takeScreenshot = false;
}
return ctx;
};
Game.prototype.update = function(deltaTime) {
this.time += deltaTime;
this.input.update();
// Update your level here
Audio.muteAll(Game.parameters.get('muteAudio'));
};
/**
* Mouse/touch handler for pressing down a mouse button or touch.
* @param {Object} event With following keys:
* currentPosition: Vec2 with current pointer coordinates in the canvas coordinate system.
* lastDown: Vec2 with coordinates of the latest press event in the canvas coordinate system.
* isDown: Boolean telling if the pointer is down.
* index: Integer index of the pointer being tracked.
*/
Game.prototype.canvasPress = function(event) {
};
/**
* Mouse/touch handler for releasing a mouse button or touch.
* @param {Object} event With following keys:
* currentPosition: Vec2 with current pointer coordinates in the canvas coordinate system.
* lastDown: Vec2 with coordinates of the latest press event in the canvas coordinate system.
* isDown: Boolean telling if the pointer is down.
* index: Integer index of the pointer being tracked.
*/
Game.prototype.canvasRelease = function(event) {
};
/**
* Mouse/touch handler when a pointer is being moved.
* @param {Object} event With following keys:
* currentPosition: Vec2 with current pointer coordinates in the canvas coordinate system.
* lastDown: Vec2 with coordinates of the latest press event in the canvas coordinate system.
* isDown: Boolean telling if the pointer is down.
* index: Integer index of the pointer being tracked.
*/
Game.prototype.canvasMove = function(event) {
};
/**
* Set the takeScreenshot flag so that a screenshot is taken on the next frame.
*/
Game.prototype.devModeTakeScreenshot = function() {
this.takeScreenshot = true;
};
// Parameters added here can be tuned at run time when in developer mode
Game.parameters = new GameParameters({
'muteAudio': {initial: false}
});
var DEV_MODE = querystringUtil.get('devMode') !== undefined;
window['start'] = function() {
var DEBUG_MAIN_LOOP = DEV_MODE && true; // Set to true to allow fast-forwarding main loop with 'f'
Game.parameters.set('muteAudio', (DEV_MODE && true)); // Set to true if sounds annoy developers
seedrandom();
var canvas = document.createElement('canvas');
var canvasWrapper = document.createElement('div');
canvasWrapper.appendChild(canvas);
commonUI.createUI({
parent: canvasWrapper,
fullscreenElement: document.body,
twitterAccount: 'Oletus',
fillStyle: '#ffffff',
opacity: 0.5,
scale: 1
});
var resizer = new CanvasResizer({
mode: CanvasResizer.Mode.DYNAMIC,
canvas: canvas,
wrapperElement: canvasWrapper
});
var game = new Game(resizer);
// Create event handlers for mouse and touch based input that will call on the canvas* members of game.
resizer.createPointerEventListener(game, true);
// Initialize after CanvasResizer so it is always drawn on top
if (DEV_MODE) {
Game.parameters.initGUI();
}
startMainLoop([resizer, game, new LoadingBar(), resizer.pixelator()], {debugMode: DEBUG_MAIN_LOOP});
};
</script>
</head>
<body onload="window.start()" style="background: black; height: 100%">
</body>
</html>