Jump to content

Phaser Pathfinder with Easystar


rootkiv
 Share

Recommended Posts

Hi!

I need some help with pathfinder written in phaser and easystar.

 I'm not too experienced with it, but I trying mix it togather :> Look at code (sorry for chaos... its my 1000`th attempt .__. )

There is probably one little mistake and it doesnt work :< daaamn

map 40x40 tiles: 1,2,3,4 --> acceptable 1 and 4.

Thanks!

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

var easystar = new EasyStar.js();

var currentCowboyXtile 
var currentCowboyYtile 

function preload() {

    game.load.tilemap('desert', 'img/desert.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('tiles',   'img/objects.png');
	 game.load.image('bullet', 'img/bullet.png');
	  game.load.image('arrow', 'img/arrow.png');
	  game.load.image('cactus1', 'img/cactus1.png');
	  game.load.image('cowboy', 'img/arrow.png');

	  currentCowboyXtile = 400;
	  currentCowboyYtile = 400;
	  
}

var map;
var layer;

var marker;
var currentTile;
var cursors;
var sprite;

var weapon;
var fireButton;
var enemyDirection;

var tileSize = 32;
var mapSize = 40;

var currentPlayerXtile;
var currentPlayerYtile;



	easystar.setGrid(map);
    easystar.setAcceptableTiles([1,4]);
	easystar.enableDiagonals();
	easystar.enableCornerCutting();
	easystar.setIterationsPerCalculation(1000); 
	easystar.enableDiagonals();
	


function create() {

	  obstacleGroup = game.add.group();
	

game.add.tileSprite(0, 0, 1920, 1920, 'desert');

    game.world.setBounds(0, 0, 1920, 1920);


	    game.physics.startSystem(Phaser.Physics.ARCADE);


    map = game.add.tilemap('desert');

    map.addTilesetImage('Desert', 'tiles');
	
	
	
    currentTile = map.getTile(24, 28);

    layer = map.createLayer('Ground');

    layer.resizeWorld();

    marker = game.add.graphics();
    marker.lineStyle(2, 0x000000, 1);
    marker.drawRect(0, 0, 32, 32);

    cursors = game.input.keyboard.createCursorKeys();
	
	///////////////////////////////////////////////
	
	
	
	
	//  Creates 30 bullets, using the 'bullet' graphic
    weapon = game.add.weapon(30, 'bullet');

    //  The bullet will be automatically killed when it leaves the world bounds
    weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;

    //  Because our bullet is drawn facing up, we need to offset its rotation:
    weapon.bulletAngleOffset = 90;

    //  The speed at which the bullet is fired
    weapon.bulletSpeed = 700;

    //  Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms
    weapon.fireRate = 90;

    //  Add a variance to the bullet angle by +- this value
    weapon.bulletAngleVariance = 5;
	
	sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'arrow');
    sprite.anchor.setTo(0.5, 0.5);
	
	cowboy = game.add.sprite(currentCowboyXtile, currentCowboyYtile, 'arrow' ,obstacleGroup);
    cowboy.anchor.setTo(0.5, 0.5);
	
  
//map.putTile(30, 30, 10, layer);
    map.setCollisionByExclusion([1,4]);

	
   game.physics.enable(sprite, Phaser.Physics.ARCADE);
   game.physics.enable(cowboy, Phaser.Physics.ARCADE);
sprite.body.collideWorldBounds = true;
	
	//game.physics.startSystem(sprite, Phaser.Physics.P2JS);
	    weapon.trackSprite(sprite, 18, 6, true);

game.camera.follow(sprite, Phaser.Camera.FOLLOW_LOCKON, 0.1, 0.1);

/////////////////////////////////////////////////////////
   
	fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);
	
	var cactus1;

	   for (var yt = 0; yt < map.length; yt++) {
	        	
   var tile = map[yt];
	        	
	for (var xt = 0; xt < map[yt].length; xt++) {
   
             if (tile[xt] == 1) {
	        cactus1 = game.add.isoSprite(xt * tileSize, 
                                             yt * tileSize, 0, 
                                                 'cactus1', 0, 
                                                  obstacleGroup);
	     }
	     else if (tile[xt] == 2) {
	        cactus1 = game.add.isoSprite(xt * tileSize, 
                                             yt * tileSize, 0,
                                                 'cactus2', 0, 
                                                  obstacleGroup);
	     }
	  
	     if (tile[xt] == 1 || tile[xt] == 2) {
		        cactus1.anchor.set(0.5);
	
		     // Let the physics engine do its job on this tile type
		        game.physics.enable(cactus1, Phaser.Physics.ARCADE);
	
		     // This will prevent our physic bodies from going out of the screen
		        cactus1.body.collideWorldBounds = true;
		                
		     // Make the cactus body immovable
		        cactus1.body.immovable = true;
		             
	         }	

	     }
	        	
	 }

setInterval(function(){ 
            	
easystar.findPath(currentCowboyXtile, currentCowboyYtile, currentPlayerXtile, currentPlayerYtile, function( path ) {

if (path === null) {
    console.log("The path to the destination point was not found.");
} 
	        	    
if (path) {
    currentNextPointX = path[1].x;
    currentNextPointY = path[1].y;
}
	        	    
if (currentNextPointX < currentCowboyXtile && currentNextPointY < currentCowboyYtile) {
       // left up
	        	    	
       console.log("GO LEFT UP");
	        	    	
       enemyDirection = "NW";
    }
    else if (currentNextPointX == currentCowboyXtile && currentNextPointY < currentCowboyYtile)
    {
       // up
	        	    	
       console.log("GO UP");
	        	    	
       enemyDirection = "N";
	        	    	
    }
    else if (currentNextPointX > currentCowboyXtile && currentNextPointY < currentCowboyYtile)
    {
       // right up
	        	    	
       console.log("GO RIGHT UP");
	        	    	
       enemyDirection = "NE";
	        	    	
   }
   else if (currentNextPointX < currentCowboyXtile && currentNextPointY == currentCowboyYtile)
   {
      // left
	        	    	
       console.log("GO LEFT");
	        	    	
       enemyDirection = "W";
	        	    	
   }
   else if (currentNextPointX > currentCowboyXtile && currentNextPointY == currentCowboyYtile)
   {
      // right
	        	    	
       console.log("GO RIGHT");
	        	    	
       enemyDirection = "E";
	        	    
   }
   else if (currentNextPointX > currentCowboyXtile && currentNextPointY > currentCowboyYtile)
   {
      // right down
	        	    	
	console.log("GO RIGHT DOWN");
	        	    	
	enemyDirection = "SE";
	        	    	
   }
   else if (currentNextPointX == currentCowboyXtile && currentNextPointY > currentCowboyYtile)
   {
      // down
	        	    	
	 console.log("GO DOWN");
	        	    	
	 enemyDirection = "S";
	        	    	
   }
   else if (currentNextPointX < currentCowboyXtile && currentNextPointY > currentCowboyYtile)
   {
     // left down
	        	    	
	console.log("GO LEFT DOWN");
	        	    	
	enemyDirection = "SW";
	        	    	
   }
   else
   {
	        	    	
	enemyDirection = "STOP";
	        	    	
   }
	        	    
	        	    
   });

   easystar.calculate();
   
   	
   
	        	
}, timeStep); 
	
}


function update() {

game.physics.arcade.collide(sprite, layer);

    marker.x = layer.getTileX(game.input.activePointer.worldX) * 32;
    marker.y = layer.getTileY(game.input.activePointer.worldY) * 32;

    if (game.input.mousePointer.isDown)
    {
       /* if (game.input.keyboard.isDown(Phaser.Keyboard.SHIFT))
        {
            currentTile = map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y));
        }
        else*/
        {
            if (map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)) != currentTile)
            {
                map.putTile(currentTile, layer.getTileX(marker.x), layer.getTileY(marker.y))
            }
        }
    }

    if (game.input.keyboard.isDown(Phaser.Keyboard.A))
    {
        game.camera.x -= 4;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.D))
    {
        game.camera.x += 4;
    }

    if (game.input.keyboard.isDown(Phaser.Keyboard.W))
    {
        game.camera.y -= 4;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.S))
    {
        game.camera.y += 4;
    }
	
	
	if (fireButton.isDown)
    {
        weapon.fire();
    }
	
	//////////////////////////////////////////////////
	 sprite.body.velocity.x = 0;
    sprite.body.velocity.y = 0;
    sprite.body.angularVelocity = 0;

    if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
    {
        sprite.body.angularVelocity = -250;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
    {
        sprite.body.angularVelocity = 250;
    }

    if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
    {
        game.physics.arcade.velocityFromAngle(sprite.angle, 125, sprite.body.velocity);
    }
	else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
		{
        game.physics.arcade.velocityFromAngle(sprite.angle, -125, sprite.body.velocity);
		}
///////////////////////////////////////

  var enemySpeed = 90;
	       
	        if (enemyDirection == "N") {
	        	cowboy.body.velocity.x = -enemySpeed;
	        	cowboy.body.velocity.y = -enemySpeed;
	        }
	        else if (enemyDirection == "S")
	        {
	        	cowboy.body.velocity.x = enemySpeed;
	        	cowboy.body.velocity.y = enemySpeed;
	        }
	        else if (enemyDirection == "E") {
	        	cowboy.body.velocity.x = enemySpeed;
	        	cowboy.body.velocity.y = -enemySpeed;
	        }
	        else if (enemyDirection == "W")
	        {
	        	cowboy.body.velocity.x = -enemySpeed;
	        	cowboy.body.velocity.y = enemySpeed;
	        }
	        else if (enemyDirection == "SE")
	        {
	        	cowboy.body.velocity.x = enemySpeed;
	        	cowboy.body.velocity.y = 0;
	        }
	        else if (enemyDirection == "NW")
	        {
	        	cowboy.body.velocity.x = -enemySpeed;
	        	cowboy.body.velocity.y = 0;   	
	        }
	        else if (enemyDirection == "SW")
	        {
	        	cowboy.body.velocity.x = 0;
	        	cowboy.body.velocity.y = enemySpeed;    	
	        }
	       
	        else if (enemyDirection == "NE")
	        {
	        	cowboy.body.velocity.x = 0;
	        	cowboy.body.velocity.y = -enemySpeed;
	        }
	        else if (enemyDirection == "STOP")
	        {
	        	cowboy.body.velocity.x = 0;
	        	cowboy.body.velocity.y = 0;
	        }
	        else // JUST IN CASE IF enemyDirection wouldnt exist we stop the cowboy movement
	        {
	        	cowboy.body.velocity.x = 0;
	        	cowboy.body.velocity.y = 0;
	        }
			
			currentPlayerXtile = Math.floor(sprite.body.position.x / tileSize);
		    currentPlayerYtile = Math.floor(sprite.body.position.y / tileSize);	
			
			if (currentPlayerXtile < 0) currentPlayerXtile = 0;
	       if (currentPlayerYtile < 0) currentPlayerYtile = 0;
	       
	       if (currentPlayerXtile > 28) currentPlayerXtile = 28;
	       if (currentPlayerYtile > 28) currentPlayerYtile = 28;

		       currentCowboyXtile = Math.floor(cowboy.body.position.x / tileSize);
		       currentCowboyYtile = Math.floor(cowboy.body.position.y / tileSize);
	       
		   // PREVENT FROM GOING OUT FROM THE LOGICAL ARRAY BECAUSE OF THE PHASER PHYSICS ENGINE    
		       
	       if (currentCowboyXtile < 0) currentCowboyXtile = 0;
	       if (currentCowboyYtile < 0) currentCowboyYtile = 0;
	       
	       if (currentCowboyXtile > 28) currentCowboyXtile = 28;
	       if (currentCowboyYtile > 28) currentCowboyYtile = 28;

}

function render() {

	
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...