Exemplo n.º 1
0
        /// <summary>
        /// Creates a new implementation instance of <see cref="ICreature"/> depending on the chosen type.
        /// </summary>
        /// <param name="creationArguments">The arguments for the <see cref="IThing"/> creation.</param>
        /// <returns>A new instance of the chosen <see cref="ICreature"/> implementation.</returns>
        public ICreature CreateCreature(IThingCreationArguments creationArguments)
        {
            if (!(creationArguments is CreatureCreationArguments creatureCreationArguments))
            {
                throw new ArgumentException($"Invalid type of arguments '{creationArguments.GetType().Name}' supplied, expected {nameof(CreatureCreationArguments)}", nameof(creationArguments));
            }

            switch (creatureCreationArguments.Type)
            {
            // TODO: suppport other types
            // case CreatureType.NonPlayerCharacter:
            case CreatureType.Monster:
                if (creatureCreationArguments.Metadata?.Id == null)
                {
                    throw new ArgumentException("Invalid metadata in creation arguments for a monster.", nameof(creatureCreationArguments));
                }

                using (var unitOfWork = this.ApplicationContext.CreateNewUnitOfWork())
                {
                    if (!(unitOfWork.MonsterTypes.GetById(creatureCreationArguments.Metadata.Id) is IMonsterTypeEntity monsterType))
                    {
                        throw new ArgumentException($"Unknown monster with Id {creatureCreationArguments.Metadata.Id} in creation arguments for a monster.", nameof(creatureCreationArguments));
                    }

                    // TODO: go through inventory composition here.
                    // For each inventory item (and chance or whatever), add the items to the monster (as IContainerThing),
                    // which will make the items fall in place, and also not have us have to pass the item factory into the monster instance, because it's weird.
                    return(new Monster(monsterType, this.ItemFactory));
                }

            case CreatureType.Player:

                if (creatureCreationArguments == null ||
                    creatureCreationArguments.Metadata == null ||
                    !(creatureCreationArguments is PlayerCreationArguments playerCreationArguments))
                {
                    throw new ArgumentException("Invalid creation arguments for a player.", nameof(creatureCreationArguments));
                }

                return(new Player(
                           playerCreationArguments.Client,
                           playerCreationArguments.Metadata.Id,
                           playerCreationArguments.Metadata.Name,
                           playerCreationArguments.Metadata.MaxHitpoints,
                           playerCreationArguments.Metadata.MaxManapoints,
                           playerCreationArguments.Metadata.Corpse));
            }

            throw new NotSupportedException($"{nameof(CreatureFactory)} does not support creation of creatures with type {creatureCreationArguments.Type}.");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new implementation instance of <see cref="ICreature"/> depending on the chosen type.
        /// </summary>
        /// <param name="creationArguments">The arguments for the <see cref="IThing"/> creation.</param>
        /// <returns>A new instance of the chosen <see cref="ICreature"/> implementation.</returns>
        public ICreature CreateCreature(IThingCreationArguments creationArguments)
        {
            if (!(creationArguments is CreatureCreationArguments creatureCreationArguments))
            {
                throw new ArgumentException($"Invalid type of arguments '{creationArguments.GetType().Name}' supplied, expected {nameof(CreatureCreationArguments)}", nameof(creationArguments));
            }

            switch (creatureCreationArguments.Type)
            {
            // TODO: suppport other types
            // case CreatureType.NonPlayerCharacter:
            case CreatureType.Monster:
                if (creatureCreationArguments.Metadata?.Id == null)
                {
                    throw new ArgumentException("Invalid metadata in creation arguments for a monster.", nameof(creatureCreationArguments));
                }

                using (var unitOfWork = this.ApplicationContext.CreateNewUnitOfWork())
                {
                    if (!(unitOfWork.MonsterTypes.GetById(creatureCreationArguments.Metadata.Id) is IMonsterTypeEntity monsterType))
                    {
                        throw new ArgumentException($"Unknown monster with Id {creatureCreationArguments.Metadata.Id} in creation arguments for a monster.", nameof(creatureCreationArguments));
                    }

                    return(new Monster(monsterType, this.ItemFactory));
                }

            case CreatureType.Player:

                if (creatureCreationArguments == null ||
                    creatureCreationArguments.Metadata == null ||
                    !(creatureCreationArguments is PlayerCreationArguments playerCreationArguments))
                {
                    throw new ArgumentException("Invalid creation arguments for a player.", nameof(creatureCreationArguments));
                }

                return(new Player(
                           playerCreationArguments.Client,
                           playerCreationArguments.Metadata.Id,
                           playerCreationArguments.Metadata.Name,
                           playerCreationArguments.Metadata.MaxHitpoints,
                           playerCreationArguments.Metadata.MaxManapoints,
                           playerCreationArguments.Metadata.Corpse,
                           playerCreationArguments.Metadata.MaxHitpoints,    // TODO: current hitpoints.
                           playerCreationArguments.Metadata.MaxManapoints)); // TODO: current mana points.
            }

            throw new NotSupportedException($"{nameof(CreatureFactory)} does not support creation of creatures with type {creatureCreationArguments.Type}.");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new <see cref="IItem"/> given the id of its type.
        /// </summary>
        /// <param name="creationArguments">The id of the type.</param>
        /// <returns>The new <see cref="IItem"/> instance.</returns>
        public IItem CreateItem(IThingCreationArguments creationArguments)
        {
            if (!(creationArguments is ItemCreationArguments itemCreationArguments))
            {
                throw new ArgumentException($"Invalid type of arguments '{creationArguments.GetType().Name}' supplied, expected {nameof(ItemCreationArguments)}", nameof(creationArguments));
            }

            // TODO: magic number here- anything under 100 was not a valid type item.
            if (itemCreationArguments.TypeId < 100)
            {
                return(null);
            }

            using var unitOfWork = this.ApplicationContext.CreateNewUnitOfWork();

            var itemType = unitOfWork.ItemTypes.GetByTypeId(itemCreationArguments.TypeId.ToString());

            if (itemType == null)
            {
                throw new ArgumentException($"Unknown item type with Id {itemCreationArguments.TypeId} in creation arguments for an item.", nameof(creationArguments));
            }

            IItem newItem = null;

            // TODO: chest actually means a quest chest...
            if (itemType.HasItemFlag(ItemFlag.IsContainer) || itemType.HasItemFlag(ItemFlag.IsQuestChest))
            {
                newItem = new ContainerItem(itemType);
            }
            else
            {
                newItem = new Item(itemType);
            }

            if (itemCreationArguments.Attributes != null)
            {
                foreach (var(attribute, attributeValue) in itemCreationArguments.Attributes)
                {
                    newItem.Attributes[attribute] = attributeValue;
                }
            }

            this.ItemCreated?.Invoke(newItem);

            return(newItem);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new <see cref="IItem"/> given the id of its type.
        /// </summary>
        /// <param name="creationArguments">The id of the type.</param>
        /// <returns>The new <see cref="IItem"/> instance.</returns>
        public IItem CreateItem(IThingCreationArguments creationArguments)
        {
            if (!(creationArguments is ItemCreationArguments itemCreationArguments))
            {
                throw new ArgumentException($"Invalid type of arguments '{creationArguments.GetType().Name}' supplied, expected {nameof(ItemCreationArguments)}", nameof(creationArguments));
            }

            if (itemCreationArguments.TypeId < ItemConstants.MaximumAmountOfCummulativeItems)
            {
                return(null);
            }

            using var unitOfWork = this.ApplicationContext.CreateNewUnitOfWork();

            if (!(unitOfWork.ItemTypes.GetById(itemCreationArguments.TypeId.ToString()) is IItemTypeEntity itemType))
            {
                throw new ArgumentException($"Unknown item type with Id {itemCreationArguments.TypeId} in creation arguments for an item.", nameof(creationArguments));
            }

            // TODO: chest actually means a quest chest...
            if (itemType.HasItemFlag(ItemFlag.IsContainer) || itemType.HasItemFlag(ItemFlag.IsQuestChest))
            {
                return(new ContainerItem(itemType));
            }

            var newItem = new Item(itemType);

            if (itemCreationArguments.Attributes != null)
            {
                foreach (var(attribute, attributeValue) in itemCreationArguments.Attributes)
                {
                    newItem.Attributes[attribute] = attributeValue;
                }
            }

            return(newItem);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new <see cref="IThing"/>.
 /// </summary>
 /// <param name="creationArguments">The arguments for the <see cref="IThing"/> creation.</param>
 /// <returns>A new instance of the <see cref="IThing"/>.</returns>
 public IThing Create(IThingCreationArguments creationArguments)
 {
     return(this.CreateCreature(creationArguments));
 }