make zombie npcs actually look at the player

This commit is contained in:
Ben 2016-04-03 23:49:22 +01:00
parent 3ff5e6d337
commit a1f284670a
2 changed files with 52 additions and 28 deletions

View File

@ -5,6 +5,28 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Giant;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;
import net.minecraft.server.v1_8_R3.AxisAlignedBB; import net.minecraft.server.v1_8_R3.AxisAlignedBB;
import net.minecraft.server.v1_8_R3.EntityBat; import net.minecraft.server.v1_8_R3.EntityBat;
import net.minecraft.server.v1_8_R3.EntityCreature; import net.minecraft.server.v1_8_R3.EntityCreature;
@ -23,29 +45,6 @@ import net.minecraft.server.v1_8_R3.PathfinderGoalRandomLookaround;
import net.minecraft.server.v1_8_R3.PathfinderGoalSelector; import net.minecraft.server.v1_8_R3.PathfinderGoalSelector;
import net.minecraft.server.v1_8_R3.WorldServer; import net.minecraft.server.v1_8_R3.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Giant;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;
public class UtilEnt public class UtilEnt
{ {
@ -259,6 +258,34 @@ public class UtilEnt
} }
} }
public static void Rotate(LivingEntity entity, float yaw, float pitch)
{
EntityLiving handle = ((CraftLivingEntity) entity).getHandle();
while (yaw < -180.0F) yaw += 360.0F;
while (yaw >= 180.0F) yaw -= 360.0F;
handle.yaw = yaw;
handle.aK = yaw;
handle.aI = yaw;
handle.aL = yaw;
handle.pitch = pitch;
}
public static void LookAt(LivingEntity entity, Location location)
{
if (!(entity.getWorld().equals(location.getWorld())))
return;
Vector dir = entity.getEyeLocation().toVector().subtract(location.toVector()).normalize();
Location loc = entity.getEyeLocation().clone();
loc.setYaw(180 - (float) Math.toDegrees(Math.atan2(dir.getX(), dir.getZ())));
loc.setPitch(90 - (float) Math.toDegrees(Math.acos(dir.getY())));
Rotate(entity, loc.getYaw(), loc.getPitch());
}
public static void populate() public static void populate()
{ {
if (creatureMap.isEmpty()) if (creatureMap.isEmpty())

View File

@ -127,16 +127,13 @@ public class AttackEnemyObjective extends OrderedObjective<ClansMainTutorial>
return; return;
} }
Vector vec = UtilAlg.getTrajectory(shooter.getLocation(), player.getEyeLocation()); UtilEnt.LookAt(shooter, player.getEyeLocation());
((CraftZombie) shooter).getHandle().yaw = UtilAlg.GetYaw(vec);
((CraftZombie) shooter).getHandle().pitch = UtilAlg.GetPitch(vec);
if (Recharge.Instance.usable(player, "ShotBy" + shooter.getUniqueId().toString())) if (Recharge.Instance.usable(player, "ShotBy" + shooter.getUniqueId().toString()))
{ {
Arrow arrow = shooter.shootArrow(); Arrow arrow = shooter.shootArrow();
arrow.setVelocity(UtilAlg.getTrajectory(arrow.getLocation(), player.getLocation()).multiply(1.3)); arrow.setVelocity(UtilAlg.getTrajectory(arrow.getLocation(), player.getEyeLocation()).multiply(1.6));
Recharge.Instance.use(player, "ShotBy" + shooter.getUniqueId().toString(), 500 + UtilMath.r(2000), false, false); Recharge.Instance.use(player, "ShotBy" + shooter.getUniqueId().toString(), 500 + UtilMath.r(2000), false, false);
} }