Skip to content

Instantly share code, notes, and snippets.

@alexonozor
Created April 22, 2018 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexonozor/96d98130fceb92046020cddabb3ce24f to your computer and use it in GitHub Desktop.
Save alexonozor/96d98130fceb92046020cddabb3ce24f to your computer and use it in GitHub Desktop.
var Engine = (function(global) {
var doc = global.document,
win = global.window,
canvas = doc.createElement('canvas'),
ctx = canvas.getContext('2d'),
lastTime;
canvas.width = 505;
canvas.height = 606;
doc.body.appendChild(canvas);
function main() {
var now = Date.now(),
dt = (now - lastTime) / 1000.0;
update(dt);
render();
lastTime = now;
win.requestAnimationFrame(main);
};
function init() {
reset();
lastTime = Date.now();
main();
}
function update(dt) {
updateEntities(dt);
// checkCollisions();
}
function updateEntities(dt) {
allEnemies.forEach(function(enemy) {
enemy.update(dt);
});
player.update();
}
function render() {
var rowImages = [
'images/water-block.png', // Top row is water
'images/stone-block.png', // Row 1 of 3 of stone
'images/stone-block.png', // Row 2 of 3 of stone
'images/stone-block.png', // Row 3 of 3 of stone
'images/grass-block.png', // Row 1 of 2 of grass
'images/grass-block.png' // Row 2 of 2 of grass
],
numRows = 6,
numCols = 5,
row, col;
for (row = 0; row < numRows; row++) {
for (col = 0; col < numCols; col++) {
ctx.drawImage(Resources.get(rowImages[row]), col * 101, row * 83);
}
}
renderEntities();
}
function renderEntities() {
allEnemies.forEach(function(enemy) {
enemy.render();
});
player.render();
}
Resources.load([
'images/stone-block.png',
'images/water-block.png',
'images/grass-block.png',
'images/enemy-bug.png',
'images/char-boy.png'
]);
Resources.onReady(init);
global.ctx = ctx;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment