Skip to content

Instantly share code, notes, and snippets.

@FalseHonesty
Last active August 14, 2017 00:18
Show Gist options
  • Save FalseHonesty/0b437e144db1f9be1e5269a81dfc3ecb to your computer and use it in GitHub Desktop.
Save FalseHonesty/0b437e144db1f9be1e5269a81dfc3ecb to your computer and use it in GitHub Desktop.
public class Cone {
/** @param entities
* List of nearby entities
* @param startPos
* starting position
* @param radius
* distance cone travels
* @param degrees
* angle of cone
* @param direction
* direction of the cone
* @return All entities inside the cone */
public static List<Entity> getEntitiesInCone(List<Entity> entities, Vector startPos, float radius, float degrees, Vector direction) {
List<Entity> newEntities = new ArrayList<Entity>(); // Returned list
float squaredRadius = radius * radius; // We don't want to use square root
for (Entity e : entities) {
Vector relativePosition = e.getLocation().toVector(); // Position of the entity relative to the cone origin
relativePosition.subtract(startPos);
if (relativePosition.lengthSquared() > squaredRadius) continue; // First check : distance
if (getAngleBetweenVectors(direction, relativePosition) > degrees) continue; // Second check : angle
newEntities.add(e); // The entity e is in the cone
}
return newEntities;
}
public static float map(float toMap, int beforeMin, int beforeMax, int afterMin, int afterMax) {
return (toMap - beforeMin) / (beforeMax - beforeMin) * (afterMax - afterMin) + afterMin;
}
public static float getAngleBetweenVectors(Vector v1, Vector v2) {
return Math.abs((float)Math.toDegrees(v1.angle(v2)));
}
}
int coneRange = 75;
if (Cone.getEntitiesInCone(
player.getNearbyEntities(flashbangRange, flashbangRange, flashbangRange),
player.getLocation().toVector(),
flashbangRange,
coneRange,
player.getLocation().getDirection()
).contains(grenade) && player.hasLineOfSight(grenade)) {
int angleBetweenFlash = ((int) Cone.getAngleBetweenVectors(
player.getLocation().getDirection(), grenade.getLocation().subtract(player.getLocation().toVector()).toVector()
));
int duration = Math.round(Cone.map(
angleBetweenFlash,
0,
coneRange,
maximumFlashtime,
minimumFlashtime
));
player.sendMessage("Flashing for " + duration + " ticks");
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, duration, 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment