Skip to content
Browse files

added collidable Wall, change the way Entity created.

  • Loading branch information...
1 parent 1a39e81 commit 87c6a25b41adfc0846fcd950c41ba3f51b075824 @alkavan committed Mar 31, 2012
Showing with 114 additions and 47 deletions.
  1. +1 −1 index.html
  2. +113 −46 js/script.js
View
2 index.html
@@ -6,7 +6,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <title>MFG 0.2.1</title>
+ <title>MFG 0.2.2</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
View
159 js/script.js
@@ -282,7 +282,7 @@ var mfg = function () {
};
}
- if(this.constructor !== Event) return new Event(settings);
+// if(this.constructor !== Event) return new Event(settings);
return mfg.ent.factory(this, settings);
};
@@ -328,8 +328,9 @@ var mfg = function () {
*/
- var layer = mfg.scene.layers.ground_player;
+ var layer = mfg.scene.layers.ground_player;
var sprite = layer.Sprite(mfg._image_path+'shot.png');
+ var grid = mfg.grid.grid;
// player off set
// @todo: relate to player
@@ -389,18 +390,68 @@ var mfg = function () {
dir_x = dir_x*speed;
dir_y = dir_y*speed;
- log('direction: ['+dir_x.toFixed(2)+','+dir_y.toFixed(2)+']');
+// log('direction: ['+dir_x.toFixed(2)+','+dir_y.toFixed(2)+']');
/**
* Shot loop
* @type {Number}
* @private
*/
sprite._interval = function () {
- // End of shot
- if((sprite.x >= mfg._scene_width-3 || sprite.y >= mfg._scene_height-mfg._scene_padding)
- || (sprite.x <= 0+mfg._scene_padding || sprite.y <= 0+mfg._scene_padding))
+
+ var collides = false;
+
+ // Check collide with player
+ for(var pi in mfg.players)
+ {
+ if(sprite.collidesWith(mfg.players[pi].sprite) && mfg.player_index != pi) collides = true;
+ }
+
+ // Use grid because can't locate nearest yet
+ for(var i in grid)
{
+ for(var j in grid[i])
+ {
+ if(grid[i][j].occupied)
+ {
+ //log(grid[i][j].occupied);
+
+ if(grid[i][j].occupied.collidable)
+ {
+ var occupied = grid[i][j].occupied;
+ var range = Math.sqrt(Math.pow(occupied.sprite.x+7-sprite.x, 2)
+ +Math.pow(occupied.sprite.y+7-sprite.y, 2));
+ range = parseFloat(range.toFixed(2));
+ if(range < 9)
+ {
+ occupied.hp.left--;
+ log('hit '+occupied.toString()+' hp: '+occupied.hp.left+'/'+occupied.hp.max);
+
+ if(occupied.hp.left == 0)
+ {
+
+// occupied.sprite.position(-100,-100);
+// occupied.sprite.setOpacity(0);
+// occupied.sprite.canvasUpdate();
+ occupied.sprite.remove();
+ grid[i][j].occupied = null;
+ mfg.scene.layers.ground.clear();
+ mfg.grid.updateOccupied();
+
+ log('BYE');
+ }
+
+ collides = true;
+ }
+ }
+ }
+ }
+ }
+
+ // End of shot
+ if(collides
+ || (sprite.x >= mfg._scene_width-3 || sprite.y >= mfg._scene_height-mfg._scene_padding)
+ || (sprite.x <= 0+(mfg._scene_padding-3) || sprite.y <= 0+(mfg._scene_padding-3))) {
var esp = new sjs.List;
var esp_layer = mfg.scene.layers.shooting
@@ -456,23 +507,37 @@ var mfg = function () {
*/
mfg.ent.Entity = function Entity(settings){
- if ( ! settings)
+ var default_settings = {
+ btime: 9999,
+ work: 0,
+ sysname: 'Entity',
+ name: 'Entity',
+ level: 1,
+ yeild: {},
+ attrs: {},
+ cost: {},
+ collidable: false,
+ hp: {max: 1000, left: 1000}
+ };
+
+// if ( ! settings)
+// {
+ for(var attr in default_settings)
{
- settings = {
- btime: 9999,
- work: 0,
- sysname: 'Entity',
- name: 'Entity',
- level: 1,
- yeild: {},
- attrs: {},
- cost: {}
- };
+ this[attr] = default_settings[attr];
}
+// }
+ if (settings)
+// {
+ for(var attr in settings)
+ {
+ this[attr] = settings[attr];
+ }
+// }
- if(this.constructor !== Entity) return new Entity(settings);
+// if(this.constructor !== Entity) return new Entity(settings);
- return mfg.ent.factory(this, settings);
+ return this;
};
/**
@@ -485,30 +550,15 @@ var mfg = function () {
*/
mfg.ent.factory = function (that, settings){
- if(that && settings)
- {
- for(var attr in settings)
- {
- that[attr] = settings[attr];
- }
+ var ent = new mfg.ent.Entity(settings);
- return that;
- }
- else if (that && !settings)
+ for(var attr in ent)
{
- var ent = new mfg.ent.Entity(settings);
-
- for(var attr in settings)
- {
- that[attr] = ent[attr];
- }
- return that;
- }
- else
- {
- return false;
+ that[attr] = ent[attr];
}
+
+ return that;
};
/**
@@ -555,7 +605,7 @@ var mfg = function () {
loc.occupied = this;
- if(this.constructor !== House) return new House(settings);
+// if(this.constructor !== House) return new House(settings);
return mfg.ent.factory(this, settings);
};
@@ -609,7 +659,7 @@ var mfg = function () {
loc.occupied = this;
- if(this.constructor !== Mine) return new Mine(settings);
+// if(this.constructor !== Mine) return new Mine(settings);
return mfg.ent.factory(loc.occupied, settings);
};
@@ -658,7 +708,7 @@ var mfg = function () {
loc.occupied = this;
- if(this.constructor !== Lab) return new Lab(settings);
+// if(this.constructor !== Lab) return new Lab(settings);
return mfg.ent.factory(this, settings);
};
@@ -701,13 +751,15 @@ var mfg = function () {
work: 0,
sysname: 'Wall',
name: 'Wall',
- cost: {ore: 25}
+ cost: {ore: 25},
+ collidable: true,
+ hp: {max: 30, left: 30}
};
}
loc.occupied = this;
- if(this.constructor !== Wall) return new Wall(settings);
+// if(this.constructor !== Wall) return new Wall(settings);
return mfg.ent.factory(this, settings);
};
@@ -721,7 +773,7 @@ var mfg = function () {
*/
mfg.ent.Player = function Player(settings){
- if(this.constructor !== Player) return new Player(settings);
+// if(this.constructor !== Player) return new Player(settings);
var ground = mfg.scene.layers.ground_player;
var sprite = ground.Sprite(mfg._image_path+'s_processor.png');
@@ -770,6 +822,7 @@ var mfg = function () {
};
}
+ log(mfg.ent.factory(this, settings));
return mfg.ent.factory(this, settings);
};
@@ -1119,7 +1172,7 @@ var mfg = function () {
player.shootAt(input.mouse.position.x, input.mouse.position.y, 6);
}
- mousecross.rotate(3.14 / 16);
+// mousecross.rotate(3.14 / 16);
mousecross.position(input.mouse.position.x-3, input.mouse.position.y-3);
mousecross.canvasUpdate();
@@ -1464,6 +1517,20 @@ var mfg = function () {
}
};
+ this.updateOccupied = function () {
+ for(var i = 0; i < this.grid.length; i++)
+ {
+ for(var j = 0; j < this.grid[i].length; j++)
+ {
+ // create the Sprite object;
+ if (this.grid[i][j].occupied)
+ {
+ var occupied = this.grid[i][j].occupied;
+ occupied.sprite.canvasUpdate();
+ }
+ }
+ }
+ };
/**
* Highlight position on grid by pixels

0 comments on commit 87c6a25

Please sign in to comment.
Something went wrong with that request. Please try again.