1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

Fix sound being played too often

This commit is contained in:
tobspr 2020-06-25 13:12:35 +02:00
parent 7ca425c8f3
commit 66eac93460
4 changed files with 44 additions and 11 deletions

View File

@ -372,7 +372,7 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
size: 12, size: 12,
offsetY: offsetY:
-globalConfig.halfTileSize - -globalConfig.halfTileSize -
clamp((this.root.time.now() * 1.5) % 1.0, 0, 1) * 1 * globalConfig.tileSize + clamp((this.root.time.realtimeNow() * 1.5) % 1.0, 0, 1) * 1 * globalConfig.tileSize +
globalConfig.halfTileSize, globalConfig.halfTileSize,
}); });
} }

View File

@ -516,15 +516,32 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
* releasing the mouse * releasing the mouse
*/ */
executeDirectionLockedPlacement() { executeDirectionLockedPlacement() {
const metaBuilding = this.currentMetaBuilding.get();
if (!metaBuilding) {
// No active building
return;
}
// Get path to place
const path = this.computeDirectionLockPath(); const path = this.computeDirectionLockPath();
// Store if we placed anything
let anythingPlaced = false;
// Perform this in bulk to avoid recalculations
this.root.logic.performBulkOperation(() => { this.root.logic.performBulkOperation(() => {
for (let i = 0; i < path.length; ++i) { for (let i = 0; i < path.length; ++i) {
const { rotation, tile } = path[i]; const { rotation, tile } = path[i];
this.currentBaseRotation = rotation; this.currentBaseRotation = rotation;
this.tryPlaceCurrentBuildingAt(tile); if (this.tryPlaceCurrentBuildingAt(tile)) {
anythingPlaced = true;
}
} }
}); });
if (anythingPlaced) {
this.root.soundProxy.playUi(metaBuilding.getPlacementSound());
}
} }
/** /**
@ -647,7 +664,9 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
// Place initial building, but only if direction lock is not active // Place initial building, but only if direction lock is not active
if (!this.isDirectionLockActive) { if (!this.isDirectionLockActive) {
this.tryPlaceCurrentBuildingAt(this.lastDragTile); if (this.tryPlaceCurrentBuildingAt(this.lastDragTile)) {
this.root.soundProxy.playUi(metaBuilding.getPlacementSound());
}
} }
return STOP_PROPAGATION; return STOP_PROPAGATION;
} }
@ -724,16 +743,25 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
var sy = y0 < y1 ? 1 : -1; var sy = y0 < y1 ? 1 : -1;
var err = dx - dy; var err = dx - dy;
let anythingPlaced = false;
let anythingDeleted = false;
while (this.currentlyDeleting || this.currentMetaBuilding.get()) { while (this.currentlyDeleting || this.currentMetaBuilding.get()) {
if (this.currentlyDeleting) { if (this.currentlyDeleting) {
// Deletion
const contents = this.root.map.getTileContentXY(x0, y0); const contents = this.root.map.getTileContentXY(x0, y0);
if (contents && !contents.queuedForDestroy && !contents.destroyed) { if (contents && !contents.queuedForDestroy && !contents.destroyed) {
this.root.logic.tryDeleteBuilding(contents); if (this.root.logic.tryDeleteBuilding(contents)) {
this.root.soundProxy.playUi(SOUNDS.destroyBuilding); anythingDeleted = true;
}
} }
} else { } else {
this.tryPlaceCurrentBuildingAt(new Vector(x0, y0)); // Placement
if (this.tryPlaceCurrentBuildingAt(new Vector(x0, y0))) {
anythingPlaced = true;
}
} }
if (x0 === x1 && y0 === y1) break; if (x0 === x1 && y0 === y1) break;
var e2 = 2 * err; var e2 = 2 * err;
if (e2 > -dy) { if (e2 > -dy) {
@ -745,6 +773,13 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
y0 += sy; y0 += sy;
} }
} }
if (anythingPlaced) {
this.root.soundProxy.playUi(metaBuilding.getPlacementSound());
}
if (anythingDeleted) {
this.root.soundProxy.playUi(SOUNDS.destroyBuilding);
}
} }
this.lastDragTile = newPos; this.lastDragTile = newPos;

View File

@ -186,7 +186,6 @@ export class GameLogic {
variant, variant,
}); });
this.root.soundProxy.playUi(building.getPlacementSound());
return entity; return entity;
} }
return null; return null;

View File

@ -5,10 +5,9 @@ import { GameRoot } from "./root";
import { Vector } from "../core/vector"; import { Vector } from "../core/vector";
import { SOUNDS } from "../platform/sound"; import { SOUNDS } from "../platform/sound";
const avgSoundDurationSeconds = 0.25; const avgSoundDurationSeconds = 0.1;
const maxOngoingSounds = 2; const maxOngoingSounds = 2;
const maxOngoingUiSounds = 5;
const maxOngoingUiSounds = 10;
// Proxy to the application sound instance // Proxy to the application sound instance
export class SoundProxy { export class SoundProxy {