Merge pull request #149 from thecodingmachine/multiple_start_positions_in_layer

Allowing several start positions in a given start layer
This commit is contained in:
David Négrier 2020-06-03 11:20:57 +02:00 committed by GitHub
commit 82e2c0d985
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -146,7 +146,9 @@ export class GameScene extends Phaser.Scene {
this.loadNextGame(layer, this.map.width, this.map.tilewidth, this.map.tileheight); this.loadNextGame(layer, this.map.width, this.map.tilewidth, this.map.tileheight);
} }
if (layer.type === 'tilelayer' && layer.name === "start") { if (layer.type === 'tilelayer' && layer.name === "start") {
this.startUser(layer); let startPosition = this.startUser(layer);
this.startX = startPosition.x;
this.startY = startPosition.y;
} }
if (layer.type === 'objectgroup' && layer.name === 'floorLayer') { if (layer.type === 'objectgroup' && layer.name === 'floorLayer') {
depth = 10000; depth = 10000;
@ -262,14 +264,18 @@ export class GameScene extends Phaser.Scene {
/** /**
* @param layer * @param layer
*/ */
private startUser(layer: ITiledMapLayer): void { private startUser(layer: ITiledMapLayer): PositionInterface {
if (this.initPosition !== undefined) { if (this.initPosition !== undefined) {
this.startX = this.initPosition.x; this.startX = this.initPosition.x;
this.startY = this.initPosition.y; this.startY = this.initPosition.y;
return; return {
x: this.initPosition.x,
y: this.initPosition.y
};
} }
let tiles : any = layer.data; let tiles : any = layer.data;
let possibleStartPositions : PositionInterface[] = [];
tiles.forEach((objectKey : number, key: number) => { tiles.forEach((objectKey : number, key: number) => {
if(objectKey === 0){ if(objectKey === 0){
return; return;
@ -277,9 +283,18 @@ export class GameScene extends Phaser.Scene {
let y = Math.floor(key / layer.width); let y = Math.floor(key / layer.width);
let x = key % layer.width; let x = key % layer.width;
this.startX = (x * 32); possibleStartPositions.push({x: x*32, y: y*32});
this.startY = (y * 32);
}); });
// Get a value at random amongst allowed values
if (possibleStartPositions.length === 0) {
console.warn('The start layer "'+layer.name+'" for this map is empty.');
return {
x: 0,
y: 0
};
}
// Choose one of the available start positions at random amongst the list of available start positions.
return possibleStartPositions[Math.floor(Math.random() * possibleStartPositions.length)];
} }
//todo: in a dedicated class/function? //todo: in a dedicated class/function?