Skip to content

Commit

Permalink
add tag location.face returns dLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 14, 2015
1 parent bc02171 commit af82adb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -748,6 +748,22 @@ else if (type == Material.TRAP_DOOR
}
}

// <--[tag]
// @attribute <l@location.face[<location>]>
// @returns dLocation
// @description
// Returns a location containing a yaw/pitch that point from the current location
// to the target location.
// -->
if (attribute.startsWith("face")
&& attribute.hasContext(1)) {
Location two = dLocation.valueOf(attribute.getContext(1));
Location rel = Rotation.faceLocation(this, two);
if (rel != null) {
return new dLocation(rel).getAttribute(attribute.fulfill(1));
}
}

// <--[tag]
// @attribute <l@location.facing[<entity>/<location>]>
// @returns Element(Boolean)
Expand Down
Expand Up @@ -169,6 +169,28 @@ public static Location eyeTrace(LivingEntity from, double range) {
}


public static Location faceLocation(Location from, Location at) {
if (from.getWorld() != at.getWorld()) return null;
Location loc = from.getBlock().getLocation().clone().add(0.5, 0.5, 0.5);

double xDiff = at.getX() - loc.getX();
double yDiff = at.getY() - loc.getY();
double zDiff = at.getZ() - loc.getZ();

double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);

double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 70;

if (zDiff < 0.0) {
yaw = yaw + (Math.abs(180 - yaw) * 2);
}

return new Location(from.getWorld(), from.getX(), from.getY(), from.getZ(), (float)yaw - 90, (float)pitch);
}


/**
* Changes an entity's yaw and pitch to make it face a location.
*
Expand Down

0 comments on commit af82adb

Please sign in to comment.