Files
novafarma/old_logic/src_backup_1768938138/entities/Scooter.js
2026-01-21 01:08:21 +01:00

160 lines
4.8 KiB
JavaScript

class Scooter {
constructor(scene, gridX, gridY) {
this.scene = scene;
this.gridX = gridX;
this.gridY = gridY;
this.iso = new IsometricUtils(48, 24); // Assuming global availability or import if needed, but classes usually have access if loaded.
// Actually Scene has this.iso, better use that.
this.isBroken = true;
this.isMounted = false;
this.type = 'scooter'; // For interaction checks
this.createSprite();
}
createSprite() {
if (this.sprite) this.sprite.destroy();
const tex = this.isBroken ? 'scooter_broken' : 'scooter';
const screenPos = this.scene.iso.toScreen(this.gridX, this.gridY);
this.sprite = this.scene.add.sprite(
screenPos.x + this.scene.terrainOffsetX,
screenPos.y + this.scene.terrainOffsetY,
tex
);
this.sprite.setOrigin(0.5, 1);
this.updateDepth();
}
updateDepth() {
if (this.sprite) {
const layerBase = this.scene.iso.LAYER_OBJECTS || 200000;
this.sprite.setDepth(layerBase + this.sprite.y);
}
}
interact(player) {
if (this.isBroken) {
this.tryFix(player);
} else {
this.toggleRide(player);
}
}
tryFix(player) {
// Use ScooterRepairSystem to check parts/tools
if (!this.scene.scooterRepairSystem) {
console.log('🚫 Repair system not available!');
return;
}
console.log('🔧 Attempting to repair Scooter...');
const success = this.scene.scooterRepairSystem.repairScooter();
if (success) {
this.isBroken = false;
this.createSprite(); // Update to fixed texture
this.scene.events.emit('show-floating-text', {
x: this.sprite.x,
y: this.sprite.y - 50,
text: "🛵 REPAIRED!",
color: '#00FF00'
});
if (this.scene.soundManager) {
this.scene.soundManager.playHarvest(); // Success sound
}
} else {
// Show what's missing
this.scene.scooterRepairSystem.listMissingParts();
this.scene.events.emit('show-floating-text', {
x: this.sprite.x,
y: this.sprite.y - 50,
text: "Missing Parts!",
color: '#FF0000'
});
}
}
toggleRide(player) {
if (this.isMounted) {
this.dismount(player);
} else {
this.mount(player);
}
}
mount(player) {
if (!player) return;
this.isMounted = true;
this.sprite.setVisible(false);
// Boost player speed
this.originalSpeed = player.moveSpeed;
this.originalMoveTime = player.gridMoveTime;
player.gridMoveTime = 100; // Faster (was 200)
// Attach a visual indicator to player?
// Ideally we'd change player sprite, but we don't have 'player_scooter'.
// We can create a "Scooter Attachment" sprite in Player, or just assume he's on it.
// Let's update Player to have a "vehicle" property.
player.vehicle = this;
this.scene.events.emit('show-floating-text', {
x: player.sprite.x,
y: player.sprite.y - 50,
text: "Riding Scooter!",
color: '#00FFFF'
});
}
dismount(player) {
if (!player) return;
this.isMounted = false;
// Reset player stats
player.gridMoveTime = this.originalMoveTime || 200;
player.vehicle = null;
// Place scooter at player's current position
this.gridX = player.gridX;
this.gridY = player.gridY;
const screenPos = this.scene.iso.toScreen(this.gridX, this.gridY);
this.sprite.setPosition(
screenPos.x + this.scene.terrainOffsetX,
screenPos.y + this.scene.terrainOffsetY
);
this.sprite.setVisible(true);
this.updateDepth();
this.scene.events.emit('show-floating-text', {
x: player.sprite.x,
y: player.sprite.y - 50,
text: "Dismounted",
color: '#CCCCCC'
});
}
update() {
if (this.isMounted && this.scene.player) {
// Keep grid position synced with player for logic, even if invisible
this.gridX = this.scene.player.gridX;
this.gridY = this.scene.player.gridY;
// Also update sprite pos just in case
const screenPos = this.scene.iso.toScreen(this.gridX, this.gridY);
this.sprite.setPosition(
screenPos.x + this.scene.terrainOffsetX,
screenPos.y + this.scene.terrainOffsetY
);
}
}
}