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); }
// Test if a new piece can be added public bool QueryAddNewPiece(int newPieceTemplateIndex, Vector2Int position) { ShipPieceTemplate newPiece = database.GetPiece(newPieceTemplateIndex); foreach (ShipPiece piece in shipPieces) { if (((position.x > piece.Position.x && position.x < piece.Position.x + piece.Width) || (position.x + newPiece.Width > piece.Position.x && position.x + newPiece.Width < piece.Position.x + piece.Width)) && ((position.y > piece.Position.y && position.y < piece.Position.y + piece.Height) || (position.y + newPiece.Height > piece.Position.y && position.y + newPiece.Height < piece.Position.y + piece.Height))) { return(false); } } return(true); }
// 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); } }