Ejemplo n.º 1
0
 public void SetCellShip(Coordinates coordinates, ShipPiece shipPiece, int section, ShipOrientation orientation)
 {
     Cell cell;
     if (!Cells.TryGetValue(coordinates, out cell))
     {
         cell = new Cell();
         cell.Coordinates = coordinates;
         Cells.Add(coordinates, cell);
     }
     cell.ShipPiece = shipPiece;
     cell.ShipSection = section;
     cell.Orientation = orientation;
 }
Ejemplo n.º 2
0
        public void SetCellShip(Coordinates coordinates, ShipPiece shipPiece, int section, ShipOrientation orientation)
        {
            Cell cell;

            if (!Cells.TryGetValue(coordinates, out cell))
            {
                cell             = new Cell();
                cell.Coordinates = coordinates;
                Cells.Add(coordinates, cell);
            }
            cell.ShipPiece   = shipPiece;
            cell.ShipSection = section;
            cell.Orientation = orientation;
        }
Ejemplo n.º 3
0
    public void OnEnable()
    {
        comp = (ShipPieceTemplate)target;
        pieceCellsPROPERTY = serializedObject.FindProperty("pieceLines");
        widthPROPERTY      = serializedObject.FindProperty("width");
        heightPROPERTY     = serializedObject.FindProperty("height");

        GameObject prefab = comp.Prefab;

        if (prefab)
        {
            ShipPiece pieceTrack = comp.Prefab.GetComponent <ShipPiece>();
            if (pieceTrack)
            {
                aSprite = pieceTrack.MainSprite;
            }
        }


        string projectPath = Application.dataPath;

        projectPath = projectPath.Substring(0, projectPath.Length - 6);

        cellVisTex = new Texture2D(1, 1);
        cellVisTex.LoadImage(System.IO.File.ReadAllBytes(projectPath + "EDITOR_EXTRA_ASSETS/cellVisualizer.png"));
        cellVisTex.filterMode = FilterMode.Point;
        cellVisTex.Apply();

        cornerVisTex = new Texture2D(1, 1);
        cornerVisTex.LoadImage(System.IO.File.ReadAllBytes(projectPath + "EDITOR_EXTRA_ASSETS/cornerVisualizer.png"));
        cornerVisTex.filterMode = FilterMode.Point;
        cornerVisTex.Apply();

        // Make sure to set the array sizes
        pieceCellsPROPERTY.arraySize = comp.Height;
        pieceCellsPROPERTY.serializedObject.ApplyModifiedProperties();

        for (int i = 0; i < pieceCellsPROPERTY.arraySize; ++i)
        {
            var line = pieceCellsPROPERTY.GetArrayElementAtIndex(i);
            SerializedProperty cells = line.FindPropertyRelative("lineCells");
            cells.arraySize = comp.Width;
        }

        pieceCellsPROPERTY.serializedObject.ApplyModifiedProperties();

        GenerateWallTextures(comp.Width, comp.Height);
    }
Ejemplo n.º 4
0
    public void Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos)
    {
        Init();
        this.targetShip = targetShip;

        if (globalStartPos == globalEndPos)
        {
            StartCoroutine("DelaySearch");
            return;
        }

        nextIndex = 1;

        CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos);
        CellTemplate endCell   = targetShip.GetCellByGlobalPos(globalEndPos);

        // Invalid path supplied
        if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0)
        {
            return;
        }

        ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos);
        ShipPiece endPiece   = targetShip.GetPieceByGlobalCellPos(globalEndPos);


        NavGrid startGrid = new NavGrid(startPiece);

        startGrid.Generate();

        NavGrid goalGrid = startGrid;

        if (startPiece != endPiece)
        {
            goalGrid = new NavGrid(endPiece);
            goalGrid.Generate();
        }

        AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos);

        int newTTmp = 0;

        curPath = aStarAlgorithm.AStarSearch(ref newTTmp);

        wiggleTimer = Random.Range(0, 6.28318f);
    }
    // Add a new piece to the ship (spawn and track the new piece at position)
    public void AddNewPiece(int newPieceTemplateIndex, Vector2Int position, bool overridePosTEMPORARY = false)
    {
        ShipPieceTemplate piece = database.GetPiece(newPieceTemplateIndex);

        float xMod = piece.Width % 2 == 0 ? (piece.Width / 4.0f) : 0;
        float yMod = piece.Height % 2 == 0 ? (piece.Height / 4.0f) : 0;

        ShipPiece newPiece = GameObject.Instantiate(piece.Prefab, new Vector3(position.x + xMod, position.y + yMod, 0) * 3.2f, Quaternion.identity).GetComponent <ShipPiece>();

        newPiece.transform.SetParent(transform);
        newPiece.InitShipPiece(piece, position);

        shipPieces.Add(newPiece);

        if (overridePosTEMPORARY)
        {
            newPiece.transform.localPosition = new Vector3(5f, 9.25f, 0);
        }
    }
Ejemplo n.º 6
0
        private Image GetShipImage(ShipPiece piece)
        {
            switch (piece)
            {
            case ShipPiece.Carrier:
                return(FieldResources.CarrierImage);

            case ShipPiece.Battleship:
                return(FieldResources.BattleshipImage);

            case ShipPiece.Cruiser:
                return(FieldResources.CruiserImage);

            case ShipPiece.Sub:
                return(FieldResources.SubImage);

            case ShipPiece.Destroyer:
                return(FieldResources.DestroyerImage);
            }
            return(null);
        }
    // Connect 2 pieces by index, positions are relative to respective pieces
    public void ConnectPiecesByIndex(int localPieceIndex, int otherPieceIndex, Vector2Int localPos, Vector2Int otherPos)
    {
        if (localPieceIndex >= 0 && localPieceIndex < shipPieces.Count && otherPieceIndex >= 0 && otherPieceIndex < shipPieces.Count)
        {
            ShipPiece localPiece = shipPieces[localPieceIndex];
            ShipPiece otherPiece = shipPieces[otherPieceIndex];

            localPiece.AddConnection(new ShipConnection(localPos, otherPos, shipPieces[otherPieceIndex]));
            otherPiece.AddConnection(new ShipConnection(otherPos, localPos, shipPieces[localPieceIndex]));

            CellTemplate localCell = localPiece.GetShipCell(localPos.x, localPos.y);
            CellTemplate otherCell = otherPiece.GetShipCell(otherPos.x, otherPos.y);

            // Flag the connection cells
            if (localCell != null)
            {
                localCell.HasConnections = true;
            }
            if (otherCell != null)
            {
                otherCell.HasConnections = true;
            }
        }
    }
 public ActionResult Index(ShipPiece shipPiece)
 {
     _db.ShipPieces.Add(shipPiece);
     _db.SaveChanges();
     return(RedirectToAction("Index"));
 }
Ejemplo n.º 9
0
 private Image GetShipImage(ShipPiece piece)
 {
     switch (piece)
     {
         case ShipPiece.Carrier:
             return FieldResources.CarrierImage;
         case ShipPiece.Battleship:
             return FieldResources.BattleshipImage;
         case ShipPiece.Cruiser:
             return FieldResources.CruiserImage;
         case ShipPiece.Sub:
             return FieldResources.SubImage;
         case ShipPiece.Destroyer:
             return FieldResources.DestroyerImage;
     }
     return null;
 }
 public ShipConnection(Vector2Int localCell, Vector2Int otherCell, ShipPiece otherPiece)
 {
     this.localCell  = localCell;
     this.otherCell  = otherCell;
     this.otherPiece = otherPiece;
 }
Ejemplo n.º 11
0
 public NavGrid(ShipPiece shipPiece)
 {
     this.width     = shipPiece.Width;
     this.height    = shipPiece.Height;
     this.shipPiece = shipPiece;
 }
    public bool Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos)
    {
        if (globalStartPos == globalEndPos)
        {
            return(false);
        }

        NavCell[] overrideStart = null;

        // Cur, prev and T evaluation
        if (curPath != null)
        {
            if (nextIndex < curPath.Length)
            {
                overrideStart = new NavCell[2];

                // Store C0 and C1
                overrideStart[0] = curPath[nextIndex - 1];
                overrideStart[1] = curPath[nextIndex];
            }
        }

        nextIndex = 1;

        CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos);
        CellTemplate endCell   = targetShip.GetCellByGlobalPos(globalEndPos);

        // Invalid path supplied
        if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0)
        {
            return(false);
        }

        ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos);
        ShipPiece endPiece   = targetShip.GetPieceByGlobalCellPos(globalEndPos);


        NavGrid startGrid = new NavGrid(startPiece);

        startGrid.Generate();

        NavGrid goalGrid = startGrid;

        if (startPiece != endPiece)
        {
            goalGrid = new NavGrid(endPiece);
            goalGrid.Generate();
        }

        AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos);

        int newTState = -1;

        curPath = aStarAlgorithm.AStarSearch(ref newTState, overrideStart);

        // Either reset, invert or leave move timer
        switch (newTState)
        {
        case -1:
            moveTimer = 0;
            break;

        case 1:
            moveTimer = 1 - moveTimer;
            break;
        }

        pathTracer.SetPositions(GetNavArray());

        return(curPath != null);
    }
Ejemplo n.º 13
0
    public override void OnInspectorGUI()
    {
        bool isDirty = false;

        CheckMouseActions();

        GUILayout.BeginVertical("box");

        // Set the prefab for this piece
        GameObject prefab = (GameObject)EditorGUILayout.ObjectField("Associated Prefab:",
                                                                    comp.Prefab,
                                                                    typeof(GameObject),
                                                                    false);

        // Try strip the sprite from the prefab (if it has changed)
        if (prefab != comp.Prefab)
        {
            comp.Prefab = prefab;
            ShipPiece pieceTrack = comp.Prefab.GetComponent <ShipPiece>();
            if (pieceTrack)
            {
                aSprite = pieceTrack.MainSprite;
            }
        }


        // Draw sprite and controls
        if (aSprite != null)
        {
            int widthTry  = EditorGUILayout.IntField("Width:", comp.Width);
            int heightTry = EditorGUILayout.IntField("Height:", comp.Height);

            widthTry  = (int)Mathf.Clamp(widthTry, 1, widthTry);
            heightTry = (int)Mathf.Clamp(heightTry, 1, heightTry);

            bool regen = false;
            if (heightTry != comp.Height)
            {
                heightPROPERTY.intValue = heightTry;

                pieceCellsPROPERTY.arraySize = heightTry;

                pieceCellsPROPERTY.serializedObject.ApplyModifiedProperties();
                regen = true;
            }
            if (widthTry != comp.Width)
            {
                widthPROPERTY.intValue = widthTry;

                for (int i = 0; i < pieceCellsPROPERTY.arraySize; ++i)
                {
                    var line = pieceCellsPROPERTY.GetArrayElementAtIndex(i);
                    SerializedProperty cells = line.FindPropertyRelative("lineCells");
                    cells.arraySize = widthTry;
                }

                pieceCellsPROPERTY.serializedObject.ApplyModifiedProperties();
                regen = true;
            }

            if (regen)
            {
                GenerateWallTextures(widthTry, heightTry);
            }

            comp.Width  = widthTry;
            comp.Height = heightTry;

            float height = (Screen.width * ((aSprite.rect.height / aSprite.rect.width)) * scroll);
            float d      = (Screen.width / (aSprite.rect.width / 32f)) * scroll;
            float d4     = (Screen.width / (aSprite.rect.width / 4f)) * scroll;


            // Drag our texture around and flag for repaint
            if (middleMouse)
            {
                pos    += (Event.current.mousePosition - lastMousePosition) * Time.deltaTime * moveSpeed;
                isDirty = true;
            }

            if (rightMouseDown)
            {
                optionsMenuShow      = true;
                mouseDownOptionsMenu = Event.current.mousePosition;
            }

            if (!optionsMenuShow)
            {
                if (leftMouseDown)
                {
                    Vector2 parametricPos = (Event.current.mousePosition - new Vector2(10, 140));

                    parametricPos -= pos;


                    parametricPos -= new Vector2(0, Screen.width * (aSprite.rect.height / aSprite.rect.width) * scroll);
                    parametricPos  = new Vector2(parametricPos.x, parametricPos.y * -1);

                    parametricPos /= scroll;


                    parametricPos /= (d * comp.Width / scroll); //

                    parametricPos  = new Vector2(parametricPos.x, parametricPos.y * ((float)comp.Width / comp.Height));
                    parametricPos *= new Vector2Int(comp.Width, comp.Height);


                    if (parametricPos.x >= 0 && parametricPos.x < comp.Width && parametricPos.y >= 0 && parametricPos.y < comp.Height)
                    {
                        Vector2    moduloPos = new Vector2(parametricPos.x % 1, parametricPos.y % 1);
                        Vector2Int floorPos  = new Vector2Int(Mathf.FloorToInt(parametricPos.x), Mathf.FloorToInt(parametricPos.y));


                        var line = pieceCellsPROPERTY.GetArrayElementAtIndex(floorPos.y);
                        SerializedProperty cells = line.FindPropertyRelative("lineCells");
                        SerializedProperty cell  = cells.GetArrayElementAtIndex(floorPos.x);

                        if (cell.FindPropertyRelative("cellState").intValue == 0)
                        {
                            cell.FindPropertyRelative("cellState").intValue = 1;
                        }
                        else
                        {
                            cell.FindPropertyRelative("cellState").intValue = 0;
                        }

                        pieceCellsPROPERTY.serializedObject.ApplyModifiedProperties();
                    }
                }
            }

            if (Event.current.type == EventType.ScrollWheel)
            {
                float scaleDelta = Event.current.delta.y * scroll * -Time.deltaTime;

                isDirty = true;
                scroll += scaleDelta;
            }

            if (Event.current.type == EventType.KeyDown)
            {
                if (Event.current.keyCode == KeyCode.F)
                {
                    pos    = new Vector2(0, 30);
                    scroll = 1f;
                }
            }

            GUILayout.BeginArea(new Rect(10, 140, Screen.width - 20, Screen.height - 260));

            // Draw sprite in area
            GUIDrawSprite(new Rect(pos.x, pos.y, Screen.width * scroll, height), aSprite);


            GUI.DrawTexture(new Rect(pos.x - (d4 / 4.0f), (pos.y + (height - (d4 - (d4 / 4.0f)))), d4, d4), cornerVisTex, ScaleMode.ScaleToFit);
            GUI.DrawTexture(new Rect(pos.x + (d * comp.Width) + (d4 / 4.0f), pos.y + height - (d * comp.Height) + (d4 / 4.0f) * 3, -d4, -d4), cornerVisTex, ScaleMode.ScaleToFit);


            // Draw cell visualizers and wall visualizers
            for (int j = 0; j < comp.Height; ++j)
            {
                for (int i = 0; i < comp.Width; ++i)
                {
                    var line = pieceCellsPROPERTY.GetArrayElementAtIndex(j);

                    SerializedProperty cells = line.FindPropertyRelative("lineCells");
                    SerializedProperty cell  = cells.GetArrayElementAtIndex(i);


                    if (cell.FindPropertyRelative("cellState").intValue == 1)
                    {
                        GUI.DrawTexture(new Rect(d * i + pos.x, d * -j + (pos.y + (height - d)), d, d), cellVisTex, ScaleMode.ScaleToFit);

                        Texture2D wallTex = cellVisualizers[j * comp.Width + i];

                        GUI.DrawTexture(new Rect(d * i + pos.x, d * -j + (pos.y + (height - d)), d, d), wallTex, ScaleMode.ScaleToFit);
                    }
                }
            }

            DoOptionsMenu(new Vector2(10, 140));

            GUILayout.EndArea();
        }

        GUILayout.EndVertical();

        // Ensure changes are saved
        EditorUtility.SetDirty(comp);

        // Cache last mouse position on our editor
        lastMousePosition = Event.current.mousePosition;

        // Repaint if necessary
        if (isDirty)
        {
            Repaint();
        }
    }