Skip to content

feat(GridEngine): charAdded subject and public characterShifted obs #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/GridEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,50 @@ describe("GridEngine", () => {
});
});

it("should emit when a character is added or removed", () => {
const player1 = "player1";
const player2 = "player2";
const nextMock = jest.fn();

gridEngine.create(tileMapMock, {
characters: [
{
id: player1,
sprite: playerSpriteMock,
},
],
});

gridEngine.characterShifted().subscribe({
complete: jest.fn(),
next: nextMock,
});

gridEngine.addCharacter({ id: player2, sprite: playerSpriteMock });
expect(nextMock).toHaveBeenCalledWith(
expect.objectContaining({
charId: player2,
action: "ADDED",
})
);

gridEngine.removeCharacter(player1);
expect(nextMock).toHaveBeenCalledWith(
expect.objectContaining({
charId: player1,
action: "REMOVED",
})
);

gridEngine.addCharacter({ id: player1, sprite: playerSpriteMock });
expect(nextMock).toHaveBeenCalledWith(
expect.objectContaining({
charId: player1,
action: "ADDED",
})
);
});

it("should notify if any provided character stepped on any of the given tiles on specified layers", () => {
const mockSubject = new Subject<PositionChange & { charId: string }>();
mockGridCharacter.positionChangeFinished.mockReturnValue(mockSubject);
Expand Down
47 changes: 46 additions & 1 deletion src/GridEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { GridTilemap } from "./GridTilemap/GridTilemap";
import { RandomMovement } from "./Movement/RandomMovement/RandomMovement";
import { Observable, Subject } from "rxjs";
import { takeUntil, filter, map, take } from "rxjs/operators";
import { takeUntil, filter, map, take, mergeWith } from "rxjs/operators";
import { Vector2 } from "./Utils/Vector2/Vector2";
import { NoPathFoundStrategy } from "./Pathfinding/NoPathFoundStrategy";
import { PathBlockedStrategy } from "./Pathfinding/PathBlockedStrategy";
Expand Down Expand Up @@ -221,6 +221,26 @@ export interface CharacterData {
charLayer?: string;
}

/**
* Result of a modification of the internal characters array
*/
export interface CharacterShift {
/** the modified character */
charId: string;
/** The action that was performed when modifying the character */
action: CharacterShiftAction;
}

/**
* Type of modification of grid engine characters
*/
export enum CharacterShiftAction {
/** removed existing character */
REMOVED = "REMOVED",
/** added new character */
ADDED = "ADDED",
}

export class GridEngine {
private gridCharacters: Map<string, GridCharacter>;
private gridTilemap: GridTilemap;
Expand All @@ -231,6 +251,7 @@ export class GridEngine {
private positionChangeStarted$: Subject<{ charId: string } & PositionChange>;
private positionChangeFinished$: Subject<{ charId: string } & PositionChange>;
private charRemoved$: Subject<string>;
private charAdded$: Subject<string>;

/**
* Should only be called by Phaser and never directly.
Expand All @@ -257,6 +278,7 @@ export class GridEngine {
this.positionChangeStarted$ = undefined;
this.positionChangeFinished$ = undefined;
this.charRemoved$ = undefined;
this.charAdded$ = undefined;
}

/**
Expand Down Expand Up @@ -332,6 +354,7 @@ export class GridEngine {
{ charId: string } & PositionChange
>();
this.charRemoved$ = new Subject<string>();
this.charAdded$ = new Subject<string>();
this.gridTilemap = new GridTilemap(tilemap);

this.addCharacters();
Expand Down Expand Up @@ -564,6 +587,8 @@ export class GridEngine {
...positionChange,
});
});

this.charAdded$.next(charData.id);
}

/** Checks whether a character with the given ID is registered. */
Expand Down Expand Up @@ -795,6 +820,26 @@ export class GridEngine {
);
}

/**
* @returns Observable that emits when a new character is added or an existing is removed.
*/
characterShifted(): Observable<CharacterShift> {
return this.charAdded$.pipe(
map((c) => ({
charId: c,
action: CharacterShiftAction.ADDED,
})),
mergeWith(
this.charRemoved$.pipe(
map((c) => ({
charId: c,
action: CharacterShiftAction.REMOVED,
}))
)
)
);
}

/**
* @returns Observable that on each start of a movement will provide the
* character ID and the direction.
Expand Down
1 change: 1 addition & 0 deletions src/main-esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
MoveToResult,
NumberOfDirections,
PathBlockedStrategy,
CharacterShiftAction
} from "./GridEngine";

export default GridEngine;