Example #1
0
    void Update()
    {
        if (GameStats.GameOver)
        {
            return;
        }

        if (mapNeedsRebuild)
        {
            cellGraph = LogicalCellGraph.BuildCellGraph(Map, GetGateLogicalPositions());
            playerController?.RebuildGraph(cellGraph);
            needsMouseUpdate = true;
            enemyBrain?.RebuildGraph(cellGraph);
            mapNeedsRebuild = false;
        }

        var mouseSceenSpace   = Input.mousePosition;
        var mouseWorldSpace   = Camera.main.ScreenToWorldPoint(mouseSceenSpace);
        var mouseGridSpace    = Map.WorldToCell(mouseWorldSpace);
        var mouseLogicalSpace = GridSpaceConversion.GetLogicalSpaceFromGridSpace(mouseGridSpace, Map);

        if (playerController != null)
        {
            bool onGrid = mouseLogicalSpace.x >= 0 && mouseLogicalSpace.y >= 0 && mouseLogicalSpace.x < cellGraph.SizeX && mouseLogicalSpace.y < cellGraph.SizeY;

            if (mouseLogicalSpace != lastMouseLogicalPosition || needsMouseUpdate)
            {
                needsMouseUpdate         = false;
                lastMouseLogicalPosition = mouseLogicalSpace;
                playerController.OnMouseMove(mouseLogicalSpace, !onGrid, cellGraph, Map);
            }

            if (Input.GetMouseButtonDown(0))
            {
                playerController.OnMouseClick();
                playerController.RebuildGraph(cellGraph);
                needsMouseUpdate = true;
            }
            var mesh = playerController.GenerateLineMesh(Map);
            meshFilter.mesh = mesh;
        }
    }