summaryrefslogtreecommitdiff
path: root/engine/components
diff options
context:
space:
mode:
Diffstat (limited to 'engine/components')
-rw-r--r--engine/components/BoundingBox.ts61
-rw-r--r--engine/components/Collide.ts2
-rw-r--r--engine/components/Control.ts15
-rw-r--r--engine/components/FacingDirection.ts2
-rw-r--r--engine/components/Forces.ts6
-rw-r--r--engine/components/Gravity.ts4
-rw-r--r--engine/components/Jump.ts2
-rw-r--r--engine/components/Mass.ts2
-rw-r--r--engine/components/Moment.ts2
-rw-r--r--engine/components/NetworkUpdateable.ts7
-rw-r--r--engine/components/Sprite.ts20
-rw-r--r--engine/components/TopCollidable.ts2
-rw-r--r--engine/components/Velocity.ts24
-rw-r--r--engine/components/WallBounded.ts2
-rw-r--r--engine/components/index.ts31
-rw-r--r--engine/components/names.ts27
16 files changed, 133 insertions, 76 deletions
diff --git a/engine/components/BoundingBox.ts b/engine/components/BoundingBox.ts
index 5e21b2f..921feb9 100644
--- a/engine/components/BoundingBox.ts
+++ b/engine/components/BoundingBox.ts
@@ -1,6 +1,6 @@
-import { Component, ComponentNames } from ".";
-import type { Coord2D, Dimension2D } from "../interfaces";
-import { dotProduct, rotateVector } from "../utils";
+import { Component, ComponentNames } from '.';
+import type { Coord2D, Dimension2D } from '../interfaces';
+import { dotProduct, rotateVector } from '../utils';
export class BoundingBox extends Component {
public center: Coord2D;
@@ -15,8 +15,27 @@ export class BoundingBox extends Component {
this.rotation = rotation ?? 0;
}
- // https://en.wikipedia.org/wiki/Hyperplane_separation_theorem
public isCollidingWith(box: BoundingBox): boolean {
+ if (this.rotation == 0 && box.rotation == 0) {
+ const thisTopLeft = this.getTopLeft();
+ const thisBottomRight = this.getBottomRight();
+
+ const thatTopLeft = box.getTopLeft();
+ const thatBottomRight = box.getBottomRight();
+
+ if (
+ thisBottomRight.x <= thatTopLeft.x ||
+ thisTopLeft.x >= thatBottomRight.x ||
+ thisBottomRight.y <= thatTopLeft.y ||
+ thisTopLeft.y >= thatBottomRight.y
+ ) {
+ return false;
+ }
+
+ return true;
+ }
+
+ // https://en.wikipedia.org/wiki/Hyperplane_separation_theorem
const boxes = [this.getVertices(), box.getVertices()];
for (const poly of boxes) {
for (let i = 0; i < poly.length; i++) {
@@ -29,8 +48,8 @@ export class BoundingBox extends Component {
const projection = dotProduct(normal, vertex);
return [Math.min(min, projection), Math.max(max, projection)];
},
- [Infinity, -Infinity],
- ),
+ [Infinity, -Infinity]
+ )
);
if (maxThis < minBox || maxBox < minThis) return false;
@@ -45,20 +64,22 @@ 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 },
+ { 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) => {
+ // translate
return {
x: vertex.x + this.center.x,
- y: vertex.y + this.center.y,
+ y: vertex.y + this.center.y
};
});
}
- public getRotationInPiOfUnitCircle() {
+ public getRotationInPiOfUnitCircle(): number {
let rads = this.rotation * (Math.PI / 180);
if (rads >= Math.PI) {
+ // Physics system guarantees rotation \in [0, 360)
rads -= Math.PI;
}
return rads;
@@ -68,17 +89,33 @@ export class BoundingBox extends Component {
let rads = this.getRotationInPiOfUnitCircle();
const { width, height } = this.dimension;
+ if (rads == 0) return this.dimension;
+
if (rads <= Math.PI / 2) {
return {
width: Math.abs(height * Math.sin(rads) + width * Math.cos(rads)),
- height: Math.abs(width * Math.sin(rads) + height * Math.cos(rads)),
+ height: Math.abs(width * Math.sin(rads) + height * Math.cos(rads))
};
}
rads -= Math.PI / 2;
return {
width: Math.abs(height * Math.cos(rads) + width * Math.sin(rads)),
- height: Math.abs(width * Math.cos(rads) + height * Math.sin(rads)),
+ height: Math.abs(width * Math.cos(rads) + height * Math.sin(rads))
+ };
+ }
+
+ public getTopLeft(): Coord2D {
+ return {
+ x: this.center.x - this.dimension.width / 2,
+ y: this.center.y - this.dimension.height / 2
+ };
+ }
+
+ public getBottomRight(): Coord2D {
+ return {
+ x: this.center.x + this.dimension.width / 2,
+ y: this.center.y + this.dimension.height / 2
};
}
}
diff --git a/engine/components/Collide.ts b/engine/components/Collide.ts
index 889ecf8..ed72b92 100644
--- a/engine/components/Collide.ts
+++ b/engine/components/Collide.ts
@@ -1,4 +1,4 @@
-import { Component, ComponentNames } from ".";
+import { Component, ComponentNames } from '.';
export class Collide extends Component {
constructor() {
diff --git a/engine/components/Control.ts b/engine/components/Control.ts
index 1e782ee..d3987d7 100644
--- a/engine/components/Control.ts
+++ b/engine/components/Control.ts
@@ -1,11 +1,18 @@
-import { Component, ComponentNames, Velocity } from ".";
+import { Component, ComponentNames, Velocity } from '.';
export class Control extends Component {
- public controlVelocity: Velocity;
+ public controlVelocityComponent: Velocity;
+ public controllableBy: string;
+ public isControllable: boolean; // computed each update in the input system
- constructor(controlVelocity: Velocity = new Velocity()) {
+ constructor(
+ controllableBy: string,
+ controlVelocityComponent: Velocity = new Velocity()
+ ) {
super(ComponentNames.Control);
- this.controlVelocity = controlVelocity;
+ this.controllableBy = controllableBy;
+ this.controlVelocityComponent = controlVelocityComponent;
+ this.isControllable = false;
}
}
diff --git a/engine/components/FacingDirection.ts b/engine/components/FacingDirection.ts
index 1c701a3..8c2a9d2 100644
--- a/engine/components/FacingDirection.ts
+++ b/engine/components/FacingDirection.ts
@@ -1,4 +1,4 @@
-import { Component, ComponentNames, Sprite } from ".";
+import { Component, ComponentNames, Sprite } from '.';
export class FacingDirection extends Component {
public readonly facingLeftSprite: Sprite;
diff --git a/engine/components/Forces.ts b/engine/components/Forces.ts
index 91ae1c1..e397985 100644
--- a/engine/components/Forces.ts
+++ b/engine/components/Forces.ts
@@ -1,6 +1,6 @@
-import type { Force2D } from "../interfaces";
-import { Component } from "./Component";
-import { ComponentNames } from ".";
+import type { Force2D } from '../interfaces';
+import { Component } from './Component';
+import { ComponentNames } from '.';
/**
* A list of forces and torque, (in newtons, and newton-meters respectively)
diff --git a/engine/components/Gravity.ts b/engine/components/Gravity.ts
index 89fcb67..dd6dd2e 100644
--- a/engine/components/Gravity.ts
+++ b/engine/components/Gravity.ts
@@ -1,7 +1,7 @@
-import { ComponentNames, Component } from ".";
+import { ComponentNames, Component } from '.';
export class Gravity extends Component {
- private static DEFAULT_TERMINAL_VELOCITY = 5;
+ private static DEFAULT_TERMINAL_VELOCITY = 4.5;
public terminalVelocity: number;
diff --git a/engine/components/Jump.ts b/engine/components/Jump.ts
index 0b40767..6cbfb08 100644
--- a/engine/components/Jump.ts
+++ b/engine/components/Jump.ts
@@ -1,4 +1,4 @@
-import { Component, ComponentNames } from ".";
+import { Component, ComponentNames } from '.';
export class Jump extends Component {
public canJump: boolean;
diff --git a/engine/components/Mass.ts b/engine/components/Mass.ts
index daa2d71..a7f98fd 100644
--- a/engine/components/Mass.ts
+++ b/engine/components/Mass.ts
@@ -1,4 +1,4 @@
-import { Component, ComponentNames } from ".";
+import { Component, ComponentNames } from '.';
export class Mass extends Component {
public mass: number;
diff --git a/engine/components/Moment.ts b/engine/components/Moment.ts
index 3d0dd2f..cd76294 100644
--- a/engine/components/Moment.ts
+++ b/engine/components/Moment.ts
@@ -1,4 +1,4 @@
-import { Component, ComponentNames } from ".";
+import { Component, ComponentNames } from '.';
export class Moment extends Component {
public inertia: number;
diff --git a/engine/components/NetworkUpdateable.ts b/engine/components/NetworkUpdateable.ts
new file mode 100644
index 0000000..014270c
--- /dev/null
+++ b/engine/components/NetworkUpdateable.ts
@@ -0,0 +1,7 @@
+import { Component, ComponentNames } from '.';
+
+export class NetworkUpdateable extends Component {
+ constructor() {
+ super(ComponentNames.NetworkUpdateable);
+ }
+}
diff --git a/engine/components/Sprite.ts b/engine/components/Sprite.ts
index bdb4982..36b944e 100644
--- a/engine/components/Sprite.ts
+++ b/engine/components/Sprite.ts
@@ -1,5 +1,5 @@
-import { Component, ComponentNames } from ".";
-import type { Dimension2D, DrawArgs, Coord2D } from "../interfaces";
+import { Component, ComponentNames } from '.';
+import type { Dimension2D, DrawArgs, Coord2D } from '../interfaces';
export class Sprite extends Component {
private sheet: HTMLImageElement;
@@ -17,7 +17,7 @@ export class Sprite extends Component {
spriteImgPos: Coord2D,
spriteImgDimensions: Dimension2D,
msPerFrame: number,
- numFrames: number,
+ numFrames: number
) {
super(ComponentNames.Sprite);
@@ -56,12 +56,12 @@ export class Sprite extends Component {
ctx.drawImage(
this.sheet,
...this.getSpriteArgs(),
- ...this.getDrawArgs(drawArgs),
+ ...this.getDrawArgs(drawArgs)
);
if (tint) {
ctx.globalAlpha = 0.5;
- ctx.globalCompositeOperation = "source-atop";
+ ctx.globalCompositeOperation = 'source-atop';
ctx.fillStyle = tint;
ctx.fillRect(...this.getDrawArgs(drawArgs));
}
@@ -74,19 +74,23 @@ export class Sprite extends Component {
this.spriteImgPos.x + this.currentFrame * this.spriteImgDimensions.width,
this.spriteImgPos.y,
this.spriteImgDimensions.width,
- this.spriteImgDimensions.height,
+ this.spriteImgDimensions.height
];
}
private getDrawArgs({
center,
- dimension,
+ 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,
+ dimension.height
];
}
+
+ public getSpriteDimensions() {
+ return this.spriteImgDimensions;
+ }
}
diff --git a/engine/components/TopCollidable.ts b/engine/components/TopCollidable.ts
index 7fb147d..05ce484 100644
--- a/engine/components/TopCollidable.ts
+++ b/engine/components/TopCollidable.ts
@@ -1,4 +1,4 @@
-import { Component, ComponentNames } from ".";
+import { Component, ComponentNames } from '.';
export class TopCollidable extends Component {
constructor() {
diff --git a/engine/components/Velocity.ts b/engine/components/Velocity.ts
index 068d8cd..0071891 100644
--- a/engine/components/Velocity.ts
+++ b/engine/components/Velocity.ts
@@ -1,23 +1,23 @@
-import type { Velocity2D } from "../interfaces";
-import { Component } from "./Component";
-import { ComponentNames } from ".";
+import type { Velocity2D } from '../interfaces';
+import { Component } from './Component';
+import { ComponentNames } from '.';
export class Velocity extends Component {
- public dCartesian: Velocity2D;
- public dTheta: number;
+ public velocity: Velocity2D;
- constructor(dCartesian: Velocity2D = { dx: 0, dy: 0 }, dTheta: number = 0) {
+ constructor(
+ velocity: Velocity2D = { dCartesian: { dx: 0, dy: 0 }, dTheta: 0 }
+ ) {
super(ComponentNames.Velocity);
- this.dCartesian = dCartesian;
- this.dTheta = dTheta;
+ this.velocity = velocity;
}
- public add(velocity?: Velocity) {
+ public add(velocity?: Velocity2D) {
if (velocity) {
- this.dCartesian.dx += velocity.dCartesian.dx;
- this.dCartesian.dy += velocity.dCartesian.dy;
- this.dTheta += velocity.dTheta;
+ this.velocity.dCartesian.dx += velocity.dCartesian.dx;
+ this.velocity.dCartesian.dy += velocity.dCartesian.dy;
+ this.velocity.dTheta += velocity.dTheta;
}
}
}
diff --git a/engine/components/WallBounded.ts b/engine/components/WallBounded.ts
index 5f787e1..c1745a8 100644
--- a/engine/components/WallBounded.ts
+++ b/engine/components/WallBounded.ts
@@ -1,4 +1,4 @@
-import { Component, ComponentNames } from ".";
+import { Component, ComponentNames } from '.';
export class WallBounded extends Component {
constructor() {
diff --git a/engine/components/index.ts b/engine/components/index.ts
index 67f1259..6d7c1e5 100644
--- a/engine/components/index.ts
+++ b/engine/components/index.ts
@@ -1,15 +1,16 @@
-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";
+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 './NetworkUpdateable';
+export * from './names';
diff --git a/engine/components/names.ts b/engine/components/names.ts
index e2ee3d3..97b4edd 100644
--- a/engine/components/names.ts
+++ b/engine/components/names.ts
@@ -1,15 +1,16 @@
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";
+ 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';
+ export const NetworkUpdateable = 'NetworkUpdateable';
}