Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some suggestions - see PR message #2

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 12 additions & 28 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function () {
(function (spriteObject, enemyObject) {
'use strict';
var canvas = document.querySelector("canvas");
var drawingSurface = canvas.getContext("2d");
Expand Down Expand Up @@ -35,28 +35,23 @@
background.height = 640;
sprites.push(background);


var hero = Object.create(spriteObject);
hero.image = heroImage;
hero.x = canvas.width / 2 - hero.width / 2;
hero.y = canvas.height - hero.height;
sprites.push(hero);


var assetsLoaded = 0;


var enemyFrequency = 120;
var enemyTimer = 0;


var LOADING = 0;
var PLAYING = 1;
var OVER = 2;
var gameState = LOADING;

//Key codes and directions

var RIGHT = 39;
var LEFT = 37;
var SPACE = 32;
Expand All @@ -66,7 +61,6 @@
var shoot = false;
var spaceKeyIdDown = false;


window.addEventListener("keydown", function (event) {
switch (event.keyCode) {
case LEFT:
Expand All @@ -81,9 +75,9 @@
spaceKeyIdDown = true;
}
}
event.preventDefault()
}, false);


window.addEventListener("keyup", function (event) {
switch (event.keyCode) {
case LEFT:
Expand All @@ -95,15 +89,16 @@
case SPACE:
spaceKeyIdDown = false;
}
event.preventDefault()
}, false);


//Start here
//Start here
update();

function update() {
requestAnimationFrame(update, canvas);

requestAnimationFrame(update, canvas);
switch (gameState) {
case LOADING:
console.log("Loading...");
Expand All @@ -115,19 +110,13 @@
endGame();
break;
}
render();
}


render();
}

function loadHandler() {
assetsLoaded++;
if (assetsLoaded === assetsToLoad.length) {

// for (var i = 0; i < images.length; i++) {
// images[i].removeEventListener("load", loadHandler, false);
// }

gameState = PLAYING;
}
}
Expand Down Expand Up @@ -208,10 +197,7 @@

}


function endGame() {

}
function endGame() {}

function render() {
drawingSurface.clearRect(0, 0, canvas.width, canvas.height);
Expand All @@ -226,7 +212,6 @@
sprite.width, sprite.height
);
}

}
}

Expand Down Expand Up @@ -277,6 +262,7 @@
enemy.startAnimation();
sprites.push(enemy);
enemies.push(enemy);
console.log(enemy)
}

function destroyEnemy(enemy) {
Expand All @@ -289,6 +275,4 @@
removeObject(enemy, sprites);
}
}


}());
}(spriteObject, enemyObject));
109 changes: 53 additions & 56 deletions js/objects.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,60 @@
var spriteObject =
{
image: "",
sourceX: 0,
sourceY: 0,
sourceWidth: 80,
sourceHeight: 110,
width: 80,
height: 110,
x: 0,
y: 0,
vx: 0,
vy: 0,
startPointX: 0,
startPointY: 100,
endPointX: 0,
endPointY: 0,
visible: true,
var spriteObject = {
image: "",
sourceX: 0,
sourceY: 0,
sourceWidth: 80,
sourceHeight: 110,
width: 80,
height: 110,
x: 0,
y: 0,
vx: 0,
vy: 0,
startPointX: 0,
startPointY: 100,
endPointX: 0,
endPointY: 0,
visible: true,

COLUMNS: 9,
numberOfFrames: 1,
currentFrame: 0,
firstAnimationFrame: 0,
lastAnimationFrame: 0,


//Getters
centerX: function () {
return this.x + (this.width / 2);
},
centerY: function () {
return this.y + (this.height / 2);
},
halfWidth: function () {
return this.width / 2;
},
halfHeight: function () {
return this.height / 2;
},
vectorA: function () {
return (this.endPointY - this.startPointY) / (this.startPointX - this.endPointX) * -1;
},
vectorB: function () {
return this.endPointY - (this.vectorA() * this.endPointX);
},
updateAnimation: function () {
this.sourceX = Math.floor(this.currentFrame % this.COLUMNS) * this.sourceWidth;
this.sourceY = Math.floor(this.currentFrame / this.COLUMNS) * this.sourceHeight;
},
startAnimation: function () {
this.currentFrame++;
if (this.currentFrame > this.lastAnimationFrame) {
this.currentFrame = this.firstAnimationFrame;
}
this.updateAnimation;
setTimeout(this.startAnimation, 100);
COLUMNS: 9,
numberOfFrames: 1,
currentFrame: 0,
firstAnimationFrame: 0,
lastAnimationFrame: 0,

//Getters
centerX: function () {
return this.x + (this.width / 2);
},
centerY: function () {
return this.y + (this.height / 2);
},
halfWidth: function () {
return this.width / 2;
},
halfHeight: function () {
return this.height / 2;
},
vectorA: function () {
return (this.endPointY - this.startPointY) / (this.startPointX - this.endPointX) * -1;
},
vectorB: function () {
return this.endPointY - (this.vectorA() * this.endPointX);
},
updateAnimation: function () {
this.sourceX = Math.floor(this.currentFrame % this.COLUMNS) * this.sourceWidth;
this.sourceY = Math.floor(this.currentFrame / this.COLUMNS) * this.sourceHeight;
},
startAnimation: function () {
this.currentFrame++;
if (this.currentFrame > this.lastAnimationFrame) {
this.currentFrame = this.firstAnimationFrame;
}
this.updateAnimation;
setTimeout(this.startAnimation, 100);

};
}
};


var enemyObject = Object.create(spriteObject);
Expand Down