예제 #1
0
 public Battery(BatteryModels _bateryModel, BatteryTypes _batteryTypes, int _idleTime, int _talkTime)
 {
     BatteryModel = _bateryModel;
     IdleTime     = _idleTime;
     TalkTime     = _talkTime;
 }
        public static void AwakePrefix(ref EnergyMixin __instance)
        {
            // This is necessary to allow the new batteries to be compatible with tools and vehicles

            if (!__instance.allowBatteryReplacement)
            {
                return; // Battery replacement not allowed - No need to make changes
            }
            if (CbDatabase.BatteryItems.Count == 0)
            {
                return;
            }

            List <TechType> compatibleBatteries = __instance.compatibleBatteries;

            List <BatteryModels> Models = new List <BatteryModels>(__instance.batteryModels ?? new BatteryModels[0]);

            GameObject batteryModel      = null;
            GameObject powerCellModel    = null;
            GameObject ionBatteryModel   = null;
            GameObject ionPowerCellModel = null;

            List <TechType>   existingTechtypes = new List <TechType>();
            List <GameObject> existingModels    = new List <GameObject>();


            //First check for models already setup
            for (int b = 0; b < Models.Count; b++)
            {
                BatteryModels model = Models[b];
                switch (model.techType)
                {
                case TechType.Battery:
                    batteryModel = model.model;
                    break;

                case TechType.PowerCell:
                    powerCellModel = model.model;
                    break;

                case TechType.PrecursorIonBattery:
                    ionBatteryModel = model.model;
                    break;

                case TechType.PrecursorIonPowerCell:
                    ionPowerCellModel = model.model;
                    break;
                }
                existingTechtypes.Add(Models[b].techType);
                existingModels.Add(Models[b].model);
            }

            //Then check for models not already setup.
            foreach (Renderer renderer in __instance.gameObject.GetComponentsInChildren <Renderer>(true))
            {
                if (renderer.gameObject.GetComponentInParent <Battery>(true) != null)
                {
                    continue;
                }

                switch (renderer?.material?.mainTexture?.name)
                {
                case "battery_01":
                    batteryModel = batteryModel ??= renderer.gameObject;
                    break;

                case "battery_ion":
                    ionBatteryModel = ionBatteryModel ??= renderer.gameObject;
                    break;

                case "power_cell_01":
                    powerCellModel = powerCellModel ??= renderer.gameObject;
                    break;

                case "engine_power_cell_ion":
                    ionPowerCellModel = ionPowerCellModel ??= renderer.gameObject;
                    break;
                }
            }

            //Add missing models that were found or create new ones if possible.
            if (batteryModel != null && !existingTechtypes.Contains(TechType.Battery))
            {
                Models.Add(new BatteryModels()
                {
                    model = batteryModel, techType = TechType.Battery
                });
                existingTechtypes.Add(TechType.Battery);
                existingModels.Add(batteryModel);
            }

            //Add missing models that were found or create new ones if possible.
            if (!existingTechtypes.Contains(TechType.PrecursorIonBattery))
            {
                if (ionBatteryModel != null)
                {
                    Models.Add(new BatteryModels()
                    {
                        model = ionBatteryModel, techType = TechType.PrecursorIonBattery
                    });
                    existingTechtypes.Add(TechType.PrecursorIonBattery);
                    existingModels.Add(ionBatteryModel);
                }
                else if (batteryModel != null)
                {
                    GameObject ionBatteryPrefab = Resources.Load <GameObject>("worldentities/tools/precursorionbattery");
                    ionBatteryModel      = GameObject.Instantiate(batteryModel, batteryModel.transform.parent);
                    ionBatteryModel.name = "precursorIonBatteryModel";


                    Material ionBatteryMaterial = ionBatteryPrefab?.GetComponentInChildren <Renderer>()?.material;
                    if (ionBatteryMaterial != null)
                    {
                        ionBatteryModel.GetComponentInChildren <Renderer>().material = new Material(ionBatteryMaterial);
                        Models.Add(new BatteryModels()
                        {
                            model = ionBatteryModel, techType = TechType.PrecursorIonBattery
                        });
                        existingTechtypes.Add(TechType.PrecursorIonBattery);
                        existingModels.Add(ionBatteryModel);
                    }
                }
            }

            //Add missing models that were found or create new ones if possible.
            if (powerCellModel != null && !existingTechtypes.Contains(TechType.PowerCell))
            {
                Models.Add(new BatteryModels()
                {
                    model = powerCellModel, techType = TechType.PowerCell
                });
                existingTechtypes.Add(TechType.PowerCell);
                existingModels.Add(powerCellModel);
            }

            //Add missing models that were found or create new ones if possible.
            if (!existingTechtypes.Contains(TechType.PrecursorIonPowerCell))
            {
                if (ionPowerCellModel != null)
                {
                    Models.Add(new BatteryModels()
                    {
                        model = ionPowerCellModel, techType = TechType.PrecursorIonPowerCell
                    });
                    existingTechtypes.Add(TechType.PrecursorIonPowerCell);
                    existingModels.Add(ionPowerCellModel);
                }
                else if (powerCellModel != null)
                {
                    GameObject ionPowerCellPrefab = Resources.Load <GameObject>("worldentities/tools/precursorionpowercell");
                    ionPowerCellModel      = GameObject.Instantiate(powerCellModel, powerCellModel.transform.parent);
                    ionPowerCellModel.name = "PrecursorIonPowerCellModel";


                    Material precursorIonPowerCellMaterial = ionPowerCellPrefab?.GetComponentInChildren <Renderer>()?.material;
                    if (precursorIonPowerCellMaterial != null)
                    {
                        ionPowerCellModel.GetComponentInChildren <Renderer>().material = new Material(precursorIonPowerCellMaterial);
                        Models.Add(new BatteryModels()
                        {
                            model = ionPowerCellModel, techType = TechType.PrecursorIonPowerCell
                        });
                        existingTechtypes.Add(TechType.PrecursorIonPowerCell);
                        existingModels.Add(ionPowerCellModel);
                    }
                }
            }

            //Remove models from the controlled objects list after we have added them as controlled models instead.
            List <GameObject> controlledObjects = new List <GameObject>(__instance.controlledObjects ?? new GameObject[0]);

            foreach (GameObject gameObject in __instance.controlledObjects ?? new GameObject[0])
            {
                if (!existingModels.Contains(gameObject))
                {
                    controlledObjects.Add(gameObject);
                }
            }
            __instance.controlledObjects = controlledObjects.ToArray();

            if (compatibleBatteries.Contains(TechType.Battery) || compatibleBatteries.Contains(TechType.PrecursorIonBattery))
            {
                // If the regular Battery or Ion Battery is compatible with this item, then modded batteries should also be compatible
                AddMissingTechTypesToList(compatibleBatteries, CbDatabase.BatteryItems);

                if (batteryModel != null && ionBatteryModel != null)
                {
                    //If we have enough information to make custom models for this tool or vehicle then create them.
                    AddCustomModels(batteryModel, ionBatteryModel, ref Models, CbDatabase.BatteryModels, existingTechtypes);
                }
            }

            if (compatibleBatteries.Contains(TechType.PowerCell) || compatibleBatteries.Contains(TechType.PrecursorIonPowerCell))
            {
                // If the regular Power Cell or Ion Power Cell is compatible with this item, then modded power cells should also be compatible
                AddMissingTechTypesToList(compatibleBatteries, CbDatabase.PowerCellItems);

                if (powerCellModel != null && ionPowerCellModel != null)
                {
                    //If we have enough information to make custom models for this tool or vehicle then create them.
                    AddCustomModels(powerCellModel, ionPowerCellModel, ref Models, CbDatabase.PowerCellModels, existingTechtypes);
                }
            }

            __instance.batteryModels = Models.ToArray();
        }