Modify look (#848)

* Added modify(e,'look',...

* Added modify(e,'look',...

* Not change yaw and pitch if vector length is 0 or straight up/down

* Added to docs

* Update docs/scarpet/api/Entities.md

Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com>
This commit is contained in:
replaceitemandgnembon authored and GitHub committed 2021-05-09 15:14:05 -03:00
1 parent 47f8e0d26b
commit 6a06b0fb61
2 files changed
+22 -1

No files matched your search

@@ -1034,7 +1034,22 @@ public class EntityValue extends Value
updatePosition(e, e.getX(), e.getY(), e.getZ(), e.yaw, MathHelper.clamp((float)NumericValue.asNumber(v).getDouble(), -90, 90));
});
//"look"
put("look", (e, v) -> {
if (!(v instanceof ListValue)) throw new InternalExpressionException("Expected a list of 3 parameters as a second argument");
List<Value> vec = ((ListValue)v).getItems();
float x = NumericValue.asNumber(vec.get(0)).getFloat();
float y = NumericValue.asNumber(vec.get(1)).getFloat();
float z = NumericValue.asNumber(vec.get(2)).getFloat();
float l = MathHelper.sqrt(x*x + y*y + z*z);
if(l==0) return;
x /= l;
y /= l;
z /= l;
float pitch = (float) -Math.asin(y) / 0.017453292F;
float yaw = (float) (x==0 && z==0 ? e.yaw : MathHelper.atan2(-x,z) / 0.017453292F);
updatePosition(e, e.getX(), e.getY(), e.getZ(), yaw, pitch);
});
//"turn"
//"nod"