예제 #1
0
    void Start()
    {
        // Range - Max in Exclusive and not inclusize https://docs.unity3d.com/ScriptReference/Random.Range.html
        Func <int> plusMinusGenerator = () => UnityEngine.Random.Range(0, 2) * 2 - 1;

        _randomMovingDir = plusMinusGenerator();

        ObservableCollision2DTrigger collisionObservable = gameObject.GetComponent <ObservableCollision2DTrigger>();

        collisionObservable = collisionObservable == null?gameObject.AddComponent <ObservableCollision2DTrigger>() : collisionObservable;

        collisionObservable
        .OnCollisionEnter2DAsObservable()
        .Where(coll => coll.gameObject.GetComponent <BoxCollider2D>() != null)
        .Select(coll => coll.gameObject.GetComponent <BoxCollider2D>())
        .Where(boxColl => groundLayers.Any(layer => boxColl.IsTouchingLayers(layer.value)))
        .Select(_ => plusMinusGenerator())
        .Subscribe(randDirection => _randomMovingDir = randDirection);
    }
예제 #2
0
    void Start()
    {
        ObservableCollision2DTrigger collisionObservable = gameObject.GetComponent <ObservableCollision2DTrigger>();

        collisionObservable = collisionObservable == null?gameObject.AddComponent <ObservableCollision2DTrigger>() : collisionObservable;

        LifeObservable = collisionObservable
                         .OnCollisionEnter2DAsObservable()
                         .Where(coll => coll.gameObject.GetComponent <DamageInfo>() != null)
                         .Where(coll => TagsToIgnoreOnCollision.All(tag => coll.gameObject.tag != tag.ToString()))
                         .Select(coll => coll.gameObject.GetComponent <DamageInfo>().DamageHitPoints)
                         .Do(damadgeHitPoints => Life -= damadgeHitPoints)
                         .Select(_ => Life)
                         .Share();

        IsCountingStarted.OnCompleted(); // Publish that the LifeObservable is initialized

        LifeObservable.Where(currentLife => currentLife <= 0)
        .Subscribe(_ => Destroy(gameObject));
    }