Skip to content

Commit 4d73ec3

Browse files
committed
Add FlxSound.scrollFactor, getPosition
1 parent 4d8e99f commit 4d73ec3

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

source/flixel/sound/FlxSound.hx

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import flixel.math.FlxMath;
2121
import flixel.math.FlxPoint;
2222
import flixel.system.FlxAssets.FlxSoundAsset;
2323
import flixel.tweens.FlxTween;
24+
import flixel.util.FlxDestroyUtil;
2425
import flixel.util.FlxSignal;
2526
import flixel.util.FlxStringUtil;
2627
import flixel.FlxBasic;
@@ -136,11 +137,18 @@ class FlxSound extends FlxBasic {
136137
public var radius:Float;
137138

138139
/**
139-
* Whether the proximity alters the pan or not
140+
* Whether the proximity alters the pan or not.
140141
* @since raltyMod
141142
*/
142143
public var proximityPan:Bool;
143144

145+
/**
146+
* Controls how much this object is affected by camera scrolling. `0` = no movement (e.g. a static sound),
147+
* This is only useful if used with proximity (Initialized once proximity is used).
148+
* @since raltyMod
149+
*/
150+
public var scrollFactor(default, null):FlxPoint;
151+
144152
/**
145153
* Stores for how much channels are in the loaded sound.
146154
* @since raltyMod
@@ -302,6 +310,8 @@ class FlxSound extends FlxBasic {
302310
_lastTime = null;
303311

304312
if (destroySound) {
313+
scrollFactor = FlxDestroyUtil.put(scrollFactor);
314+
305315
if (group != null) group.remove(this);
306316

307317
if (_channel != null) {
@@ -348,15 +358,24 @@ class FlxSound extends FlxBasic {
348358

349359
_amplitudeUpdate = true;
350360

351-
// Distance-based volume control (TODO for Ralty: REDO THIS)
361+
// Distance-based volume control
352362
if (target != null) {
353-
var targetPosition = target.getPosition();
354-
var radialMultiplier = targetPosition.distanceTo(FlxPoint.weak(x, y)) / radius;
355-
targetPosition.put();
356-
radialMultiplier = 1 - FlxMath.bound(radialMultiplier, 0, 1);
363+
var targetPosition = target.getPosition(), position = getPosition();
364+
var camera = camera;
365+
if (camera != null) {
366+
targetPosition.subtract(camera.scroll.x * target.scrollFactor.x, camera.scroll.y * target.scrollFactor.y);
367+
if (scrollFactor != null) position.subtract(camera.scroll.x * scrollFactor.x, camera.scroll.y * scrollFactor.y);
368+
else position.subtract(camera.scroll.x, camera.scroll.y);
369+
}
370+
371+
var radialMultiplier = targetPosition.distanceTo(position) / radius;
372+
373+
// Make it so it affects the 3d position of the source and not just the panning?
374+
_volumeAdjust = 1 - FlxMath.bound(radialMultiplier, 0, 1);
375+
if (proximityPan) _panAdjust = (position.x - targetPosition.x) / radius;
357376

358-
_volumeAdjust = radialMultiplier;
359-
if (proximityPan) _panAdjust = (x - target.x) / radius;
377+
targetPosition.put();
378+
position.put();
360379
}
361380
else
362381
_volumeAdjust = 1.0;
@@ -536,27 +555,44 @@ class FlxSound extends FlxBasic {
536555
* @param Pan Whether panning should be used in addition to the volume changes.
537556
* @return This FlxSound instance (nice for chaining stuff together, if you're into that).
538557
*/
539-
public function proximity(x = 0.0, y = 0.0, ?targetObject:FlxObject, ?radius:Float, pan = true):FlxSound {
558+
public function proximity(x = 0.0, y = 0.0, ?targetObject:FlxObject, ?radius:Float, pan = true, ?scrollFactor:FlxPoint):FlxSound {
540559
setPosition(x, y);
541560
if (targetObject != null) this.target = targetObject;
542561
if (radius != null) this.radius = radius;
543562
proximityPan = pan;
544563

564+
if (this.scrollFactor == null) this.scrollFactor = FlxPoint.get(1, 1);
565+
if (scrollFactor != null) this.scrollFactor.copyFrom(scrollFactor);
566+
545567
return this;
546568
}
547569

548570
/**
549571
* Helper function to set the coordinates of this object.
550572
* Sound positioning is used in conjunction with proximity/panning.
551573
*
552-
* @param x The new x position
553-
* @param y The new y position
574+
* @param x The new x position
575+
* @param y The new y position
554576
*/
555-
public inline function setPosition(x = 0.0, y = 0.0):Void {
577+
public function setPosition(x = 0.0, y = 0.0):Void {
556578
this.x = x;
557579
this.y = y;
558580
}
559581

582+
/**
583+
* Returns the world position of this object.
584+
*
585+
* @param result Optional arg for the returning point.
586+
* @return The world position of this object.
587+
* @since raltyMod
588+
*/
589+
public function getPosition(?result:FlxPoint):FlxPoint {
590+
if (result == null)
591+
result = FlxPoint.get();
592+
593+
return result.set(x, y);
594+
}
595+
560596
/**
561597
* Call this function to play the sound - also works on paused sounds.
562598
*

0 commit comments

Comments
 (0)