Skip to content

Commit 695cb5a

Browse files
committed
fix map sound distance
1 parent 0352b8a commit 695cb5a

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

Intersect.Client.Core/Core/Sounds/MapSound.cs

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Intersect.Client.Framework.GenericClasses;
44
using Intersect.Client.General;
55
using Intersect.Client.Maps;
6+
using Intersect.Config;
67

78
namespace Intersect.Client.Core.Sounds;
89

@@ -18,6 +19,12 @@ public partial class MapSound : Sound, IMapSound
1819

1920
private int mY;
2021

22+
private readonly MapOptions _mapOptions = Options.Instance.Map;
23+
private readonly int _tileWidth;
24+
private readonly int _tileHeight;
25+
private readonly int _mapPixelWidth;
26+
private readonly int _mapPixelHeight;
27+
2128
public MapSound(
2229
string filename,
2330
int x,
@@ -40,6 +47,10 @@ public MapSound(
4047
mMapId = mapId;
4148
mEntity = parent;
4249
mSound.Volume = 0;
50+
_tileWidth = _mapOptions.TileWidth;
51+
_tileHeight = _mapOptions.TileHeight;
52+
_mapPixelWidth = _mapOptions.MapWidth * _tileWidth;
53+
_mapPixelHeight = _mapOptions.MapHeight * _tileHeight;
4354
}
4455

4556
public void UpdatePosition(int x, int y, Guid mapId)
@@ -91,7 +102,7 @@ private void UpdateSoundVolume()
91102
var volume = 0f;
92103
if (mDistance > 0 && Globals.GridMaps.ContainsKey(mMapId))
93104
{
94-
volume = 100 - 100 / ((mDistance + 1) * CalculateSoundDistance());
105+
volume = 100 - 100f * CalculateSoundDistance() / (mDistance + 1);
95106
if (volume < 0)
96107
{
97108
volume = 0f;
@@ -107,40 +118,56 @@ private void UpdateSoundVolume()
107118

108119
private float CalculateSoundDistance()
109120
{
110-
if (Globals.Me == null)
121+
if (Globals.Me is not {} player)
122+
{
123+
return 0f;
124+
}
125+
var soundMapId = mMapId;
126+
if (!MapInstance.TryGet(soundMapId, out var soundMap))
111127
{
112128
return 0f;
113129
}
114130

115-
var distance = 0f;
116-
var map = MapInstance.Get(mMapId);
117-
var pMap = MapInstance.Get(Globals.Me.MapId);
118-
if (map != null && pMap != null)
131+
MapInstance? playerMap;
132+
var playerMapId = player.MapId;
133+
if (playerMapId == soundMapId)
119134
{
120-
var playerx = pMap.X + Globals.Me.X * Options.Instance.Map.TileWidth + (Options.Instance.Map.TileWidth / 2);
121-
var playery = pMap.Y + Globals.Me.Y * Options.Instance.Map.TileHeight + (Options.Instance.Map.TileHeight / 2);
122-
if (mX == -1 || mY == -1 || mDistance == -1)
123-
{
124-
var player = new Point() {
125-
X = (int)playerx,
126-
Y = (int)playery
127-
};
128-
129-
var mapRect = new Rectangle(
130-
(int)map.X, (int)map.Y, Options.Instance.Map.MapWidth * Options.Instance.Map.TileWidth,
131-
Options.Instance.Map.MapHeight * Options.Instance.Map.TileHeight
132-
);
133-
134-
distance = DistancePointToRectangle(player, mapRect) /
135-
((Options.Instance.Map.TileHeight + Options.Instance.Map.TileWidth) / 2f);
136-
}
137-
else
138-
{
139-
var soundx = map.X + mX * Options.Instance.Map.TileWidth + (Options.Instance.Map.TileWidth / 2);
140-
var soundy = map.Y + mY * Options.Instance.Map.TileHeight + (Options.Instance.Map.TileHeight / 2);
141-
distance = (float) Math.Sqrt(Math.Pow(playerx - soundx, 2) + Math.Pow(playery - soundy, 2)) /
142-
((Options.Instance.Map.TileHeight + Options.Instance.Map.TileWidth) / 2f);
143-
}
135+
playerMap = soundMap;
136+
}
137+
else if (!MapInstance.TryGet(playerMapId, out playerMap))
138+
{
139+
return 0f;
140+
}
141+
142+
float distance;
143+
144+
Point playerPosition = new(
145+
playerMap.X + player.X * _tileWidth + (_tileWidth / 2),
146+
playerMap.Y + player.Y * _tileHeight + (_tileHeight / 2)
147+
);
148+
149+
if (mX == -1 || mY == -1 || mDistance == -1)
150+
{
151+
var mapRect = new Rectangle(
152+
soundMap.X,
153+
soundMap.Y,
154+
_mapPixelWidth,
155+
_mapPixelHeight
156+
);
157+
158+
distance = DistancePointToRectangle(playerPosition, mapRect) / ((_tileHeight + _tileWidth) / 2f);
159+
}
160+
else
161+
{
162+
Point soundPosition = new(
163+
soundMap.X + mX * _tileWidth + (_tileWidth / 2),
164+
soundMap.Y + mY * _tileHeight + (_tileHeight / 2)
165+
);
166+
167+
var delta = playerPosition - soundPosition;
168+
169+
distance = (float)Math.Sqrt(Math.Pow(delta.X, 2) + Math.Pow(delta.Y, 2)) /
170+
((_tileHeight + _tileWidth) / 2f);
144171
}
145172

146173
return distance;

0 commit comments

Comments
 (0)