Exemplo n.º 1
0
    /// <summary>
    /// Sets up our attack target, registers callback for its death and
    /// updates our state.
    /// If target is in range, attaks it, otherwise starts moving towards it.
    /// </summary>
    /// <param name="unit"></param>
    /// <returns>Returns true if target is in range, false otherwise</returns>
    public bool attackTarget <A>(GameEntity <A> entity, bool selfDefense) where A : struct, IConvertible
    {
        // HACK: Ideally, we should not have to consider an special case for WatchTower / LightHouse Revealer
        if (entity.info.isPseudoUnit)
        {
            Barrack barrack = entity.transform.GetComponentInParent <Barrack>();
            if (barrack.status != EntityStatus.DESTROYED)
            {
                return(attackTarget(barrack, selfDefense));
            }

            return(false);
        }

        // Note: Cast is redundant but avoids warning
        if (_target != (IGameEntity)entity)
        {
            // Stop attacking current target
            Selectable selectable = null;
            if (status == EntityStatus.ATTACKING)
            {
                selectable = _target.getGameObject().GetComponent <Selectable>();
                selectable.NotAttackedEntity();
                stopAttack();
            }

            // Register for DEAD/DESTROYED and HIDDEN
            _auto += entity.registerFatalWounds(onTargetDied);
            _auto += entity.GetComponent <FOWEntity>().register(FOWEntity.Actions.HIDDEN, onTargetHidden);

            _target      = entity;
            _selfDefense = selfDefense;
            _lastAttack  = Time.time - (1f / info.unitAttributes.attackRate);

            // Show target health
            selectable = _target.getGameObject().GetComponent <Selectable>();
            selectable.AttackedEntity();

            // Update distance for immediate usage (ie. canDoRangedAttack)
            updateDistanceToTarget();

            // Update status and return
            setStatus(EntityStatus.ATTACKING);
            return(true);
        }

        // TODO: Hack to get AI working
        return(true);
    }