Skip to content

Commit

Permalink
server: Add hp to entity updates from server
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVector committed Sep 8, 2024
1 parent ae92a1b commit a0074a4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
30 changes: 18 additions & 12 deletions server/src/implementation/entities/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@ import { WorldService } from "world/world.service";
import { LiveEntity } from "./live-entity";

/** Represent an abstract creature in the game
*
*
* Creature is a LiveEntity that has direction and other state properties.
*
*
* @param direction The direction the creature is facing in
* @param isMoving Whether the creature is moving
*/
export abstract class Creature extends LiveEntity {
/** The direction the Creature is facing in */
public dir: Direction = "down";
/** Whether the Creature is moving */
public isMoving: boolean = false;
/** Whether the Creature is dashing */
public isDashing: boolean = false;
/** The direction the Creature is facing in */
public dir: Direction = "down";
/** Whether the Creature is moving */
public isMoving: boolean = false;
/** Whether the Creature is dashing */
public isDashing: boolean = false;

/**
* Override the default Entity tick to emit updates
*/
public override tick(tick: number, world: WorldService, emitter: EventEmitter2): void {
emitter.emit("entity.update", { id: this.id, dir: this.dir, isMoving: this.isMoving, isDashing: this.isDashing });
}
public override tick(tick: number, world: WorldService, emitter: EventEmitter2): void {
emitter.emit("entity.update", {
id: this.id,
dir: this.dir,
isMoving: this.isMoving,
isDashing: this.isDashing,
hp: this.hp
});
}
}

export type Direction = "up" | "down" | "left" | "right";
export type Direction = "up" | "down" | "left" | "right";
1 change: 1 addition & 0 deletions server/src/updater/updater.event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class EntityUpdateEvent extends Event {
dir: Direction;
isMoving: boolean;
isDashing: boolean;
hp: number;
}

/** An event emitted when an entity attacks */
Expand Down
3 changes: 2 additions & 1 deletion server/src/updater/updater.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class UpdaterService {
id: event.id,
dir: event.dir,
isMoving: event.isMoving,
isDashing: event.isDashing
isDashing: event.isDashing,
hp: event.hp
});
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/world/world.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class WorldService {
player.dir = dir;
player.isMoving = isMoving;
player.isDashing = isDashing;
this.eventEmitter.emit("entity.update", { id, dir, isMoving, isDashing });
this.eventEmitter.emit("entity.update", { id, dir, isMoving, isDashing, hp: player.hp });
this.logger.debug(
`Updated player ${player.name} {dir: ${dir}, isMoving: ${isMoving}, isDashing: ${isDashing}} (${id})`
);
Expand Down

0 comments on commit a0074a4

Please sign in to comment.