Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[~] Refactoring stage 2

  • Loading branch information...
commit a3025338d34dae67dc551be5e8d472efe26736cb 1 parent 4a5655d
Andrew Kulakov authored
3  index.html
View
@@ -9,10 +9,11 @@
<script src="./js/init.js"></script>
<script src="./js/class.js"></script>
+ <script src="./js/primitive.js"></script>
<script src="./js/controller.js"></script>
+ <script src="./js/bullet.js"></script>
<script src="./js/player.js"></script>
<!--script src="./js/collection.js"></script>
- <script src="./js/bullet.js"></script>
<script src="./js/bullets.js"></script>
<script src="./js/player.js"></script>
<script src="./js/enemy.js"></script>
29 js/bullet.js
View
@@ -25,26 +25,31 @@ TextGalactic.BulletTypes = {
}
};
-TextGalactic.Bullet = atom.Class(
-/**
- * @lends TextGalactic.Bullet#
- * @augments LibCanvas.Scene.Element#
- */
-{
- Extends: LibCanvas.Scene.Element,
-
+TextGalactic.Bullet = TextGalactic.Primitive.extend({
direction: 'up',
type: TextGalactic.BulletTypes['simple'],
- /** @constructs */
- initialize: function (scene, options) {
- this.parent(scene, options);
- this.moveSpeed = (this.type.speed);
+ init: function (canvas, options) {
+ this._super(canvas, options);
+
this.type = options.type;
this.direction = options.direction;
+ this.animationSpeeed = this.type.speed;
+
+ this.shape.attr({
+ fill: options.type.color
+ });
+
+ return this.shape;
},
+ /*move: function (direction) {
+ this.shape.animate({
+ y: 0
+ }, this.type.speed, "<");
+ },*/
+
getContainer: function () {
return this.scene.resources.rectangle.clone().grow( -this.shape.radius*2 );
},
2  js/bullets.js
View
@@ -1,4 +1,4 @@
-TextGalactic.Bullets = atom.Class({
+TextGalactic.Bullets = Class.extend({
Extends: TextGalactic.Collection,
91 js/controller.js
View
@@ -1,24 +1,71 @@
TextGalactic.Controller = Class.extend({
+ activeKeys: [0,0,0,0,0],
+
init: function (container) {
this.canvas = Raphael(document.getElementById(container),400,300);
- this.player = this.createPlayer();
-
- console.log(this.player);
+ this.player = this.createPlayer();
+
+ this.bullets = this.canvas.set();
+ // console.log(this.player);
},
createPlayer: function (center) {
- var player = {
- height: TextGalactic.Settings.font_size,
- width: TextGalactic.Settings.font_stretch
+ return new TextGalactic.Player(this.canvas, {
+ x: 10,
+ y: 20,
+ 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");
+ },
+
+ movePlayer: function () {
+ if (this.activeKeys['aleft'] && this.activeKeys['aup']) {
+ this.player.move(-1, -1);
+ } else if (this.activeKeys['aleft'] && this.activeKeys['adown']) {
+ this.player.move(-1, 1);
+ } else if (this.activeKeys['aright'] && this.activeKeys['aup']) {
+ this.player.move(1, -1);
+ } else if (this.activeKeys['aright'] && this.activeKeys['adown']) {
+ this.player.move(1, 1);
+ } else if (this.activeKeys['aleft']) {
+ this.player.move(-1, 0);
+ } else if (this.activeKeys['aright']) {
+ this.player.move(1, 0);
+ } else if (this.activeKeys['aup']) {
+ this.player.move(0, -1);
+ } else if (this.activeKeys['adown']) {
+ this.player.move(0, 1);
}
+ },
- return new TextGalactic.Player(this.canvas);
+ 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
+ ));
+ }
},
- update: function() {
- console.log(this);
- /// this.player.onUpdate();
+ draw: function () {
+
},
ctx: null,
@@ -84,4 +131,28 @@ TextGalactic.Controller = Class.extend({
increaseScore: function (score) {
this.score = this.score + score;
},
+
+ changeKey: function (which, to) {
+ switch (which){
+ case 65: case 37: // left
+ this.activeKeys['aleft'] = to;
+ break;
+ case 87: case 38: // up
+ this.activeKeys['aup'] = to;
+ break;
+ case 68: case 39: // right
+ this.activeKeys['aright'] = to;
+ break;
+ case 83: case 40: // down
+ this.activeKeys['adown'] = to;
+ break;
+ case 32: // space bar;
+ this.activeKeys[4] = to;
+ break;
+ case 17: // ctrl
+ this.activeKeys['ctrl'] = to;
+ break;
+ }
+ },
+
});
37 js/init.js
View
@@ -34,36 +34,23 @@ var now = 0;
var x = 0;
var y = 0;
-function update() {
- if (key[0] == 1) {
- x++;
- }
-
- var c = canvas.circle(x, 50, 40);
-}
-
-function changeKey(which, to) {
- switch (which){
- case 65:case 37: key[0]=to; break; // left
- case 87: case 38: key[2]=to; break; // up
- case 68: case 39: key[1]=to; break; // right
- case 83: case 40: key[3]=to; break;// down
- case 32: key[4]=to; break; // space bar;
- case 17: key[5]=to; break; // ctrl
- }
-}
-
-document.onkeydown=function(e){changeKey((e||window.event).keyCode, 1);};
-document.onkeyup=function(e){changeKey((e||window.event).keyCode, 0);};
-
//window.onerror=function(){
// alert('An error has occured, the most likely reason is because you are using an incompatible browser.\nYou must be using one of the following browsers or a newer version:\n\n- Internet Explorer 6\n- Firefox 1.5\n- Safari 1.3\n- Opera 9');
// window.onerror=function(){};
// return true;
//}
-window.onload=function(){
+window.onload=function() {
var controler = new TextGalactic.Controller("canvas_container");
-
- setInterval(function() { controler.update() }, 35);
+
+ // Bind events
+ document.onkeydown = function(e) {
+ controler.changeKey((e||window.event).keyCode, 1);
+ };
+ document.onkeyup = function(e) {
+ controler.changeKey((e||window.event).keyCode, 0);
+ };
+
+ // Start game
+ setInterval(function() { controler.update() }, 34);
};
65 js/player.js
View
@@ -1,8 +1,7 @@
-TextGalactic.Player = Class.extend({
+TextGalactic.Player = TextGalactic.Primitive.extend({
/** @constructs */
- initialize: function (canvas, options) {
- this.canvas = canvas;
- this.options = options;
+ initialize: function (options) {
+
},
getCollisionRectangle: function (radius) {
@@ -17,9 +16,26 @@ TextGalactic.Player = Class.extend({
rate: 0,
- speed: 250,
+ bulletType: TextGalactic.BulletTypes['simple'],
- bulletType: '',//TextGalactic.BulletTypes['simple'],
+ canShoot: function() {
+
+ this.rate++;
+
+ if (this.rate > this.bulletType.rate / 2) {
+ this.rate = 0;
+ return true;
+ }
+
+ return false;
+ },
+
+ getPosition: function () {
+ return {
+ x: this.shape.attr('x'),
+ y: this.shape.attr('y')
+ }
+ },
hit: function () {
this.health = this.health - 25;
@@ -50,34 +66,10 @@ TextGalactic.Player = Class.extend({
},
onUpdate: function (time) {
- var keys = this.scene.resources.keyboard;
+ /*var keys = this.scene.resources.keyboard;
var moveSpeed = (this.speed * time).toSeconds().round();
var dx = 0; var dy = 0;
- if (keys.keyState('aleft') && keys.keyState('aup')) {
- dx = this.normalize_dx(-moveSpeed);
- dy = this.normalize_dy(-moveSpeed);
- } else if (keys.keyState('aleft') && keys.keyState('adown')) {
- dx = this.normalize_dx(-moveSpeed);
- dy = this.normalize_dy(moveSpeed);
- } else if (keys.keyState('aright') && keys.keyState('aup')) {
- dx = this.normalize_dx(moveSpeed);
- dy = this.normalize_dy(-moveSpeed);
- } else if (keys.keyState('aright') && keys.keyState('adown')) {
- dx = this.normalize_dx(moveSpeed);
- dy = this.normalize_dy(moveSpeed);
- } if (keys.keyState('aleft')) {
- dx = this.normalize_dx(-moveSpeed);
- } else if (keys.keyState('aright')) {
- dx = this.normalize_dx(moveSpeed);
- } else if (keys.keyState('aup')) {
- dy = this.normalize_dy(-moveSpeed);
- } else if (keys.keyState('adown')) {
- dy = this.normalize_dy(moveSpeed);
- }
-
- move(this, dx, dy);
-
if (this.rate > this.bulletType.rate / 2) {
this.options.controller.getBullets().create(
new Point(this.shape.from.x, this.shape.from.y),
@@ -88,15 +80,14 @@ TextGalactic.Player = Class.extend({
this.rate = 0;
}
- this.rate++;
+ this.rate++;*/
},
- renderTo: function (ctx) {
+ update: function (canvas) {
var healthq = Math.round(this.health/300 * 255);
- ctx.fillStyle = "rgb(255, " + (healthq) + "," + (healthq) + ")";
- ctx.font = "normal normal " + TextGalactic.Settings.font_size + "px courier";
- ctx.fillText("A", this.shape.from.x, this.shape.to.y);
- return this.parent();
+ this.shape.attr({
+ fill: "rgb(255, " + (healthq) + "," + (healthq) + ")"
+ })
}
});
28 js/primitive.js
View
@@ -0,0 +1,28 @@
+TextGalactic.Primitive = Class.extend({
+ moveSpeed: 3,
+
+ animationSpeeed: 0,
+
+ init: function (canvas, options) {
+ this.x = (options.x != 'undefined') ? options.x : 0;
+ this.y = (options.y != 'undefined') ? options.y : 0;
+
+ if (options.animationSpeed != 'undefined') {
+ this.animationSpeed = options.animationSpeed;
+ }
+
+ this.shape = canvas.text(
+ (options.x != 'undefined') ? options.x : 0,
+ (options.y != 'undefined') ? options.y : 0,
+ (options.text != 'undefined') ? options.text : "A"
+ );
+
+ },
+
+ 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");
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.