-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06b4a30
commit df153cd
Showing
14 changed files
with
243 additions
and
55 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
'use strict'; | ||
var GameAssets = {}; | ||
let scriptOrder = [ | ||
newScript('settings'), | ||
newScript('gameObjects/blocks'), | ||
newScript('gameObjects/menuing'), | ||
newScript('gameObjects/gamePlay'), | ||
newScript('render/audio'), | ||
newScript('render/graphics'), | ||
newScript('render/particles'), | ||
newScript('input/input'), | ||
newScript('main') | ||
]; | ||
|
||
let assetOrder = [{ | ||
key: 'background', | ||
source: '/images/background.png' | ||
}, { | ||
key: 'block', | ||
source: '/images/block.png' | ||
} | ||
]; | ||
|
||
//------------------------------------------------------------------ | ||
// | ||
// Helper function used to load scripts in the order specified by the | ||
// 'scripts' parameter. 'scripts' expects an array of objects with | ||
// the following format... | ||
// { | ||
// scripts: [script1, script2, ...], | ||
// message: 'Console message displayed after loading is complete', | ||
// onComplete: function to call when loading is complete, may be null | ||
// } | ||
// | ||
//------------------------------------------------------------------ | ||
function loadScripts(scripts, onComplete) { | ||
// | ||
// When we run out of things to load, that is when we call onComplete. | ||
if (scripts.length > 0) { | ||
let entry = scripts[0]; | ||
require(entry.scripts, function() { | ||
if (entry.onComplete) { | ||
entry.onComplete(); | ||
} | ||
scripts.shift(); // Alternatively: scripts.splice(0, 1); | ||
loadScripts(scripts, onComplete); | ||
}); | ||
} else { | ||
onComplete(); | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------ | ||
// | ||
// Helper function used to load assets in the order specified by the | ||
// 'assets' parameter. 'assets' expects an array of objects with | ||
// the following format... | ||
// { | ||
// key: 'asset-1', | ||
// source: 'asset/.../asset.png' | ||
// } | ||
// | ||
// onSuccess is invoked per asset as: onSuccess(key, asset) | ||
// onError is invoked per asset as: onError(error) | ||
// onComplete is invoked once per 'assets' array as: onComplete() | ||
// | ||
//------------------------------------------------------------------ | ||
function loadAssets(assets, onSuccess, onError, onComplete) { | ||
// | ||
// When we run out of things to load, that is when we call onComplete. | ||
if (assets.length > 0) { | ||
let entry = assets[0]; | ||
loadAsset(entry.source, | ||
function(asset) { | ||
onSuccess(entry, asset); | ||
assets.shift(); // Alternatively: assets.splice(0, 1); | ||
loadAssets(assets, onSuccess, onError, onComplete); | ||
}, | ||
function(error) { | ||
onError(error); | ||
assets.shift(); // Alternatively: assets.splice(0, 1); | ||
loadAssets(assets, onSuccess, onError, onComplete); | ||
}); | ||
} else { | ||
onComplete(); | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------ | ||
// | ||
// This function is used to asynchronously load image and audio assets. | ||
// On success the asset is provided through the onSuccess callback. | ||
// Reference: http://www.html5rocks.com/en/tutorials/file/xhr2/ | ||
// | ||
//------------------------------------------------------------------ | ||
function loadAsset(source, onSuccess, onError) { | ||
let xhr = new XMLHttpRequest(); | ||
let fileExtension = source.substr(source.lastIndexOf('.') + 1); // Source: http://stackoverflow.com/questions/680929/how-to-extract-extension-from-filename-string-in-javascript | ||
|
||
if (fileExtension) { | ||
xhr.open('GET', source, true); | ||
xhr.responseType = 'blob'; | ||
|
||
xhr.onload = function() { | ||
let asset = null; | ||
if (xhr.status === 200) { | ||
if (fileExtension === 'png' || fileExtension === 'jpg') { | ||
asset = new Image(); | ||
} else if (fileExtension === 'mp3') { | ||
asset = new Audio(); | ||
} else { | ||
if (onError) { onError('Unknown file extension: ' + fileExtension); } | ||
} | ||
asset.onload = function() { | ||
window.URL.revokeObjectURL(asset.src); | ||
}; | ||
asset.src = window.URL.createObjectURL(xhr.response); | ||
if (onSuccess) { onSuccess(asset); } | ||
} else { | ||
if (onError) { onError('Failed to retrieve: ' + source); } | ||
} | ||
}; | ||
} else { | ||
if (onError) { onError('Unknown file extension: ' + fileExtension); } | ||
} | ||
|
||
xhr.send(); | ||
} | ||
|
||
//------------------------------------------------------------------ | ||
// | ||
// Called when all the scripts are loaded, it kicks off the demo app. | ||
// | ||
//------------------------------------------------------------------ | ||
function mainComplete() { | ||
main(); | ||
} | ||
|
||
// | ||
// Start with loading the assets, then the scripts. | ||
loadAssets(assetOrder, | ||
function(source, asset) { // Store it on success | ||
GameAssets[source.key] = asset; | ||
}, | ||
function(error) { | ||
console.log(error); | ||
}, | ||
function() { | ||
loadScripts(scriptOrder, mainComplete); | ||
} | ||
); | ||
|
||
function newScript(name) { | ||
return { | ||
scripts: [name], | ||
onComplete: null | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
let http = require('http'); | ||
let path = require('path'); | ||
let fs = require('fs'); | ||
|
||
let mimeTypes = { | ||
'.js' : 'text/javascript', | ||
'.map' : 'text/javascript', | ||
'.html' : 'text/html', | ||
'.css' : 'text/css', | ||
'.png' : 'image/png', | ||
'.jpg' : 'image/jpeg', | ||
'.mp3' : 'audio/mpeg3', | ||
'.ttf' : 'font/ttf', | ||
'.ico' : 'image/x-icon' | ||
}; | ||
const port = 3000; | ||
|
||
function handleRequest(request, response) { | ||
console.log('request : ', request.url); | ||
let lookup = (request.url === '/') ? '/index.html' : decodeURI(request.url); | ||
let file = lookup.substring(1, lookup.length); | ||
|
||
fs.access(file, fs.constants.R_OK, function(err) { | ||
if (!err) { | ||
fs.readFile(file, function(error, data) { | ||
if (error) { | ||
response.writeHead(500); | ||
response.end('Server Error!'); | ||
} else { | ||
let headers = {'Content-type': mimeTypes[path.extname(lookup)]}; | ||
response.writeHead(200, headers); | ||
response.end(data); | ||
} | ||
}); | ||
} else { | ||
console.log(`${lookup} doesn't exist`); | ||
response.writeHead(404); | ||
response.end(); | ||
} | ||
}); | ||
} | ||
|
||
http.createServer(handleRequest).listen(port, function() { | ||
console.log(`Server is listening on port ${port}`); | ||
}); |