Skip to content

Commit

Permalink
Don't load sounds for events that are too far to be heard
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jul 15, 2024
1 parent d0d0330 commit 87c1111
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Changelog
- added support for animated entity overlay effects
- added transformation effect to vampires
- cleaned up bestiary window layout
- performance optimization: sound events too far away to be heard are not loaded


1.47
Expand Down
21 changes: 21 additions & 0 deletions src/js/stendhal/entity/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ export class User extends Player {
&& (Math.abs(this["y"] - entity["y"]) < hearingRange));
}

/**
* Checks if player is within range to hear a sound event.
*
* @param {number} radius
* Distance at which sound can be heard.
* @param {Entity} entity
* Entity emitting sound event.
* @returns {boolean}
* `true` if sound should be loaded (when event radius is more than -1 & user position is within
* radial distance from event origin).
*/
isInSoundRange(radius: number, entity: Entity): boolean {
if (entity === this) {
return true;
}
if (radius < 0) {
return false;
}
return Math.abs(this["x"] - entity["x"]) + Math.abs(this["y"] - entity["y"]) <= radius;
}

/**
* Actions when player leaves a zone.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/js/stendhal/event/SoundEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { RPEvent } from "./RPEvent";
import { SoundID } from "../data/sound/SoundID";
import { SoundManager } from "../data/sound/SoundManager";

declare var marauroa: any;
declare var stendhal: any;


Expand All @@ -28,10 +29,18 @@ export class SoundEvent extends RPEvent {


execute(entity: any) {
if (!marauroa.me) {
return;
}
let radius = SoundManager.DEFAULT_RADIUS;
if (typeof(this["radius"]) === "number") {
radius = this["radius"];
}
if (!marauroa.me.isInSoundRange(radius, entity)) {
// too far away to hear so don't load
return;
}

let volume = 1;
// Adjust by the server specified volume, if any
if (this.hasOwnProperty("volume")) {
Expand Down

0 comments on commit 87c1111

Please sign in to comment.