Skip to content

Commit

Permalink
set as node server
Browse files Browse the repository at this point in the history
  • Loading branch information
drollinger committed Apr 23, 2021
1 parent 06b4a30 commit df153cd
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 55 deletions.
Binary file added favicon.ico
Binary file not shown.
12 changes: 3 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@
<link rel="stylesheet" type="text/css" href="styles/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="styles/style.css"/>
<script src='styles/bootstrap.bundle.min.js'></script>
<script src='scripts/settings.js'></script>
<script src='scripts/audio.js'></script>
<script src='scripts/menuing.js'></script>
<script src='scripts/gamePlay.js'></script>
<script src='scripts/input.js'></script>
<script src='scripts/graphics.js'></script>
<script src='scripts/blocks.js'></script>
<script src='scripts/main.js'></script>
</head>
<body onload="main()">
<body>
<div class"container">
<div id="canvasSection" class="row row-section">
<div id="canvasSection" class="row d-flex justify-content-center">
Expand Down Expand Up @@ -136,5 +128,7 @@ <h2>Customize Section</h2>
</div>
</div>
</div>

<script data-main = "scripts/loader" src = "scripts/require.js"></script>
</body>
</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
158 changes: 158 additions & 0 deletions scripts/loader.js
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
};
};
5 changes: 1 addition & 4 deletions scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ function Initialize() {
blocks.InitBlockHandlers(keyInput);

//Game Graphics
let graphics = Graphics({
background: settings.images.background,
block: settings.images.block,
});
let graphics = Graphics({});

return [
graphics,
Expand Down
File renamed without changes.
67 changes: 29 additions & 38 deletions scripts/graphics.js → scripts/render/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ let Graphics = function(spec) {
let canvas = document.getElementById(settings.elements.canvas);
let context = canvas.getContext('2d');

//Load all images
let imgs = {};
imgs.background = createImg(spec.background);
imgs.block = createImg(spec.block);

let bWidth = canvas.width/SD.width;
let bHeight = canvas.height/SD.height;
let gridX = bWidth*SG.startX;
Expand All @@ -32,14 +27,12 @@ let Graphics = function(spec) {
};

let RenderBackground = function() {
if (imgs.background.isReady) {
context.save();
context.imageSmoothingEnabled = false;
context.drawImage(imgs.background, 0, 0,
canvas.width + 0.5, canvas.height + 0.5
);
context.restore();
};
context.save();
context.imageSmoothingEnabled = false;
context.drawImage(GameAssets['background'], 0, 0,
canvas.width + 0.5, canvas.height + 0.5
);
context.restore();
};

let RenderGamePlay = function(spec) {
Expand Down Expand Up @@ -95,33 +88,31 @@ let Graphics = function(spec) {
};

let RenderBlocks = function(spec) {
if (imgs.block.isReady) {
context.save();
context.imageSmoothingEnabled = false;
for (let block of spec.blocks.Info.blocks) {
if (block.loc.y >= 0) {
let x = gridX + bWidth*block.loc.x;
let y = gridY + bHeight*block.loc.y;
let w = bWidth;
let h = bHeight;
context.fillStyle = block.color;
//Rect needs to fit inside block
context.fillRect(x+1, y+1, w-2, h-2);
context.drawImage(imgs.block, x, y, w, h);
};
};
for (let block of spec.blocks.Info.nextShape) {
let nw = bWidth*SL.next.scale;
let nh = bHeight*SL.next.scale;
let x = bWidth*(SL.next.x+(SL.next.scale*block.rLoc.x)+2.25);
let y = bHeight*(SL.next.y+(SL.next.scale*block.rLoc.y));
context.fillStyle = block.color;
//Rect needs to fit inside block
context.fillRect(x+1, y+1, nw-2, nh-2);
context.drawImage(imgs.block, x, y, nw, nh);
context.save();
context.imageSmoothingEnabled = false;
for (let block of spec.blocks.Info.blocks) {
if (block.loc.y >= 0) {
let x = gridX + bWidth*block.loc.x;
let y = gridY + bHeight*block.loc.y;
let w = bWidth;
let h = bHeight;
context.fillStyle = block.color;
//Rect needs to fit inside block
context.fillRect(x+1, y+1, w-2, h-2);
context.drawImage(GameAssets['block'], x, y, w, h);
};
context.restore();
};
for (let block of spec.blocks.Info.nextShape) {
let nw = bWidth*SL.next.scale;
let nh = bHeight*SL.next.scale;
let x = bWidth*(SL.next.x+(SL.next.scale*block.rLoc.x)+2.25);
let y = bHeight*(SL.next.y+(SL.next.scale*block.rLoc.y));
context.fillStyle = block.color;
//Rect needs to fit inside block
context.fillRect(x+1, y+1, nw-2, nh-2);
context.drawImage(GameAssets['block'], x, y, nw, nh);
};
context.restore();
};

let RenderCustomControls = function(spec) {
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions scripts/require.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ var settings = {
name: 'drollTetris',
highscores: 'drollTetris.highScores',
},
images: {
background: 'images/background.png',
block: 'images/block.png',
},
fonts: {
plainText: {size:1, font:"CalculatorRegular"},
gameOver: {size:1, font:"Comic Sans MS"},
Expand Down
47 changes: 47 additions & 0 deletions server.js
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}`);
});

0 comments on commit df153cd

Please sign in to comment.