Add input handling

This commit is contained in:
schmelczerandras 2020-07-18 22:02:09 +02:00
parent 29c6b14440
commit d5be727040
36 changed files with 417 additions and 113 deletions

View file

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

View file

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

View file

@ -0,0 +1,5 @@
import { Command } from './command';
export interface CommandReceiver {
sendCommand(command: Command): void;
}

View file

@ -0,0 +1,3 @@
import { Typed } from '../transport/serializable';
export abstract class Command extends Typed {}

View file

@ -0,0 +1,7 @@
import { Command } from '../command';
export class KeyDownCommand extends Command {
public constructor(public readonly key: string) {
super();
}
}

View file

@ -0,0 +1,7 @@
import { Command } from '../command';
export class KeyUpCommand extends Command {
public constructor(public readonly key: string) {
super();
}
}

View file

@ -0,0 +1,8 @@
import { Command } from '../command';
import { Vec2 } from '../../math/vec2';
export class PrimaryActionCommand extends Command {
public constructor(public readonly position: Vec2) {
super();
}
}

View file

@ -0,0 +1,8 @@
import { Command } from '../command';
import { Vec2 } from '../../math/vec2';
export class SecondaryActionCommand extends Command {
public constructor(public readonly position: Vec2) {
super();
}
}

View file

@ -0,0 +1,8 @@
import { Command } from '../command';
import { Vec2 } from '../../math/vec2';
export class SwipeCommand extends Command {
public constructor(public readonly delta: Vec2) {
super();
}
}