Example #1
0
    /// -------------------------------------------------------------------------------------------------------------------------------------------------------------
    /// Update
    /// -------------------------------------------------------------------------------------------------------------------------------------------------------------
    /// <summary> The Update method of MonoBehaviour class. </summary>
    private void Update()
    {
        // If movement is disabled, loop will be ignored.
        if (!movementEnable)
        {
            return;
        }

        // If not...
        // Transform position will calculated by using mouse position.
        // Also snapping mechanism are done here.
        var position = Building.CalculateObjectPosition();

        transform.position = position;

        // Grids at the transform position will be checked if they are occupied or not.
        var areGridsFree = Building.CheckGrids(Managers.Instance.gridManager.Grids[(int)position.x, (int)position.y]);

        // Color will be selected according to the occupancy.
        spriteRenderer.color = Building.ColorizeObject(areGridsFree);

        // If there is no right click, loop will be ignored.
        if (!Input.GetMouseButtonDown(0))
        {
            return;
        }

        // If not...
        // It will be checked that pointer is on UI element or not and the clicked grid is free or not. If so, game object will be destroyed.
        if (EventSystem.current.IsPointerOverGameObject() || !areGridsFree)
        {
            Destroy(gameObject);
            return;
        }

        // If not...
        // Building will be positioned to the click position. And the grids will be set as occupied.
        Building.OccupyGrids(Managers.Instance.gridManager.Grids[(int)position.x, (int)position.y]);
        // Default color of the building will be set.
        spriteRenderer.color = Building.ColorizeObject(false, true);
        // Movement will be disabled.
        movementEnable = false;
    }