private void OnDrawGizmosSelected() { if (currentLevel?.grid == null || !Application.isPlaying) { return; } Grid grid = currentLevel.grid; for (int y = 0; y < grid.size; y++) { for (int x = 0; x < grid.size; x++) { float value = grid.nodes[x, y].preferDriveAroundPenalty / (float)Node.maxMovePenalty; if (value < 0) { value = 1f; } value = 1f - value; float red = grid.nodes[x, y].type == NodeType.Impassable ? 1f : value; Gizmos.color = new Color(red, value, value, 0.8f); Gizmos.DrawCube(grid.GridToWorld(x, y), Vector3.one * (0.9f / Grid.resolution)); } } }
void Start() { grid = new PathFinding.Grid(yItems, xItems); for (int j = 0; j < yItems; j++) { for (int i = 0; i < xItems; i++) { var point = new Point(i, j); var tile = grid[point]; var item = Instantiate(prefabItem, transform); item.Init(this, tile); items[point] = item; } } prefabItem.gameObject.SetActive(false); Render(); }
static void Main() { var builder = new NodeBuilder(Width, Height); var solver = new SimpleSolver(); ConsoleKeyInfo pressedKey = new ConsoleKeyInfo(); while (pressedKey.Key != ConsoleKey.X) { var grid = new Grid(builder.Build()); grid.Render(); var start = grid.Nodes.SingleOrDefault(node => node.X == 1 && node.Y == 1); var end = grid.Nodes.SingleOrDefault(node => node.X == Width && node.Y == Height); var solution = solver.Solve(start, end); grid.Render(solution); Console.WriteLine("Press X to exit"); pressedKey = Console.ReadKey(); } }
public void BuildMap() { spawnWaves = SpawnWavesFromMapTexture(mapTexture); for (int i = 0; i < spawnWaves.Length; i++) { RandomizeArray(spawnWaves[i].spawnings, random); } mapObject = new GameObject("Map", typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider)); mapObject.layer = LayerMask.NameToLayer("Ground"); grid = new Grid(map); mapMesh = map.BuildMesh(); mapObject.GetComponent <MeshFilter>().mesh = mapMesh; var material = new Material(LevelBootstrap.mapMaterial); mapObject.GetComponent <MeshRenderer>().material = material; mapTexture = MapTextureGenerator.Generate(map, 2048); material.mainTexture = mapTexture; var collider = mapObject.GetComponent <MeshCollider>(); collider.inflateMesh = true; collider.sharedMesh = mapMesh; SpawnBreakables(TileType.WeakWall, LevelBootstrap.brickBlockPrefab); SpawnBreakables(TileType.StrongWall, LevelBootstrap.concreteBlockPrefab); SpawnProps(TileType.Water, LevelBootstrap.waterPrefab); SpawnRandomProps(TileType.Woods, LevelBootstrap.RandomTree); enemySpawnPoints = map.EnemySpawnPoints(); RandomizeArray(enemySpawnPoints, random); var basePosition = map.BasePosition(); enemyController.playerBasePosition = basePosition; playerBaseBreakable = Object.Instantiate(LevelBootstrap.playerBasePrefab, basePosition, Quaternion.identity, mapObject.transform); playerBaseBreakable.OnBreak += defeatCallback; // Tank base colour float hue = (float)random.NextDouble() % 1f; const float saturation = 0.7f; const float value = 0.7f; enemyController.tankBaseColor = new ColorHSV(hue, saturation, value); BuildCollidersOnEdges(); // Set Background color and create same coloured plane on ground to hide tanks spawning underneath map var backgroundColor = LevelBootstrap.RandomSkyColor(random); Camera.main.backgroundColor = backgroundColor; RenderSettings.ambientLight = backgroundColor; var hideBelowGroundObject = GameObject.CreatePrimitive(PrimitiveType.Plane); hideBelowGroundObject.name = "Hide Below Ground"; hideBelowGroundObject.transform.position = new Vector3(map.size / 2f, -0.0001f, map.size / 2f); hideBelowGroundObject.transform.localScale = Vector3.one * 2 * map.size; hideBelowGroundObject.transform.parent = mapObject.transform; var hideBelowMaterial = new Material(Shader.Find("Unlit/Color")); hideBelowMaterial.color = backgroundColor; hideBelowGroundObject.GetComponent <Renderer>().material = hideBelowMaterial; } // build map