Skip to content

Commit

Permalink
Merge pull request #167 from JernejHabjan/components-to-actors
Browse files Browse the repository at this point in the history
Components to actors
  • Loading branch information
JernejHabjan authored Feb 3, 2024
2 parents 0937ff7 + d3c1d75 commit bda0847
Show file tree
Hide file tree
Showing 110 changed files with 2,042 additions and 1,556 deletions.
22 changes: 11 additions & 11 deletions apps/client/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ const flySquasherRoutes = [
] satisfies Routes;

const dungeonCrawlerRoutes = [
{
path: "dungeon-crawler",
children: [
{
path: "",
loadComponent: () =>
import("./dungeon-crawler/dungeon-crawler.component").then((m) => m.DungeonCrawlerComponent)
},
{ path: "**", redirectTo: "" }
]
}
// {
// path: "dungeon-crawler",
// children: [
// {
// path: "",
// loadComponent: () =>
// import("./dungeon-crawler/dungeon-crawler.component").then((m) => m.DungeonCrawlerComponent)
// },
// { path: "**", redirectTo: "" }
// ]
// }
] satisfies Routes;

const routes = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FlyMovementComponent } from "./fly-movement-component";
import { FlySoundComponent } from "./fly-sound-component";
import { FlyHealthComponent } from "./fly-health-component";
import { Fly } from "../fly";
import { DamageTypes } from "../../../../probable-waffle/game/entity/combat/damage-types";
import { DamageType } from "../../../../probable-waffle/game/entity/combat/damage-type";

export class FlyHealthSystem implements IComponent {
private flyPrefabPointerHitSubscription!: Subscription;
Expand All @@ -28,7 +28,7 @@ export class FlyHealthSystem implements IComponent {

private flyHit = () => {
if (this.fly.killed) return;
this.healthComponent.takeDamage(1, DamageTypes.DamageTypeNormal);
this.healthComponent.takeDamage(1, DamageType.Physical);
if (this.healthComponent.isAlive()) {
this.flySoundComponent.playHitSound();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ export class GameInstanceClientService implements GameInstanceClientServiceInter
switch (payload.property) {
case "joined":
currentUserHasJoined =
!!this.authService.userId && payload.data.playerControllerData!.userId === this.authService.userId;
// offline
(!this.authService.userId &&
payload.data.playerControllerData!.playerDefinition!.playerType === ProbableWafflePlayerType.Human) ||
// logged in
(!!this.authService.userId && payload.data.playerControllerData!.userId === this.authService.userId);
if (currentUserHasJoined) {
this.currentPlayerNumber = payload.data.playerControllerData!.playerDefinition!.player.playerNumber;
}
Expand Down

This file was deleted.

96 changes: 75 additions & 21 deletions apps/client/src/app/probable-waffle/game/data/actor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,86 @@ import Transform = Phaser.GameObjects.Components.Transform;
import { getActorComponent } from "./actor-component";
import { OwnerComponent } from "../entity/actor/components/owner-component";
import { SelectableComponent } from "../entity/actor/components/selectable-component";
import StairsLeft from "../prefabs/buildings/tivara/wall/StairsLeft";
import WallBottomLeft from "../prefabs/buildings/tivara/wall/WallBottomLeft";
import WallBottomLeftBottomRight from "../prefabs/buildings/tivara/wall/WallBottomLeftBottomRight";
import StairsRight from "../prefabs/buildings/tivara/wall/StairsRight";
import WallBottomRight from "../prefabs/buildings/tivara/wall/WallBottomRight";
import WallEmpty from "../prefabs/buildings/tivara/wall/WallEmpty";
import WallTopLeft from "../prefabs/buildings/tivara/wall/WallTopLeft";
import WallTopLeftBottomLeft from "../prefabs/buildings/tivara/wall/WallTopLeftBottomLeft";
import WallTopLeftBottomRight from "../prefabs/buildings/tivara/wall/WallTopLeftBottomRight";
import WallTopLeftTopRight from "../prefabs/buildings/tivara/wall/WallTopLeftTopRight";
import WallTopRight from "../prefabs/buildings/tivara/wall/WallTopRight";
import WallTopRightBottomLeft from "../prefabs/buildings/tivara/wall/WallTopRightBottomLeft";
import WallTopRightBottomRight from "../prefabs/buildings/tivara/wall/WallTopRightBottomRight";
import WatchTower from "../prefabs/buildings/tivara/wall/WatchTower";

export type ActorConstructor = new (scene: Phaser.Scene) => GameObject;
export type ActorMap = { [name: string]: ActorConstructor };
export class ActorManager {
public static actorMap: { [name: string]: ActorConstructor } = {
// ANIMALS
private static animals: ActorMap = {
[Hedgehog.name]: Hedgehog,
[Sheep.name]: Sheep,
// END ANIMALS
[Sheep.name]: Sheep
};

// GENERAL
[GeneralWarrior.name]: GeneralWarrior,
// END GENERAL
private static general: ActorMap = {
[GeneralWarrior.name]: GeneralWarrior
};

// TIVARA
[TivaraMacemanMale.name]: TivaraMacemanMale,
[TivaraSlingshotFemale.name]: TivaraSlingshotFemale,
private static tivaraWorkers: ActorMap = {
[TivaraWorkerFemale.name]: TivaraWorkerFemale,
[TivaraWorkerMale.name]: TivaraWorkerMale,
[TivaraWorkerMale.name]: TivaraWorkerMale
};

private static tivaraUnits: ActorMap = {
[TivaraMacemanMale.name]: TivaraMacemanMale,
[TivaraSlingshotFemale.name]: TivaraSlingshotFemale
};

private static tivaraBuildings: ActorMap = {
[AnkGuard.name]: AnkGuard,
[Olival.name]: Olival,
[Sandhold.name]: Sandhold,
[Temple.name]: Temple,
[WorkMill.name]: WorkMill,
// END TIVARA
[WorkMill.name]: WorkMill
};

private static tivaraWall: ActorMap = {
[StairsLeft.name]: StairsLeft,
[StairsRight.name]: StairsRight,
[WallBottomLeft.name]: WallBottomLeft,
[WallBottomLeftBottomRight.name]: WallBottomLeftBottomRight,
[WallBottomRight.name]: WallBottomRight,
[WallEmpty.name]: WallEmpty,
[WallTopLeft.name]: WallTopLeft,
[WallTopLeftBottomLeft.name]: WallTopLeftBottomLeft,
[WallTopLeftBottomRight.name]: WallTopLeftBottomRight,
[WallTopLeftTopRight.name]: WallTopLeftTopRight,
[WallTopRight.name]: WallTopRight,
[WallTopRightBottomLeft.name]: WallTopRightBottomLeft,
[WallTopRightBottomRight.name]: WallTopRightBottomRight,
[WatchTower.name]: WatchTower
};

// SKADUWEE
private static skaduweeWorkers: ActorMap = {
[SkaduweeWorkerMale.name]: SkaduweeWorkerMale,
[SkaduweeWorkerFemale.name]: SkaduweeWorkerFemale
};
private static skaduweeUnits: ActorMap = {
[SkaduweeOwl.name]: SkaduweeOwl,
[SkaduweeRangedFemale.name]: SkaduweeRangedFemale,
[SkaduweeMagicianFemale.name]: SkaduweeMagicianFemale,
[SkaduweeWarriorMale.name]: SkaduweeWarriorMale,
[SkaduweeWorkerMale.name]: SkaduweeWorkerMale,
[SkaduweeWorkerFemale.name]: SkaduweeWorkerFemale,
[SkaduweeWarriorMale.name]: SkaduweeWarriorMale
};

private static skaduweeBuildings: ActorMap = {
[FrostForge.name]: FrostForge,
[InfantryInn.name]: InfantryInn,
[Owlery.name]: Owlery,
// END SKADUWEE
[Owlery.name]: Owlery
};

// Trees
private static trees: ActorMap = {
[Tree1.name]: Tree1,
[Tree4.name]: Tree4,
[Tree5.name]: Tree5,
Expand All @@ -80,7 +122,19 @@ export class ActorManager {
[Tree9.name]: Tree9,
[Tree10.name]: Tree10,
[Tree11.name]: Tree11
// END Trees
};

public static actorMap: ActorMap = {
...ActorManager.animals,
...ActorManager.general,
...ActorManager.tivaraWorkers,
...ActorManager.tivaraUnits,
...ActorManager.tivaraBuildings,
...ActorManager.tivaraWall,
...ActorManager.skaduweeWorkers,
...ActorManager.skaduweeUnits,
...ActorManager.skaduweeBuildings,
...ActorManager.trees
} as const;

static getActorDefinitionFromActor(actor: GameObject): ActorDefinition | undefined {
Expand Down
33 changes: 12 additions & 21 deletions apps/client/src/app/probable-waffle/game/data/save-game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import TivaraMacemanMale from "../prefabs/characters/tivara/TivaraMacemanMale";
import Hedgehog from "../prefabs/animals/Hedgehog";
import Sheep from "../prefabs/animals/Sheep";
import { ActorManager } from "./actor-manager";
import { filter } from "rxjs";
import { filter, Subscription } from "rxjs";
import GameObject = Phaser.GameObjects.GameObject;
import {
SceneActorCreatorCommunicator,
SceneActorSaveCommunicator
} from "../scenes/components/scene-actor-creator-communicator";

export class SaveGame {
static SaveGameEvent = "SaveGameEvent";
private saveSubscription: Subscription;

constructor(private scene: GameProbableWaffleScene) {
scene.onPostCreate.subscribe(() => this.postCreate());
// only ones that have name: SaveGame.SaveGameEvent
scene.communicator.allScenes
this.saveSubscription = scene.communicator.allScenes
.pipe(filter((scene) => scene.name === SaveGame.SaveGameEvent))
.subscribe(() => this.onSaveGame());
scene.onDestroy.subscribe(() => this.destroy());
Expand Down Expand Up @@ -42,26 +47,11 @@ export class SaveGame {
toRemove.forEach((child) => child.destroy());

this.scene.baseGameData.gameInstance.gameState!.data.actors.forEach((actorDefinition) => {
this.createActorFromDefinition(actorDefinition);
this.scene.events.emit(SceneActorCreatorCommunicator, actorDefinition);
});
console.log("Loaded game");
}

private createActorFromDefinition(actorDefinition: ActorDefinition) {
const actor = ActorManager.createActor(this.scene, actorDefinition.name, actorDefinition);
this.scene.add.existing(actor);
}

private saveAllKnownActorsToSaveGame() {
this.scene.baseGameData.gameInstance.gameState!.data.actors = [];
this.scene.children.each((child) => {
const actorDefinition = ActorManager.getActorDefinitionFromActor(child);
if (actorDefinition) {
this.scene.baseGameData.gameInstance.gameState!.data.actors.push(actorDefinition);
}
});
}

private demoPostNewActors() {
const actors = [
{
Expand All @@ -85,17 +75,18 @@ export class SaveGame {
z: 0
} as ActorDefinition
];
actors.forEach((actor) => {
this.createActorFromDefinition(actor);
actors.forEach((actorDefinition) => {
this.scene.events.emit(SceneActorCreatorCommunicator, actorDefinition);
});
}

private destroy() {
this.scene.events.off(SaveGame.SaveGameEvent);
this.saveSubscription.unsubscribe();
}

private onSaveGame() {
this.saveAllKnownActorsToSaveGame();
this.scene.events.emit(SceneActorSaveCommunicator);
this.scene.communicator.saveGame.emit();
console.log("Saved game");
}
Expand Down
14 changes: 14 additions & 0 deletions apps/client/src/app/probable-waffle/game/data/scene-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ProbableWafflePlayer } from "@fuzzy-waddle/api-interfaces";
import { Scene } from "phaser";
import { ProbableWaffleScene } from "../core/probable-waffle.scene";

export function getPlayerController(scene: Scene, playerNumber?: number): ProbableWafflePlayer | undefined {
// if not instanceof BaseScene then throw error
if (!(scene instanceof ProbableWaffleScene)) {
throw new Error("scene is not instanceof BaseScene");
}
if (playerNumber === undefined) {
playerNumber = scene.baseGameData.user.playerNumber!;
}
return scene.baseGameData.gameInstance.getPlayerByNumber(playerNumber);
}
Loading

0 comments on commit bda0847

Please sign in to comment.