void Update()
    {
        Vector3 localPoint;
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Input.GetKeyUp(KeyCode.Mouse0))
        {
            selected.GetComponent<PictureCell>().cell.pos = new Vector2(selected.localPosition.x, selected.localPosition.y);
            selected = null;
            alreadyPainted = new List<Transform>();
        }
        if (selected != null)
        {          
            if(Physics.Raycast(ray, out hit, 1000f, bgMask))
            {
                localPoint = map.transform.worldToLocalMatrix * new Vector3(hit.point.x, hit.point.y, 0);
                selected.localPosition = localPoint;
            }
        }
        else if (Input.GetKeyDown(KeyCode.Mouse0) && _editMode != EditMode.OFF)
        {
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.CompareTag("Background"))
                {
                    if (_editMode == EditMode.ADD)
                    {
                        localPoint = map.transform.worldToLocalMatrix * new Vector3(hit.point.x, hit.point.y, 0);
                        Transform newCell = Instantiate(cellPrefab, map.cellsContainer).transform;
                        newCell.localPosition = localPoint;
                        Cell site = new Cell { pos = new Vector2(localPoint.x, localPoint.y) };
                        newCell.GetComponent<PictureCell>().cell = site;
                        map.GetDiagram().cells.Add(site);
                    }
                }
                else if (hit.collider.CompareTag("Site"))
                {
                    if (_editMode == EditMode.DELETE)
                    {
                        map.GetDiagram().cells.Remove(hit.collider.gameObject.GetComponent<PictureCell>().cell);
                        Destroy(hit.collider.gameObject);
                    }
                    else if (_editMode == EditMode.MOVE)
                    {
                        selected = hit.collider.transform;
                    }
                }
            }

        }
        else if (Input.GetKey(KeyCode.Mouse0) && (edgePaint != EdgePaint.OFF || cellPaint != CellPaint.OFF))
        {
            if(Physics.Raycast(ray, out hit))
            {
                if (hit.collider.CompareTag("Site"))
                {
                    if(cellPaint == CellPaint.ERASER)
                    {
                        hit.collider.GetComponent<MeshRenderer>().enabled = false;
                    }
                    else if(cellPaint != CellPaint.OFF)
                    {
                        MeshRenderer rend = hit.collider.GetComponent<MeshRenderer>();
                        switch (cellPaint)
                        {
                            case CellPaint.FOREST:
                                rend.material = forestMat;
                                break;
                            case CellPaint.FARM:
                                rend.material = farmMat;
                                break;
                            case CellPaint.RURAL:
                                rend.material = ruralMat;
                                break;
                            case CellPaint.URBAN:
                                rend.material = urbanMat;
                                break;
                            case CellPaint.WATER:
                                rend.material = waterMat;
                                break;
                        }
                        rend.enabled = true;
                        if (!alreadyPainted.Contains(rend.transform))
                        {
                            alreadyPainted.Add(rend.transform);
                            if (cellPaint == CellPaint.FARM || cellPaint == CellPaint.URBAN)
                                rend.gameObject.GetComponent<PictureCell>().Rotate(Random.Range(0, 360));
                            else
                                rend.gameObject.GetComponent<PictureCell>().Rotate(0);
                        }
                    }
                }
                else if (hit.collider.CompareTag("Edge"))
                {
                    if(edgePaint != EdgePaint.OFF)
                    {
                        Transform parent = hit.collider.transform.parent;
                        LineRenderer rend = parent.GetComponent<LineRenderer>();
                        PictureEdge edge = parent.GetComponent<PictureEdge>();

                        switch (edgePaint)
                        {
                            case EdgePaint.RIVER:
                                rend.startColor = riverColor;
                                rend.endColor = riverColor;
                                edge.painted = true;
                                break;
                            case EdgePaint.ROAD:
                                rend.startColor = roadColor;
                                rend.endColor = roadColor;
                                edge.painted = true;
                                break;
                            case EdgePaint.WALL:
                                rend.startColor = wallColor;
                                rend.endColor = wallColor;
                                edge.painted = true;
                                break;
                            case EdgePaint.ERASER:
                                rend.startColor = StyleManager.Instance.GetForeground();
                                rend.endColor = StyleManager.Instance.GetForeground();
                                edge.painted = false;
                                break;
                        }
                    }
                    
                }
                
            }
        }
        
    }