void EnemyEntity::Update(float timeStep)
{
    if(isBusy && isDead)
    {
        CurrentTime+= timeStep;
        if(CurrentTime > timeChargue)
        {
            using namespace EnemyDied;
            VariantMap& eventData = GetEventDataMap();
            eventData[P_NODE] = node_;
            SendEvent(E_ENEMYDIED, eventData);
            isBusy = false;
        }
        return;
    }

    if(isDead)
        return;

    UpdateCast();
    RigidBody2D* body = GetComponent<RigidBody2D>();
    float y = body->GetLinearVelocity().y_;

    if(isBusy)
    {
        CurrentTime+= timeStep;
        if(isCharge)
        {
            if(CurrentTime > timeChargue)
            {
                isCharge = false;
                Shoot();
                isCooldDown = true;
                timeCoolDown = 1;
            }
        }
        if(CurrentTime > timeBusy)
        {
            isBusy = false;
            CurrentTime = 0;
        }
    }
    else
    {
        AnimatedSprite2D* animatesprite = GetComponent<AnimatedSprite2D>();
        if(isCooldDown)
        {
            if(animatesprite->GetAnimation()!= "idle")
            {
                animatesprite->SetAnimation("idle", LM_DEFAULT);
            }
            CurrentTime+= timeStep;
            if(CurrentTime > timeCoolDown)
                isCooldDown = false;
        }
        else
        {
            if(animatesprite->GetAnimation()!= "walk")
            {
                animatesprite->SetAnimation("walk", LM_DEFAULT);
            }
            body->SetLinearVelocity(Vector2::ZERO);
            body->SetLinearVelocity(Vector2(vel.x_, y));
        }
    }
}
void EnemyEntity::UpdateCast()
{
    DebugRenderer* debug = GetScene()->GetComponent<DebugRenderer>();
    PhysicsWorld2D* physicsWorld = GetScene()->GetComponent<PhysicsWorld2D>();
    RigidBody2D* body = GetComponent<RigidBody2D>();

    /*cast ground*/
    Vector2 originpos = node_->GetPosition2D();
    Vector2 FinishC;
    Vector2 FinishD;

    if(!isLeft)
    {
        FinishC = originpos + Vector2(-0.60,-0.35);
        FinishD = originpos + Vector2(-0.60,0);
    }

    else
    {
        FinishC = originpos + Vector2(0.60,-0.35);
        FinishD = originpos + Vector2(0.60,0);
    }


    PhysicsRaycastResult2D RayCastC;
    physicsWorld->RaycastSingle(RayCastC, originpos , FinishC,1);
    if ( !RayCastC.body_ && body->GetLinearVelocity().y_ >= -0.05f)
    {
        SetReturn();
    }

    PhysicsRaycastResult2D RayCastD;
    physicsWorld->RaycastSingle(RayCastD, originpos , FinishD,1);
    if ( RayCastD.body_)
    {
        SetReturn();
    }
    /*end cast ground*/

    /*cast player*/
    if(!isBusy && !isCooldDown)
    {
        Node* target = GetScene()->GetChild("Player",false);
        if(target)
        {
            Vector2 targetpos = target->GetPosition2D();
            Vector2 direction = targetpos - originpos;
            direction.Normalize();
            Vector2 FinishT = originpos + Vector2(direction.x_*2.5f,direction.y_*2.5);
            PhysicsRaycastResult2D RayCastT;
            physicsWorld->RaycastSingle(RayCastT, originpos , FinishT, 4);
            if (RayCastT.body_)
            {
                Node* node = RayCastT.body_->GetNode();
                if(node->GetName() == "Player")
                {
                    if(direction.x_ < 0)
                        setRight();
                    else
                        setLeft();
                    SetAtack();
                }
            }

            debug->AddLine( Vector3(originpos.x_, originpos.y_,0),
                        Vector3(FinishT.x_, FinishT.y_,0),
                        Color(0, 1, 1, 1),
                        false );

        }

    }

    /*end castplayer*/
    debug->AddLine( Vector3(originpos.x_, originpos.y_,0),
                            Vector3(FinishC.x_, FinishC.y_,0),
                            Color(1, 0, 0, 1),
                            false );

    debug->AddLine( Vector3(originpos.x_, originpos.y_,0),
                            Vector3(FinishD.x_, FinishD.y_,0),
                            Color(1, 0, 0, 1),
                            false );

}