Refactor backend to use commands
This commit is contained in:
parent
503c99cb1f
commit
7cf33b9f1a
4 changed files with 144 additions and 156 deletions
|
|
@ -4,19 +4,19 @@ import { BoundingBoxTree } from './bounding-box-tree';
|
|||
import { Physical } from '../physicals/physical';
|
||||
import { StaticPhysical } from '../physicals/static-physical';
|
||||
import { DynamicPhysical } from '../physicals/dynamic-physical';
|
||||
import {
|
||||
GeneratesPoints,
|
||||
generatesPoints,
|
||||
} from '../../objects/capabilities/generates-points';
|
||||
import { timeDependent } from '../../objects/capabilities/time-dependent';
|
||||
import { Command, CommandReceiver } from 'shared';
|
||||
|
||||
export class PhysicalContainer {
|
||||
export class PhysicalContainer extends CommandReceiver {
|
||||
private isTreeInitialized = false;
|
||||
private staticBoundingBoxesWaitList: Array<StaticPhysical> = [];
|
||||
private staticBoundingBoxes = new BoundingBoxTree();
|
||||
private dynamicBoundingBoxes = new BoundingBoxList();
|
||||
private objects: Array<Physical> = [];
|
||||
|
||||
protected defaultCommandExecutor(c: Command) {
|
||||
this.objects.forEach((o) => o.handleCommand(c));
|
||||
}
|
||||
|
||||
public initialize() {
|
||||
this.staticBoundingBoxes.build(this.staticBoundingBoxesWaitList);
|
||||
this.isTreeInitialized = true;
|
||||
|
|
@ -41,29 +41,10 @@ export class PhysicalContainer {
|
|||
this.dynamicBoundingBoxes.remove(object);
|
||||
}
|
||||
|
||||
public stepObjects(deltaTime: number) {
|
||||
this.objects.forEach((o) => timeDependent(o) && o.step(deltaTime));
|
||||
}
|
||||
|
||||
public resetRemoteCalls() {
|
||||
this.objects.forEach((o) => o.gameObject.resetRemoteCalls());
|
||||
}
|
||||
|
||||
public getPointsGenerated(): { decla: number; red: number } {
|
||||
return this.objects
|
||||
.filter((o) => generatesPoints(o))
|
||||
.reduce(
|
||||
(sum, o) => {
|
||||
const { decla, red } = ((o as unknown) as GeneratesPoints).getPoints();
|
||||
return {
|
||||
decla: sum.decla + decla,
|
||||
red: sum.red + red,
|
||||
};
|
||||
},
|
||||
{ decla: 0, red: 0 },
|
||||
);
|
||||
}
|
||||
|
||||
public findIntersecting(box: BoundingBoxBase): Array<Physical> {
|
||||
return [
|
||||
...this.staticBoundingBoxes.findIntersecting(box),
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { exertsForce } from '../../objects/capabilities/exerts-force';
|
||||
import { PlanetPhysical } from '../../objects/planet-physical';
|
||||
import { Physical } from '../physicals/physical';
|
||||
|
||||
export const forceAtPosition = (position: vec2, objects: Array<Physical>) =>
|
||||
objects.reduce(
|
||||
(sum: vec2, o: Physical) =>
|
||||
exertsForce(o) ? vec2.add(sum, sum, o.getForce(position)) : sum,
|
||||
vec2.create(),
|
||||
);
|
||||
objects
|
||||
.filter((o) => o instanceof PlanetPhysical)
|
||||
.reduce(
|
||||
(sum: vec2, o: Physical) =>
|
||||
vec2.add(sum, sum, (o as PlanetPhysical).getForce(position)),
|
||||
vec2.create(),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { Circle, GameObject, rotate90Deg } from 'shared';
|
||||
import { CirclePhysical } from '../../objects/circle-physical';
|
||||
import { reactsToCollision } from '../../objects/capabilities/reacts-to-collision';
|
||||
import { evaluateSdf } from './evaluate-sdf';
|
||||
import { Physical } from '../physicals/physical';
|
||||
import { ReactToCollisionCommand } from '../../commands/react-to-collision';
|
||||
|
||||
export const moveCircle = (
|
||||
circle: CirclePhysical,
|
||||
|
|
@ -43,13 +43,8 @@ export const moveCircle = (
|
|||
if (ignoreCollision) {
|
||||
circle.center = vec2.add(circle.center, circle.center, delta);
|
||||
} else {
|
||||
if (reactsToCollision(intersecting)) {
|
||||
intersecting.onCollision(circle.gameObject);
|
||||
}
|
||||
|
||||
if (reactsToCollision(circle)) {
|
||||
circle.onCollision(intersecting.gameObject);
|
||||
}
|
||||
intersecting.handleCommand(new ReactToCollisionCommand(circle.gameObject));
|
||||
circle.handleCommand(new ReactToCollisionCommand(intersecting.gameObject));
|
||||
}
|
||||
|
||||
vec2.add(
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { GameObject } from 'shared';
|
||||
import { CommandReceiver, GameObject } from 'shared';
|
||||
import { BoundingBoxBase } from '../bounding-boxes/bounding-box-base';
|
||||
|
||||
export interface PhysicalBase {
|
||||
export interface PhysicalBase extends CommandReceiver {
|
||||
readonly canCollide: boolean;
|
||||
readonly canMove: boolean;
|
||||
readonly boundingBox: BoundingBoxBase;
|
||||
|
|
|
|||
Loading…
Reference in a new issue