示例#1
0
 void OnTriggerStay2D(Collider2D other)
 {
     if (other.gameObject.GetComponent(typeof(IPushable <Vector3>)) as IPushable <Vector3> != null)
     {
         IPushable <Vector3> iPushable = other.gameObject.GetComponent(typeof(IPushable <Vector3>)) as IPushable <Vector3>;
         iPushable.Push(velocity);
     }
 }
示例#2
0
 void OnTriggerExit2D(Collider2D other)
 {
     if (other.gameObject.GetComponent(typeof(IPushable <Vector3>)) as IPushable <Vector3> != null)
     {
         IPushable <Vector3> iPushable = other.gameObject.GetComponent(typeof(IPushable <Vector3>)) as IPushable <Vector3>;
         iPushable.Push(Vector3.zero);
     }
 }
 private void OnControllerColliderHit(ControllerColliderHit hit)
 {
     if (hit.gameObject.CompareTag("Player"))
     {
         IPushable collision = hit.gameObject.GetComponent <IPushable>();
         if (collision != null)
         {
             collision.Push(speedX, hit.moveDirection.y);
         }
     }
 }
示例#4
0
        public static void Check()
        {
            var dogs = new Stack <Dog>();

            dogs.Push(new Dog());

            //IPoppable<Animal> animals = dogs;

            IPushable <Animal> animals = new Stack <Animal>();
            IPushable <Cat>    cats    = animals; //allowed

            cats.Push(new Cat());
            //Stack<Animal> animals = dogs;
        }
示例#5
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!PhysicsHelper.IsInLayerMask(collision.gameObject, _layersToPush))
        {
            return;
        }

        IPushable pushable = collision.GetComponent <IPushable>();

        if (pushable != null)
        {
            Vector2 direction = (transform.position - collision.gameObject.transform.position) * -1;
            pushable.Push(direction, _knockbackAmount, _knockbackDuration);
        }
    }
示例#6
0
        public static void Main()
        {
            Stack <Dog> dogs = new Stack <Dog>();

            //Stack<Animal> animals = dogs; //error CS0029
            //AnimalCleaner.Wash(dogs); // error CS1503

            dogs.Push(new Dog());
            IPoppable <Animal> animals = dogs;

            AnimalCleaner.Wash(animals);

            ////////////////////////////////////////////////////
            IPushable <Animal> animals2 = new Stack <Animal>();
            IPushable <Dog>    dogs2    = animals2;

            dogs2.Push(new Dog());
        }
        private static void GenericTypesTests()
        {
            Console.WriteLine("--- {0} ---", System.Reflection.MethodBase.GetCurrentMethod().Name);
            Stack <int> stack = new Stack <int>();

            stack.Push(5);
            stack.Push(10);
            int x = stack.Pop();
            int y = stack.Pop();
            int xx = 5, yy = 10;

            Generics.Swap(b: ref yy, a: ref xx);

            Type g1 = typeof(G <>);
            Type g2 = typeof(G <,>);

            Console.WriteLine(g2.GetGenericArguments().Count());
            Type g3 = typeof(G <int, int>);

            Console.WriteLine(g3.GetGenericArguments().Count());

            Console.WriteLine(++Bob <int> .Count);
            Console.WriteLine(++Bob <int> .Count);
            Console.WriteLine(++Bob <string> .Count);
            Console.WriteLine(++Bob <object> .Count);

            // Covariance
            {
                // Assuming that Bear subclasses Animal:
                var bears = new Stack <Bear>();
                bears.Push(new Bear());
                // Because bears implements IPoppable<Bear>, we can convert it to IPoppable<Animal>:
                IPoppable <Animal> animals = bears;  // Legal
                Animal             a       = animals.Pop();
            }
            // Contravariance
            {
                IPushable <Animal> animals = new Stack <Animal>();
                IPushable <Bear>   bears   = animals; // Legal
                bears.Push(new Bear());
            }
            Console.WriteLine("=====================================================================");
        }
示例#8
0
    // Update is called once per frame
    void Update()
    {
        if (canMove == false)
        {
            return;
        }

        // Move
        rigidbody.MovePosition(rigidbody.position + (velocity * moveSpeed * Time.deltaTime));

        // Change direction
        if (velocity.x != 0)
        {
            float direction = velocity.x > 0 ? 180 : 0;
            transform.eulerAngles = new Vector3(0, direction, 0);
        }

        // Interact
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            // NOTE:(Nathen) We prioratize pushables over interactables
            // This is so you never get in a situation where you can't pick up something
            // because an NPC trigger is blocking the pushable

            // Check if we can interact with a pushable or an interactable
            if (Pushable != null)
            {
                // If we are currently pushing this Pushable
                if (Pushable.GetState() == true)
                {
                    Pushable.Drop();
                }
                else
                {
                    Pushable.Push();
                }
            }
            else if (selectedInteractable != null)
            {
                selectedInteractable.Interact(this);
            }
        }
    }
示例#9
0
    public void OnAttack()
    {
        _particleSystem.Play();
        Collider[] colliders       = new Collider[10];
        int        objectsHitCount = Physics.OverlapSphereNonAlloc(_playerTransform.position + (_playerTransform.right * _attackDistance), _radius, colliders);

        for (int i = 0; i < objectsHitCount; i++)
        {
            // Check if the colliders object has an IPushable, if it HAS HIT IT!
            IPushable objectToPush = colliders[i].GetComponent <IPushable>();

            if (objectToPush != null)
            {
                Vector3 force = colliders[i].transform.position - _playerTransform.position;
                force = force.normalized * _pushForce;

                objectToPush.Push(force);
            }
        }
    }
示例#10
0
        static void Main(string[] args)
        {
            // Stack covariance
            Animal animal    = new Bear(); // Bear is convertible to Animal (asumption above)
            var    bearStack = new Stack <Bear>();

            bearStack.Push(animal as Bear);
            bearStack.Push(new Bear());
            IPoppable <Animal> animalPop = bearStack;

            animal = animalPop.Pop();
            //Stack<Animal> animalst = bears;  // not a covariant parameter, classes do not permit covariant paramters since data flow in both directions
            ZooCleaner.Wash(bearStack); // OK, covariant method

            // contravariance
            var animalStack             = new Stack <Animal>();
            IPushable <Bear>  bearPush  = animalStack;  // legal
            IPushable <Camel> camelPush = animalStack;  // legal, contravariant

            animalStack.Push(new Bear());
            animalStack.Push(new Camel());
            camelPush.Push(new Camel());
            bearPush.Push(new Bear());

            // Interface covariance
            string        s         = "Hello";
            object        o         = s;
            IFoo <string> fooString = new FooString();

            //IFoo<object> fooObject; fooObject = fooString; // HEY! interfaces should be covariant!!!! Whats up?

            // Array covariance
            Bear[]   bearArr   = new Bear[] { new Bear(), new Bear() };
            Animal[] animalArr = bearArr; // OK, covariant
            animalArr[1] = new Bear();
            //animalArr[1] = new Camel();   ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
            //animalArr[1] = new Animal();  ditto
        }
示例#11
0
        static void Main(string[] args)
        {
            //Generic Type definition
            var nowystos = new Stack <int>();

            nowystos.Push(1);
            nowystos.Push(50);
            Console.WriteLine(nowystos.Pop());

            Type a1 = typeof(A <>);
            Type a2 = typeof(A <,>);
            Type a3 = typeof(A <int, int>);

            Console.WriteLine(a2.GetGenericArguments().Count());

            Console.WriteLine("Generics with static fields");
            Console.WriteLine(++Test <int> .Count);    //1
            Console.WriteLine(++Test <int> .Count);    //2
            Console.WriteLine(++Test <object> .Count); //1
            Console.WriteLine(++Test <object> .Count); //2

            //Kowariancja z out
            Stact2 <Beer> Beeers = new Stact2 <Beer>();

            Beeers.Push(new Beer());
            IPopable <Beer>   Beers   = Beeers;
            IPopable <Animal> Animals = Beers;

            Console.WriteLine(Animals.Pop().GetType().FullName);

            //Kontrawariancja z in
            IPushable <Animal> Animalss = new Stact2 <Animal>();
            IPushable <Beer>   Beerss   = Animalss;

            Beerss.Push(new Beer());
            Console.WriteLine(Beerss.GetType().FullName);

            //Delegates
            int[] valuesInts = { 1, 2, 3 };
            Transform(valuesInts, Square);
            for (int i = 0; i < valuesInts.Length; i++)
            {
                Console.WriteLine(valuesInts[i]);
            }
            Transformer <double> t2 = Square;

            Console.WriteLine(t2.Invoke(3.3));

            int[] valuesInts2 = { 1, 2, 3 };
            Transform2 <int>(valuesInts2, Square);
            for (int i = 0; i < valuesInts2.Length; i++)
            {
                Console.WriteLine(valuesInts2[i]);
            }

            Func <double, double> squareFunc = x =>
            {
                return(x * x);
            };
            Func <double, double, double> multiplyFunc = (x, y) =>
            {
                return(x * y);
            };
            Action <int> actionX = (x) =>
            {
                Console.WriteLine("Acion has fired with value of " + x);
            };

            D1 d1 = WriteSmth;
            D2 d2;

            d2 = new D2(d1);
            d2.Invoke();

            Console.WriteLine(squareFunc(5.5));
            Console.WriteLine(multiplyFunc(5.5, 5.5));

            //Events

            List <Stock> Stocks = new List <Stock>
            {
                new Stock("X1")
                {
                    Price = 22,
                },
                new Stock("X2")
                {
                    Price = 23
                }
            };

            foreach (var stock in Stocks)
            {
                stock.PriceChanged += StockHasChanged;
            }
            Stocks[0].Price = 2;


            //LING
            var linqExamples           = new LinqExamples();
            var dymicFunctionsExamples = new Generics.dynamic.Dynamic();
            var asyncExamples          = new AsyncExamples();
            var adoNetExamples         = new AdoNetExample();



            Console.ReadKey();
        }