Example #1
0
 /// <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);
             }
         }
     }
 }
Example #2
0
        /// <summary>
        /// Obtient le tile à l'index spécifié sur la texture donnée
        /// </summary>
        /// <param name="tileIndex">L'index du tile</param>
        /// <param name="texture">La texture</param>
        /// <returns>Le tile</returns>
        private Bitmap GetCroppedTexture(int tileIndex, string textureName)
        {
            if (tileIndex == GameMapTile.EMPTY)
            {
                return(null);
            }

            int    textureIndex = this.RetrieveTextureIndex(textureName);
            string croppedKey   = RetrieveCroppedKey(tileIndex, textureIndex);

            // Si le tile n'est pas en dictionnaire de données, on l'ajoute pour une utilisation future (évite de recharger la même texture)
            if (!CroppedTextures.ContainsKey(croppedKey))
            {
                Bitmap      texture        = this.GetTexture(textureName);
                Bitmap      croppedTexture = new Bitmap(GlobalData.TileSize.Width, GlobalData.TileSize.Height);
                Graphics    graphics       = Graphics.FromImage(croppedTexture);
                GameVector2 vector         = GameMapTile.DecodeFormattedIndex(tileIndex, texture.Width / GlobalData.TileSize.Width) * GlobalData.TileSize;
                Rectangle   section        = new Rectangle(vector.X, vector.Y, GlobalData.TileSize.Width, GlobalData.TileSize.Height);

                graphics.DrawImage(texture, DESTINATION_RECT_32, section, GraphicsUnit.Pixel);
                CroppedTextures.Add(croppedKey, croppedTexture);
            }

            Bitmap resultBitmap;

            if (CroppedTextures.TryGetValue(croppedKey, out resultBitmap))
            {
                return(resultBitmap);
            }
            return(null);
        }
Example #3
0
        // 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
        }