Create network component and system

This commit is contained in:
Elizabeth Hunt 2023-08-13 16:47:58 -06:00
parent c6e9baa000
commit 98e795029b
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
10 changed files with 36 additions and 9 deletions

View File

@ -47,8 +47,9 @@ export class BoundingBox extends Component {
{ x: this.dimension.width / 2, y: this.dimension.height / 2 }, { x: this.dimension.width / 2, y: this.dimension.height / 2 },
{ x: this.dimension.width / 2, y: -this.dimension.height / 2 }, { x: this.dimension.width / 2, y: -this.dimension.height / 2 },
] ]
.map((vertex) => rotateVector(vertex, this.rotation)) .map((vertex) => rotateVector(vertex, this.rotation)) // rotate
.map((vertex) => { .map((vertex) => {
// translate
return { return {
x: vertex.x + this.center.x, x: vertex.x + this.center.x,
y: vertex.y + this.center.y, y: vertex.y + this.center.y,
@ -56,9 +57,10 @@ export class BoundingBox extends Component {
}); });
} }
public getRotationInPiOfUnitCircle() { public getRotationInPiOfUnitCircle(): number {
let rads = this.rotation * (Math.PI / 180); let rads = this.rotation * (Math.PI / 180);
if (rads >= Math.PI) { if (rads >= Math.PI) {
// Physics system guarantees rotation \in [0, 360)
rads -= Math.PI; rads -= Math.PI;
} }
return rads; return rads;

View File

@ -0,0 +1,7 @@
import { Component, ComponentNames } from ".";
export class NetworkUpdateable extends Component {
constructor() {
super(ComponentNames.NetworkUpdateable);
}
}

View File

@ -12,4 +12,5 @@ export * from "./WallBounded";
export * from "./Gravity"; export * from "./Gravity";
export * from "./Mass"; export * from "./Mass";
export * from "./Moment"; export * from "./Moment";
export * from "./NetworkUpdateable";
export * from "./names"; export * from "./names";

View File

@ -12,4 +12,5 @@ export namespace ComponentNames {
export const Forces = "Forces"; export const Forces = "Forces";
export const Mass = "Mass"; export const Mass = "Mass";
export const Moment = "Moment"; export const Moment = "Moment";
export const NetworkUpdateable = "NetworkUpdateable";
} }

View File

@ -41,17 +41,16 @@ export class QuadTree {
this.dimension = dimension; this.dimension = dimension;
} }
public insert(id: number, dimension: Dimension2D, center: Coord2D): void { public insert(boxedEntry: BoxedEntry): void {
const box: BoxedEntry = { id, center, dimension };
if (this.hasChildren()) { if (this.hasChildren()) {
this.getQuadrants(box).forEach((quadrant) => { this.getQuadrants(boxedEntry).forEach((quadrant) => {
const quadrantBox = this.children.get(quadrant); const quadrantBox = this.children.get(quadrant);
quadrantBox?.insert(id, dimension, center); quadrantBox?.insert(boxedEntry);
}); });
return; return;
} }
this.objects.push({ id, dimension, center }); this.objects.push(boxedEntry);
if ( if (
this.objects.length > this.splitThreshold && this.objects.length > this.splitThreshold &&
@ -66,6 +65,7 @@ export class QuadTree {
public clear(): void { public clear(): void {
this.objects = []; this.objects = [];
if (this.hasChildren()) { if (this.hasChildren()) {
this.children.forEach((child) => child.clear()); this.children.forEach((child) => child.clear());
this.children.clear(); this.children.clear();

View File

@ -63,7 +63,11 @@ export class Collision extends System {
dimension = boundingBox.getOutscribedBoxDims(); dimension = boundingBox.getOutscribedBoxDims();
} }
this.quadTree.insert(entity.id, dimension, boundingBox.center); this.quadTree.insert({
id: entity.id,
dimension,
center: boundingBox.center,
});
}); });
// find colliding entities and perform collisions // find colliding entities and perform collisions

View File

@ -9,7 +9,7 @@ import {
import { Game } from "../Game"; import { Game } from "../Game";
import { KeyConstants, PhysicsConstants } from "../config"; import { KeyConstants, PhysicsConstants } from "../config";
import { Action } from "../interfaces"; import { Action } from "../interfaces";
import { System, SystemNames } from "./"; import { System, SystemNames } from ".";
export class Input extends System { export class Input extends System {
private keys: Set<string>; private keys: Set<string>;

View File

@ -0,0 +1,10 @@
import { System, SystemNames } from ".";
import { Game } from "../Game";
export class NetworkUpdate extends System {
constructor() {
super(SystemNames.NetworkUpdate);
}
public update(_dt: number, _game: Game) {}
}

View File

@ -6,3 +6,4 @@ export * from "./Input";
export * from "./FacingDirection"; export * from "./FacingDirection";
export * from "./Collision"; export * from "./Collision";
export * from "./WallBounds"; export * from "./WallBounds";
export * from "./NetworkUpdate";

View File

@ -5,4 +5,5 @@ export namespace SystemNames {
export const Input = "Input"; export const Input = "Input";
export const Collision = "Collision"; export const Collision = "Collision";
export const WallBounds = "WallBounds"; export const WallBounds = "WallBounds";
export const NetworkUpdate = "NetworkUpdate";
} }