Skip to content

Commit

Permalink
not funny movement, please use the star graph lib (added) for improve…
Browse files Browse the repository at this point in the history
… the enemy movement!
  • Loading branch information
pierosifuentes committed Oct 23, 2015
1 parent b75ba70 commit 4186ecd
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 21 deletions.
51 changes: 43 additions & 8 deletions scripts/objects/enemy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var DEFAULT_ENEMY_SPEED = 128;
var DEFAULT_ENEMY_SPEED = 160 ;

function Enemy(game, x, y) {
Player.call(this, game, x, y, 'enemy', 0);
Expand All @@ -18,11 +18,14 @@ function Enemy(game, x, y) {
Enemy.prototype = Object.create(Player.prototype);
Enemy.prototype.constructor = Enemy;

Enemy.prototype.handleArificialMovement = function(rocks,rocksColliding) {
Enemy.prototype.handleArificialMovement = function(rocks,rocksColliding,moveChooserVal) {

var moving = true;
this.game.physics.arcade.collide(this, this.rocks);
moveChooser = Math.floor((Math.random() * 5) + 1);
if(!moveChooserVal)
moveChooser = Math.floor((Math.random() * 5) + 1);
else
moveChooser=moveChooserVal;
console.log(moveChooser);
this.body.velocity.x = 0;
this.body.velocity.y = 0;
Expand All @@ -31,39 +34,71 @@ Enemy.prototype.handleArificialMovement = function(rocks,rocksColliding) {

if (moveChooser == 1)
{
if(this.alive)
if(rocksColliding.left!=false)
{ if(this.alive )
{
this.body.velocity.x = -this.speed;
this.facing = "left";
moving = true;
}
}
else
{
moveChooser= Math.floor(Math.random() * 5) + 2 ;
this.handleArificialMovement(rocks,rocksColliding,moveChooser);
}
}
else if (moveChooser == 2)
{
if (this.alive)
if (rocksColliding.right!=false)
{
if(this.alive)
{
this.body.velocity.x = this.speed;
this.facing = "right";
moving = true;
}
}
else
{
moveChooser= Math.floor(Math.random() * 6) + 1 ;
this.handleArificialMovement(rocks,rocksColliding,moveChooser);
}
}
else if (moveChooser == 3)
{
if (this.alive)
if(rocksColliding.up != false)
{
if (this.alive)
{
this.body.velocity.y = -this.speed;
this.facing = "up";
moving = true;
}
}
else
{
moveChooser= Math.floor(Math.random() * 6) + 1 ;
this.handleArificialMovement(rocks,rocksColliding,moveChooser);
}

}
else if (moveChooser == 4)
else if (moveChooser == 4 )
{
if (this.alive)
if(rocksColliding.down != false)
{
if (this.alive)
{
this.body.velocity.y = this.speed;
this.facing = "down";
moving = true;
}
}
else
{
moveChooser= Math.floor(Math.random() * 3) + 1 ;
this.handleArificialMovement(rocks,rocksColliding,moveChooser);
}
}
else if (moveChooser == 5)
{
Expand Down
62 changes: 49 additions & 13 deletions scripts/states/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ Game.prototype = {
this.explosionPool.setAll('body.allowGravity', true);
this.explosionRange = 1;

//Player
this.player = new Player(this.game, 32 * 6, 32 * 4, 'red', 0);
this.bombs = this.game.add.group();

this.enemyPool = this.add.group();

Expand All @@ -77,21 +74,27 @@ Game.prototype = {
align: 'center',
};

//Player
var pos=this.availableSpaces[this.availableSpaces.length - 1];
this.player = new Player(this.game, 32 * pos.x, 32 * 0, 'red', 0);
this.bombs = this.game.add.group();

this.scoreText = this.add.text(this.game.world.width - 30, 10, this.player.score.toString(), font);

//Ajustes
this.ground.resizeWorld();
this.rocks.resizeWorld();

this.addEnemy();
this.addEnemy();
this.addEnemy();
this.game.time.events.loop(1000, this.handleEnemyMovement, this);
this.addEnemy(1);
this.addEnemy(2);
this.addEnemy(3);
this.game.time.events.loop(200, this.handleEnemyMovement, this);
},
update: function() {
this.physics.arcade.collide(this.player, this.rocks);
this.physics.arcade.collide(this.enemyPool, this.rocks);
this.physics.arcade.collide(this.player, this.enemyPool);
this.physics.arcade.collide(this.enemyPool, this.bombsPool);
this.physics.arcade.collide(this.player, this.bombsPool);
this.physics.arcade.overlap(this.player, this.explosionPool, this.destroyPlayer, null, this);
this.physics.arcade.overlap(this.enemyPool, this.explosionPool, this.destroyEnemy, null, this);
Expand All @@ -106,19 +109,35 @@ Game.prototype = {
handleEnemyMovement: function(){
for (var i = 0, len = this.enemyPool.children.length; i < len; i++) {
var rocksColliding = this.getRocksColliding(this.enemyPool.children[i].x, this.enemyPool.children[i].y);
this.enemyPool.children[i].handleArificialMovement(this.rocks,rocksColliding);
this.enemyPool.children[i].handleArificialMovement(this.rocks,rocksColliding,null);
}
},
addEnemy: function() {
addEnemy: function(enemyID) {

var currentEnemy = this.enemyPool.getFirstExists(false),
positionIndex = this.game.rnd.between(0, this.availableSpaces.length - 1),
position = this.availableSpaces[positionIndex];
var finalPos;
if(enemyID ==1)
{
finalPos = this.availableSpaces[this.availableSpaces.length - 1];
}
else if(enemyID ==2)
{
finalPos = this.availableSpaces[this.availableSpaces.length - 1];
finalPos.x=0;
}
else if(enemyID ==3)
{
finalPos = this.availableSpaces[this.availableSpaces.length - 1];
finalPos.y=0;
}

if (!currentEnemy) {
currentEnemy = new Enemy(this.game, position.x * 32, position.y * 32 - 4);
//if (!currentEnemy) {
currentEnemy = new Enemy(this.game, finalPos.x * 32, finalPos.y * 32);

this.enemyPool.add(currentEnemy);
}
//}
// else {
// currentEnemy.reset(position.x * 32, position.y * 32 - 4);
// }
Expand Down Expand Up @@ -165,21 +184,38 @@ Game.prototype = {
if (this.rocks.layer.data[row - 1] !== undefined && this.rocks.layer.data[row - 1]) {
rocksColliding.up = (this.rocks.layer.data[row - 1][column].index === this.level.rockId);
}
} if(column>=0){
}
else
{
rocksColliding.up = true;
}
if(column>=0){
if (this.rocks.layer.data[row + 1] !== undefined && this.rocks.layer.data[row + 1]) {
rocksColliding.down = (this.rocks.layer.data[row + 1][column].index === this.level.rockId);
}
}
else
{
rocksColliding.down = true;
}
if(column - 1>=0 && row>=0){
if (this.rocks.layer.data[row][column - 1] !== undefined && this.rocks.layer.data[row][column - 1]) {
rocksColliding.left = (this.rocks.layer.data[row][column - 1].index === this.level.rockId);
}
}
else
{
rocksColliding.left = true;
}
if(row>=0){
if (this.rocks.layer.data[row][column + 1] !== undefined && this.rocks.layer.data[row][column + 1]) {
rocksColliding.right = (this.rocks.layer.data[row][column + 1].index === this.level.rockId);
}
}
else
{
rocksColliding.right = true;
}

return rocksColliding;
},
Expand Down

0 comments on commit 4186ecd

Please sign in to comment.