Skip to content

Commit

Permalink
Fixed submarine not colliding with walls.
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-wgilmour committed Apr 27, 2021
1 parent 2aba187 commit 1f243b5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
20 changes: 8 additions & 12 deletions src/scripts/objects/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,34 @@ export default class Background extends Phaser.GameObjects.GameObject {

const row: number[] = [1];
const topRow: number[] = [3];
const topPhatomRow: number[] = [4];
const penultimateRow: number[] = [5];
for (let i = 0; i < numberOfColumns - 2; i++) {
row.push(0);
topRow.push(0);
topPhatomRow.push(0);
penultimateRow.push(6);
}
row.push(1);
topPhatomRow.push(4);
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, phantoms, topRow];
const level: number[][] = [air, air, seaSurface, topPhatomRow, 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);
level.push(air);

this.tilemap = scene.make.tilemap({
data: level,
Expand Down Expand Up @@ -97,22 +98,17 @@ export default class Background extends Phaser.GameObjects.GameObject {
const matterBody: Phaser.Physics.Matter.TileBody = (<any>(
tile.physics
)).matterBody;
if (tile.index === 4) {
matterBody.setCollisionCategory(
CollisionCategories.PHANTOM_WALLS
);
matterBody.setCollidesWith(CollisionCategories.FISH);
} else {
if (tile.index === 1 || tile.index === 4) {
matterBody.setCollisionCategory(CollisionCategories.WALLS);
matterBody.setCollidesWith(
CollisionCategories.SUBMARINE |
CollisionCategories.MECHANICAL_HOOK
CollisionCategories.MECHANICAL_HOOK
);
}
}

this.SafeSpawnHeight = flipOffset * size;
const treasure = this.scene.add.text(this.width/2, 9900, "The real treasure was inside you all along!")
const treasure = this.scene.add.text(this.width / 2, 9900, "The real treasure was inside you all along!")
treasure.setOrigin(0.5);
}

Expand Down
1 change: 0 additions & 1 deletion src/scripts/objects/MechanicalHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export class MechanicalHook extends Phaser.Physics.Matter.Image {

this.setCollisionCategory(CollisionCategories.MECHANICAL_HOOK);
this.setCollidesWith(
// CollisionCategories.SUBMARINE |
CollisionCategories.WALLS |
CollisionCategories.FISH |
CollisionCategories.HAZARD
Expand Down
31 changes: 14 additions & 17 deletions src/scripts/objects/Submarine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Submarine extends Phaser.Physics.Matter.Image {
this.collisionData = scene.cache.json.get("collision-data");
this.displayWidth = this.width * SCALE;
this.displayHeight = this.height * SCALE;
this.setupPhysics();
this.setupPhysics(false);

this.hook = new MechanicalArm(
scene,
Expand All @@ -35,8 +35,8 @@ export default class Submarine extends Phaser.Physics.Matter.Image {
);
}

private setupPhysics() {
const vertices = this.collisionData[`submarine-${this.flipX ? "right" : "left"}`].fixtures[0].vertices.map((x) =>
private setupPhysics(flipped: boolean) {
const vertices = this.collisionData[`submarine-${flipped ? "left" : "right"}`].fixtures[0].vertices.map((x) =>
x.map((y) => ({ x: y.x || 0, y: y.y || 0 }))
);
const verticies1D = vertices.reduce((prev, next) =>
Expand All @@ -53,13 +53,6 @@ export default class Submarine extends Phaser.Physics.Matter.Image {
const y = this.y;
const config: Phaser.Types.Physics.Matter.MatterBodyConfig = Submarine.getMatterBodyConfig(vertices);
this.setBody((<any>config).shape, config);
this.setIgnoreGravity(true);
this.setCollisionCategory(CollisionCategories.SUBMARINE);
this.setCollidesWith(
CollisionCategories.WALLS |
CollisionCategories.MECHANICAL_HOOK |
CollisionCategories.HAZARD
);
this.x = x;
this.y = y;
if (this.hook)
Expand All @@ -71,15 +64,21 @@ export default class Submarine extends Phaser.Physics.Matter.Image {
): Phaser.Types.Physics.Matter.MatterBodyConfig {
const config: Phaser.Types.Physics.Matter.MatterBodyConfig = {
frictionAir: 0.05,
mass: 500
mass: 500,
collisionFilter: {
category: CollisionCategories.SUBMARINE,
mask: CollisionCategories.WALLS |
CollisionCategories.HAZARD
},
ignoreGravity: true
};
if (vertices) {
config["shape"] = {
type: "fromPhysicsEditor",
isStatic: false,
fixtures: [
{
isSensor: true,
isSensor: false,
vertices: vertices
}
]
Expand Down Expand Up @@ -139,7 +138,7 @@ export default class Submarine extends Phaser.Physics.Matter.Image {

// Force the sub not to rotate
if (flipX !== this.flipX)
this.setupPhysics();
this.setupPhysics(flipX);
this.setFlip(flipX, false);
this.setRotation(0);

Expand All @@ -159,10 +158,8 @@ export default class Submarine extends Phaser.Physics.Matter.Image {

if (this.y < WATER_LEVEL + 20)
gameManager.submarine.isAtSurface = true;
else {
if (gameManager.submarine.isAtSurface)
gameManager.submarine.isAtSurface = false;
}
else if (gameManager.submarine.isAtSurface)
gameManager.submarine.isAtSurface = false;
}

// Function to run if you kill the submarine
Expand Down

0 comments on commit 1f243b5

Please sign in to comment.