summaryrefslogtreecommitdiff
path: root/client/lib/components
diff options
context:
space:
mode:
Diffstat (limited to 'client/lib/components')
-rw-r--r--client/lib/components/BoundingBox.ts97
-rw-r--r--client/lib/components/Collide.ts7
-rw-r--r--client/lib/components/Component.ts7
-rw-r--r--client/lib/components/Control.ts7
-rw-r--r--client/lib/components/FacingDirection.ts13
-rw-r--r--client/lib/components/Forces.ts17
-rw-r--r--client/lib/components/Gravity.ts13
-rw-r--r--client/lib/components/Jump.ts10
-rw-r--r--client/lib/components/Mass.ts10
-rw-r--r--client/lib/components/Moment.ts10
-rw-r--r--client/lib/components/Sprite.ts92
-rw-r--r--client/lib/components/TopCollidable.ts7
-rw-r--r--client/lib/components/Velocity.ts15
-rw-r--r--client/lib/components/WallBounded.ts7
-rw-r--r--client/lib/components/index.ts15
-rw-r--r--client/lib/components/names.ts15
16 files changed, 0 insertions, 342 deletions
diff --git a/client/lib/components/BoundingBox.ts b/client/lib/components/BoundingBox.ts
deleted file mode 100644
index 2b1d648..0000000
--- a/client/lib/components/BoundingBox.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { Component, ComponentNames } from ".";
-import type { Coord2D, Dimension2D } from "../interfaces";
-import { dotProduct, rotateVector, normalizeVector } from "../utils";
-
-export class BoundingBox extends Component {
- public center: Coord2D;
- public dimension: Dimension2D;
- public rotation: number;
-
- constructor(center: Coord2D, dimension: Dimension2D, rotation?: number) {
- super(ComponentNames.BoundingBox);
-
- this.center = center;
- this.dimension = dimension;
- this.rotation = rotation ?? 0;
- }
-
- public isCollidingWith(box: BoundingBox): boolean {
- const boxes = [this.getVertices(), box.getVertices()];
- for (const poly of boxes) {
- for (let i = 0; i < poly.length; ++i) {
- const [A, B] = [poly[i], poly[(i + 1) % poly.length]];
- const normal: Coord2D = { x: B.y - A.y, y: A.x - B.x };
-
- const [[minThis, maxThis], [minBox, maxBox]] = boxes.map((box) =>
- box.reduce(
- ([min, max], vertex) => {
- const projection = dotProduct(normal, vertex);
- return [Math.min(min, projection), Math.max(max, projection)];
- },
- [Infinity, -Infinity]
- )
- );
-
- if (maxThis < minBox || maxBox < minThis) return false;
- }
- }
-
- return true;
- }
-
- public getVertices(): Coord2D[] {
- return [
- { 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) => {
- return {
- x: vertex.x + this.center.x,
- y: vertex.y + this.center.y,
- };
- });
- }
-
- private getAxes() {
- const corners: Coord2D[] = this.getVerticesRelativeToCenter();
- const axes: Coord2D[] = [];
-
- for (let i = 0; i < corners.length; ++i) {
- const [cornerA, cornerB] = [
- corners[i],
- corners[(i + 1) % corners.length],
- ].map((corner) => rotateVector(corner, this.rotation));
-
- axes.push(
- normalizeVector({
- x: cornerB.y - cornerA.y,
- y: -(cornerB.x - cornerA.x),
- })
- );
- }
-
- return axes;
- }
-
- private project(axis: Coord2D): [number, number] {
- const corners = this.getCornersRelativeToCenter();
- let [min, max] = [Infinity, -Infinity];
-
- for (const corner of corners) {
- const rotated = rotateVector(corner, this.rotation);
- const translated = {
- x: rotated.x + this.center.x,
- y: rotated.y + this.center.y,
- };
- const projection = dotProduct(translated, axis);
-
- min = Math.min(projection, min);
- max = Math.max(projection, max);
- }
-
- return [min, max];
- }
-}
diff --git a/client/lib/components/Collide.ts b/client/lib/components/Collide.ts
deleted file mode 100644
index 889ecf8..0000000
--- a/client/lib/components/Collide.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { Component, ComponentNames } from ".";
-
-export class Collide extends Component {
- constructor() {
- super(ComponentNames.Collide);
- }
-}
diff --git a/client/lib/components/Component.ts b/client/lib/components/Component.ts
deleted file mode 100644
index 7331982..0000000
--- a/client/lib/components/Component.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export abstract class Component {
- public readonly name: string;
-
- constructor(name: string) {
- this.name = name;
- }
-}
diff --git a/client/lib/components/Control.ts b/client/lib/components/Control.ts
deleted file mode 100644
index 094ef1c..0000000
--- a/client/lib/components/Control.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { Component, ComponentNames } from ".";
-
-export class Control extends Component {
- constructor() {
- super(ComponentNames.Control);
- }
-}
diff --git a/client/lib/components/FacingDirection.ts b/client/lib/components/FacingDirection.ts
deleted file mode 100644
index 1c701a3..0000000
--- a/client/lib/components/FacingDirection.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { Component, ComponentNames, Sprite } from ".";
-
-export class FacingDirection extends Component {
- public readonly facingLeftSprite: Sprite;
- public readonly facingRightSprite: Sprite;
-
- constructor(facingLeftSprite: Sprite, facingRightSprite: Sprite) {
- super(ComponentNames.FacingDirection);
-
- this.facingLeftSprite = facingLeftSprite;
- this.facingRightSprite = facingRightSprite;
- }
-}
diff --git a/client/lib/components/Forces.ts b/client/lib/components/Forces.ts
deleted file mode 100644
index bf540a1..0000000
--- a/client/lib/components/Forces.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import type { Accel2D, Force2D } from "../interfaces";
-import { Component } from "./Component";
-import { ComponentNames } from ".";
-
-/**
- * A list of forces and torque, (in newtons, and newton-meters respectively)
- * to apply on one Physics system update (after which, they are cleared).
- */
-export class Forces extends Component {
- public forces: Force2D[];
-
- constructor(forces?: Force2D[]) {
- super(ComponentNames.Forces);
-
- this.forces = forces ?? [];
- }
-}
diff --git a/client/lib/components/Gravity.ts b/client/lib/components/Gravity.ts
deleted file mode 100644
index 89fcb67..0000000
--- a/client/lib/components/Gravity.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { ComponentNames, Component } from ".";
-
-export class Gravity extends Component {
- private static DEFAULT_TERMINAL_VELOCITY = 5;
-
- public terminalVelocity: number;
-
- constructor(terminalVelocity?: number) {
- super(ComponentNames.Gravity);
- this.terminalVelocity =
- terminalVelocity ?? Gravity.DEFAULT_TERMINAL_VELOCITY;
- }
-}
diff --git a/client/lib/components/Jump.ts b/client/lib/components/Jump.ts
deleted file mode 100644
index 0b40767..0000000
--- a/client/lib/components/Jump.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { Component, ComponentNames } from ".";
-
-export class Jump extends Component {
- public canJump: boolean;
-
- constructor() {
- super(ComponentNames.Jump);
- this.canJump = false;
- }
-}
diff --git a/client/lib/components/Mass.ts b/client/lib/components/Mass.ts
deleted file mode 100644
index daa2d71..0000000
--- a/client/lib/components/Mass.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { Component, ComponentNames } from ".";
-
-export class Mass extends Component {
- public mass: number;
-
- constructor(mass: number) {
- super(ComponentNames.Mass);
- this.mass = mass;
- }
-}
diff --git a/client/lib/components/Moment.ts b/client/lib/components/Moment.ts
deleted file mode 100644
index 3d0dd2f..0000000
--- a/client/lib/components/Moment.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { Component, ComponentNames } from ".";
-
-export class Moment extends Component {
- public inertia: number;
-
- constructor(inertia: number) {
- super(ComponentNames.Moment);
- this.inertia = inertia;
- }
-}
diff --git a/client/lib/components/Sprite.ts b/client/lib/components/Sprite.ts
deleted file mode 100644
index 90e1389..0000000
--- a/client/lib/components/Sprite.ts
+++ /dev/null
@@ -1,92 +0,0 @@
-import { Component, ComponentNames } from ".";
-import type { Dimension2D, DrawArgs, Coord2D } from "../interfaces";
-
-export class Sprite extends Component {
- private sheet: HTMLImageElement;
-
- private spriteImgPos: Coord2D;
- private spriteImgDimensions: Dimension2D;
-
- private msPerFrame: number;
- private msSinceLastFrame: number;
- private currentFrame: number;
- private numFrames: number;
-
- constructor(
- sheet: HTMLImageElement,
- spriteImgPos: Coord2D,
- spriteImgDimensions: Dimension2D,
- msPerFrame: number,
- numFrames: number
- ) {
- super(ComponentNames.Sprite);
-
- this.sheet = sheet;
- this.spriteImgPos = spriteImgPos;
- this.spriteImgDimensions = spriteImgDimensions;
- this.msPerFrame = msPerFrame;
- this.numFrames = numFrames;
-
- this.msSinceLastFrame = 0;
- this.currentFrame = 0;
- }
-
- public update(dt: number) {
- this.msSinceLastFrame += dt;
- if (this.msSinceLastFrame >= this.msPerFrame) {
- this.currentFrame = (this.currentFrame + 1) % this.numFrames;
- this.msSinceLastFrame = 0;
- }
- }
-
- public draw(ctx: CanvasRenderingContext2D, drawArgs: DrawArgs) {
- const { center, rotation, tint, opacity } = drawArgs;
-
- ctx.save();
- ctx.translate(center.x, center.y);
- if (rotation != 0) {
- ctx.rotate(rotation * (Math.PI / 180));
- }
- ctx.translate(-center.x, -center.y);
-
- if (opacity) {
- ctx.globalAlpha = opacity;
- }
-
- ctx.drawImage(
- this.sheet,
- ...this.getSpriteArgs(),
- ...this.getDrawArgs(drawArgs)
- );
-
- if (tint) {
- ctx.globalAlpha = 0.5;
- ctx.globalCompositeOperation = "source-atop";
- ctx.fillStyle = tint;
- ctx.fillRect(...this.getDrawArgs(drawArgs));
- }
-
- ctx.restore();
- }
-
- private getSpriteArgs(): [sx: number, sy: number, sw: number, sh: number] {
- return [
- this.spriteImgPos.x + this.currentFrame * this.spriteImgDimensions.width,
- this.spriteImgPos.y,
- this.spriteImgDimensions.width,
- this.spriteImgDimensions.height,
- ];
- }
-
- private getDrawArgs({
- center,
- dimension,
- }: DrawArgs): [dx: number, dy: number, dw: number, dh: number] {
- return [
- center.x - dimension.width / 2,
- center.y - dimension.height / 2,
- dimension.width,
- dimension.height,
- ];
- }
-}
diff --git a/client/lib/components/TopCollidable.ts b/client/lib/components/TopCollidable.ts
deleted file mode 100644
index 7fb147d..0000000
--- a/client/lib/components/TopCollidable.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { Component, ComponentNames } from ".";
-
-export class TopCollidable extends Component {
- constructor() {
- super(ComponentNames.TopCollidable);
- }
-}
diff --git a/client/lib/components/Velocity.ts b/client/lib/components/Velocity.ts
deleted file mode 100644
index 119427d..0000000
--- a/client/lib/components/Velocity.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import type { Velocity2D } from "../interfaces";
-import { Component } from "./Component";
-import { ComponentNames } from ".";
-
-export class Velocity extends Component {
- public dCartesian: Velocity2D;
- public dTheta: number;
-
- constructor(dCartesian: Velocity2D, dTheta: number) {
- super(ComponentNames.Velocity);
-
- this.dCartesian = dCartesian;
- this.dTheta = dTheta;
- }
-}
diff --git a/client/lib/components/WallBounded.ts b/client/lib/components/WallBounded.ts
deleted file mode 100644
index 5f787e1..0000000
--- a/client/lib/components/WallBounded.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { Component, ComponentNames } from ".";
-
-export class WallBounded extends Component {
- constructor() {
- super(ComponentNames.WallBounded);
- }
-}
diff --git a/client/lib/components/index.ts b/client/lib/components/index.ts
deleted file mode 100644
index 67f1259..0000000
--- a/client/lib/components/index.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-export * from "./Component";
-export * from "./BoundingBox";
-export * from "./Velocity";
-export * from "./Forces";
-export * from "./Sprite";
-export * from "./FacingDirection";
-export * from "./Jump";
-export * from "./TopCollidable";
-export * from "./Collide";
-export * from "./Control";
-export * from "./WallBounded";
-export * from "./Gravity";
-export * from "./Mass";
-export * from "./Moment";
-export * from "./names";
diff --git a/client/lib/components/names.ts b/client/lib/components/names.ts
deleted file mode 100644
index e2ee3d3..0000000
--- a/client/lib/components/names.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-export namespace ComponentNames {
- export const Sprite = "Sprite";
- export const BoundingBox = "BoundingBox";
- export const Velocity = "Velocity";
- export const FacingDirection = "FacingDirection";
- export const Control = "Control";
- export const Jump = "Jump";
- export const TopCollidable = "TopCollidable";
- export const Collide = "Collide";
- export const WallBounded = "WallBounded";
- export const Gravity = "Gravity";
- export const Forces = "Forces";
- export const Mass = "Mass";
- export const Moment = "Moment";
-}