Esempio n. 1
0
        public void CreateNewCharacter(CharacterType characterType, ICharacterController controller, Battlefield battlefield, string customName = null)
        {
            Character newCharacter;
            switch (characterType)
            {
                case CharacterType.Barbarian: newCharacter = new Barbarian(); break;
                case CharacterType.Ranger: newCharacter = new Ranger(); break;
                case CharacterType.Mage: newCharacter = new Mage(); break;
                case CharacterType.Paladin: newCharacter = new Paladin(); break;
                case CharacterType.Troll: newCharacter = new Troll(); break;
                case CharacterType.Goblin: newCharacter = new Goblin(); break;
                case CharacterType.Warlock: newCharacter = new Warlock(); break;
                case CharacterType.Necromancer: newCharacter = new Necromancer(); break;
                case CharacterType.Skeleton: newCharacter = new Skeleton(); break;
                default: throw new NotSupportedException();
            }

            newCharacter.Initialize(battlefield, controller, customName);
            Characters.Insert(0, (TCharacter)newCharacter);
        }
        // HACK: ADDITIONAL, EXTENDED INFORMATION ABOUT CASTING: https://stackoverflow.com/questions/15394032/difference-between-casting-and-using-the-convert-to-method

        /// <summary>
        /// This method is accessible from Battlegrounds only (it's PRIVATE),
        /// accepts a characterName (string) as an input parameter,
        /// and returns a Character object to the caller
        /// </summary>
        private Character BoxingStuff(string characterName) // NOTE that even private method names are written in PascalCase
        {
            // I'm defining 2 variables here, one is less specific, the other is more specific
            // - their scope will be the BoxingStuff method: when it's called and it reaches those two lines, the variables will be created,
            //   when the methods ends its execution, they will both disappear and won't be accessible anymore from anywhere
            // - PAY ATTENTION: it's only the VARIABLE that will disappear (IE: genericObject), not necessarily its CONTENT (IE: I'm returning
            //   that "Generic Hero" Character to the caller of this method, and it can continue using it).
            // - When there's no other variable referencing our "Generic Hero", it will disappear as well
            Character specificObject;
            object    genericObject;

            specificObject = new Barbarian(this); // That's the normal way of doing it
            genericObject  = new Barbarian(this); // I CAN do that, since 'Character' INHERITS from 'object', as ANYTHING ELSE in C#.
            // ^ This practice is called BOXING (like putting your stuff into a generic box)

            //specificObject = new Battleground(); // I CAN NOT do that, since Battleground IS NOT a Character, and my 'specificObject' variable can contain only Characters
            genericObject = new Battleground(); // I CAN do that as well, since 'Battleground' INHERITS from 'object', as ANYTHING ELSE in C#
            genericObject = 53;                 // I CAN do that as well, since 'int' INHERITS from 'object', as ANYTHING ELSE in C#
            genericObject = "I'm a nice egg";   // I CAN do that as well, since 'string' INHERITS from 'object', as ANYTHING ELSE in C#

            specificObject.Accuracy = 0.25f;
            //genericObject.Name = characterName; // I CAN NOT do that, since 'object' does not have any field named "Name"
            if (genericObject is Character)
            {
                ((Character)genericObject).Accuracy = 0.25f; // To be able to do that, I have to CAST it, basically telling the compiler:
            }
            // "Listen, I know this variable deals with generic objects,
            // BUT I'M SURE it now contains a Character, so please treat it like that
            // and allow me to access its methods and fields"
            // WARNING: if for any reason there's not a Character inside of that variable,
            // the whole program will explode.
            // ^ This practice is called UNBOXING

            // Please also note that BOXING/UNBOXING is a memory/performance consuming task

            // DO NOT USE IT unless you have an extremely valid reason to do that (IE: you really have no other options)

            return(specificObject);
        }