Start refactoring

This commit is contained in:
schmelczerandras 2020-07-29 22:33:46 +02:00
parent 6fc53ee51e
commit 9b47d56d8f
39 changed files with 423 additions and 234 deletions

View file

@ -2,9 +2,8 @@ import { vec2 } from 'gl-matrix';
import { Command } from '../commands/command';
import { CommandReceiver } from '../commands/command-receiver';
import { IdentityManager } from '../identity/identity-manager';
import { Typed } from '../transport/serializable';
export abstract class GameObject extends Typed implements CommandReceiver {
export abstract class GameObject implements CommandReceiver {
public readonly id = IdentityManager.generateId();
protected _position = vec2.create();
@ -21,12 +20,12 @@ export abstract class GameObject extends Typed implements CommandReceiver {
[commandType: string]: (e: Command) => void;
} = {};
// can only be called inside the constructor
// can only be called inside the constructor
protected addCommandExecutor<T extends Command>(
commandType: new () => T,
handler: (command: T) => void
) {
this.commandExecutors[commandType.name] = handler;
this.commandExecutors[new commandType().type] = handler;
}
public reactsToCommand(commandType: string): boolean {
@ -34,7 +33,7 @@ export abstract class GameObject extends Typed implements CommandReceiver {
}
public sendCommand(command: Command) {
const commandType = command.constructor.name;
const commandType = command.type;
if (this.commandExecutors.hasOwnProperty(commandType)) {
this.commandExecutors[commandType](command);

View file

@ -4,13 +4,13 @@ import { CursorMoveCommand } from '../../commands/types/cursor-move-command';
import { MoveToCommand } from '../../commands/types/move-to';
import { ZoomCommand } from '../../commands/types/zoom';
import { GameObject } from '../game-object';
import { CircleLight } from './circle-light';
import { Lamp } from './lamp';
export class Camera extends GameObject {
private inViewArea = 1920 * 1080 * 5;
private cursorPosition = vec2.create();
constructor(private light: CircleLight) {
constructor(private light: Lamp) {
super();
this.addCommandExecutor(BeforeRenderCommand, this.draw.bind(this));

View file

@ -1,39 +0,0 @@
import { vec2, vec3 } from 'gl-matrix';
import { RenderCommand } from '../../commands/types/draw';
import { MoveToCommand } from '../../commands/types/move-to';
import { Circle } from '../../drawing/primitives/circle';
import { GameObject } from '../game-object';
const range = 2000;
export class CircleLight extends GameObject {
private boundingCircle: Circle;
constructor(
private center: vec2,
private radius: number,
private value: vec3
) {
super();
this.boundingCircle = new Circle(center, range);
this.addCommandExecutor(RenderCommand, this.draw.bind(this));
this.addCommandExecutor(MoveToCommand, this.moveTo.bind(this));
}
private draw(c: RenderCommand) {
if (c.renderer.isPositionOnScreen(this.center)) {
c.renderer.appendToUniformList('lights', {
center: this.center,
radius: this.radius,
value: this.value,
});
}
}
private moveTo(c: MoveToCommand) {
this.center = c.position;
this.boundingCircle.center = c.position;
}
}

View file

@ -0,0 +1,28 @@
import { vec2, vec3 } from 'gl-matrix';
import { RenderCommand } from '../../commands/types/draw';
import { MoveToCommand } from '../../commands/types/move-to';
import { GameObject } from '../game-object';
import { CircleLight } from '../../drawing/lights/circle-light';
const range = 2000;
export class Lamp extends GameObject {
private light: CircleLight;
constructor(center: vec2, radius: number, color: vec3, lightness: number) {
super();
this.light = new CircleLight(center, radius, color, lightness);
this.addCommandExecutor(RenderCommand, this.draw.bind(this));
this.addCommandExecutor(MoveToCommand, this.moveTo.bind(this));
}
private draw(c: RenderCommand) {
c.renderer.drawLight(this.light);
}
private moveTo(c: MoveToCommand) {
this.light.center = c.position;
}
}

View file

@ -2,13 +2,14 @@ import { vec2, vec3 } from 'gl-matrix';
import { ObjectContainer } from '../object-container';
import { Camera } from '../types/camera';
import { Character } from '../types/character';
import { CircleLight } from '../types/circle-light';
import { Lamp } from '../types/lamp';
export const createCharacter = (objects: ObjectContainer) => {
const light = new CircleLight(
const light = new Lamp(
vec2.create(),
40,
vec3.fromValues(0.67, 0.0, 0.33)
vec3.fromValues(0.67, 0.0, 0.33),
2
);
const camera = new Camera(light);

View file

@ -1,6 +1,6 @@
import { vec2, vec3 } from 'gl-matrix';
import { ObjectContainer } from '../object-container';
import { CircleLight } from '../types/circle-light';
import { Lamp } from '../types/lamp';
import { Tunnel } from '../types/tunnel';
export const createDungeon = (objects: ObjectContainer) => {
@ -19,14 +19,15 @@ export const createDungeon = (objects: ObjectContainer) => {
if (deltaHeight > 0 && Math.random() > 0.8) {
objects.addObject(
new CircleLight(
new Lamp(
currentEnd,
Math.random() * 20 + 30,
vec3.scale(
vec3.create(),
vec3.normalize(vec3.create(), vec3.fromValues(0.5, 0.1, 0.8)),
Math.random() * 0.5 + 0.5
)
),
1
)
);
}