Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.freeforums.geforce.SnipingMod.entitys;
- import io.netty.buffer.ByteBuf;
- import java.util.List;
- import javax.vecmath.Vector3f;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.init.Blocks;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.scoreboard.Team;
- import net.minecraft.util.DamageSource;
- import net.minecraft.util.EntityDamageSourceIndirect;
- import net.minecraft.util.MathHelper;
- import net.minecraft.util.MovingObjectPosition;
- import net.minecraft.util.Vec3;
- import net.minecraft.world.World;
- import cpw.mods.fml.common.FMLCommonHandler;
- import cpw.mods.fml.common.network.ByteBufUtils;
- import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
- import cpw.mods.fml.relauncher.Side;
- public class EntitySniperBullet extends Entity
- {
- private static int bulletLife = 600; //Kill bullets after 30 seconds
- public Entity owner;
- private int ticksInAir;
- public float damage;
- public boolean shotgun = false;
- public EntitySniperBullet(World world)
- {
- super(world);
- ticksInAir = 0;
- setSize(0.5F, 0.5F);
- }
- /** Private partial constructor to avoid repeated code */
- private EntitySniperBullet(World world, EntityLivingBase shooter, float gunDamage)
- {
- this(world);
- owner = shooter;
- damage = gunDamage;
- }
- /** Method called by ItemGun for creating bullets from a hand held weapon */
- public EntitySniperBullet(World world, EntityLivingBase shooter, float spread, float gunDamage, float speed, boolean shot)
- {
- this(world, Vec3.createVectorHelper(shooter.posX, shooter.posY + shooter.getEyeHeight(), shooter.posZ), shooter.rotationYaw, shooter.rotationPitch, shooter, spread, gunDamage, speed);
- shotgun = shot;
- }
- /** Machinegun / AAGun bullet constructor */
- public EntitySniperBullet(World world, Vec3 origin, float yaw, float pitch, EntityLivingBase shooter, float spread, float gunDamage)
- {
- this(world, origin, yaw, pitch, shooter, spread, gunDamage, 3.0F);
- }
- /** More generalised bullet constructor */
- public EntitySniperBullet(World world, Vec3 origin, float yaw, float pitch, EntityLivingBase shooter, float spread, float gunDamage, float speed)
- {
- this(world, shooter, gunDamage);
- damage = gunDamage;
- setLocationAndAngles(origin.xCoord, origin.yCoord, origin.zCoord, yaw, pitch);
- float offset = 0F;//(1F - FlansModClient.zoomProgress) * 0.16F;
- posX -= MathHelper.cos((rotationYaw / 180F) * 3.141593F) * offset;
- posY -= 0F;//0.10000000149011612D * (1F - FlansModClient.zoomProgress);
- posZ -= MathHelper.sin((rotationYaw / 180F) * 3.141593F) * offset;
- setPosition(posX, posY, posZ);
- yOffset = 0.0F;
- motionX = -MathHelper.sin((rotationYaw / 180F) * 3.141593F) * MathHelper.cos((rotationPitch / 180F) * 3.141593F);
- motionZ = MathHelper.cos((rotationYaw / 180F) * 3.141593F) * MathHelper.cos((rotationPitch / 180F) * 3.141593F);
- motionY = -MathHelper.sin((rotationPitch / 180F) * 3.141593F);
- setArrowHeading(motionX, motionY, motionZ, spread / 2F, speed);
- }
- /** */
- public EntitySniperBullet(World world, Vector3f origin, Vector3f direction, EntityLivingBase shooter, float spread, float gunDamage, float speed)
- {
- this(world, shooter, gunDamage);
- damage = gunDamage;
- setPosition(origin.x, origin.y, origin.z);
- motionX = direction.x;
- motionY = direction.y;
- motionZ = direction.z;
- setArrowHeading(motionX, motionY, motionZ, spread, speed);
- }
- /** Bomb constructor. Inherits the motion and rotation of the plane */
- public EntitySniperBullet(World world, Vec3 origin, float yaw, float pitch, double motX, double motY, double motZ, EntityLivingBase shooter, float gunDamage)
- {
- super(world);
- ticksInAir = 0;
- owner = shooter;
- damage = gunDamage;
- setSize(0.5F, 0.5F);
- setLocationAndAngles(origin.xCoord, origin.yCoord, origin.zCoord, yaw, pitch);
- setPosition(posX, posY, posZ);
- yOffset = 0.0F;
- motionX = motX;
- motionY = motY;
- motionZ = motZ;
- }
- protected void entityInit()
- {
- }
- public void setArrowHeading(double d, double d1, double d2, float spread, float speed)
- {
- float f2 = MathHelper.sqrt_double(d * d + d1 * d1 + d2 * d2);
- d /= f2;
- d1 /= f2;
- d2 /= f2;
- d += rand.nextGaussian() * 0.005D * spread;
- d1 += rand.nextGaussian() * 0.005D * spread;
- d2 += rand.nextGaussian() * 0.005D * spread;
- d *= speed;
- d1 *= speed;
- d2 *= speed;
- motionX = d;
- motionY = d1;
- motionZ = d2;
- float f3 = MathHelper.sqrt_double(d * d + d2 * d2);
- prevRotationYaw = rotationYaw = (float) ((Math.atan2(d, d2) * 180D) / 3.1415927410125732D);
- prevRotationPitch = rotationPitch = (float) ((Math.atan2(d1, f3) * 180D) / 3.1415927410125732D);
- }
- public void setVelocity(double d, double d1, double d2)
- {
- motionX = d;
- motionY = d1;
- motionZ = d2;
- if (prevRotationPitch == 0.0F && prevRotationYaw == 0.0F)
- {
- float f = MathHelper.sqrt_double(d * d + d2 * d2);
- prevRotationYaw = rotationYaw = (float) ((Math.atan2(d, d2) * 180D) / 3.1415927410125732D);
- prevRotationPitch = rotationPitch = (float) ((Math.atan2(d1, f) * 180D) / 3.1415927410125732D);
- setLocationAndAngles(posX, posY, posZ, rotationYaw, rotationPitch);
- }
- }
- public void onUpdate()
- {
- super.onUpdate();
- if(ticksExisted > bulletLife)
- {
- setDead();
- }
- //Iterate over all EntityDriveables
- for(int i = 0; i < worldObj.loadedEntityList.size(); i++)
- {
- Object obj = worldObj.loadedEntityList.get(i);
- }
- //Ray trace the bullet by comparing its next position to its current position
- Vec3 posVec = Vec3.createVectorHelper(posX, posY, posZ);
- Vec3 nextPosVec = Vec3.createVectorHelper(posX + motionX, posY + motionY, posZ + motionZ);
- MovingObjectPosition hit = worldObj.func_147447_a(posVec, nextPosVec, false, true, true);
- //Reset the position vectors since the ray tracer messes them up
- posVec = Vec3.createVectorHelper(posX, posY, posZ);
- nextPosVec = Vec3.createVectorHelper(posX + motionX, posY + motionY, posZ + motionZ);
- //If there is something in the way of the bullet's motion, put the bullet at that position next tick
- if(hit != null)
- {
- nextPosVec = Vec3.createVectorHelper(hit.hitVec.xCoord, hit.hitVec.yCoord, hit.hitVec.zCoord);
- }
- //If we hit something
- if (!isDead && hit != null)
- {
- //If the hit wasn't an entity hit, then it must've been a block hit
- if(hit.entityHit == null)
- {
- int xTile = hit.blockX;
- int yTile = hit.blockY;
- int zTile = hit.blockZ;
- Block block = worldObj.getBlock(xTile, yTile, zTile);
- Material mat = block.getMaterial();
- //If the bullet breaks glass, and can do so according to FlansMod, do so.
- }
- }
- //If the bullet was not stopped by a block
- if(!isDead)
- {
- //Iterate over entities close to the bullet to see if any of them have been hit and hit them
- List list = worldObj.getEntitiesWithinAABBExcludingEntity(this, boundingBox.addCoord(motionX, motionY, motionZ).expand(0.5F, 0.5F, 0.5F));
- for (int l = 0; l < list.size(); l++)
- {
- Entity checkEntity = (Entity) list.get(l);
- //Stop the bullet hitting stuff that can't be collided with or the person shooting immediately after firing it
- if (!checkEntity.canBeCollidedWith() || checkEntity == owner) //TODO !checkEntity.canBeCollidedWith() ||
- {
- System.out.println("Can not be collided with!");
- continue;
- }
- //Calculate the hit damage
- float hitDamage = damage;
- //Create a damage source object
- //DamageSource damagesource = owner == null ? DamageSource.generic : getBulletDamage();
- DamageSource damagesource = DamageSource.generic;
- //Attack the entity!
- if(checkEntity.attackEntityFrom(damagesource, hitDamage))
- {
- System.out.println("Attacking entity!");
- //If the attack was allowed and the entity is alive, we should remove their immortality cooldown so we can shoot them again. Without this, any rapid fire gun become useless
- if (checkEntity instanceof EntityLivingBase)
- {
- ((EntityLivingBase) checkEntity).arrowHitTimer++;
- ((EntityLivingBase) checkEntity).hurtResistantTime = ((EntityLivingBase) checkEntity).maxHurtResistantTime / 2;
- }
- //Yuck.
- //PacketDispatcher.sendPacketToAllAround(posX, posY, posZ, 50, dimension, PacketPlaySound.buildSoundPacket(posX, posY, posZ, type.hitSound, true));
- }
- //Unless the bullet penetrates, kill it
- if(checkEntity != null && checkEntity instanceof EntityLivingBase)
- {
- setPosition(checkEntity.posX, checkEntity.posY, checkEntity.posZ);
- setDead();
- break;
- }
- }
- }
- //Apply motion
- posX += motionX;
- posY += motionY;
- posZ += motionZ;
- //Recalculate the angles from the new motion
- float motionXZ = MathHelper.sqrt_double(motionX * motionX + motionZ * motionZ);
- rotationYaw = (float) ((Math.atan2(motionX, motionZ) * 180D) / 3.1415927410125732D);
- rotationPitch = (float) ((Math.atan2(motionY, motionXZ) * 180D) / 3.1415927410125732D);
- //Reset the range of the angles
- for (; rotationPitch - prevRotationPitch < -180F; prevRotationPitch -= 360F){}
- for (; rotationPitch - prevRotationPitch >= 180F; prevRotationPitch += 360F){}
- for (; rotationYaw - prevRotationYaw < -180F; prevRotationYaw -= 360F){}
- for (; rotationYaw - prevRotationYaw >= 180F; prevRotationYaw += 360F){}
- rotationPitch = prevRotationPitch + (rotationPitch - prevRotationPitch) * 0.2F;
- rotationYaw = prevRotationYaw + (rotationYaw - prevRotationYaw) * 0.2F;
- //Movement dampening variables
- float drag = 0.99F;
- float gravity = 0.02F;
- //If the bullet is in water, spawn particles and increase the drag
- if (isInWater())
- {
- for(int i = 0; i < 4; i++)
- {
- float bubbleMotion = 0.25F;
- worldObj.spawnParticle("bubble", posX - motionX * bubbleMotion, posY - motionY * bubbleMotion, posZ - motionZ * bubbleMotion, motionX, motionY, motionZ);
- }
- drag = 0.8F;
- }
- motionX *= drag;
- motionY *= drag;
- motionZ *= drag;
- motionY -= gravity * 0.05F; //TODO
- setPosition(posX, posY, posZ);
- }
- public void writeEntityToNBT(NBTTagCompound tag)
- {
- if (owner == null)
- tag.setString("owner", "null");
- else
- tag.setString("owner", owner.getCommandSenderName());
- }
- public void readEntityFromNBT(NBTTagCompound tag)
- {
- String ownerName = tag.getString("owner");
- if (ownerName != null && !ownerName.equals("null")){
- owner = FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().getPlayerForUsername(ownerName);
- }
- }
- public float getShadowSize()
- {
- return 0.1F;
- }
- public int getBrightnessForRender(float par1)
- {
- int i = MathHelper.floor_double(this.posX);
- int j = MathHelper.floor_double(this.posZ);
- if (this.worldObj.blockExists(i, 0, j))
- {
- double d0 = (this.boundingBox.maxY - this.boundingBox.minY) * 0.66D;
- int k = MathHelper.floor_double(this.posY - (double)this.yOffset + d0);
- return this.worldObj.getLightBrightnessForSkyBlocks(i, k, j, 0);
- }
- else
- {
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment