Skip to content

Commit

Permalink
Remove extra spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
githubteacher committed Nov 27, 2018
1 parent 19d242b commit 6c63c3c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 56 deletions.
70 changes: 35 additions & 35 deletions engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame =
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}

if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};

if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());


var Game = new function() {

var Game = new function() {
var boards = [];

// Game Initialization
Expand All @@ -43,15 +43,15 @@ var Game = new function() {

this.setupInput();

this.loop();
this.loop();

if(this.mobile) {
this.setBoard(4,new TouchControls());
}

SpriteSheet.load(sprite_data,callback);
};


// Handle Input
var KEY_CODES = { 37:'left', 39:'right', 32 :'fire' };
Expand All @@ -67,7 +67,7 @@ var Game = new function() {

window.addEventListener('keyup',function(e) {
if(KEY_CODES[e.keyCode]) {
Game.keys[KEY_CODES[e.keyCode]] = false;
Game.keys[KEY_CODES[e.keyCode]] = false;
e.preventDefault();
}
},false);
Expand All @@ -77,21 +77,21 @@ var Game = new function() {
var lastTime = new Date().getTime();
var maxTime = 1/30;
// Game Loop
this.loop = function() {
this.loop = function() {
var curTime = new Date().getTime();
requestAnimationFrame(Game.loop);
var dt = (curTime - lastTime)/1000;
if(dt > maxTime) { dt = maxTime; }

for(var i=0,len = boards.length;i<len;i++) {
if(boards[i]) {
if(boards[i]) {
boards[i].step(dt);
boards[i].draw(Game.ctx);
}
}
lastTime = curTime;
};

// Change an active game board
this.setBoard = function(num,board) { boards[num] = board; };

Expand All @@ -100,7 +100,7 @@ var Game = new function() {
var container = document.getElementById("container"),
hasTouch = !!('ontouchstart' in window),
w = window.innerWidth, h = window.innerHeight;

if(hasTouch) { this.mobile = true; }

if(screen.width >= 1280 || !hasTouch) { return false; }
Expand Down Expand Up @@ -139,9 +139,9 @@ var Game = new function() {


var SpriteSheet = new function() {
this.map = { };
this.map = { };

this.load = function(spriteData,callback) {
this.load = function(spriteData,callback) {
this.map = spriteData;
this.image = new Image();
this.image.onload = callback;
Expand All @@ -152,9 +152,9 @@ var SpriteSheet = new function() {
var s = this.map[sprite];
if(!frame) frame = 0;
ctx.drawImage(this.image,
s.sx + frame * s.w,
s.sy,
s.w, s.h,
s.sx + frame * s.w,
s.sy,
s.w, s.h,
Math.floor(x), Math.floor(y),
s.w, s.h);
};
Expand All @@ -173,7 +173,7 @@ var TitleScreen = function TitleScreen(title,subtitle,callback) {
ctx.fillStyle = "#FFFFFF";

ctx.font = "bold 40px bangers";
var measure = ctx.measureText(title);
var measure = ctx.measureText(title);
ctx.fillText(title,Game.width/2 - measure.width/2,Game.height/2);

ctx.font = "bold 20px bangers";
Expand All @@ -191,18 +191,18 @@ var GameBoard = function() {
this.cnt = {};

// Add a new object to the object list
this.add = function(obj) {
obj.board=this;
this.objects.push(obj);
this.add = function(obj) {
obj.board=this;
this.objects.push(obj);
this.cnt[obj.type] = (this.cnt[obj.type] || 0) + 1;
return obj;
return obj;
};

// Mark an object for removal
this.remove = function(obj) {
this.remove = function(obj) {
var idx = this.removed.indexOf(obj);
if(idx == -1) {
this.removed.push(obj);
this.removed.push(obj);
return true;
} else {
return false;
Expand All @@ -223,7 +223,7 @@ var GameBoard = function() {
}
};

// Call the same method on all current objects
// Call the same method on all current objects
this.iterate = function(funcName) {
var args = Array.prototype.slice.call(arguments,1);
for(var i=0,len=this.objects.length;i<len;i++) {
Expand All @@ -242,7 +242,7 @@ var GameBoard = function() {

// Call step on all objects and them delete
// any object that have been marked for removal
this.step = function(dt) {
this.step = function(dt) {
this.resetRemoved();
this.iterate('step',dt);
this.finalizeRemoved();
Expand All @@ -253,7 +253,7 @@ var GameBoard = function() {
this.iterate('draw',ctx);
};

// Check for a collision between the
// Check for a collision between the
// bounding rects of two objects
this.overlap = function(o1,o2) {
return !((o1.y+o1.h-1<o2.y) || (o1.y>o2.y+o2.h-1) ||
Expand Down Expand Up @@ -318,9 +318,9 @@ Level.prototype.step = function(dt) {

// Start, End, Gap, Type, Override
// [ 0, 4000, 500, 'step', { x: 100 } ]
while((curShip = this.levelData[idx]) &&
while((curShip = this.levelData[idx]) &&
(curShip[0] < this.t + 2000)) {
// Check if we've passed the end time
// Check if we've passed the end time
if(this.t > curShip[1]) {
remove.push(curShip);
} else if(curShip[0] < this.t) {
Expand All @@ -343,7 +343,7 @@ Level.prototype.step = function(dt) {
if(remIdx != -1) this.levelData.splice(remIdx,1);
}

// If there are no more enemies on the board or in
// If there are no more enemies on the board or in
// levelData, this level is done
if(this.levelData.length === 0 && this.board.cnt[OBJECT_ENEMY] === 0) {
if(this.callback) this.callback();
Expand Down Expand Up @@ -371,8 +371,8 @@ var TouchControls = function() {

var txtSize = ctx.measureText(txt);

ctx.fillText(txt,
x+blockWidth/2-txtSize.width/2,
ctx.fillText(txt,
x+blockWidth/2-txtSize.width/2,
y+3*blockWidth/4+5);
};

Expand Down Expand Up @@ -400,10 +400,10 @@ var TouchControls = function() {
x = touch.pageX / Game.canvasMultiplier - Game.canvas.offsetLeft;
if(x < unitWidth) {
Game.keys['left'] = true;
}
}
if(x > unitWidth && x < 2*unitWidth) {
Game.keys['right'] = true;
}
}
}

if(e.type == 'touchstart' || e.type == 'touchend') {
Expand Down
40 changes: 19 additions & 21 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ var sprites = {
};

var enemies = {
straight: { x: 0, y: -50, sprite: 'enemy_ship', health: 10,
straight: { x: 0, y: -50, sprite: 'enemy_ship', health: 10,
E: 100 },
ltr: { x: 0, y: -100, sprite: 'enemy_purple', health: 10,
ltr: { x: 0, y: -100, sprite: 'enemy_purple', health: 10,
B: 75, C: 1, E: 100, missiles: 2 },
circle: { x: 250, y: -50, sprite: 'enemy_circle', health: 10,
circle: { x: 250, y: -50, sprite: 'enemy_circle', health: 10,
A: 0, B: -100, C: 1, E: 20, F: 100, G: 1, H: Math.PI/2 },
wiggle: { x: 100, y: -50, sprite: 'enemy_bee', health: 20,
wiggle: { x: 100, y: -50, sprite: 'enemy_bee', health: 20,
B: 50, C: 4, E: 100, firePercentage: 0.001, missiles: 2 },
step: { x: 0, y: -50, sprite: 'enemy_circle', health: 10,
B: 150, C: 1.2, E: 75 }
Expand All @@ -38,8 +38,8 @@ var startGame = function() {
Game.setBoard(0,new Starfield(20,0.4,100,true));
Game.setBoard(1,new Starfield(50,0.6,100));
Game.setBoard(2,new Starfield(100,1.0,50));
}
Game.setBoard(3,new TitleScreen("Alien Invasion",
}
Game.setBoard(3,new TitleScreen("Alien Invasion",
"Press fire to start playing",
playGame));
};
Expand Down Expand Up @@ -67,13 +67,13 @@ var playGame = function() {
};

var winGame = function() {
Game.setBoard(3,new TitleScreen("You win!",
Game.setBoard(3,new TitleScreen("You win!",
"Press fire to play again",
playGame));
};

var loseGame = function() {
Game.setBoard(3,new TitleScreen("You lose!",
Game.setBoard(3,new TitleScreen("You lose!",
"Press fire to play again",
playGame));
};
Expand All @@ -82,13 +82,13 @@ var Starfield = function(speed,opacity,numStars,clear) {

// Set up the offscreen canvas
var stars = document.createElement("canvas");
stars.width = Game.width;
stars.width = Game.width;
stars.height = Game.height;
var starCtx = stars.getContext("2d");

var offset = 0;

// If the clear option is set,
// If the clear option is set,
// make the background black instead of transparent
if(clear) {
starCtx.fillStyle = "#000";
Expand Down Expand Up @@ -139,7 +139,7 @@ var Starfield = function(speed,opacity,numStars,clear) {
};
};

var PlayerShip = function() {
var PlayerShip = function() {
this.setup('ship', { vx: 0, reloadTime: 0.25, maxVel: 200 });

this.reload = this.reloadTime;
Expand All @@ -154,7 +154,7 @@ var PlayerShip = function() {
this.x += this.vx * dt;

if(this.x < 0) { this.x = 0; }
else if(this.x > Game.width - this.w) {
else if(this.x > Game.width - this.w) {
this.x = Game.width - this.w;
}

Expand Down Expand Up @@ -182,7 +182,7 @@ PlayerShip.prototype.hit = function(damage) {
var PlayerMissile = function(x,y) {
this.setup('missile',{ vy: -700, damage: 10 });
this.x = x - this.w/2;
this.y = y - this.h;
this.y = y - this.h;
};

PlayerMissile.prototype = new Sprite();
Expand All @@ -194,8 +194,8 @@ PlayerMissile.prototype.step = function(dt) {
if(collision) {
collision.hit(this.damage);
this.board.remove(this);
} else if(this.y < -this.h) {
this.board.remove(this);
} else if(this.y < -this.h) {
this.board.remove(this);
}
};

Expand All @@ -209,9 +209,9 @@ var Enemy = function(blueprint,override) {
Enemy.prototype = new Sprite();
Enemy.prototype.type = OBJECT_ENEMY;

Enemy.prototype.baseParameters = { A: 0, B: 0, C: 0, D: 0,
Enemy.prototype.baseParameters = { A: 0, B: 0, C: 0, D: 0,
E: 0, F: 0, G: 0, H: 0,
t: 0, reloadTime: 0.75,
t: 0, reloadTime: 0.75,
reload: 0 };

Enemy.prototype.step = function(dt) {
Expand Down Expand Up @@ -253,7 +253,7 @@ Enemy.prototype.hit = function(damage) {
if(this.health <=0) {
if(this.board.remove(this)) {
Game.points += this.points || 100;
this.board.add(new Explosion(this.x + this.w/2,
this.board.add(new Explosion(this.x + this.w/2,
this.y + this.h/2));
}
}
Expand All @@ -275,7 +275,7 @@ EnemyMissile.prototype.step = function(dt) {
collision.hit(this.damage);
this.board.remove(this);
} else if(this.y > Game.height) {
this.board.remove(this);
this.board.remove(this);
}
};

Expand All @@ -299,5 +299,3 @@ Explosion.prototype.step = function(dt) {
window.addEventListener("load", function() {
Game.initialize("game",sprites,startGame);
});


0 comments on commit 6c63c3c

Please sign in to comment.