2

I am building a tower defence game and I am facing an issue in Phaser with tiled maps.

You see different layers in tiled always have coordinates from (0,0) to (600,600) -> or whatever your tiled width and height is. I have a backgroundLayer and a path. The creeps properly collide with the path.

Now I am trying to change the cursor to a building sprite (for UI when I click on some button and select a tower I want it to turn red when it is on the road or above another tower/creep) and I am using an overlap function for path and buildings.

This overlap function in Phaser checks the rectangle of building sprite and path, but path rectangle is the whole map. The collision works but I only need an overlap. Any ideas how to achieve this? Here is my code.

var RedPlanetGame = RedPlanetGame || {};

var pressed = {
    is: false
};
var buildingOverlaps = {
    is: false
};
//title screen
RedPlanetGame.Game = function () {
};

RedPlanetGame.Game.prototype = {
    create: function create() {
    //A door for multyplayer
    this.players = [];
    this.player = new Player(1, 'Daniel', 300);
    this.players.push(this.player);
    this.playerInfo = {};

    //Tile map
    this.map = this.game.add.tilemap('sample2');
    this.map.addTilesetImage('32x32_map_tile v3.1 [MARGINLESS]', 'gameTiles');

    //background and layers
    this.backgroundlayer = this.map.createLayer('backgroundLayer');
    this.path = this.map.createLayer('path');
    this.map.setCollisionBetween(1, 2000, true, 'backgroundLayer');
    this.map.setCollisionBetween(1, 2000, true, 'path');
    //objects from tile map
    this.spawnCreepsAt = this.map.objects['objectsLayer'][0];
    this.destinationForCreeps = this.map.objects['objectsLayer'][1];

    //resize world
    this.backgroundlayer.resizeWorld();
    //this.game.world.setBounds(0, 0, 100, 100);

    //groups
    this.game.enemies = new UnitsPoolFactory(this.game);
    this.game.buildings = this.game.add.group();//TODO: make buildings for each player
    this.game.bullets = new BulletsPoolFactory(this.game);

    //creep spawning
    var _this = this;
    const creepYOffset = 15;
    setInterval(function () {
        _this.game.enemies.factory(_this.spawnCreepsAt.x, _this.spawnCreepsAt.y + creepYOffset, UNIT_TYPES.CREEP1);
    }, 1000);

    //text and player info
    var textX = 150;
    var textY = 0;
    this.playerInfo.gold = this.game.add.text(textX, textY, 'Player gold: ' + this.player.gold,
        {font: "24px Arial", fill: '#FFD700'}
    );

    //Here is test straight forward code for building towers
    this.game.build = this.game.add.group();
    this.buildingSprite = this.game.add.sprite(0, 0, 'tower1-1');
    this.game.physics.enable(this.buildingSprite, Phaser.Physics.ARCADE);
    this.buildingSprite.anchor.setTo(0.5);
    this.buildingSprite.scale.setTo(0.5);
    this.game.build.add(this.buildingSprite);
    console.log(this.path.getBounds());
    console.log(this.backgroundlayer.getBounds());
},
update: function update() {
    var _this = this;
    //Camera follow cursor
    if (this.game.input.mousePointer.x > gameHeight - gameHeight / 10) {
        this.game.camera.x += 10;
    } else if (this.game.input.mousePointer.x <= 100) {
        this.game.camera.x -= 10;
    }
    if (this.game.input.mousePointer.y > gameWidth - gameWidth / 10) {
        this.game.camera.y += 10;
    } else if (this.game.input.mousePointer.y <= 100) {
        this.game.camera.y -= 10;
    }

    //check for collision between enemy and non-path layer
    this.game.physics.arcade.collide(this.game.enemies, this.backgroundlayer);
    //checks for collision between bullets and enemies
    this.game.physics.arcade.overlap(this.game.bullets, this.game.enemies, function (bullet, enemy) {
        enemy.takeHit(bullet, _this.player);
        bullet.kill();
    }, null, this);

    //updates enemies
    this.game.enemies.forEach(function (enemy) {
        enemy.onUpdate(_this.destinationForCreeps);
    });

    //updates buildings
    this.game.buildings.forEach(function (building) {
        building.onUpdate(_this.game.bullets);
    });

    //on mouse down event
    if (this.game.input.activePointer.leftButton.isDown && !pressed.is) {//yo Yoda
        //builds new building of type TOWER1
        buffer(pressed, 1000);
        if (Building.prototype.canBuild(this.player.gold, Tower1.prototype.moneyCost)) {
            var poss = this.game.input.mousePointer;
            BuildingsFactory(this.game, poss.x, poss.y, this.player, BUILDING_TYPES.TOWER1);
            this.player.gold -= Tower1.prototype.moneyCost
        } else {
            alert('Not enought gold');
        }

    }

    //Here is test straight forward code for building towers
    if (Phaser.Rectangle.contains(this.buildingSprite.body, this.game.input.x, this.game.input.y)) {
        this.buildingSprite.body.velocity.setTo(0, 0);
    }

    else {
        this.game.physics.arcade.moveToPointer(this.buildingSprite, 700);
    }
    this.game.physics.arcade.collideGroupVsTilemapLayer(this.game.build, this.path, function (sprite) {
        if (!buildingOverlaps.is) {
            buffer(buildingOverlaps, 500);
            console.log('overlap')
        }
    }, null, this, true);

},
render: function render() {
    this.playerInfo.gold.text = 'Player gold: ' + this.player.gold;

}
};
James Skemp
  • 8,018
  • 9
  • 64
  • 107
Daniel petrov
  • 875
  • 9
  • 14

0 Answers0