Beispiel #1
0
        public MyInventorySynchronizer(MyInventory inventory, MyMustBeInventorySynchronizedDelegate mustBeInventorySynchronizedDelegate)
        {
            m_inventoryItemsHelper                = new List <MyInventoryItem>();
            m_inventoryItemsToAdd                 = new List <MyInventoryItem>();
            m_inventoryItemsAmountChanges         = new List <MyInventoryItemAmountDefinition>();
            m_mustBeInventorySynchronizedDelegate = mustBeInventorySynchronizedDelegate;
            m_inventory = inventory;

            //MyMinerGame.OnGameUpdate += MyMinerGame_OnGameUpdate;
        }
Beispiel #2
0
        private bool RemoveItemFromInventoryPrivate(MyInventoryItem item, bool closeInventoryItem)
        {
            bool result = m_inventoryItems.Remove(item);

            item.OnAmountChange -= m_onItemAmountChangeHandler;
            if (item.Amount > 0f)
            {
                OnAmountChange(item, -item.Amount);
            }
            if (closeInventoryItem)
            {
                MyInventory.CloseInventoryItem(item);
            }
            else
            {
                item.Owner = null;
            }
            return(result);
        }
Beispiel #3
0
        public void Close()
        {
            foreach (var item in m_inventoryItemsToAdd)
            {
                MyInventory.CloseInventoryItem(item);
            }
            m_inventoryItemsToAdd.Clear();
            m_inventoryItemsToAdd = null;

            m_inventoryItemsAmountChanges.Clear();
            m_inventoryItemsAmountChanges = null;

            m_inventoryItemsHelper.Clear();
            m_inventoryItemsHelper = null;

            m_mustBeInventorySynchronizedDelegate = null;

            m_inventory = null;

            //MyMinerGame.OnGameUpdate -= MyMinerGame_OnGameUpdate;
        }
Beispiel #4
0
        /// <summary>
        /// Adds new inventory item from object builder
        /// </summary>
        /// <param name="objectBuilder">Item object builder</param>
        /// <param name="amount">Amount of inventory item</param>
        /// <param name="allAmountAddAsNewInventoryItems">If true, then all amount adds as new inventory items, if false, then try find old inventory items of same type, and add amount to them as first</param>
        public float AddInventoryItem(MyMwcObjectBuilder_Base objectBuilder, float amount, bool allAmountAddAsNewInventoryItems, bool increaseCapacityIfIsFull = false)
        {
            bool added = false;
            MyMwcObjectBuilderTypeEnum objectBuilderType = objectBuilder.GetObjectBuilderType();
            int?objectBuilderId = objectBuilder.GetObjectBuilderId();

            m_helperInventoryItemsForAddAndRemove.Clear();
            GetInventoryItems(ref m_helperInventoryItemsForAddAndRemove, objectBuilderType, objectBuilderId);

            float amountToAddLeft = amount;

            if (!allAmountAddAsNewInventoryItems)
            {
                foreach (MyInventoryItem inventoryItem in m_helperInventoryItemsForAddAndRemove)
                {
                    if (amountToAddLeft <= 0f)
                    {
                        break;
                    }
                    float amountToAdd = Math.Min(inventoryItem.MaxAmount - inventoryItem.Amount, amountToAddLeft);
                    if (InventorySynchronizer != null && InventorySynchronizer.MustBeSynchronized())
                    {
                        InventorySynchronizer.AddInventoryItemAmountChangeForSynchronization(objectBuilderType, objectBuilderId, inventoryItem.Amount, amountToAdd);
                    }
                    else
                    {
                        inventoryItem.Amount += amountToAdd;
                    }
                    amountToAddLeft -= amountToAdd;
                }
            }

            while (amountToAddLeft > 0f)
            {
                if (IsFull)
                {
                    if (increaseCapacityIfIsFull)
                    {
                        MaxItems++;
                    }
                    else
                    {
                        break;
                    }
                }
                MyInventoryItem newInventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(objectBuilder);
                float           amountToAdd      = Math.Min(newInventoryItem.MaxAmount, amountToAddLeft);
                //AddInventoryItem(newInventoryItem);
                AddItemToInventoryPrivate(newInventoryItem);
                newInventoryItem.Amount = amountToAdd; //After AddInventoryItem otherwise Amount event is not called
                amountToAddLeft        -= amountToAdd;
                added = true;
            }

            if (added)
            {
                CallContentChange();
            }

            return(amountToAddLeft);
        }
Beispiel #5
0
        /// <summary>
        /// Fills inventory with all items
        /// </summary>
        /// <param name="maxItems">Max items limit</param>
        public void FillInventoryWithAllItems(int?maxItems = null, int?increaseMaxItemsAbout = null, bool loadSmallShips = false)
        {
            if (maxItems != null)
            {
                MaxItems = maxItems.Value;
            }
            MyInventoryItem        inventoryItem;
            List <MyInventoryItem> inventoryItemsToAdd = new List <MyInventoryItem>();

            // ammo
            foreach (MyMwcObjectBuilder_SmallShip_Ammo_TypesEnum item in MyGuiSmallShipHelpers.MyMwcObjectBuilder_SmallShip_Ammo_TypesEnumValues)
            {
                inventoryItem        = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallShip_Ammo(item));
                inventoryItem.Amount = inventoryItem.MaxAmount;
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // engines
            foreach (MyMwcObjectBuilder_SmallShip_Engine_TypesEnum item in MyGuiSmallShipHelpers.MyMwcObjectBuilder_SmallShip_Engine_TypesEnumValues)
            {
                inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallShip_Engine(item));
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // weapons
            foreach (MyMwcObjectBuilder_SmallShip_Weapon_TypesEnum item in MyGuiSmallShipHelpers.MyMwcObjectBuilder_SmallShip_Weapon_TypesEnumValues)
            {
                inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallShip_Weapon(item));
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // tools
            foreach (MyMwcObjectBuilder_SmallShip_Tool_TypesEnum item in MyGuiSmallShipHelpers.MyMwcObjectBuilder_SmallShip_ToolEnumValues)
            {
                inventoryItem        = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallShip_Tool(item));
                inventoryItem.Amount = inventoryItem.MaxAmount;
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // armors
            foreach (MyMwcObjectBuilder_SmallShip_Armor_TypesEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_SmallShip_Armor_TypesEnum)))
            {
                inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallShip_Armor(item));
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // radars
            foreach (MyMwcObjectBuilder_SmallShip_Radar_TypesEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_SmallShip_Radar_TypesEnum)))
            {
                inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallShip_Radar(item));
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // blueprints
            foreach (MyMwcObjectBuilder_Blueprint_TypesEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_Blueprint_TypesEnum)))
            {
                inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_Blueprint(item));
                inventoryItemsToAdd.Add(inventoryItem);
            }
            //ore
            foreach (MyMwcObjectBuilder_Ore_TypesEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_Ore_TypesEnum)))
            {
                inventoryItem        = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_Ore(item));
                inventoryItem.Amount = 1;
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // fakeId
            foreach (MyMwcObjectBuilder_FactionEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_FactionEnum)))
            {
                inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_FalseId(item));
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // hacking tool
            foreach (MyMwcObjectBuilder_SmallShip_HackingTool_TypesEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_SmallShip_HackingTool_TypesEnum)))
            {
                inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallShip_HackingTool(item));
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // drone
            foreach (MyMwcObjectBuilder_Drone_TypesEnum droneType in Enum.GetValues(typeof(MyMwcObjectBuilder_Drone_TypesEnum)))
            {
                var droneBuilder = new MyMwcObjectBuilder_Drone(droneType);
                inventoryItem        = CreateInventoryItemFromObjectBuilder(droneBuilder);
                inventoryItem.Amount = inventoryItem.MaxAmount;
                inventoryItemsToAdd.Add(inventoryItem);
            }
            // playerships
            if (loadSmallShips)
            {
                foreach (MyMwcObjectBuilder_SmallShip_TypesEnum shipType in Enum.GetValues(typeof(MyMwcObjectBuilder_SmallShip_TypesEnum)))
                {
                    inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(MyMwcObjectBuilder_SmallShip_Player.CreateDefaultShip(shipType, MySession.PlayerShip.Faction, MyShipTypeConstants.GetShipTypeProperties(shipType).GamePlay.CargoCapacity));
                    inventoryItemsToAdd.Add(inventoryItem);
                }
            }

            //// prefabs
            //foreach(MyMwcObjectBuilder_Prefab_TypesEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_Prefab_TypesEnum))) {
            //    inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_Prefab(item, new MyMwcVector3Short(0, 0, 0), new Vector3(0, 0, 0), MyGameplayConstants.MAXHEALTH_PREFAB));
            //    inventoryItemsToAdd.Add(inventoryItem);
            //}
            //// small debris
            //foreach (MyMwcObjectBuilder_SmallDebris_TypesEnum item in Enum.GetValues(typeof(MyMwcObjectBuilder_SmallDebris_TypesEnum)))
            //{
            //    inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(new MyMwcObjectBuilder_SmallDebris(item, true));
            //    inventoryItemsToAdd.Add(inventoryItem);
            //}
            // foundation factory
            inventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(MyMwcObjectBuilder_Base.CreateNewObject(MyMwcObjectBuilderTypeEnum.PrefabFoundationFactory, (int)MyMwcObjectBuilder_PrefabFoundationFactory_TypesEnum.DEFAULT));
            inventoryItemsToAdd.Add(inventoryItem);

            if (maxItems != null && !UnlimitedCapacity)
            {
                while (inventoryItemsToAdd.Count > maxItems.Value)
                {
                    int lastIndex = inventoryItemsToAdd.Count - 1;
                    inventoryItem = inventoryItemsToAdd[lastIndex];
                    inventoryItemsToAdd.RemoveAt(lastIndex);
                    MyInventory.CloseInventoryItem(inventoryItem);
                }
            }
            else
            {
                if (inventoryItemsToAdd.Count > MaxItems)
                {
                    MaxItems = inventoryItemsToAdd.Count;
                }
            }
            if (increaseMaxItemsAbout != null)
            {
                MaxItems += increaseMaxItemsAbout.Value;
            }
            AddInventoryItems(inventoryItemsToAdd);
        }
Beispiel #6
0
 public static MyInventoryItem CreateInventoryItemFromObjectBuilder(MyMwcObjectBuilder_Base objectBuilder)
 {
     return(MyInventory.CreateInventoryItemFromObjectBuilder(objectBuilder, 1f));
 }
Beispiel #7
0
 public static bool MustBePlayerShipInventorySynchronized(MyInventory inventory)
 {
     return(MySession.PlayerShip != null &&
            MySession.PlayerShip.Inventory == inventory &&
            MyGuiScreenInventoryManagerForGame.IsInventoryOpen());
 }