Permalink
Browse files

[~] Refactoring stage 2

  • Loading branch information...
1 parent a302533 commit 3fc05539f4f40973555789f36451f00522c23e93 @Andrew8xx8 committed Apr 3, 2012
Showing with 165 additions and 101 deletions.
  1. +31 −19 js/bullet.js
  2. +60 −55 js/class.js
  3. +28 −15 js/controller.js
  4. +1 −1 js/init.js
  5. +16 −5 js/player.js
  6. +29 −6 js/primitive.js
View
@@ -2,24 +2,24 @@ TextGalactic.BulletTypes = {
simple: {
text: ".",
rate: 10,
- speed: 450,
+ speed: 5,
color: "#ccc"
},
big: {
text: "o",
rate: 5,
- speed: 100,
+ speed: 4,
color: "#ccc"
},
rocket: {
text: "^",
rate: 3,
- speed: 100,
+ speed: 2,
color: "#ccc"
},
lazer: {
text: "|",
- rate: 3,
+ rate: 4,
speed: 300,
color: "#ccc"
}
@@ -30,10 +30,16 @@ TextGalactic.Bullet = TextGalactic.Primitive.extend({
type: TextGalactic.BulletTypes['simple'],
+ exploded: false,
+
init: function (canvas, options) {
+ this.type = options.type;
+ options.text = options.type.text;
+
this._super(canvas, options);
- this.type = options.type;
+ this.moveSpeed = this.type.speed;
+
this.direction = options.direction;
this.animationSpeeed = this.type.speed;
@@ -44,11 +50,19 @@ TextGalactic.Bullet = TextGalactic.Primitive.extend({
return this.shape;
},
- /*move: function (direction) {
- this.shape.animate({
- y: 0
- }, this.type.speed, "<");
- },*/
+ update: function () {
+ this.move(0, (this.direction == 'up') ? -1 : 1);
+ },
+
+ canMove: function (toX, toY) {
+ if (toY < 0 || toY > this.bounds.height) {
+ this.explode()
+
+ return false;
+ }
+
+ return true;
+ },
getContainer: function () {
return this.scene.resources.rectangle.clone().grow( -this.shape.radius*2 );
@@ -73,16 +87,14 @@ TextGalactic.Bullet = TextGalactic.Primitive.extend({
}
},
- explode: function (){
- this.destroy;
- this.options.bullets._destroy(this);
+ explode: function () {
+ this.exploded = true;
+ this.shape.remove();
},
- renderTo: function (ctx) {
- ctx.fillStyle = this.type.color;
- ctx.font = "normal normal " + TextGalactic.Settings.font_size + "px courier";
- ctx.fillText(this.type.text, this.shape._center.x, this.shape._center.y);
-
- return this.parent();
+ destroy: function () {
+ for(var x in this) {
+ delete this[x];
+ }
}
});
View
@@ -4,60 +4,65 @@
*/
// Inspired by base2 and Prototype
(function(){
- var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
- // The base Class implementation (does nothing)
- this.Class = function(){};
-
- // Create a new Class that inherits from this class
- Class.extend = function(prop) {
- var _super = this.prototype;
-
- // Instantiate a base class (but only create the instance,
- // don't run the init constructor)
- initializing = true;
- var prototype = new this();
- initializing = false;
-
- // Copy the properties over onto the new prototype
- for (var name in prop) {
- // Check if we're overwriting an existing function
- prototype[name] = typeof prop[name] == "function" &&
- typeof _super[name] == "function" && fnTest.test(prop[name]) ?
- (function(name, fn){
- return function() {
- var tmp = this._super;
-
- // Add a new ._super() method that is the same method
- // but on the super-class
- this._super = _super[name];
-
- // The method only need to be bound temporarily, so we
- // remove it when we're done executing
- var ret = fn.apply(this, arguments);
- this._super = tmp;
-
- return ret;
- };
- })(name, prop[name]) :
- prop[name];
- }
-
- // The dummy class constructor
- function Class() {
- // All construction is actually done in the init method
- if ( !initializing && this.init )
- this.init.apply(this, arguments);
- }
-
- // Populate our constructed prototype object
- Class.prototype = prototype;
-
- // Enforce the constructor to be what we expect
- Class.prototype.constructor = Class;
+ var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
+ // The base Class implementation (does nothing)
+ this.Class = function(){};
- // And make this class extendable
- Class.extend = arguments.callee;
-
- return Class;
- };
+ // Create a new Class that inherits from this class
+ Class.extend = function(prop) {
+ var _super = this.prototype;
+
+ // Instantiate a base class (but only create the instance,
+ // don't run the init constructor)
+ initializing = true;
+ var prototype = new this();
+ initializing = false;
+
+ // Copy the properties over onto the new prototype
+ for (var name in prop) {
+ // Check if we're overwriting an existing function
+ prototype[name] = typeof prop[name] == "function" &&
+ typeof _super[name] == "function" && fnTest.test(prop[name]) ?
+ (function(name, fn){
+ return function() {
+ var tmp = this._super;
+
+ // Add a new ._super() method that is the same method
+ // but on the super-class
+ this._super = _super[name];
+
+ // The method only need to be bound temporarily, so we
+ // remove it when we're done executing
+ var ret = fn.apply(this, arguments);
+ this._super = tmp;
+
+ return ret;
+ };
+ })(name, prop[name]) :
+ prop[name];
+ }
+
+ // The dummy class constructor
+ function Class() {
+ // All construction is actually done in the init method
+ if ( !initializing && this.init )
+ this.init.apply(this, arguments);
+ }
+
+ Class.destroy = function () {
+ for(var x in this) {
+ delete this[x];
+ }
+ }
+ // Populate our constructed prototype object
+ Class.prototype = prototype;
+
+ // Enforce the constructor to be what we expect
+ Class.prototype.constructor = Class;
+
+ // And make this class extendable
+ Class.extend = arguments.callee;
+
+ return Class;
+ };
})();
View
@@ -2,37 +2,45 @@ TextGalactic.Controller = Class.extend({
activeKeys: [0,0,0,0,0],
init: function (container) {
- this.canvas = Raphael(document.getElementById(container),400,300);
+ this.canvas = Raphael(document.getElementById(container), 400, 300);
this.player = this.createPlayer();
- this.bullets = this.canvas.set();
+ this.bullets = [];
// console.log(this.player);
},
createPlayer: function (center) {
return new TextGalactic.Player(this.canvas, {
- x: 10,
- y: 20,
+ x: 200,
+ y: 280,
text: "A"
});
},
update: function() {
- //this.player.onUpdate();
- //console.log(this.activeKeys);
this.movePlayer();
this.playerShoot();
this.player.update(this.canvas);
-console.log(this.bullets);
- this.bullets.attr({
- fill: "#fff"
- });
- this.bullets.animate({
- scale: 1,
- }, 10, "linear");
+
+ this.moveBullets();
+ console.log(this.bullets);
+ },
+
+ moveBullets: function() {
+ for (i = 0; i < this.bullets.length; i++) {
+ if (typeof(this.bullets[i]) == 'object') {
+ this.bullets[i].update();
+
+ if (this.bullets[i].exploded) {
+ this.bullets[i].destroy();
+ delete this.bullets[i];
+ this.bullets.splice(i, 1);
+ }
+ }
+ }
},
movePlayer: function () {
@@ -58,8 +66,13 @@ console.log(this.bullets);
playerShoot: function () {
if (this.activeKeys['ctrl'] && this.player.canShoot()) {
var start = this.player.getPosition();
- this.bullets.push(this.canvas.text(
- start.x, start.y, this.player.bulletType.text
+ this.bullets.push(new TextGalactic.Bullet(
+ this.canvas, {
+ x: start.x,
+ y: start.y,
+ type: this.player.bulletType,
+ direction: 'up'
+ }
));
}
},
View
@@ -52,5 +52,5 @@ window.onload=function() {
};
// Start game
- setInterval(function() { controler.update() }, 34);
+ setInterval(function() { controler.update() }, 24);
};
View
@@ -1,9 +1,4 @@
TextGalactic.Player = TextGalactic.Primitive.extend({
- /** @constructs */
- initialize: function (options) {
-
- },
-
getCollisionRectangle: function (radius) {
if (!this.collisionRectangle) {
this.collisionRectangle = this.shape.clone();
@@ -37,6 +32,22 @@ TextGalactic.Player = TextGalactic.Primitive.extend({
}
},
+ canMoveY: function (toY) {
+ if (toY < 0 || toY > this.bounds.height) {
+ return false;
+ }
+
+ return true;
+ },
+
+ canMoveX: function (toX) {
+ if (toX < 0 || toX > this.bounds.width) {
+ return false;
+ }
+
+ return true;
+ },
+
hit: function () {
this.health = this.health - 25;
this.redraw();
View
@@ -16,13 +16,36 @@ TextGalactic.Primitive = Class.extend({
(options.y != 'undefined') ? options.y : 0,
(options.text != 'undefined') ? options.text : "A"
);
-
+
+ this.bounds = {
+ width: canvas.width,
+ height: canvas.height,
+ };
+ },
+
+ move: function (dX, dY) {
+ x = this.shape.attr('x');
+ y = this.shape.attr('y');
+ toX = x + (dX * this.moveSpeed);
+ toY = y + (dY * this.moveSpeed);
+
+ if (this.canMove(toX, toY)) {
+ this.shape.animate({
+ x: this.canMoveX(toX) ? toX : x,
+ y: this.canMoveY(toY) ? toY : y,
+ }, this.animationSpeed, "linear");
+ }
+ },
+
+ canMove: function (toX, toY) {
+ return true;
+ },
+
+ canMoveX: function (toX) {
+ return true;
},
- move: function (dx, dy) {
- this.shape.animate({
- x: this.shape.attr('x') + (dx * this.moveSpeed),
- y: this.shape.attr('y') + (dy * this.moveSpeed),
- }, this.animationSpeed, "linear");
+ canMoveY: function (toY) {
+ return true;
}
});

0 comments on commit 3fc0553

Please sign in to comment.