Пример #1
0
        public override void Update(long gameTime, NamelessGame namelessGame)
        {
            IEntity        worldEntity   = namelessGame.TimelineEntity;
            IWorldProvider worldProvider = null;

            if (worldEntity != null)
            {
                worldProvider = worldEntity.GetComponentOfType <TimeLine>().CurrentTimelineLayer.Chunks;
            }


            IEntity playerentity = namelessGame.PlayerEntity;

            if (playerentity != null)
            {
                Chunk    currentChunk    = null;
                Point?   currentChunkKey = null;
                Position playerPosition  = playerentity.GetComponentOfType <Position>();
                //look for current chunk
                foreach (Point key in worldProvider.GetRealityBubbleChunks().Keys)
                {
                    Chunk ch = worldProvider.GetRealityBubbleChunks()[key];
                    if (ch.IsPointInside(playerPosition.Point))
                    {
                        currentChunk    = ch;
                        currentChunkKey = key;
                        break;
                    }
                }

                //if there is none, that means we just loaded the namelessGame, look for current in all chunks
                if (currentChunk == null)
                {
                    foreach (Point key in worldProvider.GetChunks().Keys)
                    {
                        Chunk ch = worldProvider.GetChunks()[key];
                        if (ch.IsPointInside(playerPosition.Point))
                        {
                            currentChunk    = ch;
                            currentChunkKey = key;
                            break;
                        }
                    }
                }

                if (currentChunk != null)
                {
                    for (int x = -Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.X;
                         x <= Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.X;
                         x++)
                    {
                        for (int y = -Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.Y;
                             y <= Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.Y;
                             y++)
                        {
                            Point p = new Point(x, y);
                            if (!worldProvider.GetRealityBubbleChunks().ContainsKey(p))
                            {
                                if (worldProvider.GetChunks().ContainsKey(p))
                                {
                                    Chunk chunk = worldProvider.GetChunks()[p];



                                    worldProvider.GetRealityBubbleChunks().Add(p, chunk);
                                    worldProvider.RealityChunks.Add(chunk);
                                }
                            }
                        }
                    }

                    List <Point> keysToRemove = new List <Point>();
                    foreach (Point key in
                             worldProvider.GetRealityBubbleChunks().Keys)
                    {
                        double distX = Math.Abs(key.X - currentChunkKey.Value.X);
                        double distY = Math.Abs(key.Y - currentChunkKey.Value.Y);
                        if (distX > Constants.RealityBubbleRangeInChunks || distY > Constants.RealityBubbleRangeInChunks)
                        {
                            keysToRemove.Add(key);
                        }
                    }

                    foreach (Point key in
                             keysToRemove)
                    {
                        if (worldProvider.GetRealityBubbleChunks()[key].IsActive)
                        {
                            worldProvider.GetRealityBubbleChunks()[key].Deactivate();
                            worldProvider.RealityChunks.Remove(worldProvider.GetRealityBubbleChunks()[key]);
                            worldProvider.GetRealityBubbleChunks().Remove(key);
                        }
                    }
                }
            }

            var justcreated = worldProvider.GetRealityBubbleChunks().Where(x => x.Value.JustCreated);

            foreach (var realityBubbleChunk in justcreated)
            {
                foreach (var tileArray in realityBubbleChunk.Value.GetChunkTiles())
                {
                    foreach (var tile in tileArray)
                    {
                        var entity = TerrainFurnitureFactory.GetExteriorEntities(namelessGame, tile);
                        if (entity != null)
                        {
                            if (tile.GetEntities().Count == 0)
                            {
                                tile.AddEntity(entity);

                                namelessGame.AddEntity(entity);
                            }
                        }
                    }
                }
                realityBubbleChunk.Value.JustCreated = false;
            }
        }
        public static ConcreteSettlement GenerateSettlement(NamelessGame namelessGame, WorldTile tile, TimelineLayer board,
                                                            IWorldProvider worldProvider)
        {
            var result = new ConcreteSettlement();

            int chunksPerTile = worldProvider.ChunkResolution / namelessGame.WorldSettings.WorldBoardWidth;

            var squareToCheck = 5;

            List <KeyValuePair <Point, Chunk> > allChunksToWorkWith = new List <KeyValuePair <Point, Chunk> >();

            //find chunks to work with
            for (int x = tile.WorldBoardPosiiton.X - squareToCheck; x <= tile.WorldBoardPosiiton.X + squareToCheck; x++)
            {
                for (int y = tile.WorldBoardPosiiton.Y - squareToCheck;
                     y <= tile.WorldBoardPosiiton.Y + squareToCheck;
                     y++)
                {
                    List <KeyValuePair <Point, Chunk> > chunks = new List <KeyValuePair <Point, Chunk> >();

                    int chunkX = x * chunksPerTile;
                    int chunkY = y * chunksPerTile;

                    for (int i = chunkX; i < chunkX + chunksPerTile; i++)
                    {
                        for (int j = chunkY; j < chunkY + chunksPerTile; j++)
                        {
                            var point = new Point(i, j);
                            chunks.Add(new KeyValuePair <Point, Chunk>(point, worldProvider.GetChunks()[point]));
                        }
                    }

                    foreach (var keyValuePair in chunks)
                    {
                        //place them into reality bubble for convenience
                        worldProvider.GetRealityBubbleChunks().Add(keyValuePair.Key, keyValuePair.Value);
                        worldProvider.RealityChunks.Add(keyValuePair.Value);
                        allChunksToWorkWith.Add(keyValuePair);
                    }
                }
            }

            Point minPoint, maxPoint;

            var firstChunk = allChunksToWorkWith.First().Value;

            minPoint = firstChunk.ChunkWorldMapLocationPoint;
            maxPoint = firstChunk.ChunkWorldMapLocationPoint;

            foreach (var keyValuePair in allChunksToWorkWith)
            {
                var currentPoint = keyValuePair.Value.ChunkWorldMapLocationPoint;
                if (currentPoint.X > maxPoint.X || currentPoint.Y > maxPoint.Y)
                {
                    maxPoint = currentPoint;
                }

                if (currentPoint.X < minPoint.X || currentPoint.Y < minPoint.Y)
                {
                    minPoint = currentPoint;
                }
            }

            maxPoint.X += Constants.ChunkSize;
            maxPoint.Y += Constants.ChunkSize;

            var maxVector = maxPoint.ToVector2();
            var minVector = minPoint.ToVector2();

            var center = (minVector + maxVector) / 2;

            var citySize    = 100;
            var streetWidth = 10;

            var slots = new CitySlot[citySize / streetWidth, citySize / streetWidth];



            GenerateCityBuildingForTest(center, namelessGame, worldProvider);



            result.Center = center.ToPoint();

            foreach (var keyValuePair in allChunksToWorkWith)
            {
                worldProvider.GetRealityBubbleChunks().Remove(keyValuePair.Key);
            }



            return(result);
        }