Implement Bluetooth device scanning and proximity-based unlocking

- Add Bluetooth scanning constants and interval-based scanning
- Modify object interaction to check for Bluetooth device proximity
- Implement proximity-based unlocking for tablets
- Add debug logging for Bluetooth scanning and device detection
- Adjust tablet and scanner positioning in reception room

----
Fixed broken funcitons of previous bluetooth
This commit is contained in:
Damian-I
2025-02-18 17:01:17 +00:00
parent a94b8fe40c
commit 1889b1b1d9
3 changed files with 62 additions and 37 deletions

View File

@@ -231,8 +231,8 @@
"type":"",
"visible":true,
"width":48,
"x":340,
"y":168
"x":430,
"y":380
},
{
"height":48,

View File

@@ -190,6 +190,10 @@
const INTERACTION_RANGE = 2 * TILE_SIZE;
const INTERACTION_RANGE_SQ = INTERACTION_RANGE * INTERACTION_RANGE;
// Bluetooth constants
const BLUETOOTH_SCAN_RANGE = TILE_SIZE * 2; // 2 tiles range for Bluetooth scanning
let lastBluetoothScan = 0; // Track last scan time
const BLUETOOTH_SCAN_INTERVAL = 500; // Scan every 500ms
// preloads the assets
function preload() {
@@ -461,6 +465,13 @@
// checks for room transitions
checkRoomTransitions.call(this);
// Check for Bluetooth devices
const currentTime = this.time.now;
if (currentTime - lastBluetoothScan >= BLUETOOTH_SCAN_INTERVAL) {
checkBluetoothDevices.call(this);
lastBluetoothScan = currentTime;
}
// adds a circle to the start of the path
if (currentPath && currentPath.length > 0 && isMoving) {
this.add.circle(currentPath[0].x, currentPath[0].y, 5, 0xff0000).setDepth(1000);
@@ -1838,19 +1849,7 @@
break;
case 'bluetooth':
const requiredMac = lockRequirements.requires;
// Find the scanner in inventory
const scanner = inventory.items.find(item =>
item.scenarioData?.type === "bluetooth_scanner"
);
console.log('Bluetooth unlock attempt:', {
itemName: lockable.scenarioData?.name,
itemMac: lockable.scenarioData?.mac,
hasScanner: !!scanner
});
if (!scanner) {
if (lockable.scenarioData?.locked) {
alert("You need a Bluetooth scanner to unlock this device.");
// Don't return here - allow the item to be picked up even without scanner
if (type === 'item' && lockable.scenarioData?.takeable) {
@@ -1863,26 +1862,27 @@
return;
}
// If scanner is in inventory, automatically unlock the device
if (inventory.items.includes(scanner)) {
console.log('Bluetooth unlock success: Scanner in inventory', {
// Calculate distance between player and tablet
const distance = Phaser.Math.Distance.Between(
player.x, player.y,
lockable.x, lockable.y
);
console.log('Distance to tablet:', distance);
// Check if player is within range (using BLUETOOTH_SCAN_RANGE)
if (distance <= BLUETOOTH_SCAN_RANGE) {
console.log('Bluetooth unlock success: Player in range', {
itemName: lockable.scenarioData?.name,
itemMac: lockable.scenarioData?.mac
itemMac: lockable.scenarioData?.mac,
distance: distance
});
unlockTarget(lockable, type, lockable.layer);
alert(`Using Bluetooth scanner from inventory to unlock device.`);
alert(`Bluetooth connection established. Device unlocked.`);
return;
}
const devices = getBluetoothDevicesInRange({ x: scanner.x, y: scanner.y });
const matchingDevice = devices.find(d => d.mac === requiredMac && d.signalStrength >= BLUETOOTH_MIN_SIGNAL);
if (matchingDevice) {
unlockTarget(lockable, type, lockable.layer);
alert(`Bluetooth connection established with ${matchingDevice.name}. Device unlocked.`);
} else {
alert("No compatible Bluetooth device in range or signal too weak.");
}
alert("Too far from device to establish Bluetooth connection.");
break;
default:
@@ -2110,6 +2110,39 @@
alert('You collected the items from the container.');
}
function checkBluetoothDevices() {
// Find scanner in inventory
const scanner = inventory.items.find(item =>
item.scenarioData?.type === "bluetooth_scanner"
);
if (!scanner) return;
// Find all tablets in the current room
if (!currentRoom || !rooms[currentRoom] || !rooms[currentRoom].objects) return;
Object.values(rooms[currentRoom].objects).forEach(obj => {
if (obj.scenarioData?.type === "tablet" && obj.scenarioData?.locked) {
const distance = Phaser.Math.Distance.Between(
player.x, player.y,
obj.x, obj.y
);
if (distance <= BLUETOOTH_SCAN_RANGE) {
console.log('🔍 TABLET IN RANGE:', {
distance: Math.round(distance),
range: BLUETOOTH_SCAN_RANGE
});
// Unlock the tablet
obj.scenarioData.locked = false;
console.log('🔓 TABLET UNLOCKED!');
alert('Bluetooth connection established. Device unlocked.');
}
}
});
}
</script>
</body>
</html>

View File

@@ -1711,14 +1711,6 @@
return;
}
// If scanner is in inventory, automatically unlock the device
if (inventory.items.includes(scanner)) {
unlockTarget(lockable, type, lockable.layer);
alert(`Using Bluetooth scanner from inventory to unlock device.`);
return;
}
// Otherwise check signal strength based on position
const devices = getBluetoothDevicesInRange({ x: scanner.x, y: scanner.y });
const matchingDevice = devices.find(d => d.mac === requiredMac && d.signalStrength >= BLUETOOTH_MIN_SIGNAL);