Simplify types
This commit is contained in:
parent
6eddb4834b
commit
ba8b1a29fd
30 changed files with 52 additions and 76 deletions
|
|
@ -34,5 +34,5 @@
|
||||||
3000,
|
3000,
|
||||||
8080
|
8080
|
||||||
],
|
],
|
||||||
"postCreateCommand": "npm install && npx lerna run initialize",
|
"postCreateCommand": "npm install && npx lerna run initialize"
|
||||||
}
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import {
|
||||||
settings,
|
settings,
|
||||||
CommandExecutors,
|
CommandExecutors,
|
||||||
MoveActionCommand,
|
MoveActionCommand,
|
||||||
|
typeToBaseType,
|
||||||
} from 'shared';
|
} from 'shared';
|
||||||
|
|
||||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||||
|
|
@ -14,6 +15,7 @@ import { CirclePhysical } from './circle-physical';
|
||||||
import { Physical } from '../physics/physical';
|
import { Physical } from '../physics/physical';
|
||||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||||
|
|
||||||
|
@typeToBaseType
|
||||||
export class CharacterPhysical extends CharacterBase implements Physical {
|
export class CharacterPhysical extends CharacterBase implements Physical {
|
||||||
public readonly canCollide = true;
|
public readonly canCollide = true;
|
||||||
public readonly isInverted = false;
|
public readonly isInverted = false;
|
||||||
|
|
@ -34,7 +36,7 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
||||||
private static readonly leftFootOffset = vec2.fromValues(-20, -10);
|
private static readonly leftFootOffset = vec2.fromValues(-20, -10);
|
||||||
private static readonly rightFootOffset = vec2.fromValues(20, -10);
|
private static readonly rightFootOffset = vec2.fromValues(20, -10);
|
||||||
|
|
||||||
constructor(container: PhysicalContainer) {
|
constructor(private readonly container: PhysicalContainer) {
|
||||||
super(
|
super(
|
||||||
id(),
|
id(),
|
||||||
new CirclePhysical(vec2.clone(CharacterPhysical.headOffset), 50, null, container),
|
new CirclePhysical(vec2.clone(CharacterPhysical.headOffset), 50, null, container),
|
||||||
|
|
@ -159,6 +161,12 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
||||||
);*/
|
);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public destroy() {
|
||||||
|
this.container.removeObject(this.head);
|
||||||
|
this.container.removeObject(this.leftFoot);
|
||||||
|
this.container.removeObject(this.rightFoot);
|
||||||
|
}
|
||||||
|
|
||||||
public toJSON(): any {
|
public toJSON(): any {
|
||||||
const { type, id, head, leftFoot, rightFoot } = this;
|
const { type, id, head, leftFoot, rightFoot } = this;
|
||||||
return [type, id, head, leftFoot, rightFoot];
|
return [type, id, head, leftFoot, rightFoot];
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import { vec2, vec3 } from 'gl-matrix';
|
import { vec2, vec3 } from 'gl-matrix';
|
||||||
import { LampBase, settings, id } from 'shared';
|
import { LampBase, settings, id, typeToBaseType } from 'shared';
|
||||||
|
|
||||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||||
|
|
||||||
import { Physical } from '../physics/physical';
|
import { Physical } from '../physics/physical';
|
||||||
|
|
||||||
|
@typeToBaseType
|
||||||
export class LampPhysical extends LampBase implements Physical {
|
export class LampPhysical extends LampBase implements Physical {
|
||||||
public readonly canCollide = false;
|
public readonly canCollide = false;
|
||||||
public readonly isInverted = false;
|
public readonly isInverted = false;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
import { vec2 } from 'gl-matrix';
|
import { vec2 } from 'gl-matrix';
|
||||||
import { clamp01, mix, TunnelBase, id } from 'shared';
|
import { clamp01, mix, TunnelBase, id, typeToBaseType } from 'shared';
|
||||||
|
|
||||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||||
import { StaticPhysical } from '../physics/containers/static-physical-object';
|
import { StaticPhysical } from '../physics/containers/static-physical-object';
|
||||||
|
|
||||||
|
@typeToBaseType
|
||||||
export class TunnelPhysical extends TunnelBase implements StaticPhysical {
|
export class TunnelPhysical extends TunnelBase implements StaticPhysical {
|
||||||
public readonly canCollide = true;
|
public readonly canCollide = true;
|
||||||
public readonly isInverted = true;
|
public readonly isInverted = true;
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,7 @@ export class Player extends CommandReceiver {
|
||||||
|
|
||||||
public destroy() {
|
public destroy() {
|
||||||
this.isActive = false;
|
this.isActive = false;
|
||||||
|
this.character.destroy();
|
||||||
this.objects.removeObject(this.character);
|
this.objects.removeObject(this.character);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
frontend/.vscode/settings.json
vendored
8
frontend/.vscode/settings.json
vendored
|
|
@ -1,8 +0,0 @@
|
||||||
{
|
|
||||||
"typescript.tsdk": "node_modules\\typescript\\lib",
|
|
||||||
"editor.tabSize": 2,
|
|
||||||
"editor.formatOnSave": true,
|
|
||||||
"editor.codeActionsOnSave": {
|
|
||||||
"source.organizeImports": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { vec2 } from 'gl-matrix';
|
||||||
import { Command } from 'shared';
|
import { Command } from 'shared';
|
||||||
|
|
||||||
export class MoveToCommand extends Command {
|
export class MoveToCommand extends Command {
|
||||||
public static readonly type = 'MoveToCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly position: vec2) {
|
public constructor(public readonly position: vec2) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { Renderer } from 'sdf-2d';
|
||||||
import { Command } from 'shared';
|
import { Command } from 'shared';
|
||||||
|
|
||||||
export class RenderCommand extends Command {
|
export class RenderCommand extends Command {
|
||||||
public static readonly type = 'RenderCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly renderer: Renderer) {
|
public constructor(public readonly renderer: Renderer) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ export const deserialize = (json: string): GameObject => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deserializeJsonArray = (json: string): Array<GameObject> => {
|
export const deserializeJsonArray = (json: string): Array<GameObject> => {
|
||||||
return (JSON.parse(json) as Array<any>).map(
|
return (JSON.parse(json) as Array<any>).map(([type, ...values]) => {
|
||||||
([type, ...values]) => new constructors[type](...values)
|
return new constructors[type](...values);
|
||||||
);
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,3 @@
|
||||||
import { Id } from '../transport/identity';
|
import { Typed } from '../transport/typed';
|
||||||
|
|
||||||
export abstract class Command {
|
export abstract class Command extends Typed { }
|
||||||
public get type(): string {
|
|
||||||
return (this as any).constructor.type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class CreateObjectsCommand extends Command {
|
export class CreateObjectsCommand extends Command {
|
||||||
public static readonly type = 'CreateObjectsCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly serializedObjects: string) {
|
public constructor(public readonly serializedObjects: string) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class CreatePlayerCommand extends Command {
|
export class CreatePlayerCommand extends Command {
|
||||||
public static readonly type = 'CreatePlayerCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly serializedPlayer: string) {
|
public constructor(public readonly serializedPlayer: string) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { Id } from '../../transport/identity';
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class DeleteObjectsCommand extends Command {
|
export class DeleteObjectsCommand extends Command {
|
||||||
public static readonly type = 'DeleteObjectsCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly ids: Array<Id>) {
|
public constructor(public readonly ids: Array<Id>) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { vec2 } from 'gl-matrix';
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class MoveActionCommand extends Command {
|
export class MoveActionCommand extends Command {
|
||||||
public static readonly type = 'MoveActionCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly delta: vec2) {
|
public constructor(public readonly delta: vec2) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { vec2 } from 'gl-matrix';
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class PrimaryActionCommand extends Command {
|
export class PrimaryActionCommand extends Command {
|
||||||
public static readonly type = 'PrimaryActionCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly position: vec2) {
|
public constructor(public readonly position: vec2) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { vec2 } from 'gl-matrix';
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class SecondaryActionCommand extends Command {
|
export class SecondaryActionCommand extends Command {
|
||||||
public static readonly type = 'SecondaryActionCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly position: vec2) {
|
public constructor(public readonly position: vec2) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { Rectangle } from '../../helper/rectangle';
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class SetViewAreaActionCommand extends Command {
|
export class SetViewAreaActionCommand extends Command {
|
||||||
public static readonly type = 'SetViewAreaAction';
|
|
||||||
|
|
||||||
public constructor(public readonly viewArea: Rectangle) {
|
public constructor(public readonly viewArea: Rectangle) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class StepCommand extends Command {
|
export class StepCommand extends Command {
|
||||||
public static readonly type = 'StepCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly deltaTimeInMiliseconds: DOMHighResTimeStamp) {
|
public constructor(public readonly deltaTimeInMiliseconds: DOMHighResTimeStamp) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import { vec2 } from 'gl-matrix';
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class TernaryActionCommand extends Command {
|
export class TernaryActionCommand extends Command {
|
||||||
public static readonly type = 'TernaryActionCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly position: vec2) {
|
public constructor(public readonly position: vec2) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import { Command } from '../command';
|
import { Command } from '../command';
|
||||||
|
|
||||||
export class UpdateObjectsCommand extends Command {
|
export class UpdateObjectsCommand extends Command {
|
||||||
public static readonly type = 'UpdateObjectsCommand';
|
|
||||||
|
|
||||||
public constructor(public readonly serializedObjects: string) {
|
public constructor(public readonly serializedObjects: string) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,3 +29,4 @@ export * from './objects/types/tunnel-base';
|
||||||
export * from './settings';
|
export * from './settings';
|
||||||
export * from './transport/transport-events';
|
export * from './transport/transport-events';
|
||||||
export * from './transport/identity';
|
export * from './transport/identity';
|
||||||
|
export * from './transport/type-to-base-type';
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,12 @@ import { CommandReceiver } from '../commands/command-receiver';
|
||||||
import { Id } from '../transport/identity';
|
import { Id } from '../transport/identity';
|
||||||
|
|
||||||
export abstract class GameObject extends CommandReceiver {
|
export abstract class GameObject extends CommandReceiver {
|
||||||
|
public static get type(): string {
|
||||||
|
return (this as any).name;
|
||||||
|
}
|
||||||
|
|
||||||
public get type(): string {
|
public get type(): string {
|
||||||
return (this as any).constructor.type;
|
return (this as any).constructor.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(public readonly id: Id) {
|
constructor(public readonly id: Id) {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,8 @@
|
||||||
import { vec2 } from 'gl-matrix';
|
|
||||||
import { Circle } from '../../helper/circle';
|
import { Circle } from '../../helper/circle';
|
||||||
import { Id } from '../../main';
|
import { Id } from '../../main';
|
||||||
import { GameObject } from '../game-object';
|
import { GameObject } from '../game-object';
|
||||||
|
|
||||||
export abstract class CharacterBase extends GameObject {
|
export abstract class CharacterBase extends GameObject {
|
||||||
public static readonly type = 'Character';
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
id: Id,
|
id: Id,
|
||||||
public head: Circle,
|
public head: Circle,
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@ import { Id } from '../../main';
|
||||||
import { GameObject } from '../game-object';
|
import { GameObject } from '../game-object';
|
||||||
|
|
||||||
export abstract class LampBase extends GameObject {
|
export abstract class LampBase extends GameObject {
|
||||||
public static readonly type = 'Lamp';
|
|
||||||
|
|
||||||
constructor(id: Id, public center: vec2, public color: vec3, public lightness: number) {
|
constructor(id: Id, public center: vec2, public color: vec3, public lightness: number) {
|
||||||
super(id);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
import { vec2 } from 'gl-matrix';
|
import { vec2 } from 'gl-matrix';
|
||||||
import { GameObject, Id } from '../../main';
|
import { GameObject, Id } from '../../main';
|
||||||
import { typed } from '../../transport/typed';
|
|
||||||
|
|
||||||
export abstract class TunnelBase extends GameObject {
|
export abstract class TunnelBase extends GameObject {
|
||||||
public static readonly type = 'Tunnel';
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
id: Id,
|
id: Id,
|
||||||
public readonly from: vec2,
|
public readonly from: vec2,
|
||||||
|
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
import { v4 } from 'uuid';
|
|
||||||
import { Id } from './identity';
|
|
||||||
|
|
||||||
export class IdentityManager {
|
|
||||||
public static generateId(): Id {
|
|
||||||
return v4();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
14
shared/src/transport/type-to-base-type.ts
Normal file
14
shared/src/transport/type-to-base-type.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
export const typeToBaseType = <T extends { new(...args: any[]): {} }>(
|
||||||
|
constructor: T
|
||||||
|
) => {
|
||||||
|
const parent = constructor;
|
||||||
|
return class extends constructor {
|
||||||
|
public static get type(): string {
|
||||||
|
return Object.getPrototypeOf((parent as any).prototype).constructor.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get type(): string {
|
||||||
|
return Object.getPrototypeOf((parent as any).prototype).constructor.name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
/**
|
export abstract class Typed {
|
||||||
* static type: string must be implemented
|
public static get type(): string {
|
||||||
* @param constructor
|
return (this as any).name;
|
||||||
*/
|
}
|
||||||
export const typed = <T extends { new (...args: any[]): {} }>(constructor: T) => {
|
|
||||||
return class extends constructor {
|
public get type(): string {
|
||||||
public get type(): string {
|
return (this as any).constructor.name;
|
||||||
return (this as any).constructor.type;
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue