// Get the saved vehicle index within the player item manager prefab component public static int GetSelectedVehicleIndex(PlayerItemManager playerItemManager) { int val = PlayerPrefs.GetInt("SelectedVehicleIndex", -1); val = Mathf.Clamp(val, -1, playerItemManager.vehicles.Count - 1); return(val); }
/// <summary> /// Initialize the module selection UI with all of the module information. /// </summary> /// <param name="itemManager">A prefab that contains references to all of the module prefabs available in the module selection UI.</param> public void Initialize(PlayerItemManager itemManager) { // Create the module selection menu for (int i = 0; i < itemManager.modulePrefabs.Count; ++i) { ModuleMenuItemController buttonController = (ModuleMenuItemController)Instantiate(moduleItemButtonPrefab, moduleItemButtonParent); Module module = itemManager.modulePrefabs[i].GetComponent <Module>(); buttonController.transform.localPosition = Vector3.zero; buttonController.transform.localRotation = Quaternion.identity; buttonController.transform.localScale = new Vector3(1, 1, 1); buttonController.itemIndex = i; buttonController.SetIcon(module.MenuSprite); buttonController.SetLabel(module.Label); moduleMenuItems.Add(buttonController); // Deselect by default buttonController.Unselect(); // Hide module item by default buttonController.gameObject.SetActive(false); } }
// Get the saved module loadout public static List <int> GetModuleLoadout(int vehicleIndex, PlayerItemManager itemManager) { List <int> moduleIndexesByMount = new List <int>(); if (vehicleIndex < 0 || vehicleIndex >= itemManager.vehicles.Count) { Debug.LogError("Vehicle index out of range for the PlayerItemManager's vehicle list"); return(moduleIndexesByMount); } // Get the loadout index list from playerprefs Vehicle vehicle = itemManager.vehicles[vehicleIndex]; if (vehicleIndex == -1) { for (int i = 0; i < vehicle.ModuleMounts.Count; ++i) { moduleIndexesByMount.Add(-1); } return(moduleIndexesByMount); } string stringVal = PlayerPrefs.GetString("VehicleModuleLoadout" + vehicleIndex.ToString(), ""); if (stringVal == "") { List <ModuleMount> moduleMounts = vehicle.ModuleMounts; for (int i = 0; i < moduleMounts.Count; ++i) { if (moduleMounts[i].DefaultModulePrefabs.Count > 0) { moduleIndexesByMount.Add(itemManager.modulePrefabs.IndexOf(moduleMounts[i].DefaultModulePrefabs[0])); } else { moduleIndexesByMount.Add(-1); } } return(moduleIndexesByMount); } string[] splitStringVal = stringVal.Split(null); foreach (string element in splitStringVal) { int intVal; bool success = int.TryParse(element, out intVal); if (success) { moduleIndexesByMount.Add(intVal); } } // Verify the number of mounts int diff = vehicle.ModuleMounts.Count - moduleIndexesByMount.Count; if (diff >= 0) { for (int i = 0; i < diff; ++i) { moduleIndexesByMount.Add(-1); } } else { int startIndex = moduleIndexesByMount.Count + diff; moduleIndexesByMount.RemoveRange(startIndex, Mathf.Abs(diff)); } // Verify the type of module module for (int i = 0; i < moduleIndexesByMount.Count; ++i) { if (moduleIndexesByMount[i] == -1) { continue; } // If module index is out of range, clamp inside range if (moduleIndexesByMount[i] >= itemManager.modulePrefabs.Count) { moduleIndexesByMount[i] = Mathf.Clamp(moduleIndexesByMount[i], -1, itemManager.modulePrefabs.Count - 1); Debug.LogWarning("Module index out of range for PlayerItemManager modules list, clamping to " + moduleIndexesByMount[i].ToString()); } // If module prefab is null, set index to -1 if (itemManager.modulePrefabs[moduleIndexesByMount[i]] == null) { Debug.LogWarning("Module prefab at index " + i + " in the PlayerItemManager is null."); moduleIndexesByMount[i] = -1; continue; } // If module prefab doesn't have IModule interface, set to -1 Module module = itemManager.modulePrefabs[moduleIndexesByMount[i]]; if (module == null) { moduleIndexesByMount[i] = -1; } // If module is not compatible with the mount, set to -1 else if (!vehicle.ModuleMounts[i].MountableTypes.Contains(module.ModuleType)) { Debug.LogWarning("Attempting to load incompatible module on module mount."); moduleIndexesByMount[i] = -1; } } return(moduleIndexesByMount); }
// Save the module loadout of the vehicle public static void SaveModuleLoadout(Vehicle modifiedVehicle, int vehicleIndex, PlayerItemManager itemManager) { if (vehicleIndex < 0 || vehicleIndex >= itemManager.vehicles.Count) { Debug.LogError("Vehicle index out of range for the PlayerItemManager's vehicle list. Unable to save loadout to PlayerPrefs"); return; } string stringVal = ""; for (int i = 0; i < modifiedVehicle.ModuleMounts.Count; ++i) { if (modifiedVehicle.ModuleMounts[i].MountedModuleIndex == -1) { stringVal += modifiedVehicle.ModuleMounts[i].MountedModuleIndex.ToString(); if (i != modifiedVehicle.ModuleMounts.Count - 1) { stringVal += " "; } continue; } MountableModule mountedModule = modifiedVehicle.ModuleMounts[i].MountableModules[modifiedVehicle.ModuleMounts[i].MountedModuleIndex]; int index = itemManager.modulePrefabs.IndexOf(mountedModule.modulePrefab); if (index == -1) { Debug.LogWarning("Module found on ModuleMount has a prefab that does not exist in the PlayerItemManager allModulePrefabs list."); } stringVal += index.ToString(); if (i != modifiedVehicle.ModuleMounts.Count - 1) { stringVal += " "; } } PlayerPrefs.SetString("VehicleModuleLoadout" + vehicleIndex.ToString(), stringVal); }
/// <summary> /// Event called when a vehicle is selected in the loadout menu. /// </summary> /// <param name="newSelectionIndex">The index of the newly selected vehicle.</param> /// <param name="previousSelectionIndex">The index of the previously selected vehicle.</param> /// <param name="itemManager">A prefab containing references to all the vehicles and modules available in the menu. </param> public void OnVehicleSelection(int newSelectionIndex, int previousSelectionIndex, PlayerItemManager itemManager) { // Disable the last ship if (previousSelectionIndex != -1) { vehicles[previousSelectionIndex].CachedGameObject.SetActive(false); } // Activate the new ship vehicles[newSelectionIndex].CachedGameObject.SetActive(true); // Do the drop animation DropVehicle(); focusedVehicleTransform = vehicles[newSelectionIndex].transform; cameraPositionController.OnVehicleSelection(vehicles[newSelectionIndex]); }
/// <summary> /// Create all of the vehicles that will be displayed in the Loadout menu. /// </summary> /// <param name="vehiclePrefabs">A list of all the vehicle prefabs.</param> /// <param name="itemManager">A prefab containing references to all the vehicles and modules available in the menu. </param> /// <returns>A list of all the created vehicles. </returns> public List <Vehicle> AddDisplayVehicles(List <Vehicle> vehiclePrefabs, PlayerItemManager itemManager) { // Add ships for (int i = 0; i < vehiclePrefabs.Count; ++i) { // Instantiate and position the vehicle GameObject newVehicleGameObject = (GameObject)Instantiate(vehiclePrefabs[i].gameObject, Vector3.zero, Quaternion.identity); Transform newVehicleTransform = newVehicleGameObject.transform; newVehicleTransform.SetParent(vehicleDisplayParent); newVehicleTransform.localPosition = Vector3.zero; newVehicleTransform.localRotation = Quaternion.identity; newVehicleTransform.localScale = new Vector3(1f, 1f, 1f); // Add the vehicle to display list Vehicle createdVehicle = newVehicleGameObject.GetComponent <Vehicle>(); vehicles.Add(createdVehicle); createdVehicle.CachedRigidbody.isKinematic = true; for (int j = 0; j < createdVehicle.ModuleMounts.Count; ++j) { createdVehicle.ModuleMounts[j].createDefaultModulesAtStart = false; } // Mount modules foreach (ModuleMount moduleMount in createdVehicle.ModuleMounts) { // Clear anything that's already been loaded onto the prefab as a mountable module moduleMount.ClearMountableModules(); // Add mountable modules at this mount for all compatible modules foreach (Module modulePrefab in itemManager.modulePrefabs) { if (modulePrefab != null) { if (moduleMount.MountableTypes.Contains(modulePrefab.ModuleType)) { Module createdModule = (Module)GameObject.Instantiate(modulePrefab, null); moduleMount.AddMountableModule(createdModule, modulePrefab); } } } } // Get the loadout configuration List <int> moduleIndexesByMount = PlayerData.GetModuleLoadout(i, itemManager); // Mount modules on each module mount for (int j = 0; j < createdVehicle.ModuleMounts.Count; ++j) { // If no selection has been saved... if (moduleIndexesByMount[j] == -1) { // If there is no module loaded already if (createdVehicle.ModuleMounts[j].MountedModuleIndex == -1) { int firstSelectableIndex = Mathf.Clamp(0, -1, createdVehicle.ModuleMounts[j].MountableModules.Count - 1); if (firstSelectableIndex != -1) { createdVehicle.ModuleMounts[j].MountModule(firstSelectableIndex); } } } else { // Load the module according to the saved configuration for (int k = 0; k < createdVehicle.ModuleMounts[j].MountableModules.Count; ++k) { if (createdVehicle.ModuleMounts[j].MountableModules[k].modulePrefab == itemManager.modulePrefabs[moduleIndexesByMount[j]]) { createdVehicle.ModuleMounts[j].MountModule(k); break; } } } } // Deactivate the vehicle newVehicleGameObject.SetActive(false); } return(vehicles); }