/// <summary>
        /// Function to highlight a neighbourhood when clicked.
        /// </summary>
        /// <param name="clickedObject"></param>
        /// <param name="showInfoPanel"></param>
        public void HighlightNeighbourhood(GameObject clickedObject, bool showInfoPanel = true)
        {
            if (showInfoPanel)
            {
                string neighbourhoodName = clickedObject.name.Replace("neighbourhood-", "");
                string infoText          = $"Neighbourhood: {neighbourhoodName}";
                try
                {
                    if (clickedObject.CompareTag("Building"))
                    {
                        NeighbourhoodModel neighboorhoud =
                            CityManager.Instance.GameModel.Neighbourhoods.SingleOrDefault(
                                (x => x.Name == neighbourhoodName));
                        if (neighboorhoud != null)
                        {
                            IVisualizedObject visualizedObject =
                                neighboorhoud.VisualizedObjects.SingleOrDefault(x => x.GameObject == clickedObject);
                            _selectedObject = visualizedObject;
                            string days = neighboorhoud.Age == 1 ? "day" : "days";
                            infoText =
                                $"Neighbourhood: {neighbourhoodName}\r\nAge: {neighboorhoud.Age} {days}";
                            VisualLayerModel selectedLayer = LayerManager.Instance.SelectedLayer;
                            if (visualizedObject is IVisualizedBuilding visualizedBuilding)
                            {
                                DestroyButton.SetActive(true);
                                OpenButton.SetActive(true);
                                if (selectedLayer != null)
                                {
                                    // Only enable destroy button for buildings
                                    infoText =
                                        $"Neighbourhood: {neighbourhoodName}\r\n{selectedLayer.Name.Replace("Layer", "")}: {visualizedBuilding.LayerValues[selectedLayer.Name]} / {neighboorhoud.LayerValues.Single(x => x.LayerType == selectedLayer.Name).MaxValue} ";
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError($"[OnObjectClickManager] {e.Message}");
                }
                finally
                {
                    InfoPanel.GetComponentInChildren <TMP_Text>().text = infoText;
                    InfoPanel.gameObject.SetActive(true);
                }
            }

            Renderer[] renderComponents = clickedObject.GetComponentsInChildren <Renderer>();
            foreach (Renderer renderComponent in renderComponents)
            {
                _clickedObjects.Add(renderComponent, renderComponent.material.color);
                renderComponent.material.color = Color.green;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function will generate a button on the screen together with the texture received from the API.
        /// </summary>
        /// <param name="visualLayer"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private IEnumerator GenerateButtonWithTexture(VisualLayerModel visualLayer, float x, float y)
        {
            string          layerUrl = $"{SettingsManager.Instance.Settings.Api.BaseUrl}/{visualLayer.Icon}";
            UnityWebRequest www      = UnityWebRequestTexture.GetTexture(layerUrl);

            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                GameObject buttonObject = new GameObject();

                // Create a button
                Button button = buttonObject.AddComponent <Button>();

                // Create an image
                Image image = buttonObject.AddComponent <Image>();

                // The downloaded texture (should always have a white color)
                Texture2D webTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;

                // Make a sprite of the downloaded texture
                Sprite webSprite = webTexture.GenerateSprite();

                image.sprite = webSprite;

                image.color = Color.black;
                buttonObject.transform.SetParent(LayerPanel.transform);

                // Set the correct position for the button
                RectTransform rt = buttonObject.GetComponent <RectTransform>();
                rt.SetParent(LayerPanel.transform);
                rt.localPosition = new Vector3(x, y, 0f);
                rt.localScale    = Vector3.one;
                rt.sizeDelta     = new Vector2(32, 32);

                // Set the name of the button
                buttonObject.name = visualLayer.Name;

                SpriteImageObject spriteImageObject = new SpriteImageObject(image, visualLayer);
                _spriteObjects.Add(spriteImageObject);

                // Create the onclick function
                button.onClick.AddListener(delegate { OnClick(spriteImageObject); });
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// When a layer is clicked, set and load the selected layer
        /// </summary>
        /// <param name="selectedSpriteImageObject"></param>
        private void OnClick(SpriteImageObject selectedSpriteImageObject)
        {
            // We do not want to spam the layers since there is no point of it
            if (selectedSpriteImageObject.VisualLayerModel.Equals(SelectedLayer))
            {
                return;
            }



            foreach (SpriteImageObject sprite in _spriteObjects)
            {
                sprite.Image.color = sprite.VisualLayerModel.Equals(selectedSpriteImageObject.VisualLayerModel) ? Color.white : Color.black;
            }

            SelectedLayer = selectedSpriteImageObject.VisualLayerModel;

            LoadLayer();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Function to clear all layer effects. Destroys all effects.
        /// </summary>
        /// <param name="clearSelectedLayer">Set this to true if you want to clear the selected layer.
        /// Used in closing the layer button.</param>
        public void ClearEffects(bool clearSelectedLayer = false)
        {
            // Find all effects that exists in the game
            Effect[] effects = FindObjectsOfType <Effect>();
            foreach (Effect effect in effects)
            {
                // Destroy the effect
                Destroy(effect);
            }

            if (!clearSelectedLayer)
            {
                return;
            }

            SelectedLayer = null;
            foreach (SpriteImageObject spriteOutlineObject in _spriteObjects)
            {
                spriteOutlineObject.Image.color = Color.black;
            }
        }
Exemplo n.º 5
0
 protected bool Equals(VisualLayerModel other)
 {
     return(Name == other.Name && Icon == other.Icon);
 }
Exemplo n.º 6
0
 public SpriteImageObject(Image image, VisualLayerModel visualLayerModel)
 {
     Image            = image;
     VisualLayerModel = visualLayerModel;
 }