Beispiel #1
0
        public Sprite(CanvasBitmap image, int imageResolution, PointInt absolutePosition)
        {
            AbsolutePosition = absolutePosition;
            _spriteSheet     = new SpriteSheet(image, imageResolution);

            _currentAnimationState = SPRITE_ANIMATION.IDLE_DOWN;
            _currentAnimation      = SpriteAnimationDefinitions.Copy(_currentAnimationState);
        }
Beispiel #2
0
        public static void Cache()
        {
            if (bPauseCaching)
            {
                return;
            }
            if (bCacheInProgress)
            {
                Debug.AddTimedString("Last cache hasn't finished yet. Aborting...", Colors.Pink); return;
            }

            bCacheInProgress = true;
            int       nChunksAdded = 0;
            Stopwatch s            = Stopwatch.StartNew();

            PointInt coordinates = new PointInt(Camera.ChunkPositionX, Camera.ChunkPositionY);

            for (int i = -_cachedChunkLoadRadius; i <= Chunk.MaxChunksVisibleX + _cachedChunkLoadRadius; i++)
            {
                for (int j = -_cachedChunkLoadRadius; j <= Chunk.MaxChunksVisibleY + _cachedChunkLoadRadius; j++)
                {
                    if (CacheChunk(new PointInt(coordinates.X + i, coordinates.Y + j), bSuppressOutput: true))
                    {
                        // show debug message when caching first chunk
                        if (nChunksAdded == 0)
                        {
                            Debug.AddTimedString("Updating cache...", Colors.Yellow);
                        }
                        nChunksAdded++;
                    }
                }
            }

            s.Stop();

            // report if chunks were added or if elapsed time > 0
            if (nChunksAdded > 0 || s.ElapsedMilliseconds > 0)
            {
                Debug.AddTimedString("Cache update took " + s.ElapsedMilliseconds.ToString() + "ms", Colors.White);
                Debug.AddTimedString("Chunks added: " + nChunksAdded.ToString(), Colors.Green);
            }

            bCacheInProgress = false;
        }
Beispiel #3
0
 public static bool CacheChunk(PointInt coordinates, bool bSuppressOutput = false)
 {
     if (!ChunkCache.TryGetValue(coordinates, out Chunk c))
     {
         c = Chunk.Create(_device, coordinates);
         lock (Chunk.CacheLock) {
             if (!ChunkCache.TryGetValue(coordinates, out Chunk temp))
             {
                 ChunkCache.Add(c.Coordinates, c);
                 if (!bSuppressOutput)
                 {
                     Debug.AddTimedString("Caching: " + c.Coordinates.ToString(), Colors.White);
                 }
             }
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
        public override bool Equals(object obj)
        {
            PointInt compare = (PointInt)obj;

            return(compare.X == X && compare.Y == Y);
        }
 internal void Draw(SpriteSheet spriteSheet, PointInt position, CanvasAnimatedDrawEventArgs args)
 {
     args.DrawingSession.DrawImage(spriteSheet.Image,
                                   destinationRectangle: new Rect(position.X, position.Y, Map.TILE_RESOLUTION, Map.TILE_RESOLUTION),
                                   sourceRectangle: new Rect(_currentFrame.X * spriteSheet.SourceResolution, _currentFrame.Y * spriteSheet.SourceResolution, spriteSheet.SourceResolution, spriteSheet.SourceResolution));
 }
Beispiel #6
0
 public Chunk(CanvasDevice device, PointInt coordinates)
 {
     Coordinates = coordinates;
 }
Beispiel #7
0
        public static Chunk Create(CanvasDevice device, PointInt coordinates)
        {
            Stopwatch s     = Stopwatch.StartNew();
            Chunk     chunk = new Chunk(device, coordinates);

            chunk.Tiles = new Tile[ChunkSideLength, ChunkSideLength];

            for (int tileX = 0; tileX < ChunkSideLength; tileX++)
            {
                for (int tileY = 0; tileY < ChunkSideLength; tileY++)
                {
                    chunk.Tiles[tileX, tileY] = new Tile();// chunkCoordinateX, chunkCoordinateY, tileX, tileY);
                }
            }

            Stopwatch sHeightMap = Stopwatch.StartNew();

            chunk.GenerateHeightMap();
            sHeightMap.Stop();

            lock (Debug.DebugCollectionsLock) {
                Debug.HeightMapTimes.Add(sHeightMap.ElapsedMilliseconds);
            }

            // draw chunk to render target
            Color _debugBackgroundColor = Statics.RandomColor();

            chunk.RenderTargetBackgroundColor = new CanvasRenderTarget(device, ChunkSideLength, ChunkSideLength, 96);
            chunk.RenderTargetTerrain         = new CanvasRenderTarget(device, ChunkSideLength * Statics.OverworldTileResolution, ChunkSideLength * Statics.OverworldTileResolution, 96);
            //chunk.RenderTargetElevation = new CanvasRenderTarget(device, ChunkSideLength * Statics.OverworldTileResolution, ChunkSideLength * Statics.OverworldTileResolution, 96);
            using (CanvasDrawingSession dsBackgroundColor = chunk.RenderTargetBackgroundColor.CreateDrawingSession()) {
                using (CanvasDrawingSession dsElevation = chunk.RenderTargetTerrain.CreateDrawingSession()) {
                    for (int x = 0; x < ChunkSideLength; x++)
                    {
                        for (int y = 0; y < ChunkSideLength; y++)
                        {
                            // background color
                            dsBackgroundColor.FillRectangle(new Rect(x, y, 1, 1), _debugBackgroundColor);
                            //dsBackgroundColor.DrawRectangle(new Rect(x * Map.TILE_RESOLUTION, y * Map.TILE_RESOLUTION, Map.TILE_RESOLUTION, Map.TILE_RESOLUTION), Colors.Black);

                            // elevation
                            switch (chunk.Tiles[x, y].TileType)
                            {
                            case Tile.TILE_TYPE.GRASS:
                                dsElevation.DrawImage(Statics.BitmapOverworld,
                                                      new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution),
                                                      new Rect(Statics.OverworldTileGrass.X, Statics.OverworldTileGrass.Y, Statics.OverworldTileResolution, Statics.OverworldTileResolution));

                                // dsElevation.FillRectangle(new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution), Colors.Green);
                                break;

                            case Tile.TILE_TYPE.MOUNTAINS:
                                dsElevation.DrawImage(Statics.BitmapOverworld,
                                                      new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution),
                                                      new Rect(Statics.OverworldTileMountain.X, Statics.OverworldTileMountain.Y, Statics.OverworldTileResolution, Statics.OverworldTileResolution));

                                // dsElevation.FillRectangle(new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution), Colors.Brown);
                                break;

                            case Tile.TILE_TYPE.WATER:
                                dsElevation.DrawImage(Statics.BitmapOverworld,
                                                      new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution),
                                                      new Rect(Statics.OverworldTileWater.X, Statics.OverworldTileWater.Y, Statics.OverworldTileResolution, Statics.OverworldTileResolution));

                                // dsElevation.FillRectangle(new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution), Colors.Blue);
                                break;

                            case Tile.TILE_TYPE.FOREST:
                                dsElevation.DrawImage(Statics.BitmapOverworld,
                                                      new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution),
                                                      new Rect(Statics.OverworldTileForest.X, Statics.OverworldTileForest.Y, Statics.OverworldTileResolution, Statics.OverworldTileResolution));

                                // dsElevation.FillRectangle(new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution), Colors.DarkGreen);
                                break;

                            case Tile.TILE_TYPE.DESERT:
                                dsElevation.DrawImage(Statics.BitmapOverworld,
                                                      new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution),
                                                      new Rect(Statics.OverworldTileDesert.X, Statics.OverworldTileDesert.Y, Statics.OverworldTileResolution, Statics.OverworldTileResolution));

                                // dsElevation.FillRectangle(new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution), Colors.DarkGreen);
                                break;

                            case Tile.TILE_TYPE.GRASS_LIGHT:
                                dsElevation.DrawImage(Statics.BitmapOverworld,
                                                      new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution),
                                                      new Rect(Statics.OverworldTileGrassLight.X, Statics.OverworldTileGrassLight.Y, Statics.OverworldTileResolution, Statics.OverworldTileResolution));

                                // dsElevation.FillRectangle(new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution), Colors.DarkGreen);
                                break;
                            }
                            //dsElevation.FillRectangle(new Rect(x * Statics.OverworldTileResolution, y * Statics.OverworldTileResolution, Statics.OverworldTileResolution, Statics.OverworldTileResolution), Chunk.ElevationColor(chunk.Tiles[x, y].Elevation));
                            //dsElevation.DrawRectangle(new Rect(x * Map.TILE_RESOLUTION, y * Map.TILE_RESOLUTION, Map.TILE_RESOLUTION, Map.TILE_RESOLUTION), Colors.Black);
                        }
                    }
                }
            }

            s.Stop();
            lock (Debug.DebugCollectionsLock) { Debug.ChunkLoadTimes.Add(s.ElapsedMilliseconds); }
            return(chunk);
        }