Skip to content

Commit

Permalink
Remove underscores from the Vector2 operations' names
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Jul 1, 2024
1 parent 9459db9 commit 04585df
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
60 changes: 30 additions & 30 deletions game.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 30 additions & 30 deletions game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ export class Vector2 {
this.y = scalar;
return this;
}
add_(that: Vector2): this {
add(that: Vector2): this {
this.x += that.x;
this.y += that.y;
return this;
}
sub_(that: Vector2): this {
sub(that: Vector2): this {
this.x -= that.x;
this.y -= that.y;
return this;
}
div_(that: Vector2): this {
div(that: Vector2): this {
this.x /= that.x;
this.y /= that.y;
return this;
}
mul_(that: Vector2): this {
mul(that: Vector2): this {
this.x *= that.x;
this.y *= that.y;
return this;
Expand All @@ -106,16 +106,16 @@ export class Vector2 {
length(): number {
return Math.sqrt(this.sqrLength());
}
scale_(value: number): this {
scale(value: number): this {
this.x *= value;
this.y *= value;
return this;
}
norm_(): this {
norm(): this {
const l = this.length();
return l === 0 ? this : this.scale_(1/l);
return l === 0 ? this : this.scale(1/l);
}
rot90_(): this {
rot90(): this {
const oldX = this.x;
this.x = -this.y;
this.y = oldX;
Expand All @@ -126,15 +126,15 @@ export class Vector2 {
const dy = that.y - this.y;
return dx*dx + dy*dy;
}
lerp_(that: Vector2, t: number): this {
lerp(that: Vector2, t: number): this {
this.x += (that.x - this.x)*t;
this.y += (that.y - this.y)*t;
return this;
}
dot(that: Vector2): number {
return this.x*that.x + this.y*that.y;
}
map_(f: (x: number) => number): this {
map(f: (x: number) => number): this {
this.x = f(this.x);
this.y = f(this.y);
return this;
Expand Down Expand Up @@ -326,10 +326,10 @@ export function createPlayer(position: Vector2, direction: number): Player {

function playerFovRange(player: Player): [Vector2, Vector2] {
const l = Math.tan(FOV*0.5)*NEAR_CLIPPING_PLANE;
const p = player.position.clone().add_(Vector2.angle(player.direction).scale_(NEAR_CLIPPING_PLANE));
const wing = p.clone().sub_(player.position).rot90_().norm_().scale_(l);
const p1 = p.clone().sub_(wing);
const p2 = p.clone().add_(wing);
const p = player.position.clone().add(Vector2.angle(player.direction).scale(NEAR_CLIPPING_PLANE));
const wing = p.clone().sub(player.position).rot90().norm().scale(l);
const p1 = p.clone().sub(wing);
const p2 = p.clone().add(wing);
return [p1, p2];
}

Expand Down Expand Up @@ -382,11 +382,11 @@ function renderMinimap(ctx: CanvasRenderingContext2D, player: Player, position:
function renderWallsToImageData(imageData: ImageData, player: Player, scene: Scene) {
const [r1, r2] = playerFovRange(player);
for (let x = 0; x < imageData.width; ++x) {
const p = castRay(scene, player.position, r1.clone().lerp_(r2, x/imageData.width));
const p = castRay(scene, player.position, r1.clone().lerp(r2, x/imageData.width));
const c = hittingCell(player.position, p);
const cell = sceneGetTile(scene, c);
if (cell instanceof RGBA) {
const v = p.clone().sub_(player.position);
const v = p.clone().sub(player.position);
const d = Vector2.angle(player.direction)
const stripHeight = imageData.height/v.dot(d);
const color = cell.brightness(v.dot(d));
Expand All @@ -399,12 +399,12 @@ function renderWallsToImageData(imageData: ImageData, player: Player, scene: Sce
imageData.data[destP + 3] = color.a*255;
}
} else if (cell instanceof ImageData) {
const v = p.clone().sub_(player.position);
const v = p.clone().sub(player.position);
const d = Vector2.angle(player.direction)
const stripHeight = imageData.height/v.dot(d);

let u = 0;
const t = p.clone().sub_(c);
const t = p.clone().sub(c);
if ((Math.abs(t.x) < EPS || Math.abs(t.x - 1) < EPS) && t.y > 0) {
u = t.y;
} else {
Expand All @@ -431,17 +431,17 @@ function renderWallsToImageData(imageData: ImageData, player: Player, scene: Sce
function renderCeilingIntoImageData(imageData: ImageData, player: Player) {
const pz = imageData.height/2;
const [p1, p2] = playerFovRange(player);
const bp = p1.clone().sub_(player.position).length();
const bp = p1.clone().sub(player.position).length();
for (let y = Math.floor(imageData.height/2); y < imageData.height; ++y) {
const sz = imageData.height - y - 1;

const ap = pz - sz;
const b = (bp/ap)*pz/NEAR_CLIPPING_PLANE;
const t1 = player.position.clone().add_(p1.clone().sub_(player.position).norm_().scale_(b));
const t2 = player.position.clone().add_(p2.clone().sub_(player.position).norm_().scale_(b));
const t1 = player.position.clone().add(p1.clone().sub(player.position).norm().scale(b));
const t2 = player.position.clone().add(p2.clone().sub(player.position).norm().scale(b));

for (let x = 0; x < imageData.width; ++x) {
const t = t1.clone().lerp_(t2, x/imageData.width);
const t = t1.clone().lerp(t2, x/imageData.width);
const tile = sceneGetCeiling(t);
if (tile instanceof RGBA) {
const color = tile.brightness(Math.sqrt(player.position.sqrDistanceTo(t)));
Expand All @@ -458,17 +458,17 @@ function renderCeilingIntoImageData(imageData: ImageData, player: Player) {
function renderFloorIntoImageData(imageData: ImageData, player: Player) {
const pz = imageData.height/2;
const [p1, p2] = playerFovRange(player);
const bp = p1.clone().sub_(player.position).length();
const bp = p1.clone().sub(player.position).length();
for (let y = Math.floor(imageData.height/2); y < imageData.height; ++y) {
const sz = imageData.height - y - 1;

const ap = pz - sz;
const b = (bp/ap)*pz/NEAR_CLIPPING_PLANE;
const t1 = player.position.clone().add_(p1.clone().sub_(player.position).norm_().scale_(b));
const t2 = player.position.clone().add_(p2.clone().sub_(player.position).norm_().scale_(b));
const t1 = player.position.clone().add(p1.clone().sub(player.position).norm().scale(b));
const t2 = player.position.clone().add(p2.clone().sub(player.position).norm().scale(b));

for (let x = 0; x < imageData.width; ++x) {
const t = t1.clone().lerp_(t2, x/imageData.width);
const t = t1.clone().lerp(t2, x/imageData.width);
const tile = sceneGetFloor(t);
if (tile instanceof RGBA) {
const color = tile.brightness(Math.sqrt(player.position.sqrDistanceTo(t)));
Expand All @@ -486,10 +486,10 @@ export function renderGameIntoImageData(ctx: CanvasRenderingContext2D, backCtx:
player.velocity.setScalar(0);
let angularVelocity = 0.0;
if (player.movingForward) {
player.velocity.add_(Vector2.angle(player.direction).scale_(PLAYER_SPEED))
player.velocity.add(Vector2.angle(player.direction).scale(PLAYER_SPEED))
}
if (player.movingBackward) {
player.velocity.sub_(Vector2.angle(player.direction).scale_(PLAYER_SPEED))
player.velocity.sub(Vector2.angle(player.direction).scale(PLAYER_SPEED))
}
if (player.turningLeft) {
angularVelocity -= Math.PI;
Expand All @@ -507,9 +507,9 @@ export function renderGameIntoImageData(ctx: CanvasRenderingContext2D, backCtx:
player.position.y = ny;
}

const minimapPosition = canvasSize(ctx).scale_(0.03);
const minimapPosition = canvasSize(ctx).scale(0.03);
const cellSize = ctx.canvas.width*0.03;
const minimapSize = sceneSize(scene).scale_(cellSize);
const minimapSize = sceneSize(scene).scale(cellSize);

backImageData.data.fill(255);
renderFloorIntoImageData(backImageData, player);
Expand Down
2 changes: 1 addition & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function loadImageData(url: string): Promise<ImageData> {
]);

const player = game.createPlayer(
game.sceneSize(scene).clone().mul_(new game.Vector2(0.63, 0.63)),
game.sceneSize(scene).scale(0.63),
Math.PI*1.25);

const isDev = window.location.hostname === "localhost";
Expand Down

0 comments on commit 04585df

Please sign in to comment.