Friday, November 23, 2012

Dynamic Class Creator

So I just implemented a very interesting piece of code that will dynamically create an inheritance class dynamically rather than having to make several methods that will each do practically the same thing.  This is a Class Delegate Function and the code looks like the following.


        /**
         * InstantiateCollectible<Item> - Creates a new Collectible on the map
         *  Item - Inheritance Child of Collectible
         *
         * PARAMS
         *  position - Position of the object
         *  rotation - Rotation of the object
         *
         * RETURN
         *  Collectible - Object Player can pick up in the game
         */
        public static Collectible InstantiateCollectible<Item>(Vector3 position, Quaternion rotation) where Item : Collectible
        {
            AvatarDescription description = AvatarDescription.CreateRandom();

            Collectible collectible = (Collectible)Activator.CreateInstance(typeof(Item), description, position, rotation, PlayerStatSheet.AvatarMass);

            collectible.TeamId = 1;

            return collectible;
        }

No comments:

Post a Comment