diff options
Diffstat (limited to 'engine')
-rw-r--r-- | engine/Game.ts | 4 | ||||
-rw-r--r-- | engine/components/Control.ts | 4 | ||||
-rw-r--r-- | engine/config/constants.ts | 4 | ||||
-rw-r--r-- | engine/entities/Entity.ts | 18 | ||||
-rw-r--r-- | engine/entities/Floor.ts | 4 | ||||
-rw-r--r-- | engine/entities/Player.ts | 20 | ||||
-rw-r--r-- | engine/entities/index.ts | 7 | ||||
-rw-r--r-- | engine/entities/names.ts | 4 | ||||
-rw-r--r-- | engine/network/index.ts | 29 | ||||
-rw-r--r-- | engine/systems/Input.ts | 5 | ||||
-rw-r--r-- | engine/systems/NetworkUpdate.ts | 31 | ||||
-rw-r--r-- | engine/utils/coding.ts | 27 | ||||
-rw-r--r-- | engine/utils/index.ts | 1 |
13 files changed, 118 insertions, 40 deletions
diff --git a/engine/Game.ts b/engine/Game.ts index 8dc5db7..301c8df 100644 --- a/engine/Game.ts +++ b/engine/Game.ts @@ -60,7 +60,7 @@ export class Game { return this.systems.get(name); } - public doGameLoop = (timeStamp: number) => { + public doGameLoop(timeStamp: number) { if (!this.running) { return; } @@ -86,5 +86,5 @@ export class Game { this.systemOrder.forEach((systemName) => { this.systems.get(systemName)?.update(dt, this); }); - }; + } } diff --git a/engine/components/Control.ts b/engine/components/Control.ts index a3621b0..a8dae59 100644 --- a/engine/components/Control.ts +++ b/engine/components/Control.ts @@ -2,13 +2,15 @@ import { Component, ComponentNames, Velocity } from "."; export class Control extends Component { public controlVelocityComponent: Velocity; + public controllableBy: string; constructor( + controllableBy: string, controlVelocityComponent: Velocity = new Velocity(), - controllableBy: string ) { super(ComponentNames.Control); + this.controllableBy = controllableBy; this.controlVelocityComponent = controlVelocityComponent; } } diff --git a/engine/config/constants.ts b/engine/config/constants.ts index fa3f81b..e93986b 100644 --- a/engine/config/constants.ts +++ b/engine/config/constants.ts @@ -14,7 +14,7 @@ export namespace KeyConstants { // value -> [key] from KeyActions export const ActionKeys: Map<Action, string[]> = Object.keys( - KeyActions + KeyActions, ).reduce((acc: Map<Action, string[]>, key) => { const action = KeyActions[key]; @@ -42,6 +42,4 @@ export namespace Miscellaneous { export const DEFAULT_GRID_WIDTH = 30; export const DEFAULT_GRID_HEIGHT = 30; - - export const SERVER_TICK_RATE = 5 / 100; } diff --git a/engine/entities/Entity.ts b/engine/entities/Entity.ts index 4e9df78..88982cb 100644 --- a/engine/entities/Entity.ts +++ b/engine/entities/Entity.ts @@ -1,10 +1,13 @@ +import { EntityNames, Player } from "."; import type { Component } from "../components"; export abstract class Entity { - public readonly id: string; - public readonly components: Map<string, Component>; + public id: string; + public components: Map<string, Component>; + public name: string; - constructor(id: string = crypto.randomUUID()) { + constructor(name: string, id: string = crypto.randomUUID()) { + this.name = name; this.id = id; this.components = new Map(); } @@ -27,4 +30,13 @@ export abstract class Entity { public hasComponent(name: string): boolean { return this.components.has(name); } + + static from(entityName: string, args: any): Entity { + switch (entityName) { + case EntityNames.Player: + return new Player(args.playerId); + default: + throw new Error(".from() Entity type not implemented: " + entityName); + } + } } diff --git a/engine/entities/Floor.ts b/engine/entities/Floor.ts index b204ce0..6cfc276 100644 --- a/engine/entities/Floor.ts +++ b/engine/entities/Floor.ts @@ -1,7 +1,7 @@ import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from "../config"; import { BoundingBox, Sprite } from "../components"; import { TopCollidable } from "../components/TopCollidable"; -import { Entity } from "../entities"; +import { Entity, EntityNames } from "../entities"; export class Floor extends Entity { private static spriteSpec: SpriteSpec = SPRITE_SPECS.get( @@ -9,7 +9,7 @@ export class Floor extends Entity { ) as SpriteSpec; constructor(width: number) { - super(); + super(EntityNames.Floor); this.addComponent( new Sprite( diff --git a/engine/entities/Player.ts b/engine/entities/Player.ts index 03fa69b..cfe4dd2 100644 --- a/engine/entities/Player.ts +++ b/engine/entities/Player.ts @@ -1,4 +1,4 @@ -import { Entity } from "."; +import { Entity, EntityNames } from "."; import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from "../config"; import { Jump, @@ -21,11 +21,11 @@ export class Player extends Entity { private static MOI: number = 100; private static spriteSpec: SpriteSpec = SPRITE_SPECS.get( - Sprites.COFFEE + Sprites.COFFEE, ) as SpriteSpec; - constructor() { - super(); + constructor(playerId: string) { + super(EntityNames.Player); this.addComponent( new BoundingBox( @@ -34,12 +34,12 @@ export class Player extends Entity { y: 100, }, { width: Player.spriteSpec.width, height: Player.spriteSpec.height }, - 0 - ) + 0, + ), ); this.addComponent( - new Velocity({ dCartesian: { dx: 0, dy: 0 }, dTheta: 0 }) + new Velocity({ dCartesian: { dx: 0, dy: 0 }, dTheta: 0 }), ); this.addComponent(new Mass(Player.MASS)); @@ -48,7 +48,7 @@ export class Player extends Entity { this.addComponent(new Gravity()); this.addComponent(new Jump()); - this.addComponent(new Control()); + this.addComponent(new Control(playerId)); this.addComponent(new Collide()); this.addComponent(new WallBounded()); @@ -64,8 +64,8 @@ export class Player extends Entity { { x: 0, y: 0 }, { width: Player.spriteSpec.width, height: Player.spriteSpec.height }, Player.spriteSpec.msPerFrame, - Player.spriteSpec.frames - ) + Player.spriteSpec.frames, + ), ); this.addComponent(new FacingDirection(leftSprite, rightSprite)); diff --git a/engine/entities/index.ts b/engine/entities/index.ts index a921512..dd3dba9 100644 --- a/engine/entities/index.ts +++ b/engine/entities/index.ts @@ -1,3 +1,4 @@ -export * from "./Entity"; -export * from "./Floor"; -export * from "./Player"; +export * from "./Entity";
+export * from "./Floor";
+export * from "./Player";
+export * from "./names";
diff --git a/engine/entities/names.ts b/engine/entities/names.ts new file mode 100644 index 0000000..21594c8 --- /dev/null +++ b/engine/entities/names.ts @@ -0,0 +1,4 @@ +export namespace EntityNames { + export const Player = "Player"; + export const Floor = "Floor"; +} diff --git a/engine/network/index.ts b/engine/network/index.ts new file mode 100644 index 0000000..1726ffc --- /dev/null +++ b/engine/network/index.ts @@ -0,0 +1,29 @@ +export enum MessageType { + NEW_ENTITY = "NEW_ENTITY", + REMOVE_ENTITY = "REMOVE_ENTITY", + UPDATE_ENTITY = "UPDATE_ENTITY", +} + +export type EntityAddBody = { + entityName: string; + args: any; +}; + +export type Message = { + type: MessageType; + body: any; +}; + +export interface MessageQueueProvider { + getNewMessages(): Message[]; + clearMessages(): void; +} + +export interface MessagePublisher { + addMessage(message: Message): void; + publish(): void; +} + +export interface MessageProcessor { + process(message: Message): void; +} diff --git a/engine/systems/Input.ts b/engine/systems/Input.ts index d9b7133..a32ba9a 100644 --- a/engine/systems/Input.ts +++ b/engine/systems/Input.ts @@ -12,12 +12,14 @@ import { Action } from "../interfaces"; import { System, SystemNames } from "."; export class Input extends System { + public clientId: string; private keys: Set<string>; private actionTimeStamps: Map<Action, number>; - constructor() { + constructor(clientId: string) { super(SystemNames.Input); + this.clientId = clientId; this.keys = new Set<string>(); this.actionTimeStamps = new Map<Action, number>(); } @@ -42,6 +44,7 @@ export class Input extends System { const controlComponent = entity.getComponent<Control>( ComponentNames.Control, ); + if (controlComponent.controllableBy != this.clientId) return; if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_RIGHT))) { controlComponent.controlVelocityComponent.velocity.dCartesian.dx += diff --git a/engine/systems/NetworkUpdate.ts b/engine/systems/NetworkUpdate.ts index cdd6de7..6c1d3e4 100644 --- a/engine/systems/NetworkUpdate.ts +++ b/engine/systems/NetworkUpdate.ts @@ -1,43 +1,44 @@ import { System, SystemNames } from "."; import { Game } from "../Game"; import { ComponentNames, NetworkUpdateable } from "../components"; - -export interface MessageQueueProvider { - getNewMessages(): any[]; - clearMessages(): void; -} - -export interface MessagePublisher { - addMessage(message: any): void; - publish(): void; -} +import { + type MessageQueueProvider, + type MessagePublisher, + type MessageProcessor, +} from "../network"; export class NetworkUpdate extends System { private queueProvider: MessageQueueProvider; private publisher: MessagePublisher; + private messageProcessor: MessageProcessor; constructor( queueProvider: MessageQueueProvider, - publisher: MessagePublisher + publisher: MessagePublisher, + messageProcessor: MessageProcessor, ) { super(SystemNames.NetworkUpdate); this.queueProvider = queueProvider; this.publisher = publisher; + this.messageProcessor = messageProcessor; } public update(_dt: number, game: Game) { - const messages = this.queueProvider.getNewMessages(); - if (messages.length) console.log(messages); + this.queueProvider + .getNewMessages() + .forEach((message) => this.messageProcessor.process(message)); this.queueProvider.clearMessages(); game.forEachEntityWithComponent( ComponentNames.NetworkUpdateable, (entity) => { const networkUpdateComponent = entity.getComponent<NetworkUpdateable>( - ComponentNames.NetworkUpdateable + ComponentNames.NetworkUpdateable, ); - } + }, ); + + this.publisher.publish(); } } diff --git a/engine/utils/coding.ts b/engine/utils/coding.ts new file mode 100644 index 0000000..4c1b17f --- /dev/null +++ b/engine/utils/coding.ts @@ -0,0 +1,27 @@ +const replacer = (_key: any, value: any) => { + if (value instanceof Map) { + return { + dataType: "Map", + value: Array.from(value.entries()), + }; + } else { + return value; + } +}; + +const reviver = (_key: any, value: any) => { + if (typeof value === "object" && value !== null) { + if (value.dataType === "Map") { + return new Map(value.value); + } + } + return value; +}; + +export const stringify = (obj: any) => { + return JSON.stringify(obj, replacer); +}; + +export const parse = <T>(str: string) => { + return JSON.parse(str, reviver) as unknown as T; +}; diff --git a/engine/utils/index.ts b/engine/utils/index.ts index 82a0d05..b70734f 100644 --- a/engine/utils/index.ts +++ b/engine/utils/index.ts @@ -1,3 +1,4 @@ export * from "./rotateVector"; export * from "./dotProduct"; export * from "./clamp"; +export * from "./coding"; |