Skip to content

Instantly share code, notes, and snippets.

@EmreNtm
Created August 7, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmreNtm/9840b24059a0ca2b5f9d24503ba336e7 to your computer and use it in GitHub Desktop.
Save EmreNtm/9840b24059a0ca2b5f9d24503ba336e7 to your computer and use it in GitHub Desktop.
package me.hiro3.firespray;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import com.projectkorra.projectkorra.GeneralMethods;
import com.projectkorra.projectkorra.ability.CoreAbility;
//import com.projectkorra.projectkorra.util.DamageHandler;
public class Boid {
private Player player;
private FireSpray fireSpray;
private Location location;
private Vector direction;
private double lineOfSightRange;
private double speed = 1;
private double alignmentPower = 0.1;
private double pushPower = 0.01;
private double blockPushPower = 0.3;
private double turnSpeed = 0.75;
private Vector gravity = new Vector(0, -0.01, 0);
private double range;
private Location startingLocation;
private int ticksLived;
public Boid(Player player, FireSpray fireSpray, Location loc, Vector dir) {
this.player = player;
this.fireSpray = fireSpray;
this.location = loc;
this.direction = dir;
this.lineOfSightRange = 2;
this.ticksLived = 0;
this.startingLocation = loc.clone();
this.range = 20;
}
public void update() {
if (this.startingLocation.distance(this.location) > this.range)
this.ticksLived = fireSpray.getMaximumTick();
if (this.ticksLived > fireSpray.getMaximumTick())
return;
this.ticksLived++;
this.location.add(this.direction.normalize().multiply(this.speed));
this.speed -= 0.01;
Vector newDirection = this.direction.clone();
newDirection.add(seperationVector());
newDirection.add(alignmentVector());
Vector turnVector = newDirection.clone().subtract(this.direction).multiply(this.turnSpeed);
this.direction.add(turnVector);
this.direction.add(gravity);
}
public void show() {
if (this.ticksLived > fireSpray.getMaximumTick())
return;
player.getWorld().spawnParticle(Particle.SPELL, this.location, 0);
for (Entity e : GeneralMethods.getEntitiesAroundPoint(this.location, 1.5)) {
if (e instanceof LivingEntity && !e.getUniqueId().equals(player.getUniqueId())) {
//DamageHandler.damageEntity(e, 1, fireSpray);
e.setVelocity(e.getVelocity().add(alignmentVector()));
}
}
}
public Vector seperationVector() {
Vector seperationVector = new Vector(0, 0, 0);
for (FireSpray fs : CoreAbility.getAbilities(FireSpray.class)) {
if (fs.getPlayer().getWorld().equals(player.getWorld()))
for (Boid b : fs.getBoids()) {
if (b.getLocation().distance(this.location) <= this.lineOfSightRange) {
seperationVector.add(calculatePushVector(b, fs.getPlayer().getUniqueId().equals(player.getUniqueId()) ? 1 : 5));
}
}
}
for (Block b : GeneralMethods.getBlocksAroundPoint(this.location, this.lineOfSightRange)) {
if (GeneralMethods.isSolid(b)) {
seperationVector.add(calculatePushVector(b, 1));
}
}
return seperationVector;
}
public Vector alignmentVector() {
Vector alignmentVector = new Vector(0, 0, 0);
for (Boid b : fireSpray.getBoids()) {
if (b.getLocation().distance(this.location) <= this.lineOfSightRange) {
alignmentVector.add(b.getDirection());
}
}
alignmentVector.normalize().multiply(alignmentPower);
return alignmentVector;
}
public int getTicksLived() {
return this.ticksLived;
}
public Vector calculatePushVector(Boid b, double power) {
double push = this.pushPower * (1/(b.getLocation().distanceSquared(this.getLocation()) + 0.1)) * power;
return this.getLocation().clone().toVector().subtract(b.getLocation().clone().toVector()).multiply(push);
}
public Vector calculatePushVector(Block b, double power) {
double push = this.blockPushPower * (1/(b.getLocation().add(0.5, 0.5, 0.5).distanceSquared(this.getLocation()) + 0.1)) * power;
return this.location.clone().toVector().subtract(b.getLocation().clone().toVector()).multiply(push);
}
public Location getLocation() {
return this.location;
}
public Vector getDirection() {
return this.direction;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment