private void StoreIn_Hangar_Nearby(Inventory inventory, string name)
        {
            // Figure out where to put it
            string[] actions;
            Panel panel;
            bool isHangar;

            if (_station.PurchasedVolume - _station.UsedVolume >= inventory.Volume)
            {
                // Hangar
                actions = inventory.Ship != null ? _actions_Player_Hangar_ship : _actions_Player_Hangar_nonship;
                panel = pnlHangar;
                isHangar = true;
            }
            else
            {
                // Nearby
                actions = inventory.Ship != null ? _actions_Player_FreeSpace_ship : _actions_Player_FreeSpace_nonship;
                panel = pnlNearbyItems;
                isHangar = false;
            }

            // Create an entry with the sell price
            Tuple<decimal, decimal> credits = _station.GetPrice_Sell(inventory);

            InventoryEntry newEntry = new InventoryEntry();
            newEntry.SetInventory(inventory, name, credits.Item1 * credits.Item2, Convert.ToDouble(credits.Item2), null, _world, actions, _editorOptions);

            // Store it
            //panel.Children.Add(newEntry);
            AddToPanel(panel, newEntry);
            InventoryEntryAdded(newEntry);

            if (isHangar)
            {
                _station.PlayerInventory.Add(inventory);
                OnHangarChanged();
            }
        }
        private void StoreIn_Nearby(Inventory inventory, string name)
        {
            // Create an entry with the sell price
            Tuple<decimal, decimal> credits = _station.GetPrice_Sell(inventory);

            string[] actions = inventory.Ship != null ?
                _actions_Player_FreeSpace_ship :
                _actions_Player_FreeSpace_nonship;

            InventoryEntry newEntry = new InventoryEntry();
            newEntry.SetInventory(inventory, name, credits.Item1 * credits.Item2, Convert.ToDouble(credits.Item2), null, _world, actions, _editorOptions);

            // Store it
            AddToPanel(pnlNearbyItems, newEntry);
            InventoryEntryAdded(newEntry);
        }
        private async void SwapShip(InventoryEntry entry)
        {
            const double STARTPERCENT = .25;        // don't make it too high, or they will cheat and switch ships to refuel



            //TODO: Remember the cargo in a separate object
            //TODO: Remember the part damage in a separate object



            // Remove the old entry
            RemoveFrom_Cargo_Hangar_Neaby(entry);

            // Store the old ship
            if (_player.Ship != null)
            {
                Inventory prevInventory = new Inventory(_player.Ship.GetNewDNA(), 1d);
                string prevName = GetName_Ship(prevInventory.Ship);

                StoreIn_Hangar_Nearby(prevInventory, prevName);
            }

            //NOTE: Grabbing this now, because the ship creation is async, and station could be null
            // by the time it's finished
            Point3D stationPosition = _station.PositionWorld;

            // Need to do this now so that the old ship is off the map before adding a new one (the new one sometimes goes flying from the collision)
            _player.Ship = null;

            // Create the new ship
            ShipPlayer ship = ShipPlayer.GetNewShip(entry.Inventory.Ship, _world, _material_Ship, _map, _shipExtra);

            if (ship.Energy != null)
            {
                ship.Energy.QuantityCurrent = ship.Energy.QuantityMax * STARTPERCENT;
            }
            if (ship.Plasma != null)
            {
                ship.Plasma.QuantityCurrent = ship.Plasma.QuantityMax * STARTPERCENT;
            }
            if (ship.Fuel != null)
            {
                ship.Fuel.QuantityCurrent = ship.Fuel.QuantityMax * STARTPERCENT;
            }
            if (ship.Ammo != null)
            {
                ship.Ammo.QuantityCurrent = ship.Ammo.QuantityMax * STARTPERCENT;
            }
            ship.RecalculateMass();

            ship.PhysicsBody.Position = new Point3D(stationPosition.X, stationPosition.Y, 0);
            ship.PhysicsBody.Velocity = new Vector3D(0, 0, 0);
            ship.PhysicsBody.AngularVelocity = new Vector3D(0, 0, 0);

            _player.Ship = ship;        // the ship changed event listener will add the ship to the map
        }
        private void RemoveFrom_Mineral_Part_Ship(InventoryEntry entry)
        {
            InventoryEntryRemoved(entry);

            _station.StationInventory.Remove(entry.Inventory);

            if (entry.Inventory.Ship != null)
            {
                pnlStationShips.Children.Remove(entry);
            }
            else if (entry.Inventory.Part != null)
            {
                pnlStationParts.Children.Remove(entry);
            }
            else if (entry.Inventory.Mineral != null)
            {
                pnlStationMinerals.Children.Remove(entry);
            }
        }
 private void InventoryEntryRemoved(InventoryEntry entry)
 {
     entry.ActionClicked -= new EventHandler<InventoryActionClickedArgs>(InventoryEntry_ActionClicked);
 }
        private void ScrapShip(InventoryEntry entry)
        {
            if (entry == null || entry.Inventory == null || entry.Inventory.Ship == null)
            {
                throw new ArgumentException("entry is/contains null");
            }

            // Remove the old entry
            RemoveFrom_Cargo_Hangar_Neaby(entry);

            // Pull all the parts out of the ship, and turn them into inventory
            foreach (ShipPartDNA part in entry.Inventory.Ship.PartsByLayer.Values.SelectMany(o => o))
            {
                Inventory inventory = new Inventory(part);
                string name = GetName(inventory);

                StoreIn_Cargo_Hangar_Nearby(inventory, name);
            }
        }
 public void RemoveInventory(InventoryEntry entry, bool isPlayerOwned)
 {
     if (isPlayerOwned)
     {
         RemoveFrom_Cargo_Hangar_Neaby(entry);
     }
     else
     {
         RemoveFrom_Mineral_Part_Ship(entry);
     }
 }
        private void AddShipCargoGraphic(Inventory inventory)
        {
            string name = GetName(inventory);
            Tuple<decimal, decimal> credits = _station.GetPrice_Sell(inventory);

            string[] actions = inventory.Ship != null ?
                _actions_Player_Cargo_ship :
                _actions_Player_Cargo_nonship;

            InventoryEntry entry = new InventoryEntry();
            entry.SetInventory(inventory, name, credits.Item1 * credits.Item2, Convert.ToDouble(credits.Item2), null, _world, actions, _editorOptions);

            //pnlCargo.Children.Add(entry);
            AddToPanel(pnlCargo, entry);

            InventoryEntryAdded(entry);
        }
        private static bool Is1LessThan2_Part(InventoryEntry entry1, InventoryEntry entry2)
        {
            // Compare Names
            int compare = entry1.Name.CompareTo(entry2.Name);
            if (compare < 0) return true;
            else if (compare > 0) return false;

            // Same name
            return entry1.Credits > entry2.Credits;
        }
        private static bool Is1LessThan2_Mineral(InventoryEntry entry1, InventoryEntry entry2)
        {
            // Sort by type
            int i1 = Array.IndexOf(_mineralTypeSortOrder.Value, entry1.Inventory.Mineral.MineralType);
            int i2 = Array.IndexOf(_mineralTypeSortOrder.Value, entry2.Inventory.Mineral.MineralType);

            if (i1 < i2)
            {
                return true;
            }
            else if (i1 > i2)
            {
                return false;
            }

            // They are the same type, sort by volume
            return entry1.Inventory.Mineral.Volume > entry2.Inventory.Mineral.Volume;
        }
        private static bool? Is1LessThan2_DifferentTypes(InventoryEntry entry1, InventoryEntry entry2)
        {
            var typeSorter = new Func<Inventory, int>(o =>
            {
                if (o.Ship != null) return 0;
                else if (o.Part != null) return 1;
                else if (o.Mineral != null) return 2;
                else return 100;
            });

            int i1 = typeSorter(entry1.Inventory);
            int i2 = typeSorter(entry2.Inventory);

            if (i1 == i2)
            {
                return null;
            }
            else
            {
                return i1 < i2;
            }
        }
        /// <summary>
        /// This is sort of like a -1,0,1 compare, but only returns the -1 case or not -1
        /// </summary>
        private static bool Is1LessThan2(InventoryEntry entry1, InventoryEntry entry2)
        {
            // Sort by type
            bool? differentType = Is1LessThan2_DifferentTypes(entry1, entry2);
            if (differentType != null)
            {
                return differentType.Value;
            }

            // Sort within type
            if (entry1.Inventory.Ship != null)
            {
                return Is1LessThan2_Ship(entry1, entry2);
            }
            else if (entry1.Inventory.Part != null)
            {
                return Is1LessThan2_Part(entry1, entry2);
            }
            else if (entry1.Inventory.Mineral != null)
            {
                return Is1LessThan2_Mineral(entry1, entry2);
            }

            return false;
        }
        //TODO: use proper mvvm sort techniques
        /// <summary>
        /// This adds the item to the panel, maintaining the sort order
        /// NOTE: This is a hack that moves all the panel's entries + new to a list, sorts the list, then adds them back to the panel
        /// </summary>
        private static void AddToPanel(Panel panel, InventoryEntry entry)
        {
            // Pop existing entries
            List<InventoryEntry> entries = new List<InventoryEntry>();

            foreach (InventoryEntry existingEntry in panel.Children)
            {
                entries.Add(existingEntry);
            }

            panel.Children.Clear();

            // Add new
            entries.Add(entry);

            // Add back sorted
            foreach (InventoryEntry sorted in SortInventory(entries))
            {
                panel.Children.Add(sorted);
            }
        }
        private void RemoveFrom_Cargo_Hangar_Neaby(InventoryEntry entry)
        {
            InventoryEntryRemoved(entry);

            if (pnlCargo.Children.Contains(entry))
            {
                #region Cargo

                pnlCargo.Children.Remove(entry);

                if (entry.Inventory.Mineral != null)
                {
                    _player.Ship.CargoBays.RemoveMineral_Volume(entry.Inventory.Mineral.MineralType, entry.Inventory.Mineral.Volume);
                }
                else if (entry.Inventory.Part != null)
                {
                    _player.Ship.CargoBays.RemovePart(entry.Inventory.Part);
                }
                else
                {
                    throw new ApplicationException("Unknown type of entry");
                }

                #endregion
            }
            else if (pnlHangar.Children.Contains(entry))
            {
                #region Hangar

                pnlHangar.Children.Remove(entry);

                _station.PlayerInventory.Remove(entry.Inventory);

                OnHangarChanged();

                #endregion
            }
            else if (pnlNearbyItems.Children.Contains(entry))
            {
                #region Nearby Items

                pnlNearbyItems.Children.Remove(entry);

                #endregion
            }
            else
            {
                throw new ArgumentException("Can't find the owner of the entry: " + entry.ToString());
            }
        }
        private void Buy(InventoryEntry entry, InventoryActionClickedArgs e, UIElement clickedControl)
        {
            if (_player.Credits >= e.Credits)
            {
                _player.Credits -= e.Credits;
            }
            else
            {
                // Not enough money
                PlayFailSound(clickedControl);
                return;
            }

            RemoveFrom_Mineral_Part_Ship(entry);

            if (e.Inventory.Ship != null)
            {
                StoreIn_Hangar_Nearby(e.Inventory, e.Name);
            }
            else
            {
                StoreIn_Cargo_Hangar_Nearby(e.Inventory, e.Name);
            }
        }
        private void AddStationMineralGraphic(Inventory inventory)
        {
            string name = GetName(inventory);
            Tuple<decimal, decimal> credits = _station.GetPrice_Buy(inventory);

            InventoryEntry entry = new InventoryEntry();
            entry.SetInventory(inventory, name, credits.Item1 * credits.Item2, Convert.ToDouble(credits.Item2), null, _world, _actions_Station_Minerals, _editorOptions);

            AddToPanel(pnlStationMinerals, entry);

            InventoryEntryAdded(entry);
        }
        private void Sell(InventoryEntry entry, InventoryActionClickedArgs e)
        {
            _player.Credits += e.Credits;

            RemoveFrom_Cargo_Hangar_Neaby(entry);
            StoreIn_Mineral_Part_Ship(entry.Inventory);
        }
Esempio n. 18
0
 private void RemoveFromStation(InventoryEntry inventory)
 {
     _spaceDock.RemoveInventory(inventory, true);
 }
Esempio n. 19
0
 private void RemoveFromStation(InventoryEntry inventory)
 {
     _spaceDock.RemoveInventory(inventory, true);
 }