I’m creating a topdown rpg and I need to spread random vegetation all over the map without leaving trees above the town.
The idea was to create an invisible layer and spread the green tiles over the parts of the map that shouldn’t have trees, but when I tried to use tileIndexCallback to kill the tree sprites nothing happened.
help.
A picture of the map in Tiled
Green tiles should not have trees or grass because it is where the town and the forest paths are
Show code?
//
// Create
//
createMap: function () {
map = game.add.tilemap('town1');
//
map.addTilesetImage('tileset','tileset');
map.addTilesetImage('logic','map_logic');
map.ground = map.createLayer('Ground');
map.layer1 = map.createLayer('Layer 1');
map.layer2 = map.createLayer('Layer 2');
map.layer3 = map.createLayer('Layer 3');
map.logicLayer = map.createLayer('Logic');
map.solidLayer = map.createLayer('Solid');
map.cleanLayer = map.createLayer('Clean');
map.logicLayer.alpha = 0;
map.solidLayer.alpha = 0;
map.cleanLayer.alpha = 0;
map.ground.resizeWorld();
map.entities = game.add.group();
map.setCollision([8001],true,map.cleanLayer);
map.setCollision([8004,8005],true,map.solidLayer);
map.setCollision([8001,8003,8004],true,map.logicLayer);
// trying to kill by setting callback does not do anything
map.setTileIndexCallback(8001,this.killMistakedEntity,this);
map.needsClear = true;
map.properties = {
name: "",
type: "",
music: 0,
ambient: 0,
playerX: 0,
playerY: 0
};
// custom properties
var mapProperties = map.plus.properties;
for (var i = 0; i < mapProperties.length; i++) {
var item = mapProperties[i];
switch(item.name){
case "name":
map.properties.name = item.value;
break;
case "type":
map.properties.type = item.value;
break;
case "music":
map.properties.music = JSON.parse(item.value);
break;
case "ambient":
map.properties.ambient = JSON.parse(item.value);
break;
case "playerX":
map.properties.playerX = JSON.parse(item.value);
break;
case "playerY":
map.properties.playerY = JSON.parse(item.value);
break;
}
}
// creates resources
game.map = map;
for (var i=0; i<game.map.width*5; i++) {
this.createResources();
}
},
// creates forest (lumberTree, appleTree, stone, mushroom, flower)
createResources: function (x,y) {
var locs = [];
var vari = SEEDS.forest; // an array of functions, each one creates an element of the forest with properties as type, loot, amount, rarity
do {
var x = game.math.snapTo(game.world.randomX, 32) / 32;
var y = game.math.snapTo(game.world.randomY, 32) / 32;
if (y > game.map.height)
{
y = null;
}
var idx = (y * game.map.height) + x;
}
while (locs.indexOf(idx) !== -1) locs.push(idx);
var f = game.rnd.pick(vari); // pick random function
if (y!=null) { // y will be null if out of bounds
e = f(x*32,y*32); // create entity calling the random function. the entity is added to game.map.entities group
}
},
killMistakedEntity: function (e,t) { // 'e' is enity and 't' is tile
if (e!=PLAYER.phaser && e.properties.type!="npc") { // we dont want to kill people
e.kill();
}
console.log("fantastic! you killed the "+e.properties.type); // i run the game and i dont see this log
},
//
// function calld in update
//
handlePhysics: function () {
game.physics.arcade.overlap(PLAYER.phaser,game.map.entities,this.enablePickup); // overlap player with entities, so we can cut the trees.
game.physics.arcade.collide(PLAYER.phaser,game.map.solidLayer); //.collides player with the terrain
game.physics.arcade.collide(PLAYER.phaser,game.map.entities); // collide player with the forest
// wont work
//game.physics.arcade.overlap(game.map.entities,game.map.cleanLayer,this.clearEntity);
},
I run the game and the town looks bad