Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created January 26, 2013 17:00
Show Gist options
  • Select an option

  • Save agmcleod/4643265 to your computer and use it in GitHub Desktop.

Select an option

Save agmcleod/4643265 to your computer and use it in GitHub Desktop.
Rendered JS
(function() {
Entities.Bullet = me.ObjectEntity.extend({
init: function(x, y, settings) {
this.tx = settings.tx;
this.ty = settings.ty;
settings.image = "bullets";
settings.spritewidth = 16;
settings.spriteheight = 16;
this.width = 16;
this.height = 16;
this.speed = 30;
this.source = settings.source;
this.damage = 1;
this.parent(x, y, settings);
return this.calculateTarget();
},
calculateTarget: function() {
var angle;
angle = Math.atan2(this.ty - this.pos.y, this.tx - this.pos.x) * (180 / Math.PI);
this.velx = Math.cos(angle * Math.PI / 180) * this.speed * me.timer.tick;
this.vely = Math.sin(angle * Math.PI / 180) * this.speed * me.timer.tick;
if (angle < 0) {
angle = 360 - (-angle);
}
return this.angle = (angle - 90) * Math.PI / 180;
},
update: function() {
if (this.pos.x > 800 || this.pos.x < -this.width || this.pos.y < -this.height || this.pos.y > 640) {
me.game.remove(this);
}
this.pos.x += this.velx;
this.pos.y += this.vely;
this.parent();
return true;
},
addAnimationArray: function(arr, setActive) {
this.addAnimation("idle", arr);
if (setActive) {
return this.setCurrentAnimation("idle");
}
}
});
}).call(this);
(function() {
Entities.BaseEntity = me.ObjectEntity.extend({
init: function(x, y, settings) {
this.shootCooldown = settings.shootCooldown;
this.timer = me.timer.getTime();
this.entity_source = settings.entity_source;
this.health = settings.health;
return this.parent(x, y, settings);
},
center: function() {
return {
x: this.pos.x + (this.width / 2),
y: this.pos.y + (this.height / 2)
};
},
onCollision: function(res, obj) {
if (obj['source'] !== null && obj['source'] !== this.entity_source) {
this.health -= obj.damage;
if (obj.source === "player" && this.health <= 0) {
me.game.remove(this);
return me.game.HUD.updateItemValue("score", 100);
}
} else if (obj['entity_source'] !== null && obj['entity_source'] !== this.entity_source) {
return this.health -= 1;
}
},
shoot: function(target) {
var bullet;
bullet = new Entities.Bullet(this.center().x, this.center().y, {
tx: target.x,
ty: target.y,
source: this.entity_source
});
bullet.addAnimationArray([0], true);
if (this.entity_source === "enemy") {
return me.game.add(bullet, window.App.game.bulletZEnemyIndex);
} else if (this.entity_source === "player") {
return me.game.add(bullet, window.App.game.bulletZIndex);
}
},
update: function(target, obj) {
if (target !== null && typeof target !== "undefined" && (me.timer.getTime() - this.timer) > this.shootCooldown) {
this.shoot(target);
this.timer = me.timer.getTime();
}
this.parent(obj);
return true;
}
});
}).call(this);
(function() {
Entities.Player = Entities.BaseEntity.extend({
init: function() {
var settings, x, y;
settings = {
image: "player",
spritewidth: 128,
spriteheight: 96,
shootCooldown: 200,
entity_source: "player",
health: 5
};
x = (800 / 2) - (settings.spritewidth / 2);
y = (640 / 2) - (settings.spriteheight / 2);
this.parent(x, y, settings);
this.addAnimation("idle", [0]);
return this.setCurrentAnimation("idle");
},
update: function() {
if (me.input.isKeyPressed("shoot")) {
this.parent(me.input.mouse.pos, this);
} else {
this.parent(null, this);
}
return true;
}
});
}).call(this);
(function() {
Entities.Background = me.ObjectEntity.extend({
init: function(x, y, settings) {
settings.spritewidth = 800;
settings.spriteheight = 1280;
settings.image = "Tower_BlurBG";
return this.parent(x, y, settings);
},
update: function() {
this.pos.y -= 40 * me.timer.tick;
if (this.pos.y <= -1280) {
this.pos.y = 1280;
}
this.parent();
return true;
}
});
}).call(this);
(function() {
HUD.ScoreHUD = me.HUD_Item.extend({
init: function(x, y) {
this.parent(x, y);
return this.font = new me.Font("Verdana, Helvetica, Arial, sans-serif", 24, "#f00", "top");
},
draw: function(context, x, y) {
return this.font.draw(context, this.value, this.pos.x + x, this.pos.y + y);
}
});
}).call(this);
(function() {
window.MusicController = (function() {
function MusicController() {}
MusicController.prototype.init = function() {
this.tracks = {
intro: "112bpm_fade_in_intro",
layerOne: "112bpm_1_layer_loop",
layerTwo: "112bpm_2_layer_loop",
layerThree: "112bpm_3_layer_loop"
};
return this.play("intro");
};
MusicController.prototype.cleanup = function() {
return me.audio.stopTrack();
};
MusicController.prototype.play = function(track_name) {
if (this.playing) {
me.audio.stopTrack();
}
me.audio.playTrack(this.tracks[track_name]);
return this.playing = true;
};
return MusicController;
})();
}).call(this);
(function() {
var Game, PlayScreen;
window.App = {
init: function() {
if (!me.video.init("app", 800, 640, false, 1)) {
alert("your browser does not support the canvas");
}
me.loader.onload = this.loaded.bind(this);
me.loader.preload([
{
name: "TowerBG",
type: "image",
src: "images/TowerBG.jpg"
}, {
name: "Tower_BlurBG",
type: "image",
src: "images/Tower_BlurBG.jpg"
}, {
name: "bullets",
type: "image",
src: "images/bullets.png"
}, {
name: "player",
type: "image",
src: "images/player.png"
}
]);
me.audio.init("mp3");
return me.state.change(me.state.LOADING);
},
loaded: function() {
me.state.set(me.state.PLAY, new PlayScreen());
me.input.bindKey(me.input.KEY.X, "shoot");
me.input.bindMouse(me.input.mouse.LEFT, me.input.KEY.X);
return me.state.change(me.state.PLAY);
}
};
Game = me.InvisibleEntity.extend({
init: function() {
var player;
this.backgrounds = [new Entities.Background(0, 0, {}), new Entities.Background(0, 1280, {})];
this.bottomBackground = 0;
me.game.add(this.backgrounds[0], 0);
me.game.add(this.backgrounds[1], 0);
player = new Entities.Player();
return me.game.add(player, this.spriteZIndex);
},
spriteZIndex: 50,
bulletZIndex: 49,
bulletZEnemyIndex: 51,
update: function() {
return me.video.clearSurface(me.video.getScreenCanvas().getContext("2d"), "#000");
}
});
PlayScreen = me.ScreenObject.extend({
onDestroyEvent: function() {
me.game.disableHUD();
return window.App.game.musicController.cleanup();
},
onResetEvent: function() {
window.App.game = new Game();
me.game.add(window.App.game, 0);
me.game.addHUD(0, 0, 800, 640);
me.game.HUD.addItem("score", new HUD.ScoreHUD(650, 10));
return me.game.sort();
}
});
window.onReady(function() {
return App.init();
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment