Add KeyDataGenerator module and refactor key data generation logic in lockpicking minigame

This commit is contained in:
Z. Cliffe Schreuders
2025-10-27 15:34:04 +00:00
parent 0b9f1cb2de
commit a6d04d3e28
3 changed files with 73 additions and 48 deletions

View File

@@ -0,0 +1,64 @@
/**
* KeyDataGenerator
*
* Extracted from lockpicking-game-phaser.js
* Instantiate with: new KeyDataGenerator(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 KeyDataGenerator {
constructor(parent) {
this.parent = parent;
}
generateKeyDataFromPins() {
// Generate key cuts based on actual pin heights
// Calculate cut depths so that when key is inserted, pins align at shear line
const cuts = [];
const shearLineY = -45; // Shear line position (in pin container coordinates)
const keyBladeHeight = 110; // Key blade height (from keyConfig)
const pinContainerY = 200; // Pin container Y position (world coordinates)
for (let i = 0; i < this.parent.pinCount; i++) {
const pin = this.parent.pins[i];
const keyPinLength = pin.keyPinLength;
// Simple key cut calculation:
// The cut depth should be the key pin length minus the gap from key blade top to shear line
// Key blade is centered in keyway: keywayStartY + keywayHeight/2 = 170 + 60 = 230
// Key blade top is: 230 - keyBladeHeight/2 = 230 - 55 = 175
// Shear line is at y=155 in world coordinates (200 - 45)
const keyBladeTop_world = 175; // 170 + 60 - 55 (keywayStartY + keywayHeight/2 - keyBladeHeight/2)
const shearLine_world = 155; // 200 - 45 (pin container Y - shear line Y)
const gapFromKeyBladeTopToShearLine = keyBladeTop_world - shearLine_world; // 175 - 155 = 20
// Cut depth = key pin length - gap from key blade top to shear line
const cutDepth_needed = keyPinLength - gapFromKeyBladeTopToShearLine;
// Clamp to valid range (0 to key blade height)
const clampedCutDepth = Math.max(0, Math.min(keyBladeHeight, cutDepth_needed));
console.log(`=== KEY CUT ${i} GENERATION ===`);
console.log(` Pin properties: keyPinLength=${keyPinLength}, driverPinLength=${pin.driverPinLength}`);
console.log(` Key blade top: ${keyBladeTop_world}, shear line: ${shearLine_world}`);
console.log(` Gap from key blade top to shear line: ${gapFromKeyBladeTopToShearLine}`);
console.log(` Cut calculation: cutDepth_needed=${cutDepth_needed} (${keyPinLength} - ${gapFromKeyBladeTopToShearLine})`);
console.log(` Final cut: cutDepth=${clampedCutDepth}px (max ${keyBladeHeight}px)`);
console.log(`=====================================`);
cuts.push(clampedCutDepth);
}
this.parent.keyData = { cuts: cuts };
console.log('Generated key data from pins:', this.parent.keyData);
}
}

View File

@@ -18,7 +18,7 @@ export class LockGraphics {
this.parent = parent;
}
createLockBackground() {
createLockBackground() {
const graphics = this.parent.scene.add.graphics();
graphics.lineStyle(2, 0x666666);
graphics.strokeRect(100, 50, 400, 300);
@@ -40,7 +40,7 @@ export class LockGraphics {
this.parent.keywayGraphics.strokeRect(100, 170, 400, 120);
}
createTensionWrench() {
createTensionWrench() {
const wrenchX = 80; // Position to the left of the lock
const wrenchY = 160; // Position down by half the arm width (5 units) from shear line
@@ -176,7 +176,7 @@ export class LockGraphics {
});
}
createHookPick() {
createHookPick() {
// Create hook pick that comes in from the left side
// Handle is off-screen, long horizontal arm curves up to bottom of key pin 1

View File

@@ -1,6 +1,7 @@
import { MinigameScene } from '../framework/base-minigame.js';
import { LockConfiguration } from './lock-configuration.js';
import { LockGraphics } from './lock-graphics.js';
import { KeyDataGenerator } from './key-data-generator.js';
// Phaser Lockpicking Minigame Scene implementation
export class LockpickingMinigamePhaser extends MinigameScene {
@@ -22,6 +23,9 @@ 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);
}
// Also try to load from localStorage for persistence across sessions
@@ -294,7 +298,7 @@ export class LockpickingMinigamePhaser extends MinigameScene {
// Skip creating initial key, will show key selection instead
// But we still need to initialize keyData for the correct key
if (!self.keyData) {
self.generateKeyDataFromPins();
self.keyDataGen.generateKeyDataFromPins();
}
self.hideLockpickingTools();
self.updateFeedback("Select a key to begin");
@@ -364,49 +368,6 @@ export class LockpickingMinigamePhaser extends MinigameScene {
generateKeyDataFromPins() {
// Generate key cuts based on actual pin heights
// Calculate cut depths so that when key is inserted, pins align at shear line
const cuts = [];
const shearLineY = -45; // Shear line position (in pin container coordinates)
const keyBladeHeight = 110; // Key blade height (from keyConfig)
const pinContainerY = 200; // Pin container Y position (world coordinates)
for (let i = 0; i < this.pinCount; i++) {
const pin = this.pins[i];
const keyPinLength = pin.keyPinLength;
// Simple key cut calculation:
// The cut depth should be the key pin length minus the gap from key blade top to shear line
// Key blade is centered in keyway: keywayStartY + keywayHeight/2 = 170 + 60 = 230
// Key blade top is: 230 - keyBladeHeight/2 = 230 - 55 = 175
// Shear line is at y=155 in world coordinates (200 - 45)
const keyBladeTop_world = 175; // 170 + 60 - 55 (keywayStartY + keywayHeight/2 - keyBladeHeight/2)
const shearLine_world = 155; // 200 - 45 (pin container Y - shear line Y)
const gapFromKeyBladeTopToShearLine = keyBladeTop_world - shearLine_world; // 175 - 155 = 20
// Cut depth = key pin length - gap from key blade top to shear line
const cutDepth_needed = keyPinLength - gapFromKeyBladeTopToShearLine;
// Clamp to valid range (0 to key blade height)
const clampedCutDepth = Math.max(0, Math.min(keyBladeHeight, cutDepth_needed));
console.log(`=== KEY CUT ${i} GENERATION ===`);
console.log(` Pin properties: keyPinLength=${keyPinLength}, driverPinLength=${pin.driverPinLength}`);
console.log(` Key blade top: ${keyBladeTop_world}, shear line: ${shearLine_world}`);
console.log(` Gap from key blade top to shear line: ${gapFromKeyBladeTopToShearLine}`);
console.log(` Cut calculation: cutDepth_needed=${cutDepth_needed} (${keyPinLength} - ${gapFromKeyBladeTopToShearLine})`);
console.log(` Final cut: cutDepth=${clampedCutDepth}px (max ${keyBladeHeight}px)`);
console.log(`=====================================`);
cuts.push(clampedCutDepth);
}
this.keyData = { cuts: cuts };
console.log('Generated key data from pins:', this.keyData);
}
createKeyFromPinSizes(pinSizes) {
// Create a complete key object based on a set of pin sizes
// pinSizes: array of numbers representing the depth of each cut (0-100)
@@ -765,7 +726,7 @@ export class LockpickingMinigamePhaser extends MinigameScene {
// Generate key data from actual pin heights if not provided
if (!this.keyData) {
this.generateKeyDataFromPins();
this.keyDataGen.generateKeyDataFromPins();
}
// Key dimensions - make keyway higher so key pins align at shear line