Advertisement
Corosus

Untitled

Jan 7th, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.16 KB | None | 0 0
  1. package combat.entity.block;
  2.  
  3. import java.util.List;
  4.  
  5. public class MovingBlock extends Entity implements IEntityAdditionalSpawnData
  6. {
  7.  
  8.     //Generic moving block
  9.    
  10.     //Needs:
  11.     //- DW for rotationYaw and Pitch
  12.     //- DW for state to use for various visual changes on client
  13.     //-- ideas: spawned/freefalling, exploding/dying, unactive, fading out
  14.    
  15.    
  16.     public boolean noCollision = false;
  17.     public int age = 0;
  18.     public int blockID = 0;
  19.     public int blockMeta = 0;
  20.     public float gravity = 0.04F;
  21.     public float speedSlowing = 0.99F;
  22.     public int blockifyDelay = 30;
  23.    
  24.     public float rotationYawB = 0;
  25.     public float rotationPitchB = 0;
  26.     public float rotationRoll = 0;
  27.     public float prevRotationRoll = 0;
  28.     public float rotationYawVel = 0;
  29.     public float rotationPitchVel = 0;
  30.    
  31.     public int state = 0; //generic state var
  32.     public ChunkCoordinates coordsLastAir;
  33.     public boolean blockToEntCollision = true;
  34.    
  35.     public boolean firstTick = true;
  36.    
  37.     public float scale = 1F;
  38.    
  39.     //targeting
  40.     /*public EntityLivingBase target;
  41.     public float targetTillDist = -1;
  42.     public double targPrevPosX;
  43.     public double targPrevPosY;
  44.     public double targPrevPosZ;*/
  45.    
  46.     //synced data for render
  47.     /*public int blockNum;
  48.     public int blockRow;
  49.     public boolean createParticles = false;*/
  50.    
  51.     public MovingBlock(World var1)
  52.     {
  53.         super(var1);
  54.         setSize(scale, scale);
  55.         this.setPosition(this.posX, this.posY, this.posZ);
  56.     }
  57.  
  58.     public MovingBlock(World var1, int parBlockID, int parMeta)
  59.     {
  60.         super(var1);
  61.         blockID = parBlockID;
  62.         blockMeta = parMeta;
  63.         setSize(scale, scale);
  64.         this.setPosition(this.posX, this.posY, this.posZ);
  65.     }
  66.    
  67.     //TESTING COLLISION STUFF
  68.     @Override
  69.     public AxisAlignedBB getCollisionBox(Entity par1Entity) {
  70.         return super.getCollisionBox(par1Entity);
  71.     }
  72.    
  73.     @Override
  74.     public AxisAlignedBB getBoundingBox() {
  75.         return super.getBoundingBox();
  76.     }
  77.    
  78.     @Override
  79.     public float getCollisionBorderSize() {
  80.         return super.getCollisionBorderSize();
  81.     }
  82.    
  83.     @Override
  84.     public void applyEntityCollision(Entity par1Entity) {
  85.         super.applyEntityCollision(par1Entity);
  86.     }
  87.  
  88.     @Override
  89.     public void writeSpawnData(ByteArrayDataOutput data)
  90.     {
  91.         data.writeInt(blockID);
  92.         data.writeInt(blockMeta);
  93.         data.writeFloat(gravity);
  94.         data.writeInt(blockifyDelay);
  95.     }
  96.  
  97.     @Override
  98.     public void readSpawnData(ByteArrayDataInput data)
  99.     {
  100.         blockID = data.readInt();
  101.         blockMeta = data.readInt();
  102.         gravity = data.readFloat();
  103.         blockifyDelay = data.readInt();
  104.     }
  105.  
  106.     @Override
  107.     public boolean isInRangeToRenderDist(double var1)
  108.     {
  109.         return true;
  110.     }
  111.  
  112.     @Override
  113.     public float getShadowSize()
  114.     {
  115.         return 0.0F;
  116.     }
  117.  
  118.     @Override
  119.     public boolean isInRangeToRenderVec3D(Vec3 asd)
  120.     {
  121.         return true;
  122.     }
  123.  
  124.     @Override
  125.     public boolean canTriggerWalking()
  126.     {
  127.         return false;
  128.     }
  129.  
  130.     @Override
  131.     public void entityInit() {
  132.         this.dataWatcher.addObject(2, Float.valueOf(rotationYawB));
  133.         this.dataWatcher.addObject(3, Float.valueOf(rotationPitchB));
  134.         this.dataWatcher.addObject(4, Float.valueOf(rotationRoll));
  135.         this.dataWatcher.addObject(5, Integer.valueOf(state));
  136.         this.dataWatcher.addObject(6, Float.valueOf(scale));
  137.     }
  138.  
  139.     @Override
  140.     public boolean canBePushed()
  141.     {
  142.         return !this.isDead;
  143.     }
  144.  
  145.     @Override
  146.     public boolean canBeCollidedWith()
  147.     {
  148.         return !this.isDead && !this.noCollision;
  149.     }
  150.  
  151.     @Override
  152.     public void onUpdate()
  153.     {
  154.         //super.onUpdate();
  155.         boolean superTick = true;
  156.         if (superTick) {
  157.             super.onUpdate();
  158.         } else {
  159.             this.prevDistanceWalkedModified = this.distanceWalkedModified;
  160.             this.prevPosX = this.posX;
  161.             this.prevPosY = this.posY;
  162.             this.prevPosZ = this.posZ;
  163.             this.prevRotationPitch = this.rotationPitch = this.rotationPitchB;
  164.             this.prevRotationYaw = this.rotationYaw = this.rotationYawB;
  165.             this.prevRotationRoll = this.rotationRoll;
  166.         }
  167.        
  168.         ++this.age;
  169.        
  170.         //datawatchers
  171.         if (worldObj.isRemote) {
  172.             rotationYaw = rotationYawB = dataWatcher.getWatchableObjectFloat(2);
  173.             rotationPitch = rotationPitchB = dataWatcher.getWatchableObjectFloat(3);
  174.             rotationRoll = dataWatcher.getWatchableObjectFloat(4);
  175.             state = dataWatcher.getWatchableObjectInt(5);
  176.             scale = dataWatcher.getWatchableObjectFloat(6);
  177.         } else {
  178.             dataWatcher.updateObject(2, rotationYawB);
  179.             dataWatcher.updateObject(3, rotationPitchB);
  180.             dataWatcher.updateObject(4, rotationRoll);
  181.             dataWatcher.updateObject(5, state);
  182.             dataWatcher.updateObject(6, scale);
  183.            
  184.             if (firstTick) {
  185.                 firstTick = false;
  186.                 PacketDispatcher.sendPacketToAllInDimension(new Packet34EntityTeleport(entityId, this.myEntitySize.multiplyBy32AndRound(posX), this.myEntitySize.multiplyBy32AndRound(posY), this.myEntitySize.multiplyBy32AndRound(posZ), (byte)0, (byte)0), worldObj.provider.dimensionId);
  187.             }
  188.         }
  189.        
  190.         //Main movement
  191.         this.motionX *= (double)speedSlowing;
  192.         this.motionY *= (double)speedSlowing;
  193.         this.motionZ *= (double)speedSlowing;
  194.         this.motionY -= (double)gravity;
  195.        
  196.         this.posX += this.motionX;
  197.         this.posY += this.motionY;
  198.         this.posZ += this.motionZ;
  199.        
  200.         this.setPosition(this.posX, this.posY, this.posZ);
  201.        
  202.         this.rotationPitchB += this.rotationPitchVel;
  203.         this.rotationYawB += this.rotationYawVel;
  204.        
  205.         if (!worldObj.isRemote) {
  206.            
  207.             if (posY < 0) {
  208.                 this.setDead();
  209.                 return;
  210.             }
  211.            
  212.             int curX = MathHelper.floor_double(posX);
  213.             int curY = MathHelper.floor_double(posY);
  214.             int curZ = MathHelper.floor_double(posZ);
  215.             int idCurPos = worldObj.getBlockId(curX, curY, curZ);
  216.            
  217.             if (blockifyDelay != -1 && age > blockifyDelay) {
  218.                
  219.                 //should always raytrace ahead if motion > 1
  220.                
  221.                 int aheadEndX = MathHelper.floor_double(posX + (motionX));
  222.                 int aheadEndY = MathHelper.floor_double(posY + (motionY));
  223.                 int aheadEndZ = MathHelper.floor_double(posZ + (motionZ));
  224.                
  225.                 int id = worldObj.getBlockId(aheadEndX, aheadEndY, aheadEndZ);
  226.                 //System.out.println(idCurPos);
  227.                
  228.                 if (isSolid(id)) {
  229.                     Vec3 motion = Vec3.createVectorHelper(motionX, motionY, motionZ);
  230.                     double aheadDistEnd = motion.lengthVector();
  231.                     motion = motion.normalize();
  232.                    
  233.                     for (double curDist = 0; curDist < aheadDistEnd; curDist += 0.5D) {
  234.                         int aheadX = MathHelper.floor_double(posX + (motion.xCoord*curDist));
  235.                         int aheadY = MathHelper.floor_double(posY + (motion.yCoord*curDist));
  236.                         int aheadZ = MathHelper.floor_double(posZ + (motion.zCoord*curDist));
  237.                         int idCheck = worldObj.getBlockId(aheadX, aheadY, aheadZ);
  238.                        
  239.                         if (isSolid(idCheck)) {
  240.                             if (curDist < 1D) {
  241.                                 //System.out.println("new solidify close!");
  242.                                 //blockify(aheadX, aheadY, aheadZ);
  243.                                 //break;
  244.                             } else {
  245.                                
  246.                             }
  247.                            
  248.                             curDist -= 0.5D;
  249.                            
  250.                             int tryX = MathHelper.floor_double(posX + (motion.xCoord*curDist));
  251.                             int tryY = MathHelper.floor_double(posY + (motion.yCoord*curDist));
  252.                             int tryZ = MathHelper.floor_double(posZ + (motion.zCoord*curDist));
  253.                             int idTry = worldObj.getBlockId(tryX, tryY, tryZ);
  254.                             if (!isSolid(idTry)) {
  255.                                 //System.out.println("new solidify pull back!");
  256.                                 blockify(tryX, tryY, tryZ);
  257.                             } else {
  258.                                 //fail
  259.                                 //System.out.println("solidify fail");
  260.                                 this.setDead();
  261.                             }
  262.                             break;
  263.                         }
  264.                     }
  265.                    
  266.                     //blockify(coordsLastAir.posX, coordsLastAir.posY, coordsLastAir.posZ);
  267.                     //System.out.println("blockify - " + curX + " - " + curY + " - " + curZ);
  268.                 }
  269.                
  270.                
  271.                    
  272.             }
  273.            
  274.             /*if (idCurPos == 0 || Block.blocksList[idCurPos].blockMaterial == Material.snow || Block.blocksList[idCurPos].blockMaterial == Material.plants) {
  275.                 coordsLastAir = new ChunkCoordinates(curX, curY, curZ);
  276.             }
  277.            
  278.             if (blockifyDelay != -1) {
  279.                 if (idCurPos != 0 && worldObj.getBlockTileEntity(curX, curY, curZ) == null && isSolid(idCurPos)) {
  280.                     //final check to help verify its falling into solid ground
  281.                     if (worldObj.getBlockId(curX, curY+1, curZ) != 0) {
  282.                         blockify(curX, curY, curZ);
  283.                         //System.out.println("blockify safety - " + curX + " - " + curY + " - " + curZ);
  284.                     }
  285.                    
  286.                 }
  287.             }*/
  288.            
  289.             tickCollisionEntities();
  290.            
  291.            
  292.             //temp
  293.             //setDead();
  294.            
  295.         } else {
  296.             //temp?
  297.             //motionY = 0.00F;
  298.         }
  299.        
  300.         //if (gravity > 0) setDead();
  301.         //setDead();
  302.        
  303.         //so temp
  304.         //posY = 72F;
  305.        
  306.         //onGround = true;
  307.        
  308.         //taken from super onUpdate()
  309.     }
  310.    
  311.     public void tickCollisionEntities() {
  312.         blockToEntCollision = true;
  313.        
  314.         if (blockToEntCollision) {
  315.             double size = 0.5D;
  316.             List entities = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.expand(size, size, size));
  317.            
  318.             for (int i = 0; entities != null && i < entities.size(); ++i)
  319.             {
  320.                 Entity var10 = (Entity)entities.get(i);
  321.                
  322.                 if (var10 != null) {
  323.                     if (!var10.isDead) {
  324.                         if (var10 instanceof EntityLivingBase) {
  325.                             var10.attackEntityFrom(DamageSource.causeIndirectMagicDamage(this, this), 4);
  326.                         } else {
  327.                             double speed = Math.sqrt(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
  328.                             //double speed2 = Math.sqrt(var10.motionX * var10.motionX + var10.motionY * var10.motionY + var10.motionZ * var10.motionZ);
  329.                             if (speed < 0.3D) {
  330.                                 moveAway(this, var10, (float)speed * 0.5F);
  331.                                 //break; //hmmmmm
  332.                             }
  333.                         }
  334.                     }
  335.                 }
  336.             }
  337.         }
  338.        
  339.     }
  340.    
  341.     public boolean isSolid(int id) {
  342.         return (id != 0 && Block.blocksList[id].blockMaterial != Material.water && Block.blocksList[id].blockMaterial != Material.circuits && Block.blocksList[id].blockMaterial != Material.snow && Block.blocksList[id].blockMaterial != Material.plants && Block.blocksList[id].blockMaterial.isSolid());
  343.     }
  344.    
  345.     public void setPositionAndRotation(double par1, double par3, double par5, float par7, float par8, float parRoll)
  346.     {
  347.         this.prevPosX = this.posX = par1;
  348.         this.prevPosY = this.posY = par3;
  349.         this.prevPosZ = this.posZ = par5;
  350.         this.prevRotationYaw = this.rotationYaw = this.rotationYawB = par7;
  351.         this.prevRotationPitch = this.rotationPitch = this.rotationPitchB = par8;
  352.         this.prevRotationRoll = this.rotationRoll = parRoll;
  353.         this.ySize = 0.0F;
  354.         double d3 = (double)(this.prevRotationYaw - par7);
  355.  
  356.         if (d3 < -180.0D)
  357.         {
  358.             this.prevRotationYaw += 360.0F;
  359.         }
  360.  
  361.         if (d3 >= 180.0D)
  362.         {
  363.             this.prevRotationYaw -= 360.0F;
  364.         }
  365.  
  366.         this.setPosition(this.posX, this.posY, this.posZ);
  367.         this.setRotation(par7, par8, parRoll);
  368.     }
  369.    
  370.     protected void setRotation(float par1, float par2, float parRoll)
  371.     {
  372.         this.rotationYaw = par1 % 360.0F;
  373.         this.rotationPitch = par2 % 360.0F;
  374.         this.rotationRoll = parRoll % 360.0F;
  375.     }
  376.    
  377.     public Vec3 getMoveAwayVector(Entity ent, Entity targ) {
  378.         float vecX = (float) (ent.posX - targ.posX);
  379.         float vecY = (float) (ent.posY - targ.posY);
  380.         float vecZ = (float) (ent.posZ - targ.posZ);
  381.  
  382.         float dist2 = (float)Math.sqrt(vecX * vecX + vecY * vecY + vecZ * vecZ);
  383.         return Vec3.createVectorHelper(vecX / dist2, vecY / dist2, vecZ / dist2);
  384.     }
  385.    
  386.     public Vector3f getMoveAwayVector3f(Entity ent, Entity targ) {
  387.         float vecX = (float) (ent.posX - targ.posX);
  388.         float vecY = (float) (ent.posY - targ.posY);
  389.         float vecZ = (float) (ent.posZ - targ.posZ);
  390.  
  391.         float dist2 = (float)Math.sqrt(vecX * vecX + vecY * vecY + vecZ * vecZ);
  392.         return new Vector3f(vecX / dist2, vecY / dist2, vecZ / dist2);
  393.     }
  394.    
  395.     public void moveAway(Entity ent, Entity targ, float speed) {
  396.         double vecX = ent.posX - targ.posX;
  397.         double vecY = ent.posY - targ.posY;
  398.         double vecZ = ent.posZ - targ.posZ;
  399.  
  400.         double dist2 = (double)Math.sqrt(vecX * vecX + vecY * vecY + vecZ * vecZ);
  401.         ent.motionX += vecX / dist2 * speed;
  402.         ent.motionY += vecY / dist2 * speed;
  403.         ent.motionZ += vecZ / dist2 * speed;
  404.     }
  405.    
  406.     public void moveTowards(Entity ent, Entity targ, float speed) {
  407.         double vecX = targ.posX - ent.posX;
  408.         double vecY = targ.posY - ent.posY;
  409.         double vecZ = targ.posZ - ent.posZ;
  410.  
  411.         double dist2 = (double)Math.sqrt(vecX * vecX + vecY * vecY + vecZ * vecZ);
  412.         ent.motionX += vecX / dist2 * speed;
  413.         ent.motionY += vecY / dist2 * speed;
  414.         ent.motionZ += vecZ / dist2 * speed;
  415.     }
  416.    
  417.     public void triggerOwnerDied() {
  418.         blockifyDelay = 1;
  419.         gravity = 0.03F;
  420.         double speed = 0.3F;
  421.         motionX = rand.nextGaussian()*speed - rand.nextGaussian()*speed;
  422.         motionY = 0.3F + rand.nextGaussian()*speed - rand.nextGaussian()*speed;
  423.         motionZ = rand.nextGaussian()*speed - rand.nextGaussian()*speed;
  424.     }
  425.    
  426.     public void blockify(int x, int y, int z) {
  427.         worldObj.setBlock(x, y, z, blockID);
  428.         setDead();
  429.     }
  430.    
  431.     @Override
  432.     public boolean attackEntityFrom(DamageSource par1DamageSource, float par2) {
  433.         setDead();
  434.         return super.attackEntityFrom(par1DamageSource, par2);
  435.     }
  436.    
  437.     @SideOnly(Side.CLIENT)
  438.     public void spawnParticles() {
  439.         /*for (int i = 0; i < 1; i++) {
  440.            
  441.             float speed = 0.1F;
  442.             float randPos = 8.0F;
  443.             float ahead = 2.5F;
  444.            
  445.             EntityMeteorTrailFX particle = new EntityMeteorTrailFX(worldObj,
  446.                     posX,
  447.                     posY,
  448.                     posZ, motionX, 0.25F, motionZ, 0, posX, posY, posZ);
  449.            
  450.             particle.maxScale = 3F;
  451.             particle.setMaxAge(100);
  452.             particle.motionX = (rand.nextFloat()*2-1) * speed;
  453.             particle.motionY = (rand.nextFloat()*2-1) * 0.1F;
  454.             particle.motionZ = (rand.nextFloat()*2-1) * speed;
  455.  
  456.             particle.spawnAsWeatherEffect();
  457.         }*/
  458.     }
  459.  
  460.     @Override
  461.     protected void readEntityFromNBT(NBTTagCompound data) {
  462.         blockID = data.getInteger("blockID");
  463.         blockMeta = data.getInteger("blockMeta");
  464.         blockifyDelay = data.getInteger("blockifyDelay");
  465.         gravity = data.getFloat("gravity");
  466.     }
  467.  
  468.     @Override
  469.     protected void writeEntityToNBT(NBTTagCompound data) {
  470.         data.setInteger("blockID", blockID);
  471.         data.setInteger("blockMeta", blockMeta);
  472.         data.setInteger("blockifyDelay", blockifyDelay);
  473.         data.setFloat("gravity", gravity);
  474.     }
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement