//====================================================================== /// <summary> /// Start an attack set by player on a target. /// </summary> /// <param name="target">Target.</param> public void PlayerAttack(Attackable target) { // Can't attack targets out of range if not movable. if (!_mov && !_agg.WithinWeaponRange(target)) { Debug.Log("Target out of range"); return; } // Remove any previous player set destination. if (_mov) { _mov.Remove(DestType.PlayerSet); } // Start attacking. _agg.Attack(target); // Start PlayerAttack chasing stream. Chase enemy to death. Destination dest = null; Transform tgtTrans = target.transform; Observable.EveryUpdate() .TakeWhile(_ => this.SameTarget(target) && target.Alive && this.PlayerAttackReachable(target) ) .Do(_ => { if (!_agg.WithinWeaponRange(target)) { // Chase enemy dest = this.AddPlayerAttackDest(tgtTrans.position); } else { // Clear destination _mov.Remove(DestType.PlayerAtt); } }) .DoOnCompleted(() => { // Clear target _agg.Release(target); if (_mov) { // Clear PlayerAttack destination _mov.Remove(dest); } }) .TakeUntilDestroy(gameObject) .Subscribe(); }
/// <summary>Start an auto attack.</summary> private void AutoAttack(Attackable target) { // Start attacking Debug.Log(sel.name + " Auto attacking " + target.sel.name); _agg.Attack(target); // Set AutoReturn destination if movable. if (_mov) { if (_mov.Dest != null && _mov.Dest.Type == DestType.PlayerSet) { _returnPos = _mov.Dest.Position; } else { if (_returnPos == Vector3.zero) { _returnPos = _trans.position; } } } // Start AutoAttack chasing stream. Chase enemy while it is within // autoChaseRange. Transform tgtTrans = target.transform; Destination dest = null; Observable.EveryUpdate() .TakeUntilDestroy(gameObject) .TakeWhile(_ => this.SameTarget(target) && target.Alive && this.AutoAttackReachable(target) ) .Do(_ => { if (!_agg.WithinWeaponRange(target)) { // Chase enemy dest = this.AddAutoAttackDest(tgtTrans.position); } else if (_mov) { // Clear destination _mov.Remove(DestType.AutoAtt); _mov.Remove(DestType.AutoAttRetrun); } }) .DoOnCompleted(() => { // Clear target _agg.Release(target); if (_mov) { // Cleat autoAttack destination _mov.Remove(dest); if (_mov.Dest == null && _returnPos != Vector3.zero && _returnPos != _trans.position) { // Add AutoReturn destination this.AddReturnDest(); } } }) .Subscribe(); }