modernuo/Projects/Server/Comparers/LocationComparer.cs

56 lines
1.9 KiB
C#
Raw Permalink Normal View History

2020-10-31 23:30:51 -07:00
/*************************************************************************
* ModernUO *
2023-08-09 09:09:26 -07:00
* Copyright 2019-2023 - ModernUO Development Team *
2020-10-31 23:30:51 -07:00
* Email: hi@modernuo.com *
* File: LocationComparer.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Collections.Generic;
namespace Server;
public class LocationComparer : IComparer<IPoint3D>
2020-10-31 23:30:51 -07:00
{
private static LocationComparer m_Instance;
2020-10-31 23:30:51 -07:00
public LocationComparer(IPoint3D relativeTo) => RelativeTo = relativeTo;
2020-10-31 23:30:51 -07:00
public IPoint3D RelativeTo { get; set; }
2020-10-31 23:30:51 -07:00
public int Compare(IPoint3D x, IPoint3D y) => GetDistance(x) - GetDistance(y);
2020-10-31 23:30:51 -07:00
public static LocationComparer GetInstance(IPoint3D relativeTo)
{
if (m_Instance == null)
2020-10-31 23:30:51 -07:00
{
m_Instance = new LocationComparer(relativeTo);
2020-10-31 23:30:51 -07:00
}
else
2020-10-31 23:30:51 -07:00
{
m_Instance.RelativeTo = relativeTo;
}
2020-10-31 23:30:51 -07:00
return m_Instance;
}
2020-10-31 23:30:51 -07:00
private int GetDistance(IPoint3D p)
{
var x = RelativeTo.X - p.X;
var y = RelativeTo.Y - p.Y;
var z = RelativeTo.Z - p.Z;
x *= 11;
y *= 11;
return x * x + y * y + z * z;
2020-10-31 23:30:51 -07:00
}
}