diff --git a/src/assets/img/tiles/wall.png b/src/assets/img/tiles/wall.png index 6f19aad..51e4690 100644 Binary files a/src/assets/img/tiles/wall.png and b/src/assets/img/tiles/wall.png differ diff --git a/src/assets/img/tiles/water-top.png b/src/assets/img/tiles/water-top.png deleted file mode 100644 index ca5f64e..0000000 Binary files a/src/assets/img/tiles/water-top.png and /dev/null differ diff --git a/src/scripts/game.ts b/src/scripts/game.ts index aa7eb7d..8754a19 100644 --- a/src/scripts/game.ts +++ b/src/scripts/game.ts @@ -18,7 +18,7 @@ const config = { physics: { default: 'matter', matter: { - debug: true, + debug: false, gravity: { y: 0 } } }, diff --git a/src/scripts/objects/Background.ts b/src/scripts/objects/Background.ts index a0becea..819a96e 100644 --- a/src/scripts/objects/Background.ts +++ b/src/scripts/objects/Background.ts @@ -7,6 +7,8 @@ export default class Background extends Phaser.GameObjects.GameObject { private readonly graphics: Phaser.GameObjects.Graphics; private readonly tilemap: Phaser.Tilemaps.Tilemap; + public readonly SafeSpawnHeight: number = 0; + private width: number; private height: number; @@ -25,34 +27,43 @@ export default class Background extends Phaser.GameObjects.GameObject { const row: number[] = [1]; const topRow: number[] = [3]; + const penultimateRow: number[] = [5]; for (let i = 0; i < numberOfColumns - 2; i++) { row.push(0); topRow.push(0); + penultimateRow.push(6); } row.push(1); topRow.push(3); + penultimateRow.push(5); const air: number[] = []; + const phantoms: number[] = []; const seaSurface: number[] = []; for (let i = 0; i < numberOfColumns; i++) { air.push(0); + phantoms.push(4); seaSurface.push(2); } - const level: number[][] = [air, air, seaSurface, air, topRow]; - const numberOfRows = Math.FloorTo(this.height / size) + 1; + const level: number[][] = [air, air, seaSurface, phantoms, topRow]; + const flipOffset: number = level.length - 1; + const numberOfRows = Math.FloorTo(this.height / size) + 1 - level.length - 2; for (let i = 0; i < numberOfRows; i++) level.push(row.slice()); + level.push(penultimateRow); + level.push(phantoms); this.tilemap = scene.make.tilemap({ data: level, tileWidth: size, tileHeight: size, insertNull: false }); this.tilemap.addTilesetImage('background-tiles', undefined, 256, 256); const layer = this.tilemap.createLayer(0, 'background-tiles', -overshootX, 0); - layer.setCollisionByExclusion([0, 2]); + layer.setCollision([1, 3, 4, 5, 6]); scene.matter.world.convertTilemapLayer(layer); - for (let i = 0; i < numberOfRows; i++) - this.tilemap.getTileAt(0, i).setFlip(true, false); + const flipRows = numberOfRows + 2; + for (let i = 0; i < flipRows; i++) + this.tilemap.getTileAt(0, i + flipOffset).setFlip(true, false); const tiles = layer.getTilesWithin(0, 0, numberOfColumns, numberOfRows); for (const tile of tiles) { @@ -60,8 +71,8 @@ export default class Background extends Phaser.GameObjects.GameObject { continue; const matterBody: Phaser.Physics.Matter.TileBody = (tile.physics).matterBody; - if (tile.index === 0) { - matterBody.setCollisionCategory(CollisionCategories.WALLS); + if (tile.index === 4) { + matterBody.setCollisionCategory(CollisionCategories.PHANTOM_WALLS); matterBody.setCollidesWith(CollisionCategories.FISH); } else { matterBody.setCollisionCategory(CollisionCategories.WALLS); @@ -70,6 +81,8 @@ export default class Background extends Phaser.GameObjects.GameObject { } raycaster.registerBodies(layer, numberOfColumns, numberOfRows); + + this.SafeSpawnHeight = level.length * size; } public draw(): void { diff --git a/src/scripts/objects/CollisionCategories.ts b/src/scripts/objects/CollisionCategories.ts index a300778..075bed3 100644 --- a/src/scripts/objects/CollisionCategories.ts +++ b/src/scripts/objects/CollisionCategories.ts @@ -4,6 +4,6 @@ export class CollisionCategories public static readonly MECHANICAL_ARM_SEGMENT: number = 0x10; public static readonly SUBMARINE: number = 0x100; public static readonly WALLS: number = 0x1000; - public static readonly PHANTON_WALLS: number = 0x10000; + public static readonly PHANTOM_WALLS: number = 0x10000; public static readonly FISH: number = 0x100000; } \ No newline at end of file diff --git a/src/scripts/objects/Fishes.ts b/src/scripts/objects/Fishes.ts index c72ef93..a1f2516 100644 --- a/src/scripts/objects/Fishes.ts +++ b/src/scripts/objects/Fishes.ts @@ -16,21 +16,19 @@ interface IFishParameters { export class Fish extends Phaser.Physics.Matter.Image { private static readonly directions: Phaser.Math.Vector2[] = [ new Phaser.Math.Vector2(1, 0).rotate(60 * PMath.DEG_TO_RAD), + new Phaser.Math.Vector2(1, 0).rotate(-60 * PMath.DEG_TO_RAD), new Phaser.Math.Vector2(1, 0).rotate(45 * PMath.DEG_TO_RAD), + new Phaser.Math.Vector2(1, 0).rotate(-45 * PMath.DEG_TO_RAD), new Phaser.Math.Vector2(1, 0).rotate(30 * PMath.DEG_TO_RAD), - new Phaser.Math.Vector2(1, 0).rotate(15 * PMath.DEG_TO_RAD), - new Phaser.Math.Vector2(1, 0).rotate(-15 * PMath.DEG_TO_RAD), new Phaser.Math.Vector2(1, 0).rotate(-30 * PMath.DEG_TO_RAD), - new Phaser.Math.Vector2(1, 0).rotate(-45 * PMath.DEG_TO_RAD), - new Phaser.Math.Vector2(1, 0).rotate(-60 * PMath.DEG_TO_RAD) + new Phaser.Math.Vector2(1, 0).rotate(15 * PMath.DEG_TO_RAD), + new Phaser.Math.Vector2(1, 0).rotate(-15 * PMath.DEG_TO_RAD) ] public static readonly minSpeed: number = 0; public static readonly maxSpeed: number = 100; public static readonly lookAheadDistance: number = 100; private readonly raycaster: Raycaster; - private readonly debugLines: Phaser.GameObjects.Line[]; - private readonly text: Phaser.GameObjects.Text; worth: number; weight: number; @@ -62,18 +60,7 @@ export class Fish extends Phaser.Physics.Matter.Image { scene.add.existing(this); this.setCollisionCategory(CollisionCategories.FISH); - this.setCollidesWith(CollisionCategories.MECHANICAL_HOOK); - - // this.debugLines = []; - // this.debugLines.push(scene.add.line(this.x, this.y, 0, 0, this.velocity.x, this.velocity.y, 0xff0000)); - // this.debugLines[0].setOrigin(0, 0); - // for (let i = 0; i < Fish.directions.length; i++) { - // this.debugLines.push(scene.add - // .line(this.x, this.y, 0, 0, this.velocity.x, this.velocity.y, 0x00ff00) - // .setOrigin(0, 0)); - // } - - // this.text = scene.add.text(10, 100, 'Fish 1', { font: '16px Courier', color: 'red' }); + this.setCollidesWith(CollisionCategories.MECHANICAL_HOOK | CollisionCategories.FISH); } public update(delta: number) { @@ -82,11 +69,11 @@ export class Fish extends Phaser.Physics.Matter.Image { if (this.isGoingToCollide()) { const avoidDirection: PMath.Vector2 = this.obstacleRays(); - const collisionAvoidForce = this.steerTowards(avoidDirection); // * settings.avoidCollisionWeight; + const collisionAvoidForce = this.steerTowards(avoidDirection); acceleration.add(collisionAvoidForce); } - this.velocity.add(acceleration); + this.velocity.add(acceleration.scale(scaledDelta)); let speed: number = this.velocity.length(); let direction = this.velocity.scale(1 / speed); speed = PMath.Clamp(speed, Fish.minSpeed, Fish.maxSpeed); @@ -101,28 +88,6 @@ export class Fish extends Phaser.Physics.Matter.Image { this.y += timeVelocity.y; this.setRotationAngle(direction.angle()); this.forward = direction; - - // for (const debugLine of this.debugLines) { - // debugLine.x = this.x; - // debugLine.y = this.y; - // } - // this.debugLines[0].setTo(0, 0, this.forward.x * Fish.lookAheadDistance, this.forward.y * Fish.lookAheadDistance); - - // for (let i = 0; i < Fish.directions.length; i++) { - // const point: PMath.Vector2 = Fish.directions[i] - // .clone() - // .rotate(this.rotation); - // this.debugLines[i + 1].setTo(0, 0, point.x * Fish.lookAheadDistance, point.y * Fish.lookAheadDistance); - // } - - // this.text.setText([ - // 'Fish 1', - // `x: ${this.x}`, - // `y: ${this.y}`, - // `velocity: ${this.velocity.x}, ${this.velocity.y}`, - // `forward: ${this.forward.x}, ${this.forward.y}`, - // `acceleration: ${acceleration.x}, ${acceleration.y}` - // ]); } private setRotationAngle(angle: number): void { @@ -181,13 +146,12 @@ export class FishGroup { fishes: Fish[]; // Constructor for a fish - constructor(scene: Phaser.Scene, num: number, raycaster: Raycaster) { + constructor(scene: Phaser.Scene, raycaster: Raycaster, numberOfFish: number, minSafeHeight: number) { // Create fish group this.fishes = []; - for (let i = 0; i