-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-field.html
More file actions
268 lines (238 loc) · 8.07 KB
/
game-field.html
File metadata and controls
268 lines (238 loc) · 8.07 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!doctype html>
<html>
<head>
<title>Game Field</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div>
<button onclick="GameField.displayRun_()">Run</button>
<button onclick="GameField.displayCode_()">Code</button>
<button onclick="GameField.displayMyPrograms_()">My Programs</button>
</div>
<div id="game-field">
</div>
<div id="code-editor" style="display:none">
Program Name: <input id="program-name" value="first example"></input><br>
<span id="code-area" contenteditable="true" style="font-family:monospace">
// Your code here :-)<br>
GameField.eachTick = function() {<br>
var click = checkclick();<br>
if (click) {<br>
setpixel(click.x, click.y, randomcolor());<br>
}<br>
};
</span>
</div>
<div id="my-programs" style="display:none">
Programs:<br>
</div>
<script>
// Unnamed closure to avoid polluting the global namespace.
(function() {
var publicGameField = {};
// Constructor for a game field state container.
function GameEngine() {
this.numRows = 0;
this.numCols = 0;
this.tileSize = 1;
this.eventLoopDelay = 100;
this.reset_();
this.eventLoopInterval = null;
}
// Called before creating a new canvas to remove internal state associated
// with a run on a GameField program.
GameEngine.prototype.reset_ = function() {
this.clickQueue = [];
this.eventListenerStarted = false;
};
// Starts an event loop to call GameField.eachTick at the interval specified
// by the stored event loop internal.
GameEngine.prototype.resetEventLoop_ = function() {
if (this.eventLoopInterval) {
clearInterval(this.eventLoopInterval);
}
this.eventLoopInterval = setInterval(function() {
if (publicGameField.eachTick) {
publicGameField.eachTick();
}
}, this.eventLoopDelay);
};
// Starts recording clicks on the canvas, saving them in the GameField click
// queue.
GameEngine.prototype.listenForEvents_ = function() {
var field = document.getElementById('field');
var thisGameEngine = this;
field.addEventListener('click', function(e) {
var x;
var y;
if (e.pageX || e.pageY) {
x = e.pageX;
y = e.pageY;
} else {
x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
var fieldCanvas = document.getElementById('field');
x -= fieldCanvas.offsetLeft;
y -= fieldCanvas.offsetTop;
thisGameEngine.clickQueue.push(
[Math.floor(x / thisGameEngine.tileSize),
Math.floor(y / thisGameEngine.tileSize)]);
}, true);
};
// Creates a blank game field canvas prepared to start running a
// GameField program.
GameEngine.prototype.resetCanvas = function(numPixelsWide, numPixelsHigh) {
this.reset_();
// Wipe out old canvas.
var gameFieldDiv = document.getElementById('game-field');
gameFieldDiv.innerHTML = '';
// Determine new game fild pixel size.
var maxPixelSizeBasedOnWidth = Math.floor(
(document.documentElement.clientWidth - 20) / numPixelsWide);
var maxPixelSizeBasedOnHeight = Math.floor(
(document.documentElement.clientHeight - 30) / numPixelsHigh);
var pixelSize = Math.min(
maxPixelSizeBasedOnWidth, maxPixelSizeBasedOnHeight);
// Create new game field.
var gameFieldCanvas = document.createElement('canvas');
gameFieldCanvas.id = 'field';
gameFieldCanvas.width = pixelSize * numPixelsWide;
gameFieldCanvas.height = pixelSize * numPixelsHigh;
gameFieldCanvas.style.border = '1px solid #000000';
gameFieldDiv.appendChild(gameFieldCanvas);
this.numRows = numPixelsHigh;
this.numCols = numPixelsWide;
this.tileSize = pixelSize;
this.listenForEvents_();
this.resetEventLoop_();
};
GameEngine.prototype.setPixel = function(x, y, color) {
var field = document.getElementById('field');
var fieldContext = field.getContext('2d');
fieldContext.fillStyle = color;
fieldContext.fillRect(
x * this.tileSize, y * this.tileSize, this.tileSize, this.tileSize);
};
// Gets the next stored click if there is one.
GameEngine.prototype.checkClick = function() {
if (this.clickQueue.length > 0) {
var click = this.clickQueue[0];
this.clickQueue = this.clickQueue.slice(1);
return {x: click[0], y: click[1]};
}
return null;
};
function randomNumber(low, high) {
return Math.floor(Math.random() * ((high - low + 1))) + low;
}
var hexDigits_ = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
function randomColor() {
var digits = ['#'];
for (var i = 0; i < 6; i++) {
digits.push(hexDigits_[randomNumber(0, 15)]);
}
return digits.join('');
}
function randomColor() {
var digits = ['#'];
for (var i = 0; i < 6; i++) {
digits.push(hexDigits_[randomNumber(0, 15)]);
}
return digits.join('');
}
// Singleton GameEngine used by functions exposed for users.
var gameField = new GameEngine();
var PROGRAM_PREFIX = 'gameField-program-';
function saveProgram_() {
var programName = document.getElementById('program-name').value;
if (!programName) {
console.log('no file name given, not saving program');
return;
}
localStorage[PROGRAM_PREFIX + programName] = document.getElementById(
'code-area').innerHTML;
}
function loadProgram_(programName) {
document.getElementById('program-name').value = programName;
var code = localStorage[PROGRAM_PREFIX + programName]
if (code) {
document.getElementById('code-area').innerHTML = code;
} else {
document.getElementById('code-area').innerHTML = '// Your code here :-)';
}
}
// Functions exposed to that button click handlers can load, save, and run
// programs.
publicGameField.displayRun_ = function() {
saveProgram_();
document.getElementById('code-editor').style.display = 'none';
document.getElementById('my-programs').style.display = 'none';
var gameFieldDiv = document.getElementById('game-field');
gameFieldDiv.style.display = '';
gameField.resetCanvas(16, 16);
var code = document.getElementById('code-area').innerText;
eval(code);
};
publicGameField.displayCode_ = function() {
document.getElementById('game-field').style.display = 'none';
document.getElementById('my-programs').style.display = 'none';
document.getElementById('code-editor').style.display = '';
};
function generateProgramOpener_(programName) {
return function() {
loadProgram_(programName);
publicGameField.displayCode_();
}
}
publicGameField.displayMyPrograms_ = function() {
saveProgram_();
document.getElementById('game-field').style.display = 'none';
document.getElementById('code-editor').style.display = 'none';
var programsDiv = document.getElementById('my-programs');
programsDiv.style.display = '';
programsDiv.innerHTML = 'Programs:<br>';
// Find everything in local storage that starts with the
// 'gameField-program-' prefix.
for (var programKey in localStorage) {
if (programKey.indexOf(PROGRAM_PREFIX) == 0) {
var programName = programKey.substr(PROGRAM_PREFIX.length);
var a = document.createElement('a');
a.onclick = generateProgramOpener_(programName);
a.innerText = programName;
programsDiv.appendChild(a);
programsDiv.appendChild(document.createElement('br'));
}
}
};
// Adds a function to the public name space to allow users to call it. Exposes
// both the name given and an all lowercase name to allow users to skip having
// to shift to uppercase.
function addPublicFunction(name, f) {
publicGameField[name] = f;
window[name.toLowerCase()] = f;
}
addPublicFunction('setPixel', function(x, y, color) {
gameField.setPixel(x, y, color);
});
addPublicFunction('checkClick', function() {
return gameField.checkClick();
});
addPublicFunction('randomNumber', function(low, high) {
return randomNumber(low, high);
});
addPublicFunction('randomColor', function() {
return randomColor();
});
// Expose the GameField namespace.
window['GameField'] = publicGameField;
// Run the example program at the start.
publicGameField.displayRun_();
})();
</script>
</body>
</html>