Example #1
0
 // Update is called once per frame
 void Update()
 {
     // Building Mode Check
     if (EntityBuilder.IsBuildingMode)
     {
         // When the building mode is initialized - runs every once user enters to the building mode
         if (EntityBuilder.FirstPassBuildingMode)
         {
             _buildingSilhouetteSize.x           = EntityBuilder.SelectedEntity.SizeX;
             _buildingSilhouetteSize.y           = EntityBuilder.SelectedEntity.SizeY;
             buildShadow.transform.localScale    = (Vector2)_buildingSilhouetteSize;
             EntityBuilder.FirstPassBuildingMode = false;
             buildShadow.SetActive(true);
         }
         // If the building disable key is pressed, the building mode input check routine stops
         if (Input.GetKeyDown(buildingDisableKey))
         {
             EntityBuilder.IsBuildingMode        = false;
             EntityBuilder.FirstPassBuildingMode = true;
             buildShadow.SetActive(false);
         }
         // Build command with the mouse left click
         _mousePosCache = _mainCameraCache.ScreenToWorldPoint(Input.mousePosition);
         // Find the anchor point of the building mode node
         _cachedNode = gridSystem.NodeFromWorldPoint(_mousePosCache);
         //
         _gridExceedX = (_cachedNode.IndexX + _buildingSilhouetteSize.x) - EntityBuilder.GridLimitX;
         _gridExceedY = (_cachedNode.IndexY + _buildingSilhouetteSize.y) - EntityBuilder.GridLimitY;
         if (_gridExceedX > 0)
         {
             _cachedNode = EntityBuilder.CurrentGrid[_cachedNode.IndexX - _gridExceedX, _cachedNode.IndexY];
         }
         if (_gridExceedY > 0)
         {
             _cachedNode = EntityBuilder.CurrentGrid[_cachedNode.IndexX, _cachedNode.IndexY - _gridExceedY];
         }
         _spaceAvailable = EntityBuilder.CheckAvailableSpace(_cachedNode);
         //
         if (_spaceAvailable)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 if (!EventSystem.current.IsPointerOverGameObject())
                 {
                     EntityBuilder.CreateEntity(_cachedNode);
                     EntityBuilder.IsBuildingMode        = false;
                     EntityBuilder.FirstPassBuildingMode = true;
                     buildShadow.SetActive(false);
                     // Entity created event published
                     EventPublisher.OnEntityCreated();
                 }
                 else
                 {
                     Debug.Log("Somewhere else on UI is clicked while in building mode.");
                 }
             }
         }
         // Let the silhouette of the spawn entity follow the anchor node
         buildShadow.transform.position = _cachedNode.WorldVector2
                                          + (Vector2.left + Vector2.down) / 2;
         // Depending on the availability of the nodes, change silhouette color - Green for Buildable, Red for NotBuildable areas
         silhouetteRenderer.color = _spaceAvailable ? spaceAvailableColor : spaceNotAvailableColor;
     }
     else // General Input Mode
     {
         // Left click check
         if (Input.GetMouseButtonDown(0))
         {
             // If Clicked on UI Space, don't do anything.
             if (!EventSystem.current.IsPointerOverGameObject())         // Clicked on World Space
             {
                 //Debug.Log("On world space - Left Mouse");
                 _clickPositionCache         = _mainCameraCache.ScreenToWorldPoint(Input.mousePosition);
                 _leftClickCache.PressedNode = gridSystem.NodeFromWorldPoint(_clickPositionCache);
                 // Mouse left click event is published
                 EventPublisher.OnLeftClicked(_leftClickCache);
             }
         }
         // Right click check
         if (Input.GetMouseButtonDown(1))
         {
             // If Clicked on UI Space, don't do anything.
             if (!EventSystem.current.IsPointerOverGameObject())
             {
                 //Debug.Log("On world space - Right Mouse");
                 _clickPositionCache          = _mainCameraCache.ScreenToWorldPoint(Input.mousePosition);
                 _rightClickCache.PressedNode = gridSystem.NodeFromWorldPoint(_clickPositionCache);
                 // Mouse right click event is published
                 EventPublisher.OnRightClicked(_rightClickCache);
             }
         }
     }
 }