This commit is contained in:
schmelczerandras 2020-11-04 16:01:17 +01:00
parent 1ce961d6c2
commit 99cdb62928
76 changed files with 340 additions and 433 deletions

View file

@ -1,7 +0,0 @@
import { CommandReceiver } from './command-receiver';
import { CommandGenerator } from './command-generator';
export const broadcastCommands = (
commandGenerators: Array<CommandGenerator>,
commandReceivers: Array<CommandReceiver>,
) => commandReceivers.forEach((r) => commandGenerators.forEach((g) => g.subscribe(r)));

View file

@ -1,13 +1,17 @@
import { CommandReceiver } from './command-receiver';
import { Command } from './command';
export class CommandGenerator {
export abstract class CommandGenerator {
private subscribers: Array<CommandReceiver> = [];
public subscribe(subscriber: CommandReceiver): void {
this.subscribers.push(subscriber);
}
public clearSubscribers(): void {
this.subscribers = [];
}
protected sendCommandToSubscribers(command: Command): void {
this.subscribers.forEach((s) => s.sendCommand(command));
}

View file

@ -14,7 +14,7 @@ export abstract class CommandReceiver {
const commandType = command.type;
if (Object.prototype.hasOwnProperty.call(this.commandExecutors, commandType)) {
this.commandExecutors[commandType](command);
this.commandExecutors[commandType]!(command);
} else {
this.defaultCommandExecutor(command);
}

View file

@ -1,6 +1,6 @@
import { vec2 } from 'gl-matrix';
import { serializable } from '../../transport/serialization/serializable';
import { Command } from '../command';
import { serializable } from '../../../serialization/serializable';
import { Command } from '../../command';
@serializable
export class MoveActionCommand extends Command {

View file

@ -1,6 +1,6 @@
import { vec2 } from 'gl-matrix';
import { serializable } from '../../transport/serialization/serializable';
import { Command } from '../command';
import { serializable } from '../../../serialization/serializable';
import { Command } from '../../command';
@serializable
export class PrimaryActionCommand extends Command {

View file

@ -1,6 +1,6 @@
import { vec2 } from 'gl-matrix';
import { serializable } from '../../transport/serialization/serializable';
import { Command } from '../command';
import { serializable } from '../../../serialization/serializable';
import { Command } from '../../command';
@serializable
export class SecondaryActionCommand extends Command {

View file

@ -1,5 +1,5 @@
import { serializable } from '../../transport/serialization/serializable';
import { Command } from '../command';
import { serializable } from '../../../serialization/serializable';
import { Command } from '../../command';
@serializable
export class SetAspectRatioActionCommand extends Command {

View file

@ -1,5 +1,5 @@
import { GameObject } from '../../objects/game-object';
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable

View file

@ -1,5 +1,5 @@
import { PlayerCharacterBase } from '../../objects/types/player-character-base';
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable

View file

@ -1,5 +1,5 @@
import { Id } from '../../transport/identity';
import { serializable } from '../../transport/serialization/serializable';
import { Id } from '../../communication/id';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable

View file

@ -1,9 +1,9 @@
import { CharacterTeam } from '../../objects/types/character-team';
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable
export class GameEnd extends Command {
export class GameEndCommand extends Command {
constructor(
public readonly winningTeam: CharacterTeam,
public readonly endCardLengthInSeconds: number,

View file

@ -1,8 +1,8 @@
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable
export class GameStart extends Command {
export class GameStartCommand extends Command {
constructor() {
super();
}

View file

@ -1,14 +0,0 @@
import { vec2 } from 'gl-matrix';
import { serializable } from '../../transport/serialization/serializable';
import { Command } from '../command';
@serializable
export class PlayerDiedCommand extends Command {
public constructor(public readonly timeout: number) {
super();
}
public toArray(): Array<any> {
return [this.timeout];
}
}

View file

@ -0,0 +1,14 @@
import { PropertyUpdatesForObject } from '../../objects/game-object';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable
export class PropertyUpdatesForObjects extends Command {
constructor(public readonly updates: Array<PropertyUpdatesForObject>) {
super();
}
public toArray(): Array<any> {
return [this.updates];
}
}

View file

@ -1,14 +0,0 @@
import { serializable } from '../../transport/serialization/serializable';
import { Command } from '../command';
import { RemoteCallsForObject } from './remote-calls-for-objects';
@serializable
export class RemoteCallsForObjects extends Command {
constructor(public readonly callsForObjects: Array<RemoteCallsForObject>) {
super();
}
public toArray(): Array<any> {
return [this.callsForObjects];
}
}

View file

@ -1,15 +1,23 @@
import { Id } from '../../main';
import { RemoteCall } from '../../objects/game-object';
import { serializable } from '../../transport/serialization/serializable';
import { Id, RemoteCall } from '../../main';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable
export class RemoteCallsForObject extends Command {
constructor(public readonly id: Id, public readonly calls: Array<RemoteCall>) {
super();
}
export class RemoteCallsForObject {
constructor(public readonly id: Id, public readonly calls: Array<RemoteCall>) {}
public toArray(): Array<any> {
return [this.id, this.calls];
}
}
@serializable
export class RemoteCallsForObjects extends Command {
constructor(public readonly callsForObjects: Array<RemoteCallsForObject>) {
super();
}
public toArray(): Array<any> {
return [this.callsForObjects];
}
}

View file

@ -1,4 +1,4 @@
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable

View file

@ -1,14 +0,0 @@
import { vec2 } from 'gl-matrix';
import { serializable } from '../../transport/serialization/serializable';
import { Command } from '../command';
@serializable
export class TernaryActionCommand extends Command {
public constructor(public readonly position: vec2) {
super();
}
public toArray(): Array<any> {
return [this.position];
}
}

View file

@ -1,4 +1,4 @@
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable

View file

@ -1,7 +1,7 @@
import { vec2 } from 'gl-matrix';
import { Id } from '../../communication/id';
import { CharacterTeam } from '../../objects/types/character-team';
import { Id } from '../../transport/identity';
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { Command } from '../command';
@serializable

View file

@ -1,3 +1,5 @@
export type Id = number | null;
let currentId = 0;
export const id = (): number => {

View file

@ -1,5 +1,5 @@
import { vec2 } from 'gl-matrix';
import { serializable } from '../transport/serialization/serializable';
import { serializable } from '../serialization/serializable';
@serializable
export class Circle {

View file

@ -1,6 +1,7 @@
import { vec3 } from 'gl-matrix';
import { rgb } from './rgb';
// source: https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
export const hsl = (hue: number, saturation: number, lightness: number): vec3 => {
hue /= 360;
saturation /= 100;

View file

@ -1,4 +0,0 @@
export const prettyPrint = (o: any): string =>
JSON.stringify(o, (_, v) => (v.toFixed ? Number(v.toFixed(3)) : v), ' ')
.replace(/("|,|{|^\n)/g, '')
.replace(/(\W*}\n?)+/g, '\n\n');

View file

@ -1,4 +1,4 @@
// src
// source
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript
// Mulberry32

View file

@ -1,5 +1,5 @@
import { vec2 } from 'gl-matrix';
import { serializable } from '../transport/serialization/serializable';
import { serializable } from '../serialization/serializable';
@serializable
export class Rectangle {

View file

@ -1 +0,0 @@
export const toPercent = (value: number) => `${Math.round(value * 100)}%`;

View file

@ -2,24 +2,21 @@ export * from './commands/command';
export * from './commands/types/create-objects';
export * from './commands/types/create-player';
export * from './commands/types/delete-objects';
export * from './commands/types/ternary-action';
export * from './commands/types/move-action';
export * from './commands/types/primary-action';
export * from './commands/types/remote-calls-for-objects';
export * from './commands/types/remote-calls-for-object';
export * from './commands/types/player-died';
export * from './commands/types/server-announcement';
export * from './commands/types/property-updates-for-objects';
export * from './commands/types/game-end';
export * from './commands/types/game-start';
export * from './commands/types/update-other-player-directions';
export * from './commands/types/secondary-action';
export * from './commands/types/update-game-state';
export * from './commands/command-receiver';
export * from './commands/types/update-other-player-directions';
export * from './commands/command-executors';
export * from './commands/command-generator';
export * from './commands/broadcast-commands';
export * from './commands/types/set-aspect-ratio-action';
export * from './commands/types/actions/move-action';
export * from './commands/types/actions/primary-action';
export * from './commands/types/actions/set-aspect-ratio-action';
export * from './commands/types/actions/secondary-action';
export * from './helper/array';
export * from './helper/last';
export * from './helper/rgb';
@ -28,27 +25,25 @@ export * from './helper/rgb255';
export * from './helper/mix-rgb';
export * from './helper/clamp';
export * from './helper/calculate-view-area';
export * from './helper/pretty-print';
export * from './helper/circle';
export * from './helper/rectangle';
export * from './helper/mix';
export * from './helper/random';
export * from './helper/id';
export * from './communication/id';
export * from './communication/server-information';
export * from './communication/player-information';
export * from './helper/rotate-90-deg';
export * from './helper/rotate-minus-90-deg';
export * from './objects/game-object';
export * from './transport/serialization/deserialize';
export * from './transport/serialization/serialize';
export * from './transport/serialization/serializes-to';
export * from './transport/serialization/serializable';
export * from './transport/serialization/override-deserialization';
export * from './serialization/deserialize';
export * from './serialization/serialize';
export * from './serialization/serializes-to';
export * from './serialization/serializable';
export * from './serialization/override-deserialization';
export * from './objects/types/character-team';
export * from './objects/types/player-character-base';
export * from './objects/types/lamp-base';
export * from './objects/types/planet-base';
export * from './objects/types/projectile-base';
export * from './settings';
export * from './transport/transport-events';
export * from './transport/identity';
export * from './communication/transport-events';

View file

@ -1,6 +1,6 @@
import { Command } from '../commands/command';
import { Id } from '../transport/identity';
import { serializable } from '../transport/serialization/serializable';
import { Id } from '../communication/id';
import { serializable } from '../serialization/serializable';
@serializable
export class RemoteCall {
@ -33,17 +33,6 @@ export class PropertyUpdatesForObject {
}
}
@serializable
export class PropertyUpdatesForObjects extends Command {
constructor(public readonly updates: Array<PropertyUpdatesForObject>) {
super();
}
public toArray(): Array<any> {
return [this.updates];
}
}
export abstract class GameObject {
private remoteCalls: Array<RemoteCall> = [];

View file

@ -1,9 +1,9 @@
import { vec2 } from 'gl-matrix';
import { Random } from '../../helper/random';
import { settings } from '../../settings';
import { Id } from '../../transport/identity';
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { GameObject } from '../game-object';
import { Id } from '../../communication/id';
@serializable
export class PlanetBase extends GameObject {

View file

@ -1,6 +1,6 @@
import { Id } from '../../communication/id';
import { Circle } from '../../helper/circle';
import { Id } from '../../transport/identity';
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { GameObject } from '../game-object';
import { CharacterTeam } from './character-team';
@ -26,6 +26,8 @@ export class PlayerCharacterBase extends GameObject {
this.health = health;
}
public kill() {}
public setKillCount(killCount: number) {
this.killCount = killCount;
}

View file

@ -1,9 +1,9 @@
import { vec2 } from 'gl-matrix';
import { settings } from '../../settings';
import { Id } from '../../transport/identity';
import { serializable } from '../../transport/serialization/serializable';
import { serializable } from '../../serialization/serializable';
import { GameObject } from '../game-object';
import { CharacterTeam } from './character-team';
import { Id } from '../../communication/id';
@serializable
export class ProjectileBase extends GameObject {

View file

@ -6,8 +6,7 @@ export const deserialize = (json: string): any => {
const possibleType = v[0];
const overridableConstructor = serializableMapping.get(possibleType);
if (overridableConstructor) {
v.shift();
return new overridableConstructor.constructor(...v);
return new overridableConstructor.constructor(...v.slice(1));
}
return v;
}

View file

@ -0,0 +1 @@
export const mangledTypeKey = '__serializable_type';

View file

@ -1,8 +1,5 @@
import { DeserializableClass } from './deserializable-class';
/**
* @internal
*/
export const serializableMapping = new Map<
string,
{

View file

@ -1,3 +1,4 @@
import { mangledTypeKey } from './mangled-type-key';
import { SerializableClass } from './serializable-class';
import { serializableMapping } from './serializable-mapping';
@ -9,8 +10,8 @@ export const serializable = (type: SerializableClass): any => {
});
}
Object.defineProperty(type, '__serializable_type', { value: type.name });
Object.defineProperty(type.prototype, '__serializable_type', { value: type.name });
Object.defineProperty(type, mangledTypeKey, { value: type.name });
Object.defineProperty(type.prototype, mangledTypeKey, { value: type.name });
return type;
};

View file

@ -1,8 +1,10 @@
import { mangledTypeKey } from './mangled-type-key';
export const serialize = (object: any): string => {
return JSON.stringify(object, (_, value) => {
if (value?.__serializable_type) {
if (value && value[mangledTypeKey]) {
const props = value.toArray() as Array<any>;
props.unshift(value.__serializable_type);
props.unshift(value[mangledTypeKey]);
return props;
}
return value?.toFixed ? Number(value.toFixed(3)) : value;

View file

@ -1,3 +1,4 @@
import { mangledTypeKey } from './mangled-type-key';
import { SerializableClass } from './serializable-class';
import { serializableMapping } from './serializable-mapping';
@ -10,8 +11,8 @@ export const serializesTo = (target: SerializableClass) => {
});
}
Object.defineProperty(actual, '__serializable_type', { value: target.name });
Object.defineProperty(actual.prototype, '__serializable_type', {
Object.defineProperty(actual, mangledTypeKey, { value: target.name });
Object.defineProperty(actual.prototype, mangledTypeKey, {
value: target.name,
});

View file

@ -15,7 +15,6 @@ export const settings = {
maxVelocityX: 1000,
maxVelocityY: 1000,
radiusSteps: 500,
gravityScalingForProjectiles: 5,
spawnDespawnTime: 0.7,
worldRadius: 10000,
objectsOnCircleLength: 0.002,
@ -112,5 +111,5 @@ export const settings = {
palette: [declaColor, neutralColor, redColor],
paletteDim: [declaColorDim, neutralColor, redColorDim],
targetPhysicsDeltaTimeInSeconds: 1 / 100,
inViewAreaSize: 1920 * 1080 * 3,
inViewAreaSize: 1920 * 1080 * 4,
};

View file

@ -1 +0,0 @@
export type Id = number | null;