/// <summary> /// Retourne la reférence du tile à la position spécifiée et retourne 'null' si inexistant /// </summary> /// <param name="x">La position horizontale X du tile</param> /// <param name="y">La position verticale Y du tile</param> /// <returns>La reférence vers le tile, ou null</returns> public GameMapTile this[int x, int y] { get { if (GameMap.InBounds(new GameVector2(x, y))) { int index = GameMapTile.EncodeFormattedIndex(new Point(x, y), GlobalData.MapSize.Width); if (index < this.tiles.Count) { return(this.tiles[index]); } } return(null); } set { if (GameMap.InBounds(new GameVector2(x, y))) { int index = GameMapTile.EncodeFormattedIndex(new Point(x, y), GlobalData.MapSize.Width); if (index < this.tiles.Count) { this.tiles[index] = value; this.LayerChanged?.Invoke(this, EventArgs.Empty); } } } }
// TODO : Optimisation requise (Division en sous-methodes, calculs et déroulement impératif unique) /// <summary> /// Modifie de façon intelligente les données de la carte selon la selection du tileset, à partir de la position donnée /// </summary> /// <param name="position">La position du premier tile en haut à gauche à modifier</param> public void SetTiles(int layerIndex, GameVector2 position, TextureInfo texture, bool raiseChanged = true) { #if DEBUG Stopwatch watch = new Stopwatch(); watch.Start(); #endif if (layerIndex >= 0 && layerIndex < this.layers.Count) { if (texture != null && texture.BitmapSelection != null && texture.BitmapSource != null) { int tmpWidth = texture.BitmapSelection.Width / GlobalData.TileSize.Width; int tmpHeight = texture.BitmapSelection.Height / GlobalData.TileSize.Height; int tilesCount = texture.BitmapSource.Width / GlobalData.TileSize.Width; for (int x = 0; x < tmpWidth; x++) { for (int y = 0; y < tmpHeight; y++) { GameMapTile tile = this.layers.ElementAt(layerIndex)[position.X + x, position.Y + y]; if (tile != null) { Rectangle selection = new Rectangle( GlobalData.TileSize.Width * x, GlobalData.TileSize.Height * y, GlobalData.TileSize.Width, GlobalData.TileSize.Height); GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF bounds = texture.BitmapSource.GetBounds(ref unit); if (bounds.Contains(selection)) { // OutOfMemory eventuel, par usage de textures inexistantes (OutOfRange extension) tile.Texture = texture.BitmapSelection.Clone(selection, PixelFormat.DontCare); Point location = new Point( (selection.Location.X + texture.Selection.X) / GlobalData.TileSize.Width, (selection.Location.Y + texture.Selection.Y) / GlobalData.TileSize.Height); tile.FormattedIndex = GameMapTile.EncodeFormattedIndex(location, tilesCount); tile.TextureIndex = this.RetrieveTextureIndex(texture.Path); } } } } } else { GameMapTile tile = this.layers.ElementAt(layerIndex)[position.X, position.Y]; if (tile != null) { tile.FormattedIndex = GameMapTile.EMPTY; tile.Texture = null; } } if (raiseChanged) { this.MapChanged?.Invoke(this); } } #if DEBUG watch.Stop(); Console.WriteLine($"SetTile : {watch.ElapsedMilliseconds.ToString()} ms"); #endif }