Refactor shapes
This commit is contained in:
parent
eb39846b75
commit
006ab3c4e6
8 changed files with 203 additions and 256 deletions
26
frontend/src/scripts/drawing/drawables/drawable-tunnel.ts
Normal file
26
frontend/src/scripts/drawing/drawables/drawable-tunnel.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { IDrawable } from './i-drawable';
|
||||
import { TunnelShape } from '../../shapes/types/tunnel-shape';
|
||||
import { IDrawableDescriptor } from './i-drawable-descriptor';
|
||||
import { settings } from '../settings';
|
||||
|
||||
export class DrawableTunnel extends TunnelShape implements IDrawable {
|
||||
public static descriptor: IDrawableDescriptor = {
|
||||
uniformName: 'lines',
|
||||
countMacroName: 'lineCount',
|
||||
shaderCombinationSteps: settings.shaderCombinations.lineSteps,
|
||||
};
|
||||
|
||||
public serializeToUniforms(uniforms: any): void {
|
||||
const uniformName = DrawableTunnel.descriptor.uniformName;
|
||||
if (!uniforms.hasOwnProperty(uniformName)) {
|
||||
uniforms[uniformName] = [];
|
||||
}
|
||||
|
||||
uniforms[uniformName].push({
|
||||
from: this.from,
|
||||
toFromDelta: this.toFromDelta,
|
||||
fromRadius: this.fromRadius,
|
||||
toRadius: this.toRadius,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,6 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { ImmutableBoundingBox } from '../../physics/containers/immutable-bounding-box';
|
||||
import { GameObject } from '../../objects/game-object';
|
||||
|
||||
export interface IDrawable {
|
||||
serializeToUniforms(uniforms: any): void;
|
||||
distance(target: vec2): number;
|
||||
minimumDistance(target: vec2): number;
|
||||
readonly owner: GameObject;
|
||||
readonly boundingBox: ImmutableBoundingBox;
|
||||
serializeToUniforms(uniforms: any): void;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { vec2, vec3 } from 'gl-matrix';
|
||||
import { GameObject } from '../../../objects/game-object';
|
||||
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
|
||||
import { settings } from '../../settings';
|
||||
import { IDrawableDescriptor } from '../i-drawable-descriptor';
|
||||
import { ILight } from './i-light';
|
||||
|
|
@ -20,24 +19,18 @@ export class CircleLight implements ILight {
|
|||
public lightness: number
|
||||
) {}
|
||||
|
||||
boundingBox: ImmutableBoundingBox;
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public minimumDistance(target: vec2): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public serializeToUniforms(uniforms: any): void {
|
||||
const listName = CircleLight.descriptor.uniformName;
|
||||
const uniformName = CircleLight.descriptor.uniformName;
|
||||
|
||||
if (!uniforms.hasOwnProperty(listName)) {
|
||||
uniforms[listName] = [];
|
||||
if (!uniforms.hasOwnProperty(uniformName)) {
|
||||
uniforms[uniformName] = [];
|
||||
}
|
||||
|
||||
uniforms[listName].push({
|
||||
uniforms[uniformName].push({
|
||||
center: this.center,
|
||||
radius: this.radius,
|
||||
value: this.value,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { IDrawable } from '../i-drawable';
|
||||
|
||||
export interface ILight extends IDrawable {}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { ILight } from './i-light';
|
|||
import { vec2, vec3 } from 'gl-matrix';
|
||||
import { IDrawableDescriptor } from '../i-drawable-descriptor';
|
||||
import { settings } from '../../settings';
|
||||
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
|
||||
import { GameObject } from '../../../objects/game-object';
|
||||
|
||||
export class PointLight implements ILight {
|
||||
|
|
@ -20,16 +19,10 @@ export class PointLight implements ILight {
|
|||
public lightness: number
|
||||
) {}
|
||||
|
||||
boundingBox: ImmutableBoundingBox;
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return vec2.distance(this.center, target) - this.radius;
|
||||
}
|
||||
|
||||
public minimumDistance(target: vec2): number {
|
||||
return vec2.distance(this.center, target) - this.radius;
|
||||
}
|
||||
|
||||
public serializeToUniforms(uniforms: any): void {
|
||||
const listName = PointLight.descriptor.uniformName;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { GameObject } from '../../../objects/game-object';
|
||||
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
|
||||
import { IPrimitive } from './i-primitive';
|
||||
|
||||
export class Circle implements IPrimitive {
|
||||
public constructor(
|
||||
public readonly owner: GameObject,
|
||||
public center = vec2.create(),
|
||||
public radius = 0
|
||||
) { }
|
||||
|
||||
public serializeToUniforms(uniforms: any): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return vec2.distance(this.center, target) - this.radius;
|
||||
}
|
||||
|
||||
public minimumDistance(target: vec2): number {
|
||||
return vec2.distance(this.center, target) - this.radius;
|
||||
}
|
||||
|
||||
public get boundingBox(): ImmutableBoundingBox {
|
||||
return new ImmutableBoundingBox(
|
||||
this,
|
||||
this.center.x - this.radius,
|
||||
this.center.x + this.radius,
|
||||
this.center.y - this.radius,
|
||||
this.center.y + this.radius
|
||||
);
|
||||
}
|
||||
|
||||
public isInside(target: vec2): boolean {
|
||||
return this.distance(target) < 0;
|
||||
}
|
||||
|
||||
public areIntersecting(other: Circle): boolean {
|
||||
const distance = vec2.distance(this.center, other.center);
|
||||
return distance < this.radius + other.radius;
|
||||
}
|
||||
|
||||
public clone(): Circle {
|
||||
return new Circle(this.owner, this.center, this.radius);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
import { IDrawable } from '../i-drawable';
|
||||
|
||||
export interface IPrimitive extends IDrawable {
|
||||
clone(): IPrimitive
|
||||
}
|
||||
|
|
@ -1,99 +0,0 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { clamp01 } from '../../../helper/clamp';
|
||||
import { mix } from '../../../helper/mix';
|
||||
import { GameObject } from '../../../objects/game-object';
|
||||
import { ImmutableBoundingBox } from '../../../physics/containers/immutable-bounding-box';
|
||||
import { settings } from '../../settings';
|
||||
import { IDrawableDescriptor } from '../i-drawable-descriptor';
|
||||
import { Circle } from './circle';
|
||||
import { IPrimitive } from './i-primitive';
|
||||
|
||||
export class TunnelShape implements IPrimitive {
|
||||
public static descriptor: IDrawableDescriptor = {
|
||||
uniformName: 'lines',
|
||||
countMacroName: 'lineCount',
|
||||
shaderCombinationSteps: settings.shaderCombinations.lineSteps,
|
||||
};
|
||||
|
||||
public readonly toFromDelta: vec2;
|
||||
|
||||
private boundingCircle: Circle;
|
||||
|
||||
constructor(
|
||||
public readonly owner: GameObject,
|
||||
public readonly from: vec2,
|
||||
public readonly to: vec2,
|
||||
public readonly fromRadius: number,
|
||||
public readonly toRadius: number
|
||||
) {
|
||||
this.toFromDelta = vec2.subtract(vec2.create(), to, from);
|
||||
|
||||
this.boundingCircle = new Circle(
|
||||
this.owner,
|
||||
vec2.fromValues(from.x / 2 + to.x / 2, from.y / 2 + to.y / 2),
|
||||
Math.max(fromRadius, toRadius) + vec2.distance(from, to)
|
||||
);
|
||||
}
|
||||
|
||||
public serializeToUniforms(uniforms: any): void {
|
||||
if (!uniforms.hasOwnProperty(TunnelShape.descriptor.uniformName)) {
|
||||
uniforms[TunnelShape.descriptor.uniformName] = [];
|
||||
}
|
||||
|
||||
uniforms[TunnelShape.descriptor.uniformName].push({
|
||||
from: this.from,
|
||||
toFromDelta: this.toFromDelta,
|
||||
fromRadius: this.fromRadius,
|
||||
toRadius: this.toRadius,
|
||||
});
|
||||
}
|
||||
|
||||
public get boundingBox(): ImmutableBoundingBox {
|
||||
const xMin = Math.min(
|
||||
this.from.x - this.fromRadius,
|
||||
this.to.x - this.toRadius
|
||||
);
|
||||
const yMin = Math.min(
|
||||
this.from.y - this.fromRadius,
|
||||
this.to.y - this.toRadius
|
||||
);
|
||||
const xMax = Math.max(
|
||||
this.from.x + this.fromRadius,
|
||||
this.to.x + this.toRadius
|
||||
);
|
||||
const yMax = Math.max(
|
||||
this.from.y + this.fromRadius,
|
||||
this.to.y + this.toRadius
|
||||
);
|
||||
|
||||
return new ImmutableBoundingBox(this, xMin, xMax, yMin, yMax);
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
const targetFromDelta = vec2.subtract(vec2.create(), target, this.from);
|
||||
const h = clamp01(
|
||||
vec2.dot(targetFromDelta, this.toFromDelta) /
|
||||
vec2.dot(this.toFromDelta, this.toFromDelta)
|
||||
);
|
||||
return (
|
||||
vec2.distance(
|
||||
targetFromDelta,
|
||||
vec2.scale(vec2.create(), this.toFromDelta, h)
|
||||
) - mix(this.fromRadius, this.toRadius, h)
|
||||
);
|
||||
}
|
||||
|
||||
public minimumDistance(target: vec2): number {
|
||||
return this.boundingCircle.distance(target);
|
||||
}
|
||||
|
||||
public clone(): TunnelShape {
|
||||
return new TunnelShape(
|
||||
this.owner,
|
||||
this.from,
|
||||
this.to,
|
||||
this.fromRadius,
|
||||
this.toRadius
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue