In Put Handler
In Put Handler
js';
import { playJumpSound, audioContext, playBackgroundMusic } from './audio.js';
// Import playerCharacters to check ground state before jumping
// Import isSupported from gameLogic.js for checking if player is on a supported
surface that allows jumping
import { playerCharacters, epsilon, isSupported, deleteLastPlacedBlock,
toggleBuildMode as toggleBuildModeGameLogic, toggleBuildColorPicker } from
'./gameLogic.js';
import { renderer } from './sceneSetup.js';
// Import settings to check fly mode state
import { settings } from './settings.js';
// Flag to check if game input should be active (i.e., if the title overlay is
hidden)
let gameInputActive = false;
// Check if room and local player presence are available before processing game
input
if (!room || !room.clientId || !room.presence[room.clientId]) {
// console.warn("Input Handler: Websim room or local presence not available,
skipping keydown.");
return;
}
// Handle Delete key OR 'V' key for deleting the most recent block
if (e.code === 'Delete' || e.key === 'v' || e.key === 'V') {
console.log(`INPUT: ${e.code || e.key} key pressed, attempting to delete last
block`);
// Call the imported function
const deleted = deleteLastPlacedBlock();
if (deleted) {
console.log('INPUT: Successfully requested deletion of last placed
block');
} else {
console.log('INPUT: No blocks to delete or deletion failed (check console
for details)');
}
e.preventDefault(); // Prevent default delete/V action
return;
}
if (flyModeEnabled) {
// If in fly mode, pressing Space makes player go up
console.log("Input Handler: Fly mode active, Spacebar pressed,
initiating upward movement."); // Log fly initiation
// Update presence to signal upward movement, ensure flyDown is off
if (!localPlayerPresence.flyUp || localPlayerPresence.flyDown) { //
Update if flyUp is not true OR if flyDown is true
room.updatePresence({ flyUp: true, flyDown: false }); // Ensure
only one direction is active
console.log("Input Handler: Updating presence for flyUp: true,
flyDown: false");
}
e.preventDefault(); // Prevent default spacebar action
} else {
// If not in fly mode, Spacebar is for jumping (existing logic)
// Check if the player is currently not jumping AND is supported
// We read the jump state from the player's own presence data
const charIsSupported = localCharacter && isSupported(localCharacter); //
Use the exported isSupported function
if (flyModeEnabled) {
console.log("Input Handler: Fly mode active, Shift pressed, initiating
downward movement."); // Log fly down initiation
// Update presence to signal downward movement, ensure flyUp is off
if (!localPlayerPresence.flyDown || localPlayerPresence.flyUp) { //
Update if flyDown is not true OR if flyUp is true
room.updatePresence({ flyDown: true, flyUp: false }); // Ensure only
one direction is active
console.log("Input Handler: Updating presence for flyDown: true,
flyUp: false");
}
e.preventDefault(); // Prevent default shift key action (e.g., selecting
text)
}
}
});
// Check if room and local player presence are available before processing game
input
if (!room || !room.clientId || !room.presence[room.clientId]) {
// console.warn("Input Handler: Websim room or local presence not available,
skipping keyup.");
return;
}
if (flyModeEnabled) {
console.log("Input Handler: Fly mode active, Spacebar released,
stopping upward movement."); // Log stop fly
if (localPlayerPresence.flyUp) { // Only update if state changes
room.updatePresence({ flyUp: false });
console.log("Input Handler: Updating presence for flyUp: false");
}
}
// If not in fly mode, keyup for Space doesn't do anything specific after
initiating a jump (jump is physics-based)
}
if (flyModeEnabled) {
console.log("Input Handler: Fly mode active, Shift released, stopping
downward movement."); // Log stop fly down
if (localPlayerPresence.flyDown) { // Only update if state changes
room.updatePresence({ flyDown: false });
console.log("Input Handler: Updating presence for flyDown:
false");
}
}
}
});
document.body.addEventListener('click', () => {
// Resume AudioContext if suspended
// Ensure audioContext exists before accessing its state
if (audioContext && audioContext.state === 'suspended') {
console.log("Input Handler: AudioContext suspended, attempting to
resume...");
audioContext.resume().then(() => {
console.log('Input Handler: AudioContext resumed.');
// Once resumed, attempt to play background music
playBackgroundMusic();
}).catch(e => console.error("Input Handler: Failed to resume audio
context:", e));
} else if (audioContext && audioContext.state === 'running') {
// If already running, ensure music is playing (the function itself handles
if it's already playing)
playBackgroundMusic();
} else {
console.log("Input Handler: AudioContext not available or in unexpected
state:", audioContext?.state);
}
});
// Export keys, orbit controls, but not local jump state variables as they are
managed by presence/gameLogic
export { keys, orbitRadius, orbitAngle, orbitPitch, gameInputActive };