public void SaveToFile() { if (!VerifyLevel()) { return; } var levelDefinition = new LevelDefinition(); levelDefinition.dimensions = tileMapSize; levelDefinition.blockTypeSerializedGrid = new BlockType[tileMapSize.x * tileMapSize.y]; for (var x = 0; x < tileMapSize.x; ++x) { for (var y = 0; y < tileMapSize.y; ++y) { var linearMappedIndex = levelDefinition.CalculateLinearizedCoordinates(x, y); levelDefinition.blockTypeSerializedGrid[linearMappedIndex] = blocks[linearMappedIndex].blockType; } } var jsonRepresentation = JsonUtility.ToJson(levelDefinition); var formatter = new BinaryFormatter(); var file = OpenMapFileForWriting(); formatter.Serialize(file, jsonRepresentation); file.Close(); }
private void CreateChildren() { blocks = new EditableBlock[tileMapSize.x * tileMapSize.y]; for (var x = 0; x < tileMapSize.x; ++x) { for (var y = 0; y < tileMapSize.y; ++y) { var newCell = Instantiate(editableTilePrefab, Vector3.zero, Quaternion.identity, transform); newCell.transform.localPosition = new Vector3(x, y, 0f); var block = newCell.GetComponent <EditableBlock>(); Debug.Assert(block != null); block.Initialize(this, EditorPalette.EmptySpace); var linearizedIndex = LevelDefinition.CalculateLinearizedCoordinates(x, y, tileMapSize.x); blocks[linearizedIndex] = block; } } }
public BlockType GetBlockType(int xOffset, int yOffset) { Vector2Int intPos = new Vector2Int((int)(transform.localPosition.x - 0.5f) + xOffset, (int)(transform.localPosition.y - 0.5f) + yOffset); if (intPos.x < 0 || intPos.y < 0) { return(BlockType.Air); } if (intPos.x >= levelDefinition.dimensions.x || intPos.y >= levelDefinition.dimensions.y) { return(BlockType.Air); } BlockType type = levelDefinition.blockTypeSerializedGrid[levelDefinition.CalculateLinearizedCoordinates(intPos.x, intPos.y)]; if (type == BlockType.PlayerStart) { return(BlockType.Air); } else { return(type); } }