Example #1
0
    public static void Subscribe(MonoBehaviour mono)
    {
        mono.UpdateAsObservable()
        .Where(_ => Input.GetMouseButtonDown(0))
        .Where(_ => EventSystem.current.currentSelectedGameObject == null)
        .Subscribe(_ => TapDownPoint = Input.mousePosition)
        .AddTo(mono);

        mono.UpdateAsObservable()
        .Where(_ => Input.GetMouseButtonUp(0))
        .Where(_ => EventSystem.current.currentSelectedGameObject == null)
        .Subscribe(_ => TapUp.OnNext(Input.mousePosition))
        .AddTo(mono);

        TapUp
        .Where(point => Vector3.Distance(TapDownPoint, point) <= 24f)
        .Select(point => Camera.main.ScreenPointToRay(point))
        .Select(ray => Physics.RaycastAll(ray, 1000f))
        .Select(hits => hits.Where(hit => hit.collider != null))
        .Subscribe(hits => TapRaycastHits.OnNext(hits))
        .AddTo(mono);

        mono.UpdateAsObservable()
        .Where(_ => Input.GetMouseButtonDown(1))
        .Subscribe(_ => Tap(Input.mousePosition))
        .AddTo(mono);

        TapRaycastHits
        .Select(hits =>
        {
            var hitGrounds = hits.Where(hit => hit.collider.tag.Split('/').Any(tag => tag == "Ground"));
            var hitTargets = hits.Where(hit => hit.collider.tag.Split('/').Any(tag => tag == "Character"));
            if (hitGrounds.Any() && hitTargets.Any())
            {
                return(new Command(hitGrounds.First().point, hitTargets.First().collider.gameObject));
            }
            else if (hitGrounds.Any())
            {
                return(new Command(hitGrounds.First().point, null));
            }
            else if (hitTargets.Any())
            {
                var target = hitTargets.First().collider.gameObject;
                Debug.Log(target);
                return(new Command(target.transform.position, target));
            }
            else
            {
                return(null);
            }
        })
        .Where(command => command != null)
        .Subscribe(command => OnNext(command))
        .AddTo(mono);

        mono.UpdateAsObservable().Subscribe(_ =>
        {
            for (int i = 0; i < ActionTypes.Count() && i < ActionButtonManager.Buttons.Count(); i++)
            {
                var keyCode    = KeyCode.Alpha1 + i;
                var isTapSkill = IsTapSkills[i];

                if (Input.GetKeyDown(keyCode) && !isTapSkill)
                {
                    OnNext(ActionTypes[i]);
                }

                if (Input.GetKeyDown(keyCode) && isTapSkill)
                {
                    OnNext(ActionTypes[i]);
                    Tap(Input.mousePosition);
                }
            }
        });

        ActionTypes = new ActionType[]
        {
            ActionType.NormalPulse,
            ActionType.None,
            ActionType.None,
            ActionType.None,
            ActionType.None,
            ActionType.None,
            ActionType.None,
            ActionType.None,
        };

        for (int i = 0; i < ActionTypes.Count() && i < ActionButtonManager.Buttons.Count(); i++)
        {
            var type = ActionTypes[i];

            ActionButtonManager.Buttons[i]?.OnClickAsObservable()
            .Subscribe(_ => OnNext(type));
        }
    }