public void buildObstacles() { TileMap tilemap = (TileMap)GetNode("Navigation2D/Ground"); Vector2 cellSize = tilemap.CellSize; Rect2 usedRect = tilemap.GetUsedRect(); int startPointX = (int)usedRect.Position.x; int startPointY = (int)usedRect.Position.y; int maxLengthX = (int)usedRect.Size.x; int maxLengthY = (int)usedRect.Size.y; for (int xIndex = startPointX; xIndex < maxLengthX; xIndex++) { for (int yIndex = startPointY; yIndex < maxLengthY; yIndex++) { Vector2 position; Obstacle.Items item = Obstacle.Items.remain; if (tilemap.GetCell(xIndex, yIndex) == 0) { item = Obstacle.Items.crate_wood; } else if (tilemap.GetCell(xIndex, yIndex) == 45 || tilemap.GetCell(xIndex, yIndex) == 46) { item = Obstacle.Items.crate_steel; } else if (tilemap.GetCell(xIndex, yIndex) == 7) { item = Obstacle.Items.roadblock_red; } if (item != Obstacle.Items.remain) { position = tilemap.MapToWorld(new Vector2(xIndex, yIndex)); //for (int tileIndex = 0; tileIndex < 4; tileIndex++) //{ Obstacle obstacle = (Obstacle)((PackedScene)GD.Load("res://environments/Obstacle.tscn")).Instance(); obstacle.type = item; obstacle.Position = position + (cellSize / 2); // if (tileIndex == 0) // { // obstacle.Position = new Vector2(position.x + (cellSize.x / 2), position.y + (cellSize.y / 2)); // } // else if (tileIndex == 1) // { // obstacle.Position = new Vector2(position.x + (cellSize.x / 2) + cellSize.x, position.y + (cellSize.y / 2)); // } // else if (tileIndex == 2) // { // obstacle.Position = new Vector2(position.x + (cellSize.x / 2), position.y + (cellSize.y / 2) + cellSize.y); // } // else if (tileIndex == 3) // { // obstacle.Position = new Vector2(position.x + (cellSize.x / 2) + cellSize.x, position.y + (cellSize.y / 2) + cellSize.y); // } obstacle.Name = "obstacle_" + xIndex + "_" + yIndex; GetNode("Obstacles").AddChild(obstacle); //} } } } if (GetTree().IsNetworkServer()) { buildObstaclesCache(); } }
private void buildObstacles() { Rect2 usedRect = _tileMap.GetUsedRect(); int startPointX = (int)usedRect.Position.x; int startPointY = (int)usedRect.Position.y; int maxLengthX = (int)usedRect.Size.x; int maxLengthY = (int)usedRect.Size.y; Godot.Collections.Dictionary prebuildObstacles = new Godot.Collections.Dictionary(); if (GetTree().NetworkPeer == null || GetTree().IsNetworkServer()) { // Add pre - added obstacles foreach (Node2D obstacle in GetChildren()) { Vector2 pos = _tileMap.WorldToMap(obstacle.Position); prebuildObstacles.Add(pos.x + _obstacleIndexSeparator + pos.y, pos); float x = pos.x; float y = pos.y; prebuildObstacles.Add(x + _obstacleIndexSeparator + y, new Vector2(x, y)); } } for (int xIndex = startPointX; xIndex < maxLengthX; xIndex++) { for (int yIndex = startPointY; yIndex < maxLengthY; yIndex++) { // if there is already obstacle on it, then ignore this tile, this is also not workable tile, so skip entire logic to next tile if (prebuildObstacles.Contains(xIndex + _obstacleIndexSeparator + yIndex)) { continue; } Vector2 position; Obstacle.Items item = Obstacle.Items.remain; if (_tileMap.GetCell(xIndex, yIndex) == 0) { item = Obstacle.Items.crate_wood; } else if (_tileMap.GetCell(xIndex, yIndex) == 45 || _tileMap.GetCell(xIndex, yIndex) == 46) { item = Obstacle.Items.crate_steel; } else if (_tileMap.GetCell(xIndex, yIndex) == 7) { item = Obstacle.Items.roadblock_red; } Label mapLabel = (Label)GetNode("../MapCoordinate").Duplicate(); mapLabel.Text = ("(" + xIndex + "," + yIndex + ")"); mapLabel.Name = "maplabel_" + xIndex + _obstacleIndexSeparator + yIndex; position = _tileMap.MapToWorld(new Vector2(xIndex, yIndex)); if (item != Obstacle.Items.remain) { Obstacle obstacle = (Obstacle)((PackedScene)GD.Load("res://environments/Obstacle.tscn")).Instance(); obstacle.type = item; obstacle.Name = _obstaclePrefix + xIndex + _obstacleIndexSeparator + yIndex; AddChild(obstacle); obstacle.GlobalPosition = position + _halfCellSize; mapLabel.Set("custom_colors/font_color", new Color("#ff0000")); } else { if (GetTree().NetworkPeer == null || GetTree().IsNetworkServer()) { // As there is no obstacle, this cell is a workable path _traverableTiles.Add(new Vector2(xIndex, yIndex)); } mapLabel.Set("custom_colors/font_color", new Color("#0016ff")); } mapLabel.SetGlobalPosition(position + _halfCellSize); AddChild(mapLabel); } } if (GetTree().NetworkPeer == null || GetTree().IsNetworkServer()) { _buildObstaclesCache(); } }