Skip to content

Commit

Permalink
70% OF PROGRESS OF FANCY ENEMY MOVEMENT ANIMATION
Browse files Browse the repository at this point in the history
  • Loading branch information
pierosifuentes committed Oct 22, 2015
1 parent 6460448 commit b75ba70
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 62 deletions.
73 changes: 25 additions & 48 deletions scripts/objects/enemy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var DEFAULT_ENEMY_SPEED = 120;
var DEFAULT_ENEMY_SPEED = 128;

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

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

var moving = true;
this.game.physics.arcade.collide(this, this.rocks);
moveChooser = Math.floor((Math.random() * 4) + 1);
moveChooser = Math.floor((Math.random() * 5) + 1);
console.log(moveChooser);
this.body.velocity.x = 0;
this.body.velocity.y = 0;
var walkCounter = 5000;
var direction;

//TODO:USE rocksColliding to create a fancy movement "IA"

if (moveChooser == 1)
{
setTimeout(function(){
if(this.alive)
if(this.alive)
{
console.log("left");
this.sprite.body.velocity.x = -this.speed;
this.body.velocity.x = -this.speed;
this.facing = "left";
direction = 1;
moving = true;
}
}, walkCounter)
}
else if (moveChooser == 2)
{
setTimeout(function(){
if (this.alive)
if (this.alive)
{
this.sprite.body.velocity.x = this.speed;
this.body.velocity.x = this.speed;
this.facing = "right";
direction = 2;
moving = true;
}
}, walkCounter)
}
else if (moveChooser == 3)
{
setTimeout(function(){
if (this.alive)
if (this.alive)
{
this.sprite.body.velocity.y = -this.speed;
this.body.velocity.y = -this.speed;
this.facing = "up";
direction = 3;
moving = true;
}
}, walkCounter)
}
else if (moveChooser == 4)
{
setTimeout(function(){
if (this.alive)
if (this.alive)
{
console.log("down");
this.sprite.body.velocity.y = this.speed;
this.body.velocity.y = this.speed;
this.facing = "down";
direction = 4;
moving = true;
}
}, walkCounter)
}

//walkCounter += 560;

if (direction == 1)
else if (moveChooser == 5)
{
this.sprite.body.velocity.x = -this.speed;
this.facing = "left";
if(this.alive)
{
moving = false;
this.freeze();
}
}
else if (direction == 2)
{
this.sprite.body.velocity.x = this.speed;
this.facing = "right";
}
else if (direction == 3)
{
this.sprite.body.velocity.y = -this.speed;
this.facing = "up";
}
else if (direction == 4)
{
this.sprite.body.velocity.y = this.speed;
this.facing = "down";
}
if(moving) {
this.animations.play(this.facing);
}
Expand Down
36 changes: 22 additions & 14 deletions scripts/states/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Game.prototype = {
this.addEnemy();
this.addEnemy();
this.addEnemy();
this.game.time.events.loop(1000, this.handleEnemyMovement, this);
},
update: function() {
this.physics.arcade.collide(this.player, this.rocks);
Expand All @@ -101,9 +102,12 @@ Game.prototype = {
if (this.player.bombButtonJustPressed && this.player.canDropBombs(this.bombsPool) && this.time.now > this.nextBomb) {
this.createBomb(this.player.x + this.player.width / 2, this.player.y + this.player.height / 2, this.keyBomb, 0);
}
},
handleEnemyMovement: function(){
for (var i = 0, len = this.enemyPool.children.length; i < len; i++) {
this.enemyPool.children[i].handleArificialMovement(this.rocks);
}
var rocksColliding = this.getRocksColliding(this.enemyPool.children[i].x, this.enemyPool.children[i].y);
this.enemyPool.children[i].handleArificialMovement(this.rocks,rocksColliding);
}
},
addEnemy: function() {
var currentEnemy = this.enemyPool.getFirstExists(false),
Expand Down Expand Up @@ -157,20 +161,24 @@ Game.prototype = {
right: true
};

if (this.rocks.layer.data[row - 1]) {
rocksColliding.up = (this.rocks.layer.data[row - 1][column].index === this.level.rockId);
}

if (this.rocks.layer.data[row + 1]) {
rocksColliding.down = (this.rocks.layer.data[row + 1][column].index === this.level.rockId);
if(row - 1>=0 && column>=0){
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){
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);
}
}

if (this.rocks.layer.data[row][column - 1]) {
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);
}

if (this.rocks.layer.data[row][column + 1]) {
rocksColliding.right = (this.rocks.layer.data[row][column + 1].index === this.level.rockId);
}
}
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);
}
}

return rocksColliding;
Expand Down

0 comments on commit b75ba70

Please sign in to comment.