public ChunkData(WorldSettings settings, TimelineLayer worldBoard) { Id = Guid.NewGuid(); chunks = new Dictionary <Point, Chunk>(); realityBubbleChunks = new Dictionary <Point, Chunk>(); worldSettings = settings; this.worldBoard = worldBoard; initWorld(); }
public static void SaveTimelineLayer(String pathToFolder, TimelineLayer layer, String id) { if (!Directory.Exists(pathToFolder)) { Directory.CreateDirectory(pathToFolder); } using (StreamWriter writer = new StreamWriter(pathToFolder + "\\" + id + ".json")) using (JsonTextWriter jsonWriter = new JsonTextWriter(writer)) { JsonSerializer ser = new JsonSerializer(); ser.NullValueHandling = NullValueHandling.Ignore; ser.Serialize(jsonWriter, layer); jsonWriter.Flush(); } }
public async Task AnimateTimelineAsync(double delay) { using var animator = new TimelineAnimator(); animator.Ticks = 500; animator.Viewport = new Size(128, 128); var initial = new Keyframe(0, 1.0f, Vector2.Zero, 0.0f, Vector2.One); var mid = new Keyframe(250, 1.0f, new Vector2(32, 32), 359.9f, new Vector2(1, 1)); var quarter = new Keyframe(375, 1.0f, new Vector2(16, 16), 180.0f, new Vector2(2, 2)); var final = new Keyframe(500, 1.0f, new Vector2(0, 0), 0.0f, new Vector2(1, 1)); var square = new TimelineLayer(ImageHelper.CreateSolid(GammaPalette.GammaGreen[Gamma.Max], 32, 32), 0, 500, mid, quarter, final); var background = new TimelineLayer(ImageHelper.CreateSolid(GammaPalette.GammaGreen[Gamma.Min], 128, 128), 0, 500, initial); animator.AddLayer(background); animator.AddLayer(square); MemoryStream animation = animator.Compile(TimeSpan.FromMilliseconds(delay)); await Context.Channel.SendGifAsync(animation, "../tmp/timeline_anim2.gif"); }
public void FillWithTiles(TerrainGenerator generator, TimelineLayer board) { var surroundingChunksWithRivers = new List <TileForInlandWaterConnectivity>(); for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { var tile = board.InlandWaterConnectivity[this.ChunkWorldMapLocationPoint.X + i][this.ChunkWorldMapLocationPoint.Y + j]; if (tile.WaterBorderLines.Any()) { surroundingChunksWithRivers.Add(tile); } } } for (int x = 0; x < Constants.ChunkSize; x++) { for (int y = 0; y < Constants.ChunkSize; y++) { chunkTiles[x][y] = generator.GetTileWithoutRiverWater(x + worldPositionBottomLeftCorner.X, y + worldPositionBottomLeftCorner.Y, Constants.ChunkSize); //if (x == 0 || y == 0 || x == Constants.ChunkSize - 1 || y == Constants.ChunkSize - 1) //{ // chunkTiles[x][y].Terrain = TerrainLibrary.Terrains[TerrainTypes.Nothingness]; //} } } if (surroundingChunksWithRivers.Any()) { //TODO this part is really useful for plotting rivers/roads on map, should be moved to its own class // the point is: i must generate a chunk sized bitmap to detect which tiles are river tiles and which are not // in this bitmap, black pixels are ignored, white ones are river tiles Pen whitePen = new Pen(System.Drawing.Color.White, Constants.ChunkSize); var waterBitmap = new Bitmap(Constants.ChunkSize, Constants.ChunkSize); var graphics = Graphics.FromImage(waterBitmap); foreach (var chunkWithRivers in surroundingChunksWithRivers) { var riverLines = chunkWithRivers.WaterBorderLines; foreach (var waterBorderLine in riverLines) { var chunkRiverPoints = waterBorderLine.Points; var chunkPointIndex = chunkRiverPoints.FindIndex(p => p.X == chunkWithRivers.x && p.Y == chunkWithRivers.y); var chHalf = Constants.ChunkSize / 2; var halfV = new Microsoft.Xna.Framework.Vector2(chHalf); Point ScalePoint(Point p) { return(((p.ToVector2() * Constants.ChunkSize) + halfV).ToPoint()); } int curveLenght = 5; int curveHalfLenght = curveLenght / 2; System.Drawing.Point[] curvePoints = null; List <System.Drawing.Point> endPoints = null; if (chunkRiverPoints.Count >= curveLenght) { curvePoints = new System.Drawing.Point[curveLenght]; endPoints = new List <System.Drawing.Point>(); int rangeStart = 0, rangeEnd = 0; int rangeIndex = chunkPointIndex; if (chunkPointIndex >= curveHalfLenght && chunkPointIndex < chunkRiverPoints.Count - curveHalfLenght) { rangeStart = -curveHalfLenght; rangeEnd = curveHalfLenght; } //this chunk is a first point in this water line else if (chunkPointIndex <= curveHalfLenght) { rangeStart = 0; rangeEnd = curveLenght - 1; rangeIndex = 0; endPoints.Add(ScalePoint(chunkRiverPoints[rangeStart] - ChunkWorldMapLocationPoint).ToPoint()); } //this chunk is last else if (chunkPointIndex > chunkRiverPoints.Count - curveHalfLenght - 1) { rangeStart = -curveLenght + 1; rangeEnd = 0; rangeIndex = chunkRiverPoints.Count - 1; endPoints.Add(ScalePoint(chunkRiverPoints[rangeIndex] - ChunkWorldMapLocationPoint).ToPoint()); } for (int i = rangeStart, pointsIndex = 0; i <= rangeEnd; i++, pointsIndex++) { curvePoints[pointsIndex] = ScalePoint(chunkRiverPoints[rangeIndex + i] - ChunkWorldMapLocationPoint).ToPoint(); } } else { var allpointsCount = chunkRiverPoints.Count; curvePoints = new System.Drawing.Point[allpointsCount]; for (int i = 0; i < chunkRiverPoints.Count; i++) { curvePoints[i] = ScalePoint(chunkRiverPoints[i] - ChunkWorldMapLocationPoint).ToPoint(); } } graphics.DrawCurve(whitePen, curvePoints); if (endPoints != null) { foreach (var point in endPoints) { graphics.FillEllipse(Brushes.White, point.X - chHalf, point.Y - chHalf, Constants.ChunkSize, Constants.ChunkSize); } } } //then we use the bitmap and fill the chunk for (int x = 0; x < Constants.ChunkSize; x++) { for (int y = 0; y < Constants.ChunkSize; y++) { if (waterBitmap.GetPixel(x, y).R > 0) { chunkTiles[x][y].Biome = Biomes.River; chunkTiles[x][y].Terrain = TerrainTypes.Water; } } } } } }
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); }