Skip to content

Commit

Permalink
Fixed even more things.
Browse files Browse the repository at this point in the history
  • Loading branch information
34will committed Apr 24, 2021
1 parent 8d23a27 commit 38114d8
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 78 deletions.
Binary file modified src/assets/img/tiles/wall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/assets/img/tiles/water-top.png
Binary file not shown.
2 changes: 1 addition & 1 deletion src/scripts/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config = {
physics: {
default: 'matter',
matter: {
debug: true,
debug: false,
gravity: { y: 0 }
}
},
Expand Down
27 changes: 20 additions & 7 deletions src/scripts/objects/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,43 +27,52 @@ 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) {
if (!tile || !tile.physics || !(<any>tile.physics).matterBody)
continue;

const matterBody: Phaser.Physics.Matter.TileBody = (<any>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);
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/objects/CollisionCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
60 changes: 12 additions & 48 deletions src/scripts/objects/Fishes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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<num; i++) {

for (let i = 0; i < numberOfFish; i++) {
// Create the fish
const parameters: IFishParameters = this.spawnRandomFish(scene);
const parameters: IFishParameters = this.spawnRandomFish(scene, minSafeHeight);
this.fishes[i] = new Fish(scene, parameters, raycaster);
}
}
Expand All @@ -198,12 +162,12 @@ export class FishGroup {
}

// Method to create a fish moving in a random direction
spawnRandomFish(scene: Phaser.Scene): IFishParameters {
spawnRandomFish(scene: Phaser.Scene, minSafeHeight: number): IFishParameters {
const generator: PMath.RandomDataGenerator = new PMath.RandomDataGenerator();

// Choose a random fish to create
const x: number = generator.integerInRange(64, scene.cameras.main.width - 64);
const y: number = generator.integerInRange(64, scene.cameras.main.height - 64);
const y: number = generator.integerInRange(minSafeHeight + 64, scene.cameras.main.height - 64);
const speed: number = generator.integerInRange(Fish.maxSpeed / 2, Fish.maxSpeed);
const worth: number = generator.integerInRange(0, 10000);
const type: number = generator.integerInRange(1, 3);
Expand Down
3 changes: 1 addition & 2 deletions src/scripts/objects/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ interface Upgrade {
upgradesBrought: number;
}


// Class to manage the game by keeping track of upgrades and money earned
export default class GameManager {

Expand Down Expand Up @@ -68,7 +67,7 @@ export default class GameManager {
},
// O2 tank (units are seconds underwater)
tank: {
totalUpgrades: [1000000000, 90, 135, 180, 225, 270, 305],
totalUpgrades: [45, 90, 135, 180, 225, 270, 305],
upgradesBrought: 0
},
// Ship speed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@

import { Fish } from "./Fishes";
import { MechanicalHook } from "./MechanicalHook";
import Submarine from "./Submarine";
import {gameManager} from "./GameManager";

interface Upgrade {
current: number;
totalUpgrades: number[];
upgradesBrought: number;
}


export default class GameWorld {

scene: Phaser.Scene;
submarine: Submarine;
// Initialise the game
Expand Down Expand Up @@ -42,13 +33,11 @@ export default class GameWorld {
}
type MatchedPair = { hook: MechanicalHook, item: Phaser.GameObjects.GameObject };

function detectObjs(pair: Phaser.Types.Physics.Matter.MatterCollisionData): null | MatchedPair {
let hook: MechanicalHook | undefined;
if (pair.bodyA.gameObject instanceof MechanicalHook) {
function detectObjs(pair: Phaser.Types.Physics.Matter.MatterCollisionData): null | MatchedPair {
if (pair.bodyA.gameObject instanceof MechanicalHook)
return { hook: pair.bodyA.gameObject, item: pair.bodyB.gameObject };
} else if (pair.bodyB.gameObject instanceof MechanicalHook) {
else if (pair.bodyB.gameObject instanceof MechanicalHook)
return { hook: pair.bodyB.gameObject, item: pair.bodyA.gameObject };
}
return null

return null;
}
5 changes: 2 additions & 3 deletions src/scripts/scenes/mainScene.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Background from "../objects/Background";
import { FishGroup } from "../objects/Fishes";
import GameWorld from "../objects/GameWorld2";
import GameWorld from "../objects/GameWorld";
import { MechanicalHook } from "../objects/MechanicalHook";
import { Raycaster } from "../objects/Raycaster";
import Submarine from "../objects/Submarine";
Expand Down Expand Up @@ -30,7 +30,7 @@ export default class MainScene extends Phaser.Scene {
this.cameras.main.setBounds(0, 0, this.width, maxDepth);
this.background = new Background(this, maxDepth, raycaster);
this.submarine = new Submarine(this, this.width / 2, 200);
this.fishGroup = new FishGroup(this, 10, raycaster);
this.fishGroup = new FishGroup(this, raycaster, 10, this.background.SafeSpawnHeight);
this.gameWorld = new GameWorld(this,this.submarine);

this.cameras.main.startFollow(this.submarine);
Expand All @@ -40,6 +40,5 @@ export default class MainScene extends Phaser.Scene {
this.background.draw();
this.submarine.update();
this.fishGroup.update(delta);

}
}
1 change: 0 additions & 1 deletion src/scripts/scenes/preloadScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default class PreloadScene extends Phaser.Scene {
this.load.image('fish2', 'assets/img/fish_type2.png');
this.load.image('fish3', 'assets/img/fish_type3.png');
this.load.image('background-tiles', 'assets/img/tiles/wall.png');
this.load.image('waterline-tiles', 'assets/img/tiles/water-top.png');
this.load.image('sell-fish-button', 'assets/img/ui/sell_fish_button.png');
}

Expand Down

0 comments on commit 38114d8

Please sign in to comment.