Esempio n. 1
0
    private void Step(Vector2Int coords)//TODO solve duplicity!
    {
        int currentIndex = map.V2ToIndex(coords);

        Map.CellType cell = map.GetCell(coords);//TODO optimize to use index
        if (cell == Map.CellType.Switch)
        {
            byte state = virtualState[currentIndex];
            state = (byte)(state > 0 ? 0 : 1);//Switch
            virtualState[currentIndex] = state;
        }
    }
Esempio n. 2
0
    public void PoolCell(Cell cell, Map.CellType cellType)
    {
        //Disable?
        switch (cellType)
        {
        case Map.CellType.Empty:
            floorPool.Push(cell);
            break;

        case Map.CellType.Block:
            blockPool.Push(cell);
            break;

        case Map.CellType.Switch:
            switchPool.Push(cell);
            break;
        }
    }
Esempio n. 3
0
    public Cell GetCell(Map.CellType cellType)
    {
        Cell cell = null;

        switch (cellType)
        {
        case Map.CellType.Empty:
            if (floorPool.Count > 0)
            {
                cell = floorPool.Pop();
            }
            else
            {
                cell = Instantiate(floorPrefab);
            }
            break;

        case Map.CellType.Block:
            if (blockPool.Count > 0)
            {
                cell = blockPool.Pop();
            }
            else
            {
                cell = Instantiate(blockPrefab);
            }
            break;

        case Map.CellType.Switch:
            if (switchPool.Count > 0)
            {
                cell = switchPool.Pop();
            }
            else
            {
                cell = Instantiate(switchPrefab);
            }
            break;
        }
#if UNITY_EDITOR
        cell.hideFlags = HideFlags.DontSave;
#endif
        return(cell);
    }
Esempio n. 4
0
    private int GetStepDistanceChange(Vector2Int coords)
    {
        int currentIndex = map.V2ToIndex(coords);

        Map.CellType cell = map.GetCell(coords);//TODO optimize to use index
        if (cell == Map.CellType.Switch)
        {
            byte state = virtualState[currentIndex];
            state = (byte)(state > 0 ? 0 : 1);//Switch

            if (map.TargetState[currentIndex] == state)
            {
                return(-1);                                       //Closer
            }
            else
            {
                return(+1);//Further
            }
        }
        return(0);
    }
Esempio n. 5
0
    private void OnSceneGUI()
    {
        Matrix4x4 matrix    = map.transform.localToWorldMatrix;
        Matrix4x4 invMatrix = map.transform.worldToLocalMatrix;

        Handles.matrix = matrix;

        if (editing && Event.current.type == EventType.MouseMove)
        {
            Vector2 screenPoint = Event.current.mousePosition;
            Ray     worldRay    = HandleUtility.GUIPointToWorldRay(screenPoint);
            ray = new Ray(invMatrix.MultiplyPoint(worldRay.origin),
                          invMatrix.MultiplyVector(worldRay.direction));
            Event.current.Use();//TODO check LC prefab placer test
        }


        if (editing)
        {
            Vector3 intersection = ray.GetPoint(ray.origin.y / -ray.direction.y);
            //Debug.Log(intersection + " " + ray);

            cell = Map.GetCellIndices(new Vector2(intersection.x, intersection.z));

            int controlId = GUIUtility.GetControlID(new GUIContent("MapEditor"), FocusType.Passive);
            if (Event.current.type == EventType.Layout)
            {//This will allow us to eat the click
                HandleUtility.AddDefaultControl(controlId);
            }
            if (Event.current.type == EventType.MouseUp && Event.current.button == 0)
            {
                if (map.CheckInBounds(cell))
                {
                    Undo.RecordObject(map, "Map Changed");
                    Map.CellType value = map[cell.y, cell.x];
                    int          index = map.V2ToIndex(cell);
                    if (Event.current.control)
                    {
                        if (value == Map.CellType.Switch)
                        {
                            if (Event.current.shift)
                            {
                                map.StartState[index] = (byte)(map.StartState[index] > 0 ? 0 : 1);
                                if (index >= 0 && index < map.CurrentState.Length)
                                {
                                    map.CurrentState[index] = map.StartState[index];
                                }
                            }
                            else
                            {
                                map.TargetState[index] = (byte)(map.TargetState[index] > 0 ? 0 : 1);
                            }
                        }
                    }
                    else
                    {
                        value = (Map.CellType)(((int)value + 1) % Map.CELL_COUNT);
                        map[cell.y, cell.x]    = value;
                        map.StartState[index]  = 0;
                        map.TargetState[index] = 0;
                        /*if (index >= 0 && index < map.CurrentState.Length) */ map.CurrentState[index] = 0;
                    }
                }
                Event.current.Use();
            }
        }

        Matrix4x4 cellMatrix;

        gridMaterial.color = Color.blue;
        gridMaterial.SetPass(1);
        if (editing && map.CheckInBounds(cell))
        {
            Vector2 cellPos = map.GetCellPosition(cell);
            cellMatrix = matrix * Matrix4x4.TRS(new Vector3(cellPos.x, 0f, cellPos.y), Quaternion.identity, Vector3.one);
            Graphics.DrawMeshNow(hexMesh, cellMatrix, 1);// area
        }

        gridMaterial.color = Color.green;
        gridMaterial.SetPass(1);
        Vector2 startPos = map.GetCellPosition(map.StartingCell);

        cellMatrix = matrix * Matrix4x4.TRS(new Vector3(startPos.x, 0f, startPos.y), Quaternion.identity, Vector3.one);
        Graphics.DrawMeshNow(hexMesh, cellMatrix, 1);// area

        gridMaterial.color = Color.red;
        gridMaterial.SetPass(1);
        Vector2 endPos = map.GetCellPosition(map.EndingCell);

        cellMatrix = matrix * Matrix4x4.TRS(new Vector3(endPos.x, 0f, endPos.y), Quaternion.identity, Vector3.one);
        Graphics.DrawMeshNow(hexMesh, cellMatrix, 1);// area

        gridMaterial.color = Color.white;
        gridMaterial.SetPass(0);
        for (int i = 0; i < map.Height; ++i)
        {
            for (int j = 0; j < map.Width; ++j)
            {
                Map.CellType cell = map[i, j];

                Vector2 cp = map.GetCellPosition(i, j);
                cellMatrix = matrix * Matrix4x4.TRS(new Vector3(cp.x, 0f, cp.y), Quaternion.identity, Vector3.one);

                Graphics.DrawMeshNow(hexMesh, cellMatrix, 0);// line
                cellMatrix = matrix * Matrix4x4.TRS(new Vector3(cp.x, 0f, cp.y), Quaternion.identity, new Vector3(0.8f, 0.8f, 0.8f));
                switch (cell)
                {
                case Map.CellType.Block:
                    Graphics.DrawMeshNow(hexMesh, cellMatrix, 1);    // area
                    break;

                case Map.CellType.Switch:
                    Graphics.DrawMeshNow(hexMesh, cellMatrix, 0);    // line
                    break;
                }
            }
        }

        gridMaterial.color = Color.cyan;
        gridMaterial.SetPass(0);
        for (int i = 0; i < map.Height; ++i)
        {
            for (int j = 0; j < map.Width; ++j)
            {
                bool state = map.TargetState[map.RCToIndex(i, j)] > 0;

                Vector2 cp = map.GetCellPosition(i, j);

                cellMatrix = matrix * Matrix4x4.TRS(new Vector3(cp.x, 0f, cp.y), Quaternion.identity, new Vector3(0.8f, 0.8f, 0.8f));
                if (state)
                {
                    Graphics.DrawMeshNow(hexMesh, cellMatrix, 0);// line
                }
            }
        }

        gridMaterial.color = Color.cyan;
        gridMaterial.SetPass(1);
        for (int i = 0; i < map.Height; ++i)
        {
            for (int j = 0; j < map.Width; ++j)
            {
                int  index = map.RCToIndex(i, j);
                bool state = map.StartState[index] > 0;
                if (EditorApplication.isPlaying)
                {
                    //if (index >= 0 && index < map.CurrentState.Length)//currentState size can't be assumed
                    state = map.CurrentState[index] > 0;
                }


                Vector2 cp = map.GetCellPosition(i, j);

                cellMatrix = matrix * Matrix4x4.TRS(new Vector3(cp.x, 0f, cp.y), Quaternion.identity, new Vector3(0.8f, 0.8f, 0.8f));
                if (state)
                {
                    Graphics.DrawMeshNow(hexMesh, cellMatrix, 1);// area
                }
            }
        }

        if (solution != null)
        {
            Handles.matrix = Matrix4x4.identity;
            Handles.color  = Color.yellow;
            Handles.DrawAAPolyLine(5f, solution);
        }
    }
Esempio n. 6
0
 private bool CanMoveIn(Direction dir)
 {
     Map.CellType cell = Map.map.GetCell(transform.position + dir.ToVector3());
     return(cell != Map.CellType.Wall);
 }