Refactor backend to use commands
This commit is contained in:
parent
503c99cb1f
commit
7cf33b9f1a
18 changed files with 144 additions and 156 deletions
|
|
@ -1,7 +0,0 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
|
||||
export interface ExertsForce {
|
||||
getForce(target: vec2): vec2;
|
||||
}
|
||||
|
||||
export const exertsForce = (a: any): a is ExertsForce => a && 'getForce' in a;
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
export interface GeneratesPoints {
|
||||
getPoints(): {
|
||||
decla: number;
|
||||
red: number;
|
||||
};
|
||||
}
|
||||
|
||||
export const generatesPoints = (a: any): a is GeneratesPoints => a && 'getPoints' in a;
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
import { GameObject } from 'shared';
|
||||
|
||||
export interface ReactsToCollision {
|
||||
onCollision(other: GameObject): void;
|
||||
}
|
||||
|
||||
export const reactsToCollision = (a: any): a is ReactsToCollision =>
|
||||
a && 'onCollision' in a;
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
export interface TimeDependent {
|
||||
step(deltaTimeInSeconds: number): void;
|
||||
}
|
||||
|
||||
export const timeDependent = (a: any): a is TimeDependent => a && 'step' in a;
|
||||
|
|
@ -11,6 +11,8 @@ import {
|
|||
CharacterTeam,
|
||||
PropertyUpdatesForObject,
|
||||
UpdatePropertyCommand,
|
||||
CommandExecutors,
|
||||
CommandReceiver,
|
||||
} from 'shared';
|
||||
import { DynamicPhysical } from '../physics/physicals/dynamic-physical';
|
||||
import { CirclePhysical } from './circle-physical';
|
||||
|
|
@ -21,14 +23,12 @@ import { interpolateAngles } from '../helper/interpolate-angles';
|
|||
import { forceAtPosition } from '../physics/functions/force-at-position';
|
||||
import { getBoundingBoxOfCircle } from '../physics/functions/get-bounding-box-of-circle';
|
||||
import { PlanetPhysical } from './planet-physical';
|
||||
import { ReactsToCollision } from './capabilities/reacts-to-collision';
|
||||
import { TimeDependent } from './capabilities/time-dependent';
|
||||
import { GeneratesPoints } from './capabilities/generates-points';
|
||||
import { StepCommand } from '../commands/step';
|
||||
import { ReactToCollisionCommand } from '../commands/react-to-collision';
|
||||
import { GeneratePointsCommand } from '../commands/generate-points';
|
||||
|
||||
@serializesTo(CharacterBase)
|
||||
export class CharacterPhysical
|
||||
extends CharacterBase
|
||||
implements DynamicPhysical, ReactsToCollision, TimeDependent, GeneratesPoints {
|
||||
export class CharacterPhysical extends CharacterBase implements DynamicPhysical {
|
||||
public readonly canCollide = true;
|
||||
public readonly canMove = true;
|
||||
|
||||
|
|
@ -94,6 +94,11 @@ export class CharacterPhysical
|
|||
private leftFootVelocity = new Circle(vec2.create(), 0);
|
||||
private rightFootVelocity = new Circle(vec2.create(), 0);
|
||||
|
||||
protected commandExecutors: CommandExecutors = {
|
||||
[StepCommand.type]: this.step.bind(this),
|
||||
[ReactToCollisionCommand.type]: this.onCollision.bind(this),
|
||||
};
|
||||
|
||||
constructor(
|
||||
name: string,
|
||||
killCount: number,
|
||||
|
|
@ -134,20 +139,14 @@ export class CharacterPhysical
|
|||
}
|
||||
|
||||
private hasGeneratedPoints = false;
|
||||
public getPoints(): { decla: number; red: number } {
|
||||
private getPoints(game: CommandReceiver) {
|
||||
if (!this.isAlive && !this.hasGeneratedPoints) {
|
||||
this.hasGeneratedPoints = true;
|
||||
const decla = this.team === CharacterTeam.decla ? 0 : settings.playerKillPoint;
|
||||
const red = this.team === CharacterTeam.red ? 0 : settings.playerKillPoint;
|
||||
return {
|
||||
decla,
|
||||
red,
|
||||
};
|
||||
|
||||
game.handleCommand(new GeneratePointsCommand(decla, red));
|
||||
}
|
||||
return {
|
||||
decla: 0,
|
||||
red: 0,
|
||||
};
|
||||
}
|
||||
|
||||
public get isAlive(): boolean {
|
||||
|
|
@ -163,7 +162,7 @@ export class CharacterPhysical
|
|||
this.remoteCall('setKillCount', this.killCount);
|
||||
}
|
||||
|
||||
public onCollision(other: GameObject) {
|
||||
public onCollision({ other }: ReactToCollisionCommand) {
|
||||
if (
|
||||
other instanceof ProjectilePhysical &&
|
||||
other.team !== this.team &&
|
||||
|
|
@ -298,7 +297,8 @@ export class CharacterPhysical
|
|||
this.animateScaling(1);
|
||||
}
|
||||
|
||||
public step(deltaTime: number) {
|
||||
private step({ deltaTimeInSeconds, game }: StepCommand) {
|
||||
this.getPoints(game);
|
||||
const oldHead = new Circle(vec2.clone(this.head.center), this.head.radius);
|
||||
const oldLeftFoot = new Circle(
|
||||
vec2.clone(this.leftFoot.center),
|
||||
|
|
@ -310,35 +310,36 @@ export class CharacterPhysical
|
|||
);
|
||||
|
||||
if (this.isDestroyed) {
|
||||
if ((this.timeSinceDying += deltaTime) > settings.spawnDespawnTime) {
|
||||
if ((this.timeSinceDying += deltaTimeInSeconds) > settings.spawnDespawnTime) {
|
||||
this.destroy();
|
||||
} else {
|
||||
this.animateScaling(1 - this.timeSinceDying / settings.spawnDespawnTime);
|
||||
}
|
||||
this.setPropertyUpdates(oldHead, oldLeftFoot, oldRightFoot, deltaTime);
|
||||
this.setPropertyUpdates(oldHead, oldLeftFoot, oldRightFoot, deltaTimeInSeconds);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.hasJustBorn) {
|
||||
if ((this.timeSinceBorn += deltaTime) > settings.spawnDespawnTime) {
|
||||
if ((this.timeSinceBorn += deltaTimeInSeconds) > settings.spawnDespawnTime) {
|
||||
this.hasJustBorn = false;
|
||||
} else {
|
||||
this.animateScaling(this.timeSinceBorn / settings.spawnDespawnTime);
|
||||
}
|
||||
this.setPropertyUpdates(oldHead, oldLeftFoot, oldRightFoot, deltaTime);
|
||||
this.setPropertyUpdates(oldHead, oldLeftFoot, oldRightFoot, deltaTimeInSeconds);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((this.secondsSinceOnSurface += deltaTime) > 0.5) {
|
||||
if ((this.secondsSinceOnSurface += deltaTimeInSeconds) > 0.5) {
|
||||
this.currentPlanet = undefined;
|
||||
}
|
||||
|
||||
this.projectileStrength = Math.min(
|
||||
settings.playerMaxStrength,
|
||||
this.projectileStrength + settings.playerStrengthRegenerationPerSeconds * deltaTime,
|
||||
this.projectileStrength +
|
||||
settings.playerStrengthRegenerationPerSeconds * deltaTimeInSeconds,
|
||||
);
|
||||
|
||||
this.currentPlanet?.takeControl(this.team, deltaTime);
|
||||
this.currentPlanet?.takeControl(this.team, deltaTimeInSeconds);
|
||||
|
||||
const intersectingWithForceField = this.container.findIntersecting(
|
||||
getBoundingBoxOfCircle(
|
||||
|
|
@ -351,8 +352,8 @@ export class CharacterPhysical
|
|||
|
||||
const direction = this.averageAndResetMovementActions();
|
||||
const movementForce = vec2.scale(direction, direction, settings.maxAcceleration);
|
||||
this.applyForce(this.leftFoot, movementForce, deltaTime);
|
||||
this.applyForce(this.rightFoot, movementForce, deltaTime);
|
||||
this.applyForce(this.leftFoot, movementForce, deltaTimeInSeconds);
|
||||
this.applyForce(this.rightFoot, movementForce, deltaTimeInSeconds);
|
||||
|
||||
if (!this.currentPlanet) {
|
||||
const leftFootGravity = forceAtPosition(
|
||||
|
|
@ -364,14 +365,14 @@ export class CharacterPhysical
|
|||
intersectingWithForceField,
|
||||
);
|
||||
|
||||
this.applyForce(this.leftFoot, leftFootGravity, deltaTime);
|
||||
this.applyForce(this.rightFoot, rightFootGravity, deltaTime);
|
||||
this.applyForce(this.leftFoot, leftFootGravity, deltaTimeInSeconds);
|
||||
this.applyForce(this.rightFoot, rightFootGravity, deltaTimeInSeconds);
|
||||
|
||||
const sumForce = vec2.subtract(vec2.create(), leftFootGravity, movementForce);
|
||||
|
||||
this.setDirection(
|
||||
vec2.length(sumForce) === 0 ? vec2.fromValues(0, -1) : sumForce,
|
||||
deltaTime,
|
||||
deltaTimeInSeconds,
|
||||
);
|
||||
} else {
|
||||
const leftFootGravity = this.currentPlanet!.getForce(this.leftFoot.center);
|
||||
|
|
@ -389,7 +390,7 @@ export class CharacterPhysical
|
|||
this.leftFoot.lastNormal,
|
||||
vec2.dot(this.leftFoot.lastNormal, gravity),
|
||||
);
|
||||
this.applyForce(this.leftFoot, scaledLeftFootGravity, deltaTime);
|
||||
this.applyForce(this.leftFoot, scaledLeftFootGravity, deltaTimeInSeconds);
|
||||
|
||||
const scaledRightFootGravity = vec2.scale(
|
||||
vec2.create(),
|
||||
|
|
@ -397,20 +398,20 @@ export class CharacterPhysical
|
|||
vec2.dot(this.rightFoot.lastNormal, gravity),
|
||||
);
|
||||
|
||||
this.applyForce(this.rightFoot, scaledRightFootGravity, deltaTime);
|
||||
this.applyForce(this.rightFoot, scaledRightFootGravity, deltaTimeInSeconds);
|
||||
|
||||
if (vec2.length(gravity) <= 100) {
|
||||
this.currentPlanet = undefined;
|
||||
}
|
||||
this.setDirection(gravity, deltaTime);
|
||||
this.setDirection(gravity, deltaTimeInSeconds);
|
||||
}
|
||||
|
||||
this.keepPosture(deltaTime);
|
||||
this.stepBodyPart(this.leftFoot, deltaTime);
|
||||
this.stepBodyPart(this.rightFoot, deltaTime);
|
||||
this.stepBodyPart(this.head, deltaTime);
|
||||
this.keepPosture(deltaTimeInSeconds);
|
||||
this.stepBodyPart(this.leftFoot, deltaTimeInSeconds);
|
||||
this.stepBodyPart(this.rightFoot, deltaTimeInSeconds);
|
||||
this.stepBodyPart(this.head, deltaTimeInSeconds);
|
||||
|
||||
this.setPropertyUpdates(oldHead, oldLeftFoot, oldRightFoot, deltaTime);
|
||||
this.setPropertyUpdates(oldHead, oldLeftFoot, oldRightFoot, deltaTimeInSeconds);
|
||||
}
|
||||
|
||||
private setDirection(direction: vec2, deltaTime: number) {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { Circle, GameObject, serializesTo } from 'shared';
|
||||
import { Circle, CommandReceiver, GameObject, serializesTo } from 'shared';
|
||||
import { BoundingBox } from '../physics/bounding-boxes/bounding-box';
|
||||
import { BoundingBoxBase } from '../physics/bounding-boxes/bounding-box-base';
|
||||
import { moveCircle } from '../physics/functions/move-circle';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { DynamicPhysical } from '../physics/physicals/dynamic-physical';
|
||||
import { ReactsToCollision, reactsToCollision } from './capabilities/reacts-to-collision';
|
||||
import { ReactToCollisionCommand } from '../commands/react-to-collision';
|
||||
|
||||
@serializesTo(Circle)
|
||||
export class CirclePhysical implements Circle, DynamicPhysical, ReactsToCollision {
|
||||
export class CirclePhysical extends CommandReceiver implements Circle, DynamicPhysical {
|
||||
readonly canCollide = true;
|
||||
readonly canMove = true;
|
||||
|
||||
|
|
@ -24,6 +24,7 @@ export class CirclePhysical implements Circle, DynamicPhysical, ReactsToCollisio
|
|||
private readonly container: PhysicalContainer,
|
||||
private restitution = 0,
|
||||
) {
|
||||
super();
|
||||
this._boundingBox = new BoundingBox();
|
||||
this.recalculateBoundingBox();
|
||||
}
|
||||
|
|
@ -41,10 +42,8 @@ export class CirclePhysical implements Circle, DynamicPhysical, ReactsToCollisio
|
|||
this.recalculateBoundingBox();
|
||||
}
|
||||
|
||||
public onCollision(other: GameObject) {
|
||||
if (reactsToCollision(this.owner)) {
|
||||
this.owner.onCollision(other);
|
||||
}
|
||||
public onCollision(c: ReactToCollisionCommand) {
|
||||
this.owner.handleCommand(c);
|
||||
}
|
||||
|
||||
public get gameObject(): GameObject {
|
||||
|
|
|
|||
|
|
@ -10,30 +10,28 @@ import {
|
|||
CharacterTeam,
|
||||
PropertyUpdatesForObject,
|
||||
UpdatePropertyCommand,
|
||||
CommandExecutors,
|
||||
CommandReceiver,
|
||||
} from 'shared';
|
||||
import { GeneratePointsCommand } from '../commands/generate-points';
|
||||
import { StepCommand } from '../commands/step';
|
||||
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
import { StaticPhysical } from '../physics/physicals/static-physical';
|
||||
import { ExertsForce } from './capabilities/exerts-force';
|
||||
import { GeneratesPoints } from './capabilities/generates-points';
|
||||
import { TimeDependent } from './capabilities/time-dependent';
|
||||
|
||||
@serializesTo(PlanetBase)
|
||||
export class PlanetPhysical
|
||||
extends PlanetBase
|
||||
implements StaticPhysical, GeneratesPoints, ExertsForce, TimeDependent {
|
||||
export class PlanetPhysical extends PlanetBase implements StaticPhysical {
|
||||
public readonly canCollide = true;
|
||||
public readonly canMove = false;
|
||||
|
||||
public static neutralPlanetCount = 0;
|
||||
public static declaPlanetCount = 0;
|
||||
public static redPlanetCount = 0;
|
||||
|
||||
private _boundingBox?: ImmutableBoundingBox;
|
||||
|
||||
protected commandExecutors: CommandExecutors = {
|
||||
[StepCommand.type]: this.step.bind(this),
|
||||
};
|
||||
|
||||
constructor(vertices: Array<vec2>) {
|
||||
super(id(), vertices);
|
||||
PlanetPhysical.neutralPlanetCount++;
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
|
|
@ -79,33 +77,27 @@ export class PlanetPhysical
|
|||
}
|
||||
|
||||
private timeSinceLastPointGeneration = 0;
|
||||
public getPoints(): {
|
||||
decla: number;
|
||||
red: number;
|
||||
} {
|
||||
private getPoints(game: CommandReceiver) {
|
||||
if (this.timeSinceLastPointGeneration > settings.planetPointGenerationInterval) {
|
||||
this.timeSinceLastPointGeneration = 0;
|
||||
if (this.team !== CharacterTeam.neutral) {
|
||||
this.remoteCall('generatedPoints', settings.planetPointGenerationValue);
|
||||
}
|
||||
|
||||
return {
|
||||
decla:
|
||||
game.handleCommand(
|
||||
new GeneratePointsCommand(
|
||||
this.team === CharacterTeam.decla ? settings.planetPointGenerationValue : 0,
|
||||
red: this.team === CharacterTeam.red ? settings.planetPointGenerationValue : 0,
|
||||
};
|
||||
this.team === CharacterTeam.red ? settings.planetPointGenerationValue : 0,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
decla: 0,
|
||||
red: 0,
|
||||
};
|
||||
}
|
||||
|
||||
public step(deltaTime: number): void {
|
||||
this.timeSinceLastPointGeneration += deltaTime;
|
||||
private step({ deltaTimeInSeconds, game }: StepCommand) {
|
||||
this.timeSinceLastPointGeneration += deltaTimeInSeconds;
|
||||
// In reverse order, so that teams can achieve a 100% control.
|
||||
this.takeControl(CharacterTeam.neutral, deltaTime);
|
||||
this.getPoints(game);
|
||||
this.takeControl(CharacterTeam.neutral, deltaTimeInSeconds);
|
||||
}
|
||||
|
||||
public getPropertyUpdates(): PropertyUpdatesForObject {
|
||||
|
|
|
|||
|
|
@ -8,20 +8,19 @@ import {
|
|||
CharacterTeam,
|
||||
PropertyUpdatesForObject,
|
||||
UpdatePropertyCommand,
|
||||
CommandExecutors,
|
||||
} from 'shared';
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
import { CirclePhysical } from './circle-physical';
|
||||
import { DynamicPhysical } from '../physics/physicals/dynamic-physical';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { ReactsToCollision } from './capabilities/reacts-to-collision';
|
||||
import { CharacterPhysical } from './character-physical';
|
||||
import { moveCircle } from '../physics/functions/move-circle';
|
||||
import { TimeDependent } from './capabilities/time-dependent';
|
||||
import { StepCommand } from '../commands/step';
|
||||
import { ReactToCollisionCommand } from '../commands/react-to-collision';
|
||||
|
||||
@serializesTo(ProjectileBase)
|
||||
export class ProjectilePhysical
|
||||
extends ProjectileBase
|
||||
implements DynamicPhysical, ReactsToCollision, TimeDependent {
|
||||
export class ProjectilePhysical extends ProjectileBase implements DynamicPhysical {
|
||||
public readonly canCollide = true;
|
||||
public readonly canMove = true;
|
||||
|
||||
|
|
@ -31,6 +30,11 @@ export class ProjectilePhysical
|
|||
|
||||
public object: CirclePhysical;
|
||||
|
||||
protected commandExecutors: CommandExecutors = {
|
||||
[StepCommand.type]: this.handleStep.bind(this),
|
||||
[ReactToCollisionCommand.type]: this.onCollision.bind(this),
|
||||
};
|
||||
|
||||
constructor(
|
||||
center: vec2,
|
||||
radius: number,
|
||||
|
|
@ -91,7 +95,7 @@ export class ProjectilePhysical
|
|||
}
|
||||
}
|
||||
|
||||
public onCollision(other: GameObject) {
|
||||
public onCollision({ other }: ReactToCollisionCommand) {
|
||||
if (
|
||||
!(other instanceof CharacterPhysical && other.team === this.team) &&
|
||||
this.bounceCount++ === settings.projectileMaxBounceCount
|
||||
|
|
@ -106,8 +110,8 @@ export class ProjectilePhysical
|
|||
]);
|
||||
}
|
||||
|
||||
public step(deltaTime: number) {
|
||||
super.step(deltaTime);
|
||||
private handleStep({ deltaTimeInSeconds }: StepCommand) {
|
||||
super.step(deltaTimeInSeconds);
|
||||
|
||||
if (this.strength <= 0) {
|
||||
this.destroy();
|
||||
|
|
@ -115,7 +119,7 @@ export class ProjectilePhysical
|
|||
}
|
||||
|
||||
vec2.copy(this.object.velocity, this.velocity);
|
||||
const { velocity } = this.object.stepManually(deltaTime);
|
||||
const { velocity } = this.object.stepManually(deltaTimeInSeconds);
|
||||
vec2.copy(this.velocity, velocity);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue