From 04585df0fee169bc1c3d87a0117c86b99a2ba2c6 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 2 Jul 2024 04:41:02 +0700 Subject: [PATCH] Remove underscores from the Vector2 operations' names --- game.js | 60 ++++++++++++++++++++++++++++---------------------------- game.ts | 60 ++++++++++++++++++++++++++++---------------------------- index.js | 2 +- index.ts | 2 +- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/game.js b/game.js index 45a1c5e..0225b57 100644 --- a/game.js +++ b/game.js @@ -78,22 +78,22 @@ export class Vector2 { this.y = scalar; return this; } - add_(that) { + add(that) { this.x += that.x; this.y += that.y; return this; } - sub_(that) { + sub(that) { this.x -= that.x; this.y -= that.y; return this; } - div_(that) { + div(that) { this.x /= that.x; this.y /= that.y; return this; } - mul_(that) { + mul(that) { this.x *= that.x; this.y *= that.y; return this; @@ -104,16 +104,16 @@ export class Vector2 { length() { return Math.sqrt(this.sqrLength()); } - scale_(value) { + scale(value) { this.x *= value; this.y *= value; return this; } - norm_() { + norm() { const l = this.length(); - return l === 0 ? this : this.scale_(1 / l); + return l === 0 ? this : this.scale(1 / l); } - rot90_() { + rot90() { const oldX = this.x; this.x = -this.y; this.y = oldX; @@ -124,7 +124,7 @@ export class Vector2 { const dy = that.y - this.y; return dx * dx + dy * dy; } - lerp_(that, t) { + lerp(that, t) { this.x += (that.x - this.x) * t; this.y += (that.y - this.y) * t; return this; @@ -132,7 +132,7 @@ export class Vector2 { dot(that) { return this.x * that.x + this.y * that.y; } - map_(f) { + map(f) { this.x = f(this.x); this.y = f(this.y); return this; @@ -291,10 +291,10 @@ export function createPlayer(position, direction) { } function playerFovRange(player) { 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]; } function renderMinimap(ctx, player, position, size, scene) { @@ -336,11 +336,11 @@ function renderMinimap(ctx, player, position, size, scene) { function renderWallsToImageData(imageData, player, 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)); @@ -354,11 +354,11 @@ function renderWallsToImageData(imageData, player, scene) { } } 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; } @@ -384,15 +384,15 @@ function renderWallsToImageData(imageData, player, scene) { function renderCeilingIntoImageData(imageData, 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))); @@ -408,15 +408,15 @@ function renderCeilingIntoImageData(imageData, player) { function renderFloorIntoImageData(imageData, 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))); @@ -433,10 +433,10 @@ export function renderGameIntoImageData(ctx, backCtx, backImageData, deltaTime, 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; @@ -453,9 +453,9 @@ export function renderGameIntoImageData(ctx, backCtx, backImageData, deltaTime, if (sceneCanRectangleFitHere(scene, new Vector2(player.position.x, ny), Vector2.scalar(PLAYER_SIZE))) { 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); renderCeilingIntoImageData(backImageData, player); diff --git a/game.ts b/game.ts index c952042..3415fcf 100644 --- a/game.ts +++ b/game.ts @@ -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; @@ -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; @@ -126,7 +126,7 @@ 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; @@ -134,7 +134,7 @@ export class Vector2 { 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; @@ -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]; } @@ -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)); @@ -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 { @@ -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))); @@ -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))); @@ -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; @@ -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); diff --git a/index.js b/index.js index 705aeb7..464c1c6 100644 --- a/index.js +++ b/index.js @@ -47,7 +47,7 @@ async function loadImageData(url) { [null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null], ]); - const player = game.createPlayer(game.sceneSize(scene).clone().mul_(new game.Vector2(0.63, 0.63)), Math.PI * 1.25); + const player = game.createPlayer(game.sceneSize(scene).scale(0.63), Math.PI * 1.25); const isDev = window.location.hostname === "localhost"; if (isDev) { const ws = new WebSocket("ws://localhost:6970"); diff --git a/index.ts b/index.ts index 1e035ae..f80ce11 100644 --- a/index.ts +++ b/index.ts @@ -50,7 +50,7 @@ async function loadImageData(url: string): Promise { ]); 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";