示例#1
0
        public void RemoveResource(MResources.Resource resource, double ammount)
        {
            bool isInInventory = false;
            int  resourceIndex = 0;
            int  index         = 0;

            foreach (Entry e in inventory)
            {
                isInInventory = (isInInventory || e.resource.name == resource.name);
                if (e.resource.name == resource.name)
                {
                    resourceIndex = index;
                }
                index++;
            }

            if (isInInventory)
            {
                inventory[resourceIndex] = new Entry()
                {
                    resource = inventory[resourceIndex].resource, ammount = Math.Max(0, inventory[resourceIndex].ammount - ammount)
                };

                if (inventory[resourceIndex].ammount <= 0)
                {
                    inventory.RemoveAt(resourceIndex);
                }
            }
            else
            {
                Console.WriteLine("The inventory does not have {0}.", resource.name);
            }
        }
示例#2
0
        public void AddResource(MResources.Resource resource, double ammount)
        {
            bool isInInventory = false;
            int  resourceIndex = 0;
            int  index         = 0;

            foreach (Entry e in inventory)
            {
                isInInventory = (isInInventory || e.resource.name == resource.name);
                if (e.resource.name == resource.name)
                {
                    resourceIndex = index;
                }
                index++;
            }

            if (isInInventory)
            {
                inventory[resourceIndex] = new Entry()
                {
                    resource = inventory[resourceIndex].resource, ammount = inventory[resourceIndex].ammount + ammount
                };
            }
            else
            {
                inventory.Add(new Entry()
                {
                    resource = resource, ammount = ammount
                });
            }
        }