191 lines
5.7 KiB
Java

package com.hbm.entity.projectile;
import com.hbm.items.ModItems;
import com.hbm.lib.ModDamageSource;
import com.hbm.packet.AuxParticlePacketNT;
import com.hbm.packet.PacketDispatcher;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.util.MovingObjectPosition.MovingObjectType;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
public class EntityCog extends EntityThrowableNT {
private int turnProgress;
private double syncPosX;
private double syncPosY;
private double syncPosZ;
private double syncYaw;
private double syncPitch;
@SideOnly(Side.CLIENT)
private double velocityX;
@SideOnly(Side.CLIENT)
private double velocityY;
@SideOnly(Side.CLIENT)
private double velocityZ;
public EntityCog(World world) {
super(world);
this.setSize(1F, 1F);
}
public EntityCog(World world, double x, double y, double z) {
super(world, x, y, z);
this.setSize(1F, 1F);
}
@Override
protected void entityInit() {
super.entityInit();
this.dataWatcher.addObject(10, new Integer(0));
}
public EntityCog setOrientation(int rot) {
this.dataWatcher.updateObject(10, rot);
return this;
}
public int getOrientation() {
return this.dataWatcher.getWatchableObjectInt(10);
}
@Override
public boolean interactFirst(EntityPlayer player) {
if(!worldObj.isRemote) {
if(player.inventory.addItemStackToInventory(new ItemStack(ModItems.gear_large)))
this.setDead();
player.inventoryContainer.detectAndSendChanges();
}
return false;
}
@Override
public boolean canBeCollidedWith() {
return true;
}
@Override
protected void onImpact(MovingObjectPosition mop) {
if(worldObj != null && mop != null && mop.typeOfHit == MovingObjectType.ENTITY && mop.entityHit.isEntityAlive()) {
Entity e = mop.entityHit;
e.attackEntityFrom(ModDamageSource.rubble, 1000);
if(!e.isEntityAlive() && e instanceof EntityLivingBase) {
NBTTagCompound vdat = new NBTTagCompound();
vdat.setString("type", "giblets");
vdat.setInteger("ent", e.getEntityId());
vdat.setInteger("cDiv", 5);
PacketDispatcher.wrapper.sendToAllAround(new AuxParticlePacketNT(vdat, e.posX, e.posY + e.height * 0.5, e.posZ), new TargetPoint(e.dimension, e.posX, e.posY + e.height * 0.5, e.posZ, 150));
worldObj.playSoundEffect(e.posX, e.posY, e.posZ, "mob.zombie.woodbreak", 2.0F, 0.95F + worldObj.rand.nextFloat() * 0.2F);
}
}
if(this.ticksExisted > 1 && worldObj != null && mop != null && mop.typeOfHit == MovingObjectType.BLOCK) {
int orientation = this.dataWatcher.getWatchableObjectInt(10);
if(orientation < 6) {
if(Vec3.createVectorHelper(motionX, motionY, motionZ).lengthVector() < 0.75) {
this.dataWatcher.updateObject(10, orientation + 6);
orientation += 6;
} else {
ForgeDirection side = ForgeDirection.getOrientation(mop.sideHit);
this.motionX *= 1 - (Math.abs(side.offsetX) * 2);
this.motionY *= 1 - (Math.abs(side.offsetY) * 2);
this.motionZ *= 1 - (Math.abs(side.offsetZ) * 2);
worldObj.createExplosion(this, posX, posY, posZ, 3F, false);
if(worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ).getExplosionResistance(this) < 50) {
worldObj.func_147480_a(mop.blockX, mop.blockY, mop.blockZ, false);
}
}
}
if(orientation >= 6) {
this.motionX = 0;
this.motionY = 0;
this.motionZ = 0;
this.inGround = true;
}
}
}
@Override
public void onUpdate() {
if(!worldObj.isRemote) {
int orientation = this.dataWatcher.getWatchableObjectInt(10);
if(orientation >= 6 && !this.inGround) {
this.dataWatcher.updateObject(10, orientation - 6);
}
super.onUpdate();
} else {
if(this.turnProgress > 0) {
double interpX = this.posX + (this.syncPosX - this.posX) / (double) this.turnProgress;
double interpY = this.posY + (this.syncPosY - this.posY) / (double) this.turnProgress;
double interpZ = this.posZ + (this.syncPosZ - this.posZ) / (double) this.turnProgress;
double d = MathHelper.wrapAngleTo180_double(this.syncYaw - (double) this.rotationYaw);
this.rotationYaw = (float) ((double) this.rotationYaw + d / (double) this.turnProgress);
this.rotationPitch = (float)((double)this.rotationPitch + (this.syncPitch - (double)this.rotationPitch) / (double)this.turnProgress);
--this.turnProgress;
this.setPosition(interpX, interpY, interpZ);
} else {
this.setPosition(this.posX, this.posY, this.posZ);
}
}
}
@SideOnly(Side.CLIENT)
public void setVelocity(double p_70016_1_, double p_70016_3_, double p_70016_5_) {
this.velocityX = this.motionX = p_70016_1_;
this.velocityY = this.motionY = p_70016_3_;
this.velocityZ = this.motionZ = p_70016_5_;
}
@SideOnly(Side.CLIENT)
public void setPositionAndRotation2(double x, double y, double z, float yaw, float pitch, int theNumberThree) {
this.syncPosX = x;
this.syncPosY = y;
this.syncPosZ = z;
this.syncYaw = yaw;
this.syncPitch = pitch;
this.turnProgress = theNumberThree;
this.motionX = this.velocityX;
this.motionY = this.velocityY;
this.motionZ = this.velocityZ;
}
@Override
@SideOnly(Side.CLIENT)
public boolean isInRangeToRenderDist(double distance) {
return true;
}
@Override
public double getGravityVelocity() {
return inGround ? 0 : 0.03D;
}
@Override
protected int groundDespawn() {
return 0;
}
}