package common.ai;

import common.entity.npc.EntityHaunter;
import common.entity.types.EntityLiving;

public class EntityAIExplode extends EntityAIBase
{
    EntityHaunter entity;
    EntityLiving target;

    public EntityAIExplode(EntityHaunter entity)
    {
        this.entity = entity;
        this.setMutexBits(1);
    }

    /**
     * Returns whether the EntityAIBase should begin execution.
     */
    public boolean shouldExecute()
    {
        EntityLiving entitylivingbase = this.entity.getAttackTarget();
        return this.entity.getExplodeState() > 0 || entitylivingbase != null && this.entity.getDistanceSqToEntity(entitylivingbase) < 9.0D;
    }

    /**
     * Execute a one shot task or start executing a continuous task
     */
    public void startExecuting()
    {
        this.entity.getNavigator().clearPathEntity();
        this.target = this.entity.getAttackTarget();
    }

    /**
     * Resets the task
     */
    public void resetTask()
    {
        this.target = null;
    }

    /**
     * Updates the task
     */
    public void updateTask()
    {
        if (this.target == null)
        {
            this.entity.setExplodeState(-1);
        }
        else if (this.entity.getDistanceSqToEntity(this.target) > 49.0D)
        {
            this.entity.setExplodeState(-1);
        }
        else if (!this.entity.getEntitySenses().canSee(this.target))
        {
            this.entity.setExplodeState(-1);
        }
        else
        {
            this.entity.setExplodeState(1);
        }
    }
}
