소스 검색

BETA: Updated Angle & Heading methods in `Util` to use `ILocational` instead of `L2Object`.
* Added a new method `L2Object.calculateDirectionTo(ILocational)` read javadocs for more info.

Patch by: FBIagent

Nos 11 년 전
부모
커밋
121b1f3dc3
2개의 변경된 파일32개의 추가작업 그리고 14개의 파일을 삭제
  1. 18 0
      L2J_Server_BETA/java/com/l2jserver/gameserver/model/L2Object.java
  2. 14 14
      L2J_Server_BETA/java/com/l2jserver/gameserver/util/Util.java

+ 18 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/L2Object.java

@@ -48,6 +48,7 @@ import com.l2jserver.gameserver.network.SystemMessageId;
 import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
 import com.l2jserver.gameserver.network.serverpackets.ExSendUIEvent;
 import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
+import com.l2jserver.gameserver.util.Util;
 
 /**
  * Base class for all interactive objects.
@@ -742,6 +743,23 @@ public abstract class L2Object implements IIdentifiable, INamable, ISpawnable, I
 		return calculateDistance(loc.getX(), loc.getY(), loc.getZ(), includeZAxis, squared);
 	}
 	
+	/**
+	 * Calculates the angle in degrees from this object to the given object.<br>
+	 * The return value can be described as how much this object has to turn<br>
+	 * to have the given object directly in front of it.
+	 * @param to
+	 * @return the angle this onject has to turn to have the given object in front of it
+	 */
+	public double calculateDirectionTo(ILocational to)
+	{
+		int heading = Util.calculateHeadingFrom(this, to) - this.getHeading();
+		if (heading < 0)
+		{
+			heading = 65535 + heading;
+		}
+		return Util.convertHeadingToDegree(heading);
+	}
+	
 	@Override
 	public int getX()
 	{

+ 14 - 14
L2J_Server_BETA/java/com/l2jserver/gameserver/util/Util.java

@@ -67,25 +67,25 @@ public final class Util
 	}
 	
 	/**
-	 * @param obj1
-	 * @param obj2
+	 * @param from
+	 * @param to
 	 * @return degree value of object 2 to the horizontal line with object 1 being the origin.
 	 */
-	public static double calculateAngleFrom(L2Object obj1, L2Object obj2)
+	public static double calculateAngleFrom(ILocational from, ILocational to)
 	{
-		return calculateAngleFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
+		return calculateAngleFrom(from.getX(), from.getY(), to.getX(), to.getY());
 	}
 	
 	/**
-	 * @param obj1X
-	 * @param obj1Y
-	 * @param obj2X
-	 * @param obj2Y
+	 * @param fromX
+	 * @param fromY
+	 * @param toX
+	 * @param toY
 	 * @return degree value of object 2 to the horizontal line with object 1 being the origin
 	 */
-	public static final double calculateAngleFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
+	public static final double calculateAngleFrom(int fromX, int fromY, int toX, int toY)
 	{
-		double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
+		double angleTarget = Math.toDegrees(Math.atan2(toY - fromY, toX - fromX));
 		if (angleTarget < 0)
 		{
 			angleTarget = 360 + angleTarget;
@@ -108,14 +108,14 @@ public final class Util
 		return (int) (degree * 182.044444444);
 	}
 	
-	public static final int calculateHeadingFrom(L2Object obj1, L2Object obj2)
+	public static final int calculateHeadingFrom(ILocational from, ILocational to)
 	{
-		return calculateHeadingFrom(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
+		return calculateHeadingFrom(from.getX(), from.getY(), to.getX(), to.getY());
 	}
 	
-	public static final int calculateHeadingFrom(int obj1X, int obj1Y, int obj2X, int obj2Y)
+	public static final int calculateHeadingFrom(int fromX, int fromY, int toX, int toY)
 	{
-		double angleTarget = Math.toDegrees(Math.atan2(obj2Y - obj1Y, obj2X - obj1X));
+		double angleTarget = Math.toDegrees(Math.atan2(toY - fromY, toX - fromX));
 		if (angleTarget < 0)
 		{
 			angleTarget = 360 + angleTarget;