using Godot; using System; using System.Collections.Generic; public partial class Map : PanelContainer { [Export] GridContainer grid; private HashSet handledTiles = new HashSet(); private Dictionary textureMap = new Dictionary(); // Called when the node enters the scene tree for the first time. public override void _Ready() { grid.Columns = GameData.layerSize + 1; } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { } public void ShowMap() { if (!Visible) return; foreach (Node node in grid.GetChildren()) { grid.RemoveChild(node); node.QueueFree(); } TextureRect texture; Label label; Tile[,] tiles = GameData.map[GameData.currentLayer].tiles; int size = GameData.layerSize; for (int z = -1; z < size; z++) { for (int x = -1; x < size; x++) { if (z == -1 || x == -1) { label = new Label { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, SizeFlagsHorizontal = SizeFlags.ExpandFill, SizeFlagsVertical = SizeFlags.ExpandFill }; if (z == -1 && x == -1) { label.Text = "\u2193 Z | \u2192 X"; } else if (z == -1) { label.Text = x.ToString(); } else { label.Text = z.ToString(); } grid.AddChild(label); continue; } texture = new TextureRect(); if (tiles[x, z].wasVisited || GameData.DEBUGMODE) { if (tiles[x, z].containsResource) { texture.Texture = ResourceDistributor.resources[tiles[x, z].resource.name]; texture.TooltipText = tiles[x, z].resource.item.GetReadableName() + $"\r(X: {x},Y: {GameData.currentLayer},Z: {z})"; } else { texture.Texture = GenerateTexture(32, new Color(0, 0, 0, 0)); texture.TooltipText = ""; } } else { texture.Texture = GenerateTexture(32, new Color(0, 0, 0, 1)); texture.TooltipText = "Not explored"; } textureMap[tiles[x, z]] = texture; if (!handledTiles.Contains(tiles[x, z])) { tiles[x, z].OnTileVisited += OnTileVisited; handledTiles.Add(tiles[x, z]); } texture.SizeFlagsHorizontal = SizeFlags.ExpandFill; texture.SizeFlagsVertical = SizeFlags.ExpandFill; texture.StretchMode = TextureRect.StretchModeEnum.Scale; grid.AddChild(texture); } } } private void OnTileVisited(object sender, EventArgs e) { Tile tile = sender as Tile; if (tile == null) return; if (textureMap.TryGetValue(tile, out TextureRect texture)) { UpdateMap(tile, texture); } } public void UpdateMap(Tile tile, TextureRect texture) { if (!IsInstanceValid(texture)) return; if (tile.containsResource) { texture.Texture = ResourceDistributor.resources[tile.resource.name]; texture.TooltipText = tile.resource.item.GetReadableName() + $"\r(X: {tile.GridPosition.X},Y: {GameData.currentLayer},Z: {tile.GridPosition.Y})"; } else { texture.Texture = GenerateTexture(32, new Color(0, 0, 0, 0)); texture.TooltipText = ""; } } public Texture2D GenerateTexture(int size, Color fillColor) { Image image = Image.CreateEmpty(size, size, false, Image.Format.Rgba8); image.Fill(fillColor); return ImageTexture.CreateFromImage(image); } }