Esempio n. 1
0
        public static TankVolume FromResource(PartResource res)
        {
            var tank      = new TankVolume();
            var tank_type = SwitchableTankType.FindTankType(res.resourceName);

            if (tank_type == null)
            {
                return(null);
            }
            tank.TankType        = tank_type.name;
            tank.CurrentResource = res.resourceName;
            tank.Volume          = (float)(res.maxAmount / tank_type.Resources[res.resourceName].UnitsPerLiter / 1000 / tank_type.UsefulVolumeRatio);
            tank.InitialAmount   = (float)(res.amount / res.maxAmount);
            return(tank);
        }
 /// <summary>
 /// Forces the switch of the current resource, even if the new resource belongs to another
 /// tank type, in which case the type is also switched. If the amount of current resource
 /// is not zero, it is discarded. After the switch the tank remains empty.
 /// </summary>
 /// <returns><c>true</c>, if resource was successfully switched, <c>false</c> otherwise.</returns>
 /// <param name="new_resource">New resource name.</param>
 public bool ForceSwitchResource(string new_resource)
 {
     //if nothing to do, return true
     if (current_resource != null &&
         current_resource.resourceName == new_resource)
     {
         return(true);
     }
     //if the new resource is in the current tank type
     if (tank_type != null && tank_type.Resources.ContainsKey(new_resource))
     {
         if (current_resource != null)
         {
             current_resource.amount = 0;
         }
         CurrentResource = new_resource;
         if (switch_resource())
         {
             update_res_control(); return(true);
         }
         return(false);
     }
     else //try to find the tank type for the new resource
     {
         var new_type = SwitchableTankType.FindTankType(new_resource);
         if (new_type == null)
         {
             return(false);
         }
         if (current_resource != null)
         {
             current_resource.amount = 0;
         }
         TankType        = new_type.name;
         CurrentResource = new_resource;
         return(change_tank_type());
     }
 }