Refactor: Remove KeyPointGeometry module and update LockpickingMinigamePhaser to use KeyGeometry

This commit is contained in:
Z. Cliffe Schreuders
2025-10-27 17:49:25 +00:00
parent 741ec55864
commit 5d4a44fc70
3 changed files with 203 additions and 228 deletions

View File

@@ -78,7 +78,7 @@ export class KeyGeometry {
if (i === 0) {
// First section: from left edge to first cut
const firstCutStartX = cutX - cutWidth/2;
this.parent.keyPointGeom.addTriangularPeakToPoints(points, currentX, keyBladeBaseY, firstCutStartX, keyBladeBaseY, 0, cutDepth);
this.addTriangularPeakToPoints(points, currentX, keyBladeBaseY, firstCutStartX, keyBladeBaseY, 0, cutDepth);
currentX = firstCutStartX;
}
@@ -96,7 +96,7 @@ export class KeyGeometry {
const nextPinX = 100 + margin + (i + 1) * pinSpacing;
const nextCutX = keyBladeStartX + (nextPinX - 100);
const nextCutStartX = nextCutX - cutWidth/2;
this.parent.keyPointGeom.addTriangularPeakToPoints(points, currentX, keyBladeBaseY, nextCutStartX, keyBladeBaseY, cutDepth, nextCutDepth);
this.addTriangularPeakToPoints(points, currentX, keyBladeBaseY, nextCutStartX, keyBladeBaseY, cutDepth, nextCutDepth);
currentX = nextCutStartX;
} else if (i === this.parent.pinCount - 1) {
// Last section: pointed tip
@@ -104,8 +104,8 @@ export class KeyGeometry {
const tipExtension = 12;
const tipEndX = keyRightEdge + tipExtension;
const peakX = currentX + (keyRightEdge - currentX) * 0.3;
this.parent.keyPointGeom.addTriangularPeakToPoints(points, currentX, keyBladeBaseY, peakX, keyBladeBaseY, cutDepth, 0);
this.parent.keyPointGeom.addPointedTipToPoints(points, peakX, keyBladeBaseY, tipEndX, bladeHeight);
this.addTriangularPeakToPoints(points, currentX, keyBladeBaseY, peakX, keyBladeBaseY, cutDepth, 0);
this.addPointedTipToPoints(points, peakX, keyBladeBaseY, tipEndX, bladeHeight);
currentX = tipEndX;
}
}
@@ -148,5 +148,203 @@ export class KeyGeometry {
// Method moved to KeyOperations module - delegate to it
return this.parent.keyOps.getKeySurfaceHeightAtPosition(pinX, keyBladeStartX);
}
addTriangularPeakToPoints(points, startX, startY, endX, endY, startCutDepth, endCutDepth) {
// Add triangular peak points (same logic as addTriangularPeakToPath)
const width = Math.abs(endX - startX);
const stepSize = 4;
const steps = Math.max(1, Math.floor(width / stepSize));
const halfSteps = Math.floor(steps / 2);
for (let i = 0; i <= steps; i++) {
const progress = i / steps;
const x = startX + (endX - startX) * progress;
let y;
if (i <= halfSteps) {
const upProgress = i / halfSteps;
y = startY + startCutDepth - (startCutDepth * upProgress);
} else {
const downProgress = (i - halfSteps) / halfSteps;
y = startY + (endCutDepth * downProgress);
}
points.push({ x: x, y: y });
}
}
addPointedTipToPoints(points, startX, startY, endX, bladeHeight) {
// Add pointed tip points (same logic as addPointedTipToPath)
const width = Math.abs(endX - startX);
const stepSize = 4;
const steps = Math.max(1, Math.floor(width / stepSize));
const tipX = endX;
const tipY = startY + (bladeHeight / 2);
// From top to tip
const topToTipSteps = Math.max(1, Math.floor(width / stepSize));
for (let i = 0; i <= topToTipSteps; i++) {
const progress = i / topToTipSteps;
const x = startX + (width * progress);
const y = startY + (bladeHeight / 2 * progress);
points.push({ x: x, y: y });
}
// From tip to bottom
const tipToBottomSteps = Math.max(1, Math.floor(width / stepSize));
for (let i = 0; i <= tipToBottomSteps; i++) {
const progress = i / tipToBottomSteps;
const x = tipX - (width * progress);
const y = tipY + (bladeHeight / 2 * progress);
points.push({ x: x, y: y });
}
}
getTriangularSectionHeightAtX(relativeX, bladeWidth, bladeHeight) {
// Calculate height of triangular sections at a given X position
// Creates peaks that go up to blade top between cuts
const cutWidth = 24;
const pinSpacing = 400 / (this.parent.pinCount + 1);
const margin = pinSpacing * 0.75;
// Check triangular sections between cuts
for (let i = 0; i < this.parent.pinCount - 1; i++) {
const cut1X = margin + i * pinSpacing;
const cut2X = margin + (i + 1) * pinSpacing;
const cut1EndX = cut1X + cutWidth/2;
const cut2StartX = cut2X - cutWidth/2;
// Check if we're in the triangular section between these cuts
if (relativeX >= cut1EndX && relativeX <= cut2StartX) {
const distanceFromCut1 = relativeX - cut1EndX;
const triangularWidth = cut2StartX - cut1EndX;
const progress = distanceFromCut1 / triangularWidth;
// Get cut depths for both cuts
const cut1Depth = this.parent.keyData.cuts[i] || 0;
const cut2Depth = this.parent.keyData.cuts[i + 1] || 0;
// Create a peak: go up from cut1 to blade top, then down to cut2
const halfWidth = triangularWidth / 2;
if (distanceFromCut1 <= halfWidth) {
// First half: slope up from cut1 to blade top
const upProgress = distanceFromCut1 / halfWidth;
return cut1Depth + (bladeHeight - cut1Depth) * upProgress;
} else {
// Second half: slope down from blade top to cut2
const downProgress = (distanceFromCut1 - halfWidth) / halfWidth;
return bladeHeight - (bladeHeight - cut2Depth) * downProgress;
}
}
}
// Check triangular section from left edge to first cut
const firstCutX = margin;
const firstCutStartX = firstCutX - cutWidth/2;
if (relativeX >= 0 && relativeX < firstCutStartX) {
const progress = relativeX / firstCutStartX;
const firstCutDepth = this.parent.keyData.cuts[0] || 0;
// Create a peak: slope up from base to blade top, then down to first cut
const halfWidth = firstCutStartX / 2;
if (relativeX <= halfWidth) {
// First half: slope up from base (0) to blade top
const upProgress = relativeX / halfWidth;
return bladeHeight * upProgress;
} else {
// Second half: slope down from blade top to first cut depth
const downProgress = (relativeX - halfWidth) / halfWidth;
return bladeHeight - (bladeHeight - firstCutDepth) * downProgress;
}
}
// Check triangular section from last cut to right edge
const lastCutX = margin + (this.parent.pinCount - 1) * pinSpacing;
const lastCutEndX = lastCutX + cutWidth/2;
if (relativeX > lastCutEndX && relativeX <= bladeWidth) {
const triangularWidth = bladeWidth - lastCutEndX;
const distanceFromLastCut = relativeX - lastCutEndX;
const progress = distanceFromLastCut / triangularWidth;
const lastCutDepth = this.parent.keyData.cuts[this.parent.pinCount - 1] || 0;
// Create a peak: slope up from last cut to blade top, then down to base
const halfWidth = triangularWidth / 2;
if (distanceFromLastCut <= halfWidth) {
// First half: slope up from last cut depth to blade top
const upProgress = distanceFromLastCut / halfWidth;
return lastCutDepth + (bladeHeight - lastCutDepth) * upProgress;
} else {
// Second half: slope down from blade top to base (0)
const downProgress = (distanceFromLastCut - halfWidth) / halfWidth;
return bladeHeight * (1 - downProgress);
}
}
return 0; // Not in a triangular section
}
getTriangularSectionHeightAsKeyMoves(pinRelativeToKeyLeadingEdge, bladeWidth, bladeHeight) {
// Calculate triangular section height as the key moves underneath the pin
// This creates the sloping effect as pins follow the key's surface
const cutWidth = 24;
const pinSpacing = 400 / (this.parent.pinCount + 1);
const margin = pinSpacing * 0.75;
// Check triangular section from left edge to first cut
const firstCutX = margin;
const firstCutStartX = firstCutX - cutWidth/2;
if (pinRelativeToKeyLeadingEdge >= 0 && pinRelativeToKeyLeadingEdge < firstCutStartX) {
// Pin is in the triangular section from left edge to first cut
const progress = pinRelativeToKeyLeadingEdge / firstCutStartX;
const firstCutDepth = this.parent.keyData.cuts[0] || 0;
// Start from base level (0) and slope up to first cut depth
return Math.max(0, firstCutDepth * progress); // Ensure we never go below base level
}
// Check triangular sections between cuts
for (let i = 0; i < this.parent.pinCount - 1; i++) {
const cut1X = margin + i * pinSpacing;
const cut2X = margin + (i + 1) * pinSpacing;
const cut1EndX = cut1X + cutWidth/2;
const cut2StartX = cut2X - cutWidth/2;
if (pinRelativeToKeyLeadingEdge >= cut1EndX && pinRelativeToKeyLeadingEdge <= cut2StartX) {
// Pin is in triangular section between these cuts
const distanceFromCut1 = pinRelativeToKeyLeadingEdge - cut1EndX;
const triangularWidth = cut2StartX - cut1EndX;
const progress = distanceFromCut1 / triangularWidth;
// Get cut depths for both cuts
const cut1Depth = this.parent.keyData.cuts[i] || 0;
const cut2Depth = this.parent.keyData.cuts[i + 1] || 0;
// Interpolate between cut depths (slope from cut1 to cut2)
return cut1Depth + (cut2Depth - cut1Depth) * progress;
}
}
// Check triangular section from last cut to right edge
const lastCutX = margin + (this.parent.pinCount - 1) * pinSpacing;
const lastCutEndX = lastCutX + cutWidth/2;
if (pinRelativeToKeyLeadingEdge >= lastCutEndX && pinRelativeToKeyLeadingEdge <= bladeWidth) {
// Pin is in triangular section from last cut to right edge
const distanceFromLastCut = pinRelativeToKeyLeadingEdge - lastCutEndX;
const triangularWidth = bladeWidth - lastCutEndX;
const progress = distanceFromLastCut / triangularWidth;
const lastCutDepth = this.parent.keyData.cuts[this.parent.pinCount - 1] || 0;
return lastCutDepth * (1 - progress); // Slope down from last cut depth to 0
}
return 0; // Not in a triangular section
}
}

View File

@@ -1,219 +0,0 @@
/**
* KeyPointGeometry
*
* Extracted from lockpicking-game-phaser.js
* Instantiate with: new KeyPointGeometry(this)
*
* All 'this' references replaced with 'this.parent' to access parent instance state:
* - this.parent.pins (array of pin objects)
* - this.parent.scene (Phaser scene)
* - this.parent.lockId (lock identifier)
* - this.parent.lockState (lock state object)
* etc.
*/
export class KeyPointGeometry {
constructor(parent) {
this.parent = parent;
}
addTriangularPeakToPoints(points, startX, startY, endX, endY, startCutDepth, endCutDepth) {
// Add triangular peak points (same logic as addTriangularPeakToPath)
const width = Math.abs(endX - startX);
const stepSize = 4;
const steps = Math.max(1, Math.floor(width / stepSize));
const halfSteps = Math.floor(steps / 2);
for (let i = 0; i <= steps; i++) {
const progress = i / steps;
const x = startX + (endX - startX) * progress;
let y;
if (i <= halfSteps) {
const upProgress = i / halfSteps;
y = startY + startCutDepth - (startCutDepth * upProgress);
} else {
const downProgress = (i - halfSteps) / halfSteps;
y = startY + (endCutDepth * downProgress);
}
points.push({ x: x, y: y });
}
}
addPointedTipToPoints(points, startX, startY, endX, bladeHeight) {
// Add pointed tip points (same logic as addPointedTipToPath)
const width = Math.abs(endX - startX);
const stepSize = 4;
const steps = Math.max(1, Math.floor(width / stepSize));
const tipX = endX;
const tipY = startY + (bladeHeight / 2);
// From top to tip
const topToTipSteps = Math.max(1, Math.floor(width / stepSize));
for (let i = 0; i <= topToTipSteps; i++) {
const progress = i / topToTipSteps;
const x = startX + (width * progress);
const y = startY + (bladeHeight / 2 * progress);
points.push({ x: x, y: y });
}
// From tip to bottom
const tipToBottomSteps = Math.max(1, Math.floor(width / stepSize));
for (let i = 0; i <= tipToBottomSteps; i++) {
const progress = i / tipToBottomSteps;
const x = tipX - (width * progress);
const y = tipY + (bladeHeight / 2 * progress);
points.push({ x: x, y: y });
}
}
getTriangularSectionHeightAtX(relativeX, bladeWidth, bladeHeight) {
// Calculate height of triangular sections at a given X position
// Creates peaks that go up to blade top between cuts
const cutWidth = 24;
const pinSpacing = 400 / (this.parent.pinCount + 1);
const margin = pinSpacing * 0.75;
// Check triangular sections between cuts
for (let i = 0; i < this.parent.pinCount - 1; i++) {
const cut1X = margin + i * pinSpacing;
const cut2X = margin + (i + 1) * pinSpacing;
const cut1EndX = cut1X + cutWidth/2;
const cut2StartX = cut2X - cutWidth/2;
// Check if we're in the triangular section between these cuts
if (relativeX >= cut1EndX && relativeX <= cut2StartX) {
const distanceFromCut1 = relativeX - cut1EndX;
const triangularWidth = cut2StartX - cut1EndX;
const progress = distanceFromCut1 / triangularWidth;
// Get cut depths for both cuts
const cut1Depth = this.parent.keyData.cuts[i] || 0;
const cut2Depth = this.parent.keyData.cuts[i + 1] || 0;
// Create a peak: go up from cut1 to blade top, then down to cut2
const halfWidth = triangularWidth / 2;
if (distanceFromCut1 <= halfWidth) {
// First half: slope up from cut1 to blade top
const upProgress = distanceFromCut1 / halfWidth;
return cut1Depth + (bladeHeight - cut1Depth) * upProgress;
} else {
// Second half: slope down from blade top to cut2
const downProgress = (distanceFromCut1 - halfWidth) / halfWidth;
return bladeHeight - (bladeHeight - cut2Depth) * downProgress;
}
}
}
// Check triangular section from left edge to first cut
const firstCutX = margin;
const firstCutStartX = firstCutX - cutWidth/2;
if (relativeX >= 0 && relativeX < firstCutStartX) {
const progress = relativeX / firstCutStartX;
const firstCutDepth = this.parent.keyData.cuts[0] || 0;
// Create a peak: slope up from base to blade top, then down to first cut
const halfWidth = firstCutStartX / 2;
if (relativeX <= halfWidth) {
// First half: slope up from base (0) to blade top
const upProgress = relativeX / halfWidth;
return bladeHeight * upProgress;
} else {
// Second half: slope down from blade top to first cut depth
const downProgress = (relativeX - halfWidth) / halfWidth;
return bladeHeight - (bladeHeight - firstCutDepth) * downProgress;
}
}
// Check triangular section from last cut to right edge
const lastCutX = margin + (this.parent.pinCount - 1) * pinSpacing;
const lastCutEndX = lastCutX + cutWidth/2;
if (relativeX > lastCutEndX && relativeX <= bladeWidth) {
const triangularWidth = bladeWidth - lastCutEndX;
const distanceFromLastCut = relativeX - lastCutEndX;
const progress = distanceFromLastCut / triangularWidth;
const lastCutDepth = this.parent.keyData.cuts[this.parent.pinCount - 1] || 0;
// Create a peak: slope up from last cut to blade top, then down to base
const halfWidth = triangularWidth / 2;
if (distanceFromLastCut <= halfWidth) {
// First half: slope up from last cut depth to blade top
const upProgress = distanceFromLastCut / halfWidth;
return lastCutDepth + (bladeHeight - lastCutDepth) * upProgress;
} else {
// Second half: slope down from blade top to base (0)
const downProgress = (distanceFromLastCut - halfWidth) / halfWidth;
return bladeHeight * (1 - downProgress);
}
}
return 0; // Not in a triangular section
}
getTriangularSectionHeightAsKeyMoves(pinRelativeToKeyLeadingEdge, bladeWidth, bladeHeight) {
// Calculate triangular section height as the key moves underneath the pin
// This creates the sloping effect as pins follow the key's surface
const cutWidth = 24;
const pinSpacing = 400 / (this.parent.pinCount + 1);
const margin = pinSpacing * 0.75;
// Check triangular section from left edge to first cut
const firstCutX = margin;
const firstCutStartX = firstCutX - cutWidth/2;
if (pinRelativeToKeyLeadingEdge >= 0 && pinRelativeToKeyLeadingEdge < firstCutStartX) {
// Pin is in the triangular section from left edge to first cut
const progress = pinRelativeToKeyLeadingEdge / firstCutStartX;
const firstCutDepth = this.parent.keyData.cuts[0] || 0;
// Start from base level (0) and slope up to first cut depth
return Math.max(0, firstCutDepth * progress); // Ensure we never go below base level
}
// Check triangular sections between cuts
for (let i = 0; i < this.parent.pinCount - 1; i++) {
const cut1X = margin + i * pinSpacing;
const cut2X = margin + (i + 1) * pinSpacing;
const cut1EndX = cut1X + cutWidth/2;
const cut2StartX = cut2X - cutWidth/2;
if (pinRelativeToKeyLeadingEdge >= cut1EndX && pinRelativeToKeyLeadingEdge <= cut2StartX) {
// Pin is in triangular section between these cuts
const distanceFromCut1 = pinRelativeToKeyLeadingEdge - cut1EndX;
const triangularWidth = cut2StartX - cut1EndX;
const progress = distanceFromCut1 / triangularWidth;
// Get cut depths for both cuts
const cut1Depth = this.parent.keyData.cuts[i] || 0;
const cut2Depth = this.parent.keyData.cuts[i + 1] || 0;
// Interpolate between cut depths (slope from cut1 to cut2)
return cut1Depth + (cut2Depth - cut1Depth) * progress;
}
}
// Check triangular section from last cut to right edge
const lastCutX = margin + (this.parent.pinCount - 1) * pinSpacing;
const lastCutEndX = lastCutX + cutWidth/2;
if (pinRelativeToKeyLeadingEdge >= lastCutEndX && pinRelativeToKeyLeadingEdge <= bladeWidth) {
// Pin is in triangular section from last cut to right edge
const distanceFromLastCut = pinRelativeToKeyLeadingEdge - lastCutEndX;
const triangularWidth = bladeWidth - lastCutEndX;
const progress = distanceFromLastCut / triangularWidth;
const lastCutDepth = this.parent.keyData.cuts[this.parent.pinCount - 1] || 0;
return lastCutDepth * (1 - progress); // Slope down from last cut depth to 0
}
return 0; // Not in a triangular section
}
}

View File

@@ -13,7 +13,6 @@ import { KeyInsertion } from './key-insertion.js';
import { KeyDrawing } from './key-drawing.js';
import { KeyPathDrawing } from './key-path-drawing.js';
import { KeyGeometry } from './key-geometry.js';
import { KeyPointGeometry } from './key-point-geometry.js';
import { GameUtilities } from './game-utilities.js';
// Phaser Lockpicking Minigame Scene implementation
@@ -36,6 +35,7 @@ export class LockpickingMinigamePhaser extends MinigameScene {
// Initialize global lock storage if it doesn't exist
if (!window.lockConfigurations) {
window.lockConfigurations = {};
}
// Initialize KeyDataGenerator module
this.keyDataGen = new KeyDataGenerator(this);
@@ -73,12 +73,8 @@ export class LockpickingMinigamePhaser extends MinigameScene {
// Initialize KeyGeometry module
this.keyGeom = new KeyGeometry(this);
// Initialize KeyPointGeometry module
this.keyPointGeom = new KeyPointGeometry(this);
// Initialize GameUtilities module
this.gameUtil = new GameUtilities(this);
}
// Also try to load from localStorage for persistence across sessions
if (!window.lockConfigurations[this.lockId]) {