protected virtual bool TryParseOffset(string offset, OffsetAxis axis, FileRange range, out IXmlComponentPointOffset result) { if (offset.Length == 0) { _logger.Log(LogLevel.Error, range, $"Expected a number before '{axis.ToString().ToLowerInvariant()}'", null); result = null; return(false); } if (!double.TryParse(offset, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var offsetValue)) { _logger.Log(LogLevel.Error, range, $"Unable to parse '{offset}' as double", null); result = null; return(false); } result = new XmlComponentPointOffset(offsetValue, axis); return(true); }
protected override bool TryParseOffset(string offset, OffsetAxis axis, FileRange range, out IXmlComponentPointOffset result) { if (!offset.Contains("$")) { // Does not contain a variable return(base.TryParseOffset(offset, axis, range, out result)); } offset = offset.Replace("(", "").Replace(")", ""); var variableIndex = offset.IndexOf("$"); var variableName = offset.Substring(variableIndex + 1); // Check variable exists if (!definitionsSection.Definitions.TryGetValue(variableName, out var variableValues)) { logger.Log(LogLevel.Error, range, $"Variable '{variableName}' does not exist", null); result = null; return(false); } // Check all possible values are valid var parsedValues = new ConditionalCollection <double>(); foreach (var variableValue in variableValues) { if (!double.TryParse(variableValue.Value, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var parsedValue)) { logger.Log(LogLevel.Error, range, $"Value '{variableValue.Value}' for ${variableName} is not a valid decimal", null); result = null; return(false); } parsedValues.Add(new TypeDescription.Conditions.Conditional <double>(parsedValue, variableValue.Conditions)); } result = new ComponentPointOffsetWithDefinition(offset.First() == '-', parsedValues, axis); return(true); }
private bool TryParseOffsetAxis(char c, FileRange range, out OffsetAxis axis) { switch (c) { case 'x': { axis = OffsetAxis.X; return(true); } case 'y': { axis = OffsetAxis.Y; return(true); } default: { _logger.Log(LogLevel.Error, range, $"Unexpected '{c}'; expected 'x' or 'y'", null); axis = OffsetAxis.X; return(false); } } }
/// <summary> /// Creates a Grid /// </summary> /// <param name="width">Width of the Grid (x-axis)</param> /// <param name="height">Height of the Grid (y-axis)</param> /// <param name="tileOffsetX">Offset of the tile on the x-axis</param> /// <param name="tileOffsetY">Offset of the tile on the y-axis</param> /// <param name="offsetAxis">The axis of the row offset</param> /// <param name="offRowOffset">The offset of the off-row (uneven rows)</param> public void CreateGrid(int width, int height, float tileOffsetX, float tileOffsetY, OffsetAxis offsetAxis, float offRowOffset) { if (!GridCreated) { GridCreated = true; } else { return; } float lastX = 0; float lastY = 0; // Fill the Grid m_Grid = new Tile[width, height]; // Update the values m_GridWidth = width; m_GridHeight = height; for (int x = 0; x < width; x++) { // Create Row GameObject GameObject row = new GameObject(); // Parent the Row to the Grid row.transform.parent = transform; // Set the name of the Row row.name = "HexRow | Row: " + x; for (int y = 0; y < height; y++) { // Create Tile Component Tile tile = Instantiate(m_TilePrefab, row.transform, false) as Tile; // Set random color for testing purposes //tile.GetComponent<SpriteRenderer>().color = new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)); // Set Position on map tile.transform.localPosition = new Vector2((offsetAxis == OffsetAxis.X_AXIS ? // Is the offset axis the X axis? (y % 2 == 0 ? // Is the current Y an even number? lastX : lastX + offRowOffset) : lastX), // True : False (offsetAxis == OffsetAxis.Y_AXIS ? // Is the offset axis the Y axis? (x % 2 == 0 ? // Is the current X an even number? lastY : lastY + offRowOffset) : lastY)); // True : False // Set the name of the object tile.name = "HexTile | GridPos(x: " + x + " y: " + y + ")"; // Set the Tile's values tile.PositionInGrid = new Vector2Int(x, y); // Add Tile to the Grid m_Grid[x, y] = tile; // Update Y position lastY += tileOffsetY; } // Update X position lastX += tileOffsetX; // Reset Y because X got updated, meaning that a new row will be made lastY = 0; } // Hide Prefab m_TilePrefab.gameObject.SetActive(false); // Invoke delegate if (s_OnHexGridCreated != null) { s_OnHexGridCreated(); } }
public XmlComponentPointOffset(double offset, OffsetAxis axis) { Offset = offset; Axis = axis; }
public ComponentPointOffsetWithDefinition(bool negative, ConditionalCollection <double> values, OffsetAxis axis) { Negative = negative; Values = values; Axis = axis; }