Skip to content

Commit

Permalink
golfing
Browse files Browse the repository at this point in the history
  • Loading branch information
SalvatorePreviti committed Oct 17, 2022
1 parent 7327f82 commit 96ff2f2
Show file tree
Hide file tree
Showing 20 changed files with 2,165 additions and 2,236 deletions.
15 changes: 7 additions & 8 deletions app/game/models-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ import { devModelsAdd } from "../dev-tools/dev-models";

export let currentModelMmatrix: DOMMatrix;

let currentModelPolygons: Polygon[];

export const meshAdd: (
polygons: Polygon<Readonly<Vec3Optional>>[],
transform?: DOMMatrixReadOnly,
color?: number | undefined,
) => void = (polygons, transform = identity, color) =>
currentModelPolygons.push(...polygons_transform(polygons, transform, color));
let currentModelPolygons: Polygon[][];

export const newModel = (name: string) => {
if (DEBUG) {
Expand All @@ -31,6 +24,12 @@ export const newModel = (name: string) => {
allModels.push({ $matrix: (currentModelMmatrix = new DOMMatrix()), $polygon: (currentModelPolygons = []) });
};

export const meshAdd = (
polygons: Polygon<Readonly<Vec3Optional>>[],
transform: DOMMatrixReadOnly = identity,
color?: number | undefined,
) => currentModelPolygons.push(polygons_transform(polygons, transform, color));

const SOUL_SENSITIVITY_RADIUS = 1.6;

export const newSoul = (transform: DOMMatrixReadOnly, ...walkingPath: Circle[]) => {
Expand Down
2 changes: 1 addition & 1 deletion app/game/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Model {
readonly $matrix: DOMMatrix;

/** The temporary list of polygons, will be cleared out when building triangles */
$polygon: Polygon[] | null;
$polygon: Polygon[][] | null;

$vertexBegin?: number;
$vertexEnd?: number;
Expand Down
84 changes: 22 additions & 62 deletions app/game/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ export const player_init = NO_INLINE(() => {
let player_position_global_y: number;
let player_position_global_z: number;

const interpolate_with_hysteresis = (previous: number, desired: number, hysteresis: number, speed: number) =>
lerp(previous, desired, boot || (clamp(abs(desired - previous) ** 0.5 - hysteresis) + 1 / 7) * damp(speed * 1.5));

const loadReferenceMatrix = () =>
matrixCopy(
(player_respawned
Expand Down Expand Up @@ -107,16 +104,17 @@ export const player_init = NO_INLINE(() => {
player_position_final.z = matrixTransformPoint.z;
};

const movePlayer = (mx: number, my: number, mz: number) => {
const movePlayer = NO_INLINE((mx: number, my: number, mz: number) => {
loadReferenceMatrix().invertSelf();
matrixTransformPoint(mx, my, mz, 0);
player_position_global_x += matrixTransformPoint.x;
player_position_global_y += my;
player_position_global_z += matrixTransformPoint.z;
updatePlayerPositionFinal();
};
});

const doCollisions = NO_INLINE(() => {
const LEGS_ROWS = 36;
let modelACount = 0;
let modelB = 0;
let modelBCount = 0;
Expand All @@ -125,7 +123,7 @@ export const player_init = NO_INLINE(() => {

// vertical collisions

for (let y = 0; y < 36; ++y) {
for (let y = 0; y < LEGS_ROWS; ++y) {
for (let x = 24 * 4, yindex = y * (COLLISION_TEXTURE_SIZE * 4); x < (COLLISION_TEXTURE_SIZE - 24) * 4; x += 4) {
for (let k = 0; k < 2; ++k) {
const v = collision_buffer[yindex + x + k]!;
Expand All @@ -152,40 +150,36 @@ export const player_init = NO_INLINE(() => {

let movX = 0;
let movZ = 0;
for (let y = 36; y < COLLISION_TEXTURE_SIZE; ++y) {
for (let y = LEGS_ROWS, index = COLLISION_TEXTURE_SIZE * LEGS_ROWS * 4; y < COLLISION_TEXTURE_SIZE; ++y) {
let left = 0;
let right = 0;
let front = 0;
let back = 0;
for (let tx = 0, yindex = COLLISION_TEXTURE_SIZE * 4 * y; tx < COLLISION_TEXTURE_SIZE; ++tx) {
const index = yindex + tx * 4;

let v = collision_buffer[index]!;

if (tx < COLLISION_TEXTURE_SIZE / 2) {
for (let x = 0; x < COLLISION_TEXTURE_SIZE; ++x) {
let v = collision_buffer[index++]!;
if (x < COLLISION_TEXTURE_SIZE / 2) {
if (v > left) {
left = v;
}
} else if (v > right) {
right = v;
}

v = collision_buffer[index + 2]!;
if (v > front) {
front = v;
}

v = collision_buffer[index + 1]!;

if (tx > COLLISION_TEXTURE_SIZE / 2) {
v = collision_buffer[index++]!;
if (x > COLLISION_TEXTURE_SIZE / 2) {
if (v > left) {
left = v;
}
} else if (v > right) {
right = v;
}

v = collision_buffer[index + 3]!;
v = collision_buffer[index++]!;
if (v > front) {
front = v;
}

v = collision_buffer[index++]!;
if (v > back) {
back = v;
}
Expand All @@ -207,6 +201,9 @@ export const player_init = NO_INLINE(() => {
movePlayer(movX / 255, movY / 255, movZ / 255);
});

const interpolate_with_hysteresis = (previous: number, desired: number, hysteresis: number, speed: number) =>
lerp(previous, desired, boot || (clamp(abs(desired - previous) ** 0.5 - hysteresis) + 1 / 7) * damp(speed * 1.5));

player_update = () => {
updatePlayerPositionFinal(currentModelId);

Expand All @@ -215,35 +212,6 @@ export const player_init = NO_INLINE(() => {
// This is here because we want to read the collision results as late as possible, to give the GPU time to finish
cgl.readPixels(0, 0, COLLISION_TEXTURE_SIZE, COLLISION_TEXTURE_SIZE, gl.RGBA, gl.UNSIGNED_BYTE, collision_buffer);

// if (DEBUG) {
// const debugCanvas = document.getElementById("debug-canvas") as HTMLCanvasElement;

// const buf = new Uint8ClampedArray(COLLISION_TEXTURE_SIZE * COLLISION_TEXTURE_SIZE * 4);

// if (debugCanvas) {
// for (let y = 0; y < COLLISION_TEXTURE_SIZE; ++y) {
// for (let x = 0; x < COLLISION_TEXTURE_SIZE; ++x) {
// const i = ((COLLISION_TEXTURE_SIZE - y) * COLLISION_TEXTURE_SIZE + x) * 4;
// const r = collision_buffer[i]!;
// const g = collision_buffer[i + 1]!;
// const b = collision_buffer[i + 2]!;

// buf[(y * COLLISION_TEXTURE_SIZE + x) * 4] = r;
// buf[(y * COLLISION_TEXTURE_SIZE + x) * 4 + 1] = g * 30;
// buf[(y * COLLISION_TEXTURE_SIZE + x) * 4 + 2] = b ? 200 : 0;
// buf[(y * COLLISION_TEXTURE_SIZE + x) * 4 + 3] = 255;
// }
// }

// const imgdata = new ImageData(buf, COLLISION_TEXTURE_SIZE, COLLISION_TEXTURE_SIZE);

// if (!(window as any).debug2dctx) {
// (window as any).debug2dctx = debugCanvas.getContext("2d")!;
// }
// (window as any).debug2dctx.putImageData(imgdata, 0, 0, 0, 0, COLLISION_TEXTURE_SIZE, COLLISION_TEXTURE_SIZE);
// }
// }

// ------- process collision renderBuffer -------

doCollisions();
Expand All @@ -261,14 +229,9 @@ export const player_init = NO_INLINE(() => {
player_position_global_y = matrixTransformPoint.y;
player_position_global_z = matrixTransformPoint.z;

if (player_respawned) {
player_respawned = currentModelId ? 0 : 1;
}
}

if (player_position_final.y < (player_position_final.x < -20 || player_position_final.z < 109 ? -25 : -9)) {
// Player fell in lava
player_respawned = 2;
player_respawned &&= currentModelId ? 0 : 1;
} else if (player_position_final.y < (player_position_final.x < -20 || player_position_final.z < 109 ? -25 : -9)) {
player_respawned = 2; // Player fell in lava
}

// Special handling for the second boat LEVER_SECOND_BOAT - the boat must be on the side of the map the player is
Expand Down Expand Up @@ -408,11 +371,8 @@ export const player_init = NO_INLINE(() => {
movAngle = player_first_person ? (180 + camera_rotation.y) * DEG_TO_RAD : 0;

movePlayer(
// x
gameTimeDelta * (player_fly_velocity_x + (Math.cos(movAngle) * strafe - Math.sin(movAngle) * forward)),
// y
gameTimeDelta * -player_gravity,
// z
gameTimeDelta * (player_fly_velocity_z + (Math.sin(movAngle) * strafe + Math.cos(movAngle) * forward)),
);
};
Expand Down
24 changes: 13 additions & 11 deletions app/game/triangle-buffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ export const initTriangleBuffers = () => {

_vertexFloats[3] = index > MODEL_ID_SOUL_COLLISION - 1 ? -MODELS_WITH_FULL_TRANSFORM : index;

for (polygon of model.$polygon!) {
const { x, y, z } = plane_fromPolygon(polygon);
_vertexInts[4] = polygon.$color! | 0;
_vertexInts[5] = x * 32767;
_vertexInts[6] = y * 32767;
_vertexInts[7] = z * 32767;
for (let i = 2, a = getVertex(0), b = getVertex(1); i < polygon.length; ++i) {
_triangleIndices.push(a, b, (b = getVertex(i)));
for (const grp of model.$polygon!) {
for (polygon of grp) {
const { x, y, z } = plane_fromPolygon(polygon);
_vertexInts[4] = polygon.$color! | 0;
_vertexInts[5] = x * 32767;
_vertexInts[6] = y * 32767;
_vertexInts[7] = z * 32767;
for (let i = 2, a = getVertex(0), b = getVertex(1); i < polygon.length; ++i) {
_triangleIndices.push(a, b, (b = getVertex(i)));
}
}
}

Expand All @@ -67,6 +69,9 @@ export const initTriangleBuffers = () => {
});

[gl, cgl].map((xgl) => {
xgl.bindBuffer(xgl.ELEMENT_ARRAY_BUFFER, xgl.createBuffer());
xgl.bufferData(xgl.ELEMENT_ARRAY_BUFFER, new Uint16Array(_triangleIndices), xgl.STATIC_DRAW);

xgl.bindBuffer(xgl.ARRAY_BUFFER, xgl.createBuffer());
xgl.bufferData(xgl.ARRAY_BUFFER, new Float32Array(_vertexPositions), xgl.STATIC_DRAW);
xgl.vertexAttribPointer(0, 4, xgl.FLOAT, false, 0, 0);
Expand All @@ -79,9 +84,6 @@ export const initTriangleBuffers = () => {
xgl.bufferData(xgl.ARRAY_BUFFER, new Uint32Array(_vertexColors), xgl.STATIC_DRAW);
xgl.vertexAttribPointer(2, 4, xgl.UNSIGNED_BYTE, true, 0, 0);

xgl.bindBuffer(xgl.ELEMENT_ARRAY_BUFFER, xgl.createBuffer());
xgl.bufferData(xgl.ELEMENT_ARRAY_BUFFER, new Uint16Array(_triangleIndices), xgl.STATIC_DRAW);

xgl.enableVertexAttribArray(0);
xgl.enableVertexAttribArray(1);
xgl.enableVertexAttribArray(2);
Expand Down
Loading

0 comments on commit 96ff2f2

Please sign in to comment.