EntityProjectile



  • Bonsoir,

    J'ai créer une entity projectile pour une arme dans mon mod.

    Je rencontre deux problèmes:

    -1) J'arrive pas a augmenté la vitesse du projectile.

    -2) Le projectile décent progressivement une fois tiré (comme la flèche), je voudrais qu'il reste droit comme les fireball.

    Je vous donne la classe de mon entity:

    
    package com.CSC.net.Entity;
    
    import java.util.List;
    
    import javax.annotation.Nullable;
    
    import net.minecraft.block.Block;
    import net.minecraft.block.material.Material;
    import net.minecraft.block.state.IBlockState;
    import net.minecraft.client.model.ModelBase;
    import net.minecraft.enchantment.EnchantmentHelper;
    import net.minecraft.entity.Entity;
    import net.minecraft.entity.EntityLivingBase;
    import net.minecraft.entity.IProjectile;
    import net.minecraft.entity.monster.EntityEnderman;
    import net.minecraft.entity.passive.EntityTameable;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.entity.projectile.EntityArrow;
    import net.minecraft.entity.projectile.EntityThrowable;
    import net.minecraft.init.Blocks;
    import net.minecraft.init.SoundEvents;
    import net.minecraft.item.ItemStack;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.network.datasync.DataParameter;
    import net.minecraft.network.datasync.DataSerializers;
    import net.minecraft.network.datasync.EntityDataManager;
    import net.minecraft.network.play.server.SPacketChangeGameState;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraft.util.DamageSource;
    import net.minecraft.util.EntitySelectors;
    import net.minecraft.util.EnumParticleTypes;
    import net.minecraft.util.ResourceLocation;
    import net.minecraft.util.datafix.DataFixer;
    import net.minecraft.util.math.AxisAlignedBB;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.math.MathHelper;
    import net.minecraft.util.math.RayTraceResult;
    import net.minecraft.util.math.Vec3d;
    import net.minecraft.world.World;
    import net.minecraftforge.fml.relauncher.Side;
    import net.minecraftforge.fml.relauncher.SideOnly;
    
    import com.CSC.net.Model.ModelBullet;
    import com.google.common.base.Predicate;
    import com.google.common.base.Predicates;
    
    public class EntityBullet extends EntityThrowable
    {
    private int randomTilt;
    private boolean onfire;
    
    public EntityBullet(World world)
        {
            super(world);
            randomTilt = rand.nextInt(360);
            this.onfire = false;
            this.setSize(0.1F, 0.1F);
        }
    
        public EntityBullet(World world, EntityLivingBase entity)
        {
            super(world, entity);
            randomTilt = rand.nextInt(360);
            this.onfire = false;
            this.setSprinting(true);
            this.setSize(0.1F, 0.1F);
        }
    
        public EntityBullet(World world, double x, double y, double z, double a, double b, double c)
        {
            super(world, x, y, z);
            randomTilt = rand.nextInt(360);
            this.onfire = false;
            this.setSize(0.1F, 0.1F);
        }
    
        public void setOnFire(boolean val)
        {
        this.onfire = val;
        }
    
        public boolean getOnFire()
        {
        return this.onfire;
        }
    
        public int getRandomTilt()
        {
        return randomTilt;
        }
    
        public void setRandomTilt(int angle)
        {
        randomTilt = angle;
        }
    
        private void InflictDamage(RayTraceResult movingobjectPos)
        {
        movingobjectPos.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), 2);
        if(this.getOnFire())
        {    
        movingobjectPos.entityHit.setFire(3);
        }
        }
    
        private void DestroySelf()
        {
    this.worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
    this.setDead();
        }
    
        protected void onImpact(RayTraceResult movingobjectPos)
        {
        if (movingobjectPos.typeOfHit == RayTraceResult.Type.BLOCK)
        {
        Block block = this.worldObj.getBlockState(movingobjectPos.getBlockPos()).getBlock();
    
        /*if(block == Blocks.GLASS)
           {
        BlockPos blockpos = movingobjectPos.getBlockPos();    
        IBlockState blockstate = this.worldObj.getBlockState(blockpos);
        TileEntity te = this.worldObj.getTileEntity(blockpos);
    
        if(this.getThrower() instanceof EntityPlayer) // if the thrower is a player
               {
        EntityPlayer player = (EntityPlayer)this.getThrower();
        this.worldObj.destroyBlock(blockpos, false);
            block.harvestBlock(this.worldObj, player, blockpos, blockstate, te, player.getActiveItemStack());   
               }
        else if(this.getThrower() instanceof EntityTameable) // if the thrower is a tameable creature
        {    
        EntityTameable tameableEntity = (EntityTameable)this.getThrower();
            if(tameableEntity != null && tameableEntity.isTamed())
            {
            EntityPlayer player = (EntityPlayer)tameableEntity.getOwner();
            if(player != null)
            {
            this.worldObj.destroyBlock(blockpos, false);
                block.harvestBlock(this.worldObj, player, blockpos, blockstate, te, player.getActiveItemStack());
            }
            }    
        }    
           }*/
        /*else*/ if(block == Blocks.WATER)
        {
        this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
        }
        else
        {
        this.DestroySelf();
        }
        }
        else
        {    
        if (movingobjectPos.entityHit != null)
           {        
        if(this.getThrower() instanceof EntityPlayer) // if the thrower is a player
               {
        this.InflictDamage(movingobjectPos);
               }
               else if(this.getThrower() instanceof EntityTameable) // if the thrower is a tameable creature
               {            
                if(movingobjectPos.entityHit instanceof EntityTameable) // if the hit entity is a tameable
               {
                EntityTameable tameableEntity = (EntityTameable)movingobjectPos.entityHit;
                if(tameableEntity != null && !tameableEntity.isTamed())
                {
                this.InflictDamage(movingobjectPos);
                }
               }
                else if((movingobjectPos.entityHit instanceof EntityPlayer) == false)
                {
                this.InflictDamage(movingobjectPos);
                }            
               }
           }            
           this.DestroySelf();    
        }
        }
    
        public void setAim(Entity p_184547_1_, float p_184547_2_, float p_184547_3_, float p_184547_4_, float p_184547_5_, float p_184547_6_)
        {
            float f = -MathHelper.sin(p_184547_3_ * 0.017453292F) * MathHelper.cos(p_184547_2_ * 0.017453292F);
            float f1 = -MathHelper.sin(p_184547_2_ * 0.017453292F);
            float f2 = MathHelper.cos(p_184547_3_ * 0.017453292F) * MathHelper.cos(p_184547_2_ * 0.017453292F);
            this.setThrowableHeading((double)f, (double)f1, (double)f2, p_184547_5_, p_184547_6_);
            this.motionX += p_184547_1_.motionX;
            this.motionZ += p_184547_1_.motionZ;
    
            if (!p_184547_1_.onGround)
            {
                this.motionY += p_184547_1_.motionY;
            }
        }
    
    }
    
    

    Merci d'avance !


  • Administrateurs

    Salut,
    Pour résoudre tes deux problèmes il faudrait override la fonction onUpdate.



  • Salut,

    Quand j'override la fonction onUpdate sans rien ajouter forcément ça change rien.

    J'ai donc pris les codes de la fireball.

    J'ai réussis a augmenté la vitesse de mon entity mais elle tombe toujours comme les flèches, je cherches a faire une entity avec un trajectoire toute droite et pas en "courbe".

    Je te passe ma classe:

    
    package com.CSC.net.Entity;
    
    import java.util.List;
    
    import javax.annotation.Nullable;
    
    import net.minecraft.block.Block;
    import net.minecraft.block.material.Material;
    import net.minecraft.block.state.IBlockState;
    import net.minecraft.client.model.ModelBase;
    import net.minecraft.enchantment.EnchantmentHelper;
    import net.minecraft.entity.Entity;
    import net.minecraft.entity.EntityLivingBase;
    import net.minecraft.entity.IProjectile;
    import net.minecraft.entity.monster.EntityEnderman;
    import net.minecraft.entity.passive.EntityTameable;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.entity.projectile.EntityArrow;
    import net.minecraft.entity.projectile.EntityThrowable;
    import net.minecraft.entity.projectile.ProjectileHelper;
    import net.minecraft.init.Blocks;
    import net.minecraft.init.SoundEvents;
    import net.minecraft.item.ItemStack;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.network.datasync.DataParameter;
    import net.minecraft.network.datasync.DataSerializers;
    import net.minecraft.network.datasync.EntityDataManager;
    import net.minecraft.network.play.server.SPacketChangeGameState;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraft.util.DamageSource;
    import net.minecraft.util.EntitySelectors;
    import net.minecraft.util.EnumParticleTypes;
    import net.minecraft.util.ResourceLocation;
    import net.minecraft.util.datafix.DataFixer;
    import net.minecraft.util.math.AxisAlignedBB;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.math.MathHelper;
    import net.minecraft.util.math.RayTraceResult;
    import net.minecraft.util.math.Vec3d;
    import net.minecraft.world.World;
    import net.minecraftforge.fml.relauncher.Side;
    import net.minecraftforge.fml.relauncher.SideOnly;
    
    import com.CSC.net.Model.ModelBullet;
    import com.google.common.base.Predicate;
    import com.google.common.base.Predicates;
    
    public class EntityBullet extends EntityThrowable
    {
    private int randomTilt;
    private boolean onfire;
        public EntityLivingBase shootingEntity;
        private int ticksAlive;
        private int ticksInAir;
        private int xTile = -1;
        private int yTile = -1;
        private int zTile = -1;
        public double accelerationX;
        public double accelerationY;
        public double accelerationZ;
        private Block inTile;
    
    public EntityBullet(World world)
        {
            super(world);
            randomTilt = rand.nextInt(360);
            this.onfire = false;
            this.setSize(0.1F, 0.1F);
        }
    
        public EntityBullet(World world, EntityLivingBase entity, double accelX, double accelY, double accelZ)
        {
            super(world, entity);
            randomTilt = rand.nextInt(360);
            this.onfire = false;
            this.setSprinting(true);
            this.setSize(0.1F, 0.1F);
            this.shootingEntity = entity;
            double d0 = (double)MathHelper.sqrt_double(accelX * accelX + accelY * accelY + accelZ * accelZ);
            this.accelerationX = accelX / d0 * 0.1D;
            this.accelerationY = accelY / d0 * 0.1D;
            this.accelerationZ = accelZ / d0 * 0.1D;
        }
    
        public EntityBullet(World world, double x, double y, double z, double a, double b, double c)
        {
            super(world, x, y, z);
            randomTilt = rand.nextInt(360);
            this.onfire = false;
            this.setSize(0.1F, 0.1F);
        }
    
        public void setOnFire(boolean val)
        {
        this.onfire = val;
        }
    
        public boolean getOnFire()
        {
        return this.onfire;
        }
    
        public int getRandomTilt()
        {
        return randomTilt;
        }
    
        public void setRandomTilt(int angle)
        {
        randomTilt = angle;
        }
    
        private void InflictDamage(RayTraceResult movingobjectPos)
        {
        movingobjectPos.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), 2);
        if(this.getOnFire())
        {    
        movingobjectPos.entityHit.setFire(3);
        }
        }
    
        private void DestroySelf()
        {
    this.worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
    this.setDead();
        }
    
        protected void onImpact(RayTraceResult movingobjectPos)
        {
        if (movingobjectPos.typeOfHit == RayTraceResult.Type.BLOCK)
        {
        Block block = this.worldObj.getBlockState(movingobjectPos.getBlockPos()).getBlock();
    
        /*if(block == Blocks.GLASS)
           {
        BlockPos blockpos = movingobjectPos.getBlockPos();    
        IBlockState blockstate = this.worldObj.getBlockState(blockpos);
        TileEntity te = this.worldObj.getTileEntity(blockpos);
    
        if(this.getThrower() instanceof EntityPlayer) // if the thrower is a player
               {
        EntityPlayer player = (EntityPlayer)this.getThrower();
        this.worldObj.destroyBlock(blockpos, false);
            block.harvestBlock(this.worldObj, player, blockpos, blockstate, te, player.getActiveItemStack());   
               }
        else if(this.getThrower() instanceof EntityTameable) // if the thrower is a tameable creature
        {    
        EntityTameable tameableEntity = (EntityTameable)this.getThrower();
            if(tameableEntity != null && tameableEntity.isTamed())
            {
            EntityPlayer player = (EntityPlayer)tameableEntity.getOwner();
            if(player != null)
            {
            this.worldObj.destroyBlock(blockpos, false);
                block.harvestBlock(this.worldObj, player, blockpos, blockstate, te, player.getActiveItemStack());
            }
            }    
        }    
           }*/
        /*else*/ if(block == Blocks.WATER)
        {
        this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
        }
        else
        {
        this.DestroySelf();
        }
        }
        else
        {    
        if (movingobjectPos.entityHit != null)
           {        
        if(this.getThrower() instanceof EntityPlayer) // if the thrower is a player
               {
        this.InflictDamage(movingobjectPos);
               }
               else if(this.getThrower() instanceof EntityTameable) // if the thrower is a tameable creature
               {            
                if(movingobjectPos.entityHit instanceof EntityTameable) // if the hit entity is a tameable
               {
                EntityTameable tameableEntity = (EntityTameable)movingobjectPos.entityHit;
                if(tameableEntity != null && !tameableEntity.isTamed())
                {
                this.InflictDamage(movingobjectPos);
                }
               }
                else if((movingobjectPos.entityHit instanceof EntityPlayer) == false)
                {
                this.InflictDamage(movingobjectPos);
                }            
               }
           }            
           this.DestroySelf();    
        }
        }
    
        public void setAim(Entity p_184547_1_, float p_184547_2_, float p_184547_3_, float p_184547_4_, float p_184547_5_, float p_184547_6_)
        {
            float f = -MathHelper.sin(p_184547_3_ * 0.017453292F) * MathHelper.cos(p_184547_2_ * 0.017453292F);
            float f1 = -MathHelper.sin(p_184547_2_ * 0.017453292F);
            float f2 = MathHelper.cos(p_184547_3_ * 0.017453292F) * MathHelper.cos(p_184547_2_ * 0.017453292F);
            this.setThrowableHeading((double)f, (double)f1, (double)f2, p_184547_5_, p_184547_6_);
            this.motionX += p_184547_1_.motionX;
            this.motionZ += p_184547_1_.motionZ;
    
            if (!p_184547_1_.onGround)
            {
                this.motionY += p_184547_1_.motionY;
            }
        }
    
        @Override
        public void onUpdate()
        {
        if (this.worldObj.isRemote || (this.shootingEntity == null || !this.shootingEntity.isDead) && this.worldObj.isBlockLoaded(new BlockPos(this)))
            {
                super.onUpdate();
                if (this.inGround)
                {
                    if (this.worldObj.getBlockState(new BlockPos(this.xTile, this.yTile, this.zTile)).getBlock() == this.inTile)
                    {
                        ++this.ticksAlive;
    
                        if (this.ticksAlive == 600)
                        {
                            this.setDead();
                        }
    
                        return;
                    }
    
                    this.inGround = false;
                    this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
                    this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
                    this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
                    this.ticksAlive = 0;
                }
                else
                {
                    ++this.ticksInAir;
                }
    
                RayTraceResult raytraceresult = ProjectileHelper.forwardsRaycast(this, true, this.ticksInAir >= 25, this.shootingEntity);
    
                if (raytraceresult != null)
                {
                    this.onImpact(raytraceresult);
                }
    
                this.posX += this.motionX;
                this.posY += this.motionY;
                this.posZ += this.motionZ;
                ProjectileHelper.rotateTowardsMovement(this, 0.2F);
                float f = this.getMotionFactor();
    
                if (this.isInWater())
                {
                    for (int i = 0; i < 4; ++i)
                    {
                        float f1 = 0.25F;
                        this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * 0.25D, this.posY - this.motionY * 0.25D, this.posZ - this.motionZ * 0.25D, this.motionX, this.motionY, this.motionZ, new int[0]);
                    }
    
                    f = 0.8F;
                }
    
                this.motionX += this.accelerationX;
                this.motionY += this.accelerationY;
                this.motionZ += this.accelerationZ;
                this.motionX *= (double)f;
                this.motionY *= (double)f;
                this.motionZ *= (double)f;
                this.setPosition(this.posX, this.posY, this.posZ);
            }
            else
            {
                this.setDead();
            }
        }
    
        protected float getMotionFactor()
        {
            return 0.95F;
        }
    
    }
    
    

  • Administrateurs

    Retires cette ligne :
    this.motionY *= (double)(this.rand.nextFloat() * 0.2F);

    et celle-ci :
    this.motionY += this.accelerationY;



  • @'robin4002':

    Retires cette ligne :
    this.motionY *= (double)(this.rand.nextFloat() * 0.2F);

    et celle-ci :
    this.motionY += this.accelerationY;

    Ah ouais merci 😄