Jump to content

[SOLVED, DAMMIT] Mob attack delay


DoorCloser

Recommended Posts

How can i make delays between the attack of Mob. I tried to look at some mobs, like: IronGolem, Creeper. I tried out their part of codes and that didnt work. So after the first attack, mob have to wait like 2 secs before next attack. How can i do so? Here is the code.

 

 

 

package halflifemod.entity.monster;

 

import halflifemod.client.model.ModelGonome;

import halflifemod.client.model.ModelHeadcrab;

import halflifemod.main.mod_HalflifeMod;

 

import org.lwjgl.opengl.GL11;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.entity.Entity;

import net.minecraft.entity.EnumCreatureAttribute;

import net.minecraft.entity.ai.EntityAISwimming;

import net.minecraft.entity.monster.EntityMob;

import net.minecraft.entity.monster.EntitySlime;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.Item;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.util.DamageSource;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

 

public class EntityHeadcrab extends EntityMob

{

private int attackTimer;

    public EntityHeadcrab(World par1World)

    {

   

        super(par1World);

        this.texture = "/mob/HL/Headcrab.png";

        this.setSize(0.6F, 0.4F);

        this.moveSpeed = 0.23F;

        this.getNavigator().setAvoidsWater(true);

    }

 

    protected void entityInit()

    {

        super.entityInit();

        this.dataWatcher.addObject(16, new Byte((byte)0));

    }

 

    public int getMaxHealth()

    {

        return 16;

    }

 

   

    protected int getAttackStrength()

    {

        return 1;

    }

   

    protected boolean canDamagePlayer()

    {

        return true;

    }

 

    protected Entity findPlayerToAttack()

    {

            double d0 = 10.0D;

            return this.worldObj.getClosestVulnerablePlayerToEntity(this, d0);

    }

   

    public boolean attackEntityAsMob(Entity par1Entity)

    {

        this.attackTimer = 10;

        this.worldObj.setEntityState(this, (byte)4);

        boolean flag = par1Entity.attackEntityFrom(DamageSource.causeMobDamage(this), 3 + this.rand.nextInt(15));

 

        if (flag)

        {

            par1Entity.motionY += 0.4000000059604645D;

        }

        this.playSound("headcrab.hc_headbite", 0.5F, 1.0F);

        return flag;

    }

 

    protected String getLivingSound()

    {

        return "headcrab.hc_say";

    }

 

    protected String getHurtSound()

    {

        return "headcrab.hc_hurt";

    }

 

    protected String getDeathSound()

    {

        return "headcrab.hc_die";

    }

   

    protected void attackEntity(Entity par1Entity, float par2)

    {

        float f1 = this.getBrightness(1.0F);

 

        if (f1 > 0.5F && this.rand.nextInt(100) == 0)

        {

            this.entityToAttack = null;

        }

        else

        {

            if (par2 > 2.0F && par2 < 6.0F && this.rand.nextInt(10) == 0)

            { 

                if (this.onGround)

                {

                this.playSound("headcrab.hc_attack", 1.0F, 1.0F);

                    double d0 = par1Entity.posX - this.posX;

                    double d1 = par1Entity.posZ - this.posZ;

                    float f2 = MathHelper.sqrt_double(d0 * d0 + d1 * d1);

                    this.motionX = d0 / (double)f2 * 1.2D * 0.800000011920929D + this.motionX * 0.20000000298023224D;

                    this.motionZ = d1 / (double)f2 * 1.2D * 0.800000011920929D + this.motionZ * 0.20000000298023224D;

                    this.motionY = 0.5600000059604645D;

                }

            }

            else

            {

                super.attackEntity(par1Entity, par2);

            }         

        }

    }

     

    protected void dropFewItems(boolean par1, int par2)

    {

        super.dropFewItems(par1, par2);

 

        int j = this.rand.nextInt(3) + this.rand.nextInt(1 + par2);

        int k;

 

        for (k = 0; k < j; ++k)

        {

            this.dropItem(mod_HalflifeMod.headcrabSting.itemID, 1);

        }

    }

   

    public EnumCreatureAttribute getCreatureAttribute()

    {

        return EnumCreatureAttribute.ARTHROPOD;

    }

 

    public boolean isPotionApplicable(PotionEffect par1PotionEffect)

    {

        return par1PotionEffect.getPotionID() == Potion.poison.id ? true : super.isPotionApplicable(par1PotionEffect);

    }

}

 

 

 

 

Really need this guys, help!

Link to comment
Share on other sites

If you are coding your own mob, you can use attackTime as a built-in timer. It decrements automatically each update tick (from EntityLivingBase), so when the mob attacks, set this.attackTime = 40; and check if (attackTime == 0) before you allow the mob to attack.

 

I tried to put it all like that

 

 

package halflifemod.entity.monster;

 

import halflifemod.client.model.ModelGonome;

import halflifemod.client.model.ModelHeadcrab;

import halflifemod.main.mod_HalflifeMod;

 

import org.lwjgl.opengl.GL11;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.entity.Entity;

import net.minecraft.entity.EnumCreatureAttribute;

import net.minecraft.entity.ai.EntityAISwimming;

import net.minecraft.entity.monster.EntityMob;

import net.minecraft.entity.monster.EntitySlime;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.Item;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.util.DamageSource;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

 

public class EntityHeadcrab extends EntityMob

{

private int attackTimer;

    public EntityHeadcrab(World par1World)

    {

   

        super(par1World);

        this.texture = "/mob/HL/Headcrab.png";

        this.setSize(0.6F, 0.4F);

        this.moveSpeed = 0.23F;

        this.getNavigator().setAvoidsWater(true);

    }

 

    protected void entityInit()

    {

        super.entityInit();

        this.dataWatcher.addObject(16, new Byte((byte)0));

    }

 

    public int getMaxHealth()

    {

        return 16;

    }

 

   

    protected int getAttackStrength()

    {

        return 1;

    }

   

    protected boolean canDamagePlayer()

    {

        return true;

    }

   

    public void onLivingUpdate()

    {

        super.onLivingUpdate();

 

        if (this.attackTime > 0)

        {

            --this.attackTime;

        }

    }

 

    protected Entity findPlayerToAttack()

    {

            double d0 = 10.0D;

            return this.worldObj.getClosestVulnerablePlayerToEntity(this, d0);

    }

   

    public boolean attackEntityAsMob(Entity par1Entity)

    {

    this.attackTime = 40;

        this.worldObj.setEntityState(this, (byte)4);

        boolean flag = par1Entity.attackEntityFrom(DamageSource.causeMobDamage(this), 3 + this.rand.nextInt(15));

 

        if (flag)

        {

            par1Entity.motionY += 0.4000000059604645D;

        }

        this.playSound("headcrab.hc_headbite", 0.5F, 1.0F);

        return flag;

    }

 

    protected String getLivingSound()

    {

        return "headcrab.hc_say";

    }

 

    protected String getHurtSound()

    {

        return "headcrab.hc_hurt";

    }

 

    protected String getDeathSound()

    {

        return "headcrab.hc_die";

    }

   

    protected void attackEntity(Entity par1Entity, float par2)

    {

        float f1 = this.getBrightness(1.0F);

 

        if (f1 > 0.5F && this.rand.nextInt(100) == 0)

        {

            this.entityToAttack = null;

        }

        else

        {

            if (par2 > 2.0F && par2 < 6.0F && this.rand.nextInt(10) == 0)

            { 

                if (this.onGround)

                {

               

                this.playSound("headcrab.hc_attack", 1.0F, 1.0F);

                    double d0 = par1Entity.posX - this.posX;

                    double d1 = par1Entity.posZ - this.posZ;

                    float f2 = MathHelper.sqrt_double(d0 * d0 + d1 * d1);

                    this.motionX = d0 / (double)f2 * 1.2D * 0.800000011920929D + this.motionX * 0.20000000298023224D;

                    this.motionZ = d1 / (double)f2 * 1.2D * 0.800000011920929D + this.motionZ * 0.20000000298023224D;

                    this.motionY = 0.5600000059604645D;

                }

            }

            else

            {

                super.attackEntity(par1Entity, par2);

            }         

        }

    }

   

    @SideOnly(Side.CLIENT)

    public void handleHealthUpdate(byte par1)

    {

        if (par1 == 4)

        {

            this.attackTime = 40;

           

        }

        else

        {

            super.handleHealthUpdate(par1);

        }

    }

   

    @SideOnly(Side.CLIENT)

    public int getAttackTimer()

    {

        return this.attackTime;

    }

    protected void dropFewItems(boolean par1, int par2)

    {

        super.dropFewItems(par1, par2);

 

        int j = this.rand.nextInt(3) + this.rand.nextInt(1 + par2);

        int k;

 

        for (k = 0; k < j; ++k)

        {

            this.dropItem(mod_HalflifeMod.headcrabSting.itemID, 1);

        }

    }

   

    public EnumCreatureAttribute getCreatureAttribute()

    {

        return EnumCreatureAttribute.ARTHROPOD;

    }

 

    public boolean isPotionApplicable(PotionEffect par1PotionEffect)

    {

        return par1PotionEffect.getPotionID() == Potion.poison.id ? true : super.isPotionApplicable(par1PotionEffect);

    }

}

 

 

 

 

For an example, IronGolem have the same.

Link to comment
Share on other sites

Now that he says it, i realize that coolalias is right, your issue is that you are not checking to see if attack time is >= 0 before it tries to attack, so it doesnt get stopped by the timer

 

Well, i finally understand. Here is the code.

protected void attackEntity(Entity par1Entity, float par2)
    {
            if (par2 > 2.0F && par2 < 6.0F && this.rand.nextInt(10) == 0)
            {   
                if (this.onGround)
                {
                	this.attackTimer++;
                	if(this.attackTimer > 3)
                	{
                    double d0 = par1Entity.posX - this.posX;
                    double d1 = par1Entity.posZ - this.posZ;
                    float f2 = MathHelper.sqrt_double(d0 * d0 + d1 * d1);
                    this.motionX = d0 / (double)f2 * 1.2D * 0.800000011920929D + this.motionX * 0.20000000298023224D;
                    this.motionZ = d1 / (double)f2 * 1.2D * 0.800000011920929D + this.motionZ * 0.20000000298023224D;
                    this.motionY = 0.5600000059604645D;
                    this.attackTimer = 0;
                    this.playSound("headcrab.hc_attack", 1.0F, 1.0F);
                	}
                }
            }
            else
            {
                super.attackEntity(par1Entity, par2);
            }          
        }

 

So, finally with if(attackTimer > 10) i can create the delay between attacks. THANKS ALOT MATES! THAT WAS VERY USEFUL!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.