public Vehicle(GameObject vehicleGameObject, VisualizedVehicleModel vehicleModel,
                NeighbourhoodModel neighbourhoodModel, string vehicleName)
 {
     VehicleGameObject  = vehicleGameObject;
     VehicleModel       = vehicleModel;
     NeighbourhoodModel = neighbourhoodModel;
     VehicleName        = vehicleName;
 }
        /// <summary>
        /// Function to spawn a vehicle. The vehicle will spawn invisible with their target already set.
        /// </summary>
        /// <param name="visualizedVehicleModel"></param>
        private void SpawnVehicle(VisualizedVehicleModel visualizedVehicleModel)
        {
            // Select a target. The identifier of a vehicle is the same as the identifier for a building!
            var target = CityManager.Instance.GameModel.Neighbourhoods
                         .SelectMany(p => p.VisualizedObjects,
                                     (neighbourhood, visualizedObject) => new
                                     { Neighbourhood = neighbourhood, VisualizedObject = visualizedObject })
                         .Where(x => x.VisualizedObject is IVisualizedBuilding)
                         .FirstOrDefault(x => x.VisualizedObject.Identifier == visualizedVehicleModel.Identifier);

            // If target is null, means target is already removed. So we don't want to spawn a vehicle.
            if (target == null)
            {
                return;
            }

            // Vehicles do not have an age (yet)

            string vehicleName = AssetsManager.Instance.GetVehicleName(visualizedVehicleModel.Size);
            KeyValuePair <Vector3, Quaternion> spawnPoint =
                GridManager.Instance.VehicleSpawnPoints.PickRandom();
            GameObject vehicleGameObject = Instantiate(AssetsManager.Instance.GetVehiclePrefab(vehicleName),
                                                       spawnPoint.Key, spawnPoint.Value);

            // Disable all renderers. We make the vehicle invisible.
            // If we use the SetActive the game starts to freeze with every vehicle we spawn.
            // So we just keep the spawned vehicle but we make it invisible.
            // TODO: Implement a better way to do this
            foreach (Renderer childRenderer in vehicleGameObject.GetComponentsInChildren <Renderer>())
            {
                childRenderer.enabled = false;
            }

            // Add the vehicleNavigator component to the vehicle
            VehicleNavigator vehicleNavigator = vehicleGameObject.AddComponent <VehicleNavigator>();

            // Set vehicle name to identifier, so we know when it's clicked, to which visualized object the vehicle belongs.
            vehicleGameObject.name = $"vehicle-{visualizedVehicleModel.Identifier}";

            // Check if the game object is not null and set the target so we can already calculate the path.
            if (target.VisualizedObject.GameObject != null)
            {
                vehicleNavigator.SetTarget(target.VisualizedObject.GameObject.transform);
            }

            // Make a new vehicle object
            Vehicle v = new Vehicle(vehicleGameObject, visualizedVehicleModel, target.Neighbourhood, vehicleName);

            // And put the vehicle object in the queue so it's ready to "spawn".
            _vehicleQueue.Enqueue(v);

            // And add the vehicle to a list so we can keep track of it.
            Vehicles.Add(v);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Function to add a row to the table of traffic.
        /// </summary>
        /// <param name="sprite"></param>
        /// <param name="service"></param>
        /// <param name="vehicleModel"></param>
        /// <param name="vehicleGameObject"></param>
        private void AddRow(Sprite sprite, string service, VisualizedVehicleModel vehicleModel,
                            GameObject vehicleGameObject)
        {
            GameObject trafficRow;

            // Check if we already have this row, so we only need to update the object?
            if (InfoGameObjects.ContainsKey(vehicleModel.Identifier))
            {
                trafficRow = InfoGameObjects[vehicleModel.Identifier];
            }
            else
            {
                // Row does not exists, create one
                trafficRow =
                    Instantiate(TrafficOverviewRowPrefab);

                // Add an event trigger object so we can make the button clickable
                EventTrigger       eventTrigger = trafficRow.AddComponent <EventTrigger>();
                EventTrigger.Entry entry        = new EventTrigger.Entry {
                    eventID = EventTriggerType.PointerClick
                };

                // Add a lambda with the onclick function
                entry.callback.AddListener(eventData =>
                {
                    OnObjectClickManager.Instance.HighlightVehicle(vehicleGameObject);
                    gameObject.SetActive(false);
                });
                eventTrigger.triggers.Add(entry);
                trafficRow.transform.SetParent(ViewContent.transform, false);
                InfoGameObjects.Add(vehicleModel.Identifier, trafficRow);
            }

            TMP_Text[] texts = trafficRow.GetComponentsInChildren <TMP_Text>();
            Image      image = trafficRow.GetComponentInChildren <Image>();

            // Set the correct text. Row 0 = name of the service, Row 1 = the size of the service
            texts[0].text = service;
            texts[1].text = vehicleModel.Size.ToString();

            // Set the correct sprite
            image.sprite = sprite;
        }