Exemple #1
0
        public void Spawn()
        {
            int playerVehicleIndex = PlayerData.GetSelectedVehicleIndex(itemManager);

            if (playerVehicleIndex == -1)
            {
                if (itemManager != null && itemManager.vehicles.Count > 0)
                {
                    playerVehicleIndex = 0;
                }
            }

            // Get the player ship
            Vehicle playerVehicle = null;

            if (playerVehicleIndex != -1)
            {
                Transform vehicleTransform = ((GameObject)Instantiate(itemManager.vehicles[playerVehicleIndex].gameObject, spawnPoint.position, spawnPoint.rotation)).transform;
                playerVehicle      = vehicleTransform.GetComponent <Vehicle>();
                playerVehicle.name = "PlayerVehicle";

                List <int> selectedModuleIndexesByMount = PlayerData.GetModuleLoadout(playerVehicleIndex, itemManager);

                bool hasLoadout = false;
                for (int i = 0; i < selectedModuleIndexesByMount.Count; ++i)
                {
                    if (selectedModuleIndexesByMount[i] != -1)
                    {
                        hasLoadout = true;
                    }
                }

                // Update the vehicle loadout
                if (hasLoadout)
                {
                    for (int i = 0; i < selectedModuleIndexesByMount.Count; ++i)
                    {
                        if (selectedModuleIndexesByMount[i] == -1)
                        {
                            continue;
                        }

                        Module module = GameObject.Instantiate(itemManager.modulePrefabs[selectedModuleIndexesByMount[i]], null);

                        playerVehicle.ModuleMounts[i].AddMountableModule(module, itemManager.modulePrefabs[selectedModuleIndexesByMount[i]], true);
                    }
                }
                else
                {
                    for (int i = 0; i < playerVehicle.ModuleMounts.Count; ++i)
                    {
                        playerVehicle.ModuleMounts[i].createDefaultModulesAtStart = true;
                    }
                }

                onLoadoutVehicleSpawned.Invoke(playerVehicle);
            }
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        /// <summary>
        /// Spawn the vehicle and enter it.
        /// </summary>
        public void Spawn()
        {
            // Create a reference to the vehicle
            Vehicle vehicle = null;

            // Create the vehicle
            switch (vehicleSpawnType)
            {
            case VehicleSpawnType.Loadout:

                int playerVehicleIndex = PlayerData.GetSelectedVehicleIndex(itemManager);
                if (playerVehicleIndex == -1)
                {
                    if (itemManager != null && itemManager.vehicles.Count > 0)
                    {
                        playerVehicleIndex = 0;
                    }
                }

                if (playerVehicleIndex != -1)
                {
                    // Create the vehicle
                    Transform vehicleTransform = ((GameObject)Instantiate(itemManager.vehicles[playerVehicleIndex].gameObject, vehicleSpawnPoint.position, vehicleSpawnPoint.rotation)).transform;
                    vehicle      = vehicleTransform.GetComponent <Vehicle>();
                    vehicle.name = vehicleName;


                    // Get the vehicle loadout
                    List <int> selectedModuleIndexesByMount = PlayerData.GetModuleLoadout(playerVehicleIndex, itemManager);

                    bool hasLoadout = false;
                    for (int i = 0; i < selectedModuleIndexesByMount.Count; ++i)
                    {
                        if (selectedModuleIndexesByMount[i] != -1)
                        {
                            hasLoadout = true;
                        }
                    }

                    // Create the vehicle loadout
                    if (hasLoadout)
                    {
                        for (int i = 0; i < selectedModuleIndexesByMount.Count; ++i)
                        {
                            if (selectedModuleIndexesByMount[i] == -1)
                            {
                                continue;
                            }

                            Module module = GameObject.Instantiate(itemManager.modulePrefabs[selectedModuleIndexesByMount[i]], null);

                            vehicle.ModuleMounts[i].AddMountableModule(module, itemManager.modulePrefabs[selectedModuleIndexesByMount[i]], true);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < vehicle.ModuleMounts.Count; ++i)
                        {
                            vehicle.ModuleMounts[i].createDefaultModulesAtStart = true;
                        }
                    }
                }

                break;

            case VehicleSpawnType.Prefab:

                vehicle = Instantiate(vehiclePrefab, vehicleSpawnPoint.position, vehicleSpawnPoint.rotation);

                break;
            }

            if (vehicle != null)
            {
                // Set child vehicle that the game agent will exit to when exiting this vehicle.
                VehicleEnterExitManager enterExitManager = vehicle.GetComponent <VehicleEnterExitManager>();
                if (startingChildVehicle != null)
                {
                    if (enterExitManager != null)
                    {
                        if (startingChildVehicle.CanEnter(enterExitManager))
                        {
                            enterExitManager.SetChild(startingChildVehicle);
                        }
                    }
                }

                // Make the starting occupant enter the vehicle
                if (startingOccupant != null)
                {
                    GameAgent occupant;
                    if (spawnOccupantFromPrefab)
                    {
                        occupant = Instantiate(startingOccupant, Vector3.zero, Quaternion.identity);
                    }
                    else
                    {
                        occupant = startingOccupant;
                    }

                    occupant.EnterVehicle(vehicle);
                }

                // Call the event
                onVehicleSpawned.Invoke(vehicle);
            }
        }