Beispiel #1
0
    public Vector3 GetIntendedDirection()
    {
        var inRange = GameCache
                      .Instance
                      .FindActive <MonsterController>(_ => (_.Position - _towerController.Position).sqrMagnitude < _towerController.Range * _towerController.Range);

        MonsterController target = null;
        var desc = new Aiming3D();

        foreach (var controller in inRange)
        {
            var currentDesc = new Aiming3D(controller, _towerController);
            currentDesc.ComputeMobDirection();

            if ((ReferenceEquals(null, target)
                                ? 0f
                                : desc.CurrentPosLerpFactor) > currentDesc.CurrentPosLerpFactor)
            {
                continue;
            }

            target = controller;
            desc   = currentDesc;
        }

        if (!ReferenceEquals(null, target))
        {
            _desc = desc;
            _desc.ComputeMobPrediction();
            _desc.ComputeTowerDirection();
        }

                #if UNITY_EDITOR
        if (!ReferenceEquals(null, target))
        {
            var aimColor = IsAiming
                                ? Color.red
                                : Color.gray;

            if (_counter++ % 50 == 0)
            {
                // predict time
                Debug.DrawLine(
                    target.Position + _desc.ToPathDistance.normalized,
                    target.Position + _desc.ToPathDistance.normalized + _desc.ToPathDistance.normalized * _desc.IntervalTillCollision,
                    Color.black,
                    10f);

                // projectile path till collision
                Debug.DrawLine(
                    _towerController.ShootOrigin.position,
                    _towerController.ShootOrigin.position + _towerController.ProjectileInitialSpeed.ProjectToXZ().ProjectToXZ(_towerController.transform.position.y) * _desc.IntervalTillCollision,
                    Color.white,
                    10f);
            }

            // target path
            Debug.DrawLine(
                target.Position,
                // check both
                target.Position + (_desc.IntersectCoDir - _desc.IntersectOpDir) * _desc.CurrentPosLerpFactor,
                aimColor);

            Gizmos.Point(_towerController.Position + _desc.IntersectCoDir, Color.white, .1f);
            Gizmos.Point(_towerController.Position + _desc.IntersectOpDir, Color.black, .1f);

            // aiming
            Debug.DrawLine(
                _towerController.Position,
                _towerController.Position + _desc.ToMobDist.ProjectToXZ().ProjectToXZ(0),
                Color.gray);
            Debug.DrawLine(
                _towerController.Position,
                _towerController.Position + _desc.ToMobPredictDir.ProjectToXZ().ProjectToXZ(0),
                aimColor);

            // mob path till collision
            Debug.DrawLine(
                target.Position,
                target.Position + target.Speed * _desc.IntervalTillCollision,
                Color.white);
        }

        // tower direction
        Debug.DrawLine(
            _towerController.Position,
            _towerController.Position + _desc.TowerDirection,
            Color.yellow);
#endif
        return(_desc.TowerDirection);
    }
Beispiel #2
0
 public CannonAutoDriver(CannonTowerController towerController)
 {
     _towerController = towerController;
     _desc            = new Aiming3D(null, towerController);
 }