Solve serialization

This commit is contained in:
schmelczerandras 2020-10-06 21:33:04 +02:00
commit 8e44dd3733
5 changed files with 242 additions and 201 deletions

View file

@ -1,26 +0,0 @@
import { CreatePlayerCommand } from '../commands/types/create-player';
import { MoveActionCommand } from '../commands/types/move-action';
import { PrimaryActionCommand } from '../commands/types/primary-action';
import { SecondaryActionCommand } from '../commands/types/secondary-action';
import { SetViewAreaActionCommand } from '../commands/types/set-view-area-action';
import { TernaryActionCommand } from '../commands/types/ternary-action';
import { UpdateObjectsCommand } from '../commands/types/update-objects';
import { Command, CreateObjectsCommand, DeleteObjectsCommand } from '../main';
export const commandConstructors: {
[type: string]: new (...values: Array<any>) => any;
} = {
[CreateObjectsCommand.type]: CreateObjectsCommand,
[DeleteObjectsCommand.type]: DeleteObjectsCommand,
[CreatePlayerCommand.type]: CreatePlayerCommand,
[MoveActionCommand.type]: MoveActionCommand,
[PrimaryActionCommand.type]: PrimaryActionCommand,
[SecondaryActionCommand.type]: SecondaryActionCommand,
[TernaryActionCommand.type]: TernaryActionCommand,
[SetViewAreaActionCommand.type]: SetViewAreaActionCommand,
[UpdateObjectsCommand.type]: UpdateObjectsCommand,
};
export const deserializeCommand = ([type, ...values]: [string, Array<any>]): Command => {
return new commandConstructors[type](...values);
};

View file

@ -2,14 +2,6 @@ import { CommandReceiver } from '../commands/command-receiver';
import { Id } from '../transport/identity';
export abstract class GameObject extends CommandReceiver {
public static get type(): string {
return (this as any).name;
}
public get type(): string {
return (this as any).constructor.name;
}
constructor(public readonly id: Id) {
super();
}

View file

@ -1,7 +1,9 @@
import { Circle } from '../../helper/circle';
import { Id } from '../../main';
import { Id } from '../../transport/identity';
import { serializable } from '../../transport/serializable/serializable';
import { GameObject } from '../game-object';
@serializable()
export abstract class CharacterBase extends GameObject {
constructor(
id: Id,
@ -11,4 +13,9 @@ export abstract class CharacterBase extends GameObject {
) {
super(id);
}
public toArray(): Array<any> {
const { id, head, leftFoot, rightFoot } = this as any;
return [id, head, leftFoot, rightFoot];
}
}

View file

@ -1,9 +1,15 @@
import { vec2, vec3 } from 'gl-matrix';
import { Id } from '../../main';
import { Id, serializable } from '../../main';
import { GameObject } from '../game-object';
@serializable()
export abstract class LampBase extends GameObject {
constructor(id: Id, public center: vec2, public color: vec3, public lightness: number) {
super(id);
}
public toArray(): Array<any> {
const { id, center, color, lightness } = this as any;
return [id, center, color, lightness];
}
}

View file

@ -1,6 +1,9 @@
import { vec2 } from 'gl-matrix';
import { GameObject, Id } from '../../main';
import { Id } from '../../transport/identity';
import { serializable } from '../../transport/serializable/serializable';
import { GameObject } from '../game-object';
@serializable()
export abstract class TunnelBase extends GameObject {
constructor(
id: Id,
@ -11,4 +14,9 @@ export abstract class TunnelBase extends GameObject {
) {
super(id);
}
public toArray(): Array<any> {
const { id, from, to, fromRadius, toRadius } = this as any;
return [id, from, to, fromRadius, toRadius];
}
}