예제 #1
0
        public void Draw(SpriteBatch spriteBatch)
        {
            if (GameMain.DebugDraw)
            {
                var cells = level.GetCells(GameMain.GameScreen.Cam.WorldViewCenter, 2);
                foreach (VoronoiCell cell in cells)
                {
                    GUI.DrawRectangle(spriteBatch, new Vector2(cell.Center.X - 10.0f, -cell.Center.Y - 10.0f), new Vector2(20.0f, 20.0f), Color.Cyan, true);

                    GUI.DrawLine(spriteBatch,
                                 new Vector2(cell.edges[0].point1.X, -cell.edges[0].point1.Y),
                                 new Vector2(cell.Center.X, -cell.Center.Y),
                                 Color.Blue * 0.5f);

                    foreach (GraphEdge edge in cell.edges)
                    {
                        GUI.DrawLine(spriteBatch, new Vector2(edge.point1.X, -edge.point1.Y),
                                     new Vector2(edge.point2.X, -edge.point2.Y), cell.body == null ? Color.Cyan * 0.5f : Color.White);
                    }

                    foreach (Vector2 point in cell.bodyVertices)
                    {
                        GUI.DrawRectangle(spriteBatch, new Vector2(point.X, -point.Y), new Vector2(10.0f, 10.0f), Color.White, true);
                    }
                }
            }


            Vector2 pos = new Vector2(0.0f, -level.Size.Y);

            if (GameMain.GameScreen.Cam.WorldView.Y < -pos.Y - 1024)
            {
                return;
            }

            pos.X = GameMain.GameScreen.Cam.WorldView.X - 1024;

            int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width / 1024 + 4.0f) * 1024);

            GUI.DrawRectangle(spriteBatch, new Rectangle((int)(MathUtils.Round(pos.X, 1024)), (int)-GameMain.GameScreen.Cam.WorldView.Y, width, (int)(GameMain.GameScreen.Cam.WorldView.Y + pos.Y) - 30), Color.Black, true);
            spriteBatch.Draw(shaftTexture,
                             new Rectangle((int)(MathUtils.Round(pos.X, 1024)), (int)pos.Y - 1000, width, 1024),
                             new Rectangle(0, 0, width, -1024),
                             level.BackgroundColor, 0.0f,
                             Vector2.Zero,
                             SpriteEffects.None, 0.0f);
        }
예제 #2
0
        public void Draw(SpriteBatch spriteBatch)
        {
            if (GameMain.DebugDraw)
            {
                var cells = level.GetCells(GameMain.GameScreen.Cam.WorldViewCenter, 2);
                foreach (VoronoiCell cell in cells)
                {
                    GUI.DrawRectangle(spriteBatch, new Vector2(cell.Center.X - 10.0f, -cell.Center.Y - 10.0f), new Vector2(20.0f, 20.0f), Color.Cyan, true);

                    GUI.DrawLine(spriteBatch,
                                 new Vector2(cell.edges[0].point1.X, -cell.edges[0].point1.Y),
                                 new Vector2(cell.Center.X, -cell.Center.Y),
                                 Color.Blue * 0.5f);

                    foreach (GraphEdge edge in cell.edges)
                    {
                        GUI.DrawLine(spriteBatch, new Vector2(edge.point1.X, -edge.point1.Y),
                                     new Vector2(edge.point2.X, -edge.point2.Y), cell.body == null ? Color.Cyan * 0.5f : Color.White);
                    }

                    foreach (Vector2 point in cell.bodyVertices)
                    {
                        GUI.DrawRectangle(spriteBatch, new Vector2(point.X, -point.Y), new Vector2(10.0f, 10.0f), Color.White, true);
                    }
                }

                foreach (List <Vector2> nodeList in level.SmallTunnels)
                {
                    for (int i = 1; i < nodeList.Count; i++)
                    {
                        GUI.DrawLine(spriteBatch,
                                     new Vector2(nodeList[i - 1].X, -nodeList[i - 1].Y),
                                     new Vector2(nodeList[i].X, -nodeList[i].Y),
                                     Color.Lerp(Color.Yellow, Color.Red, i / (float)nodeList.Count), 0, 10);
                    }
                }
            }

            Vector2 pos = new Vector2(0.0f, -level.Size.Y);

            if (GameMain.GameScreen.Cam.WorldView.Y >= -pos.Y - 1024)
            {
                pos.X = GameMain.GameScreen.Cam.WorldView.X - 1024;
                int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width / 1024 + 4.0f) * 1024);

                GUI.DrawRectangle(spriteBatch, new Rectangle(
                                      (int)(MathUtils.Round(pos.X, 1024)),
                                      -GameMain.GameScreen.Cam.WorldView.Y,
                                      width,
                                      (int)(GameMain.GameScreen.Cam.WorldView.Y + pos.Y) - 30),
                                  Color.Black, true);

                spriteBatch.Draw(shaftTexture,
                                 new Rectangle((int)(MathUtils.Round(pos.X, 1024)), (int)pos.Y - 1000, width, 1024),
                                 new Rectangle(0, 0, width, -1024),
                                 level.BackgroundColor, 0.0f,
                                 Vector2.Zero,
                                 SpriteEffects.None, 0.0f);
            }

            if (GameMain.GameScreen.Cam.WorldView.Y - GameMain.GameScreen.Cam.WorldView.Height < level.SeaFloorTopPos + 1024)
            {
                pos = new Vector2(GameMain.GameScreen.Cam.WorldView.X - 1024, -level.BottomPos);

                int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width / 1024 + 4.0f) * 1024);

                GUI.DrawRectangle(spriteBatch, new Rectangle(
                                      (int)(MathUtils.Round(pos.X, 1024)),
                                      (int)-(level.BottomPos - 30),
                                      width,
                                      (int)(level.BottomPos - (GameMain.GameScreen.Cam.WorldView.Y - GameMain.GameScreen.Cam.WorldView.Height))),
                                  Color.Black, true);

                spriteBatch.Draw(shaftTexture,
                                 new Rectangle((int)(MathUtils.Round(pos.X, 1024)), (int)-level.BottomPos, width, 1024),
                                 new Rectangle(0, 0, width, -1024),
                                 level.BackgroundColor, 0.0f,
                                 Vector2.Zero,
                                 SpriteEffects.FlipVertically, 0.0f);
            }
        }
        private Vector2?FindSpritePosition(Level level, BackgroundSpritePrefab prefab, out GraphEdge closestEdge, out Vector2 edgeNormal)
        {
            closestEdge = null;
            edgeNormal  = Vector2.One;

            Vector2 randomPos = new Vector2(
                Rand.Range(0.0f, level.Size.X, Rand.RandSync.Server),
                Rand.Range(0.0f, level.Size.Y, Rand.RandSync.Server));

            if (prefab.SpawnPos == BackgroundSpritePrefab.SpawnPosType.None)
            {
                return(randomPos);
            }

            List <GraphEdge> edges   = new List <GraphEdge>();
            List <Vector2>   normals = new List <Vector2>();

            System.Diagnostics.Debug.Assert(level.ExtraWalls.Length == 1);
            List <VoronoiCell> cells = new List <VoronoiCell>();

            if (prefab.SpawnPos.HasFlag(BackgroundSpritePrefab.SpawnPosType.Wall))
            {
                cells.AddRange(level.GetCells(randomPos));
            }
            if (prefab.SpawnPos.HasFlag(BackgroundSpritePrefab.SpawnPosType.SeaFloor))
            {
                cells.AddRange(level.ExtraWalls[0].Cells);
            }

            if (cells.Any())
            {
                VoronoiCell cell = cells[Rand.Int(cells.Count, Rand.RandSync.Server)];

                foreach (GraphEdge edge in cell.edges)
                {
                    if (!edge.isSolid || edge.OutsideLevel)
                    {
                        continue;
                    }

                    Vector2 normal = edge.GetNormal(cell);

                    if (prefab.Alignment.HasFlag(Alignment.Bottom) && normal.Y < -0.5f)
                    {
                        edges.Add(edge);
                    }
                    else if (prefab.Alignment.HasFlag(Alignment.Top) && normal.Y > 0.5f)
                    {
                        edges.Add(edge);
                    }
                    else if (prefab.Alignment.HasFlag(Alignment.Left) && normal.X < -0.5f)
                    {
                        edges.Add(edge);
                    }
                    else if (prefab.Alignment.HasFlag(Alignment.Right) && normal.X > 0.5f)
                    {
                        edges.Add(edge);
                    }
                    else
                    {
                        continue;
                    }

                    normals.Add(normal);
                }
            }

            if (prefab.SpawnPos.HasFlag(BackgroundSpritePrefab.SpawnPosType.RuinWall))
            {
                foreach (RuinGeneration.Ruin ruin in Level.Loaded.Ruins)
                {
                    Rectangle expandedArea = ruin.Area;
                    expandedArea.Inflate(ruin.Area.Width, ruin.Area.Height);
                    if (!expandedArea.Contains(randomPos))
                    {
                        continue;
                    }

                    foreach (var ruinShape in ruin.RuinShapes)
                    {
                        foreach (var wall in ruinShape.Walls)
                        {
                            if (!prefab.Alignment.HasFlag(ruinShape.GetLineAlignment(wall)))
                            {
                                continue;
                            }

                            edges.Add(new GraphEdge(wall.A, wall.B));
                            normals.Add((wall.A + wall.B) / 2.0f - ruinShape.Center);
                        }
                    }
                }
            }

            if (!edges.Any())
            {
                return(null);
            }

            int index = Rand.Int(edges.Count, Rand.RandSync.Server);

            closestEdge = edges[index];
            edgeNormal  = normals[index];

            float   length = Vector2.Distance(closestEdge.point1, closestEdge.point2);
            Vector2 dir    = (closestEdge.point1 - closestEdge.point2) / length;
            Vector2 pos    = closestEdge.point2 + dir * Rand.Range(prefab.Sprite.size.X / 2.0f, length - prefab.Sprite.size.X / 2.0f, Rand.RandSync.Server);

            return(pos);
        }
예제 #4
0
        public void DrawDebugOverlay(SpriteBatch spriteBatch, Camera cam)
        {
            if (GameMain.DebugDraw && cam.Zoom > 0.1f)
            {
                var cells = level.GetCells(cam.WorldViewCenter, 2);
                foreach (VoronoiCell cell in cells)
                {
                    GUI.DrawRectangle(spriteBatch, new Vector2(cell.Center.X - 10.0f, -cell.Center.Y - 10.0f), new Vector2(20.0f, 20.0f), Color.Cyan, true);

                    GUI.DrawLine(spriteBatch,
                                 new Vector2(cell.Edges[0].Point1.X + cell.Translation.X, -(cell.Edges[0].Point1.Y + cell.Translation.Y)),
                                 new Vector2(cell.Center.X, -(cell.Center.Y)),
                                 Color.Blue * 0.5f);

                    foreach (GraphEdge edge in cell.Edges)
                    {
                        GUI.DrawLine(spriteBatch, new Vector2(edge.Point1.X + cell.Translation.X, -(edge.Point1.Y + cell.Translation.Y)),
                                     new Vector2(edge.Point2.X + cell.Translation.X, -(edge.Point2.Y + cell.Translation.Y)), edge.NextToCave ? Color.Red : (cell.Body == null ? Color.Cyan * 0.5f : (edge.IsSolid ? Color.White : Color.Gray)),
                                     width: edge.NextToCave ? 8 : 1);
                    }

                    foreach (Vector2 point in cell.BodyVertices)
                    {
                        GUI.DrawRectangle(spriteBatch, new Vector2(point.X + cell.Translation.X, -(point.Y + cell.Translation.Y)), new Vector2(10.0f, 10.0f), Color.White, true);
                    }
                }

                /*foreach (List<Point> nodeList in level.SmallTunnels)
                 * {
                 *  for (int i = 1; i < nodeList.Count; i++)
                 *  {
                 *      GUI.DrawLine(spriteBatch,
                 *          new Vector2(nodeList[i - 1].X, -nodeList[i - 1].Y),
                 *          new Vector2(nodeList[i].X, -nodeList[i].Y),
                 *          Color.Lerp(Color.Yellow, GUI.Style.Red, i / (float)nodeList.Count), 0, 10);
                 *  }
                 * }*/

                foreach (var abyssIsland in level.AbyssIslands)
                {
                    GUI.DrawRectangle(spriteBatch, new Vector2(abyssIsland.Area.X, -abyssIsland.Area.Y - abyssIsland.Area.Height), abyssIsland.Area.Size.ToVector2(), Color.Cyan, thickness: 5);
                }

                foreach (var ruin in level.Ruins)
                {
                    ruin.DebugDraw(spriteBatch);
                }
            }

            Vector2 pos = new Vector2(0.0f, -level.Size.Y);

            if (cam.WorldView.Y >= -pos.Y - 1024)
            {
                int topBarrierWidth  = level.GenerationParams.WallEdgeSprite.Texture.Width;
                int topBarrierHeight = level.GenerationParams.WallEdgeSprite.Texture.Height;

                pos.X = cam.WorldView.X - topBarrierWidth;
                int width = (int)(Math.Ceiling(cam.WorldView.Width / 1024 + 4.0f) * topBarrierWidth);

                GUI.DrawRectangle(spriteBatch, new Rectangle(
                                      (int)MathUtils.Round(pos.X, topBarrierWidth),
                                      -cam.WorldView.Y,
                                      width,
                                      (int)(cam.WorldView.Y + pos.Y) - 60),
                                  Color.Black, true);

                spriteBatch.Draw(level.GenerationParams.WallEdgeSprite.Texture,
                                 new Rectangle((int)MathUtils.Round(pos.X, topBarrierWidth), (int)(pos.Y - topBarrierHeight + level.GenerationParams.WallEdgeExpandOutwardsAmount), width, topBarrierHeight),
                                 new Rectangle(0, 0, width, -topBarrierHeight),
                                 GameMain.LightManager?.LightingEnabled ?? false ? GameMain.LightManager.AmbientLight : level.WallColor, 0.0f,
                                 Vector2.Zero,
                                 SpriteEffects.None, 0.0f);
            }

            if (cam.WorldView.Y - cam.WorldView.Height < level.SeaFloorTopPos + 1024)
            {
                int bottomBarrierWidth  = level.GenerationParams.WallEdgeSprite.Texture.Width;
                int bottomBarrierHeight = level.GenerationParams.WallEdgeSprite.Texture.Height;
                pos = new Vector2(cam.WorldView.X - bottomBarrierWidth, -level.BottomPos);
                int width = (int)(Math.Ceiling(cam.WorldView.Width / bottomBarrierWidth + 4.0f) * bottomBarrierWidth);

                GUI.DrawRectangle(spriteBatch, new Rectangle(
                                      (int)(MathUtils.Round(pos.X, bottomBarrierWidth)),
                                      -(level.BottomPos - 60),
                                      width,
                                      level.BottomPos - (cam.WorldView.Y - cam.WorldView.Height)),
                                  Color.Black, true);

                spriteBatch.Draw(level.GenerationParams.WallEdgeSprite.Texture,
                                 new Rectangle((int)MathUtils.Round(pos.X, bottomBarrierWidth), -level.BottomPos - (int)level.GenerationParams.WallEdgeExpandOutwardsAmount, width, bottomBarrierHeight),
                                 new Rectangle(0, 0, width, -bottomBarrierHeight),
                                 GameMain.LightManager?.LightingEnabled ?? false ? GameMain.LightManager.AmbientLight : level.WallColor, 0.0f,
                                 Vector2.Zero,
                                 SpriteEffects.FlipVertically, 0.0f);
            }
        }
예제 #5
0
        public void Draw(SpriteBatch spriteBatch, Camera cam)
        {
            if (GameMain.DebugDraw && cam.Zoom > 0.1f)
            {
                var cells = level.GetCells(cam.WorldViewCenter, 2);
                foreach (VoronoiCell cell in cells)
                {
                    GUI.DrawRectangle(spriteBatch, new Vector2(cell.Center.X - 10.0f, -cell.Center.Y - 10.0f), new Vector2(20.0f, 20.0f), Color.Cyan, true);

                    GUI.DrawLine(spriteBatch,
                                 new Vector2(cell.Edges[0].Point1.X + cell.Translation.X, -(cell.Edges[0].Point1.Y + cell.Translation.Y)),
                                 new Vector2(cell.Center.X, -(cell.Center.Y)),
                                 Color.Blue * 0.5f);

                    foreach (GraphEdge edge in cell.Edges)
                    {
                        GUI.DrawLine(spriteBatch, new Vector2(edge.Point1.X + cell.Translation.X, -(edge.Point1.Y + cell.Translation.Y)),
                                     new Vector2(edge.Point2.X + cell.Translation.X, -(edge.Point2.Y + cell.Translation.Y)), cell.Body == null ? Color.Cyan * 0.5f : Color.White);
                    }

                    foreach (Vector2 point in cell.BodyVertices)
                    {
                        GUI.DrawRectangle(spriteBatch, new Vector2(point.X + cell.Translation.X, -(point.Y + cell.Translation.Y)), new Vector2(10.0f, 10.0f), Color.White, true);
                    }
                }

                foreach (List <Point> nodeList in level.SmallTunnels)
                {
                    for (int i = 1; i < nodeList.Count; i++)
                    {
                        GUI.DrawLine(spriteBatch,
                                     new Vector2(nodeList[i - 1].X, -nodeList[i - 1].Y),
                                     new Vector2(nodeList[i].X, -nodeList[i].Y),
                                     Color.Lerp(Color.Yellow, GUI.Style.Red, i / (float)nodeList.Count), 0, 10);
                    }
                }

                foreach (var ruin in level.Ruins)
                {
                    ruin.DebugDraw(spriteBatch);
                }
            }

            Vector2 pos = new Vector2(0.0f, -level.Size.Y);

            if (cam.WorldView.Y >= -pos.Y - 1024)
            {
                pos.X = cam.WorldView.X - 1024;
                int width = (int)(Math.Ceiling(cam.WorldView.Width / 1024 + 4.0f) * 1024);

                GUI.DrawRectangle(spriteBatch, new Rectangle(
                                      (int)(MathUtils.Round(pos.X, 1024)),
                                      -cam.WorldView.Y,
                                      width,
                                      (int)(cam.WorldView.Y + pos.Y) - 30),
                                  Color.Black, true);

                spriteBatch.Draw(level.GenerationParams.WallEdgeSprite.Texture,
                                 new Rectangle((int)(MathUtils.Round(pos.X, 1024)), (int)pos.Y - 1000, width, 1024),
                                 new Rectangle(0, 0, width, -1024),
                                 level.BackgroundTextureColor, 0.0f,
                                 Vector2.Zero,
                                 SpriteEffects.None, 0.0f);
            }

            if (cam.WorldView.Y - cam.WorldView.Height < level.SeaFloorTopPos + 1024)
            {
                pos = new Vector2(cam.WorldView.X - 1024, -level.BottomPos);

                int width = (int)(Math.Ceiling(cam.WorldView.Width / 1024 + 4.0f) * 1024);

                GUI.DrawRectangle(spriteBatch, new Rectangle(
                                      (int)(MathUtils.Round(pos.X, 1024)),
                                      (int)-(level.BottomPos - 30),
                                      width,
                                      (int)(level.BottomPos - (cam.WorldView.Y - cam.WorldView.Height))),
                                  Color.Black, true);

                spriteBatch.Draw(level.GenerationParams.WallEdgeSprite.Texture,
                                 new Rectangle((int)(MathUtils.Round(pos.X, 1024)), (int)-level.BottomPos, width, 1024),
                                 new Rectangle(0, 0, width, -1024),
                                 level.BackgroundTextureColor, 0.0f,
                                 Vector2.Zero,
                                 SpriteEffects.FlipVertically, 0.0f);
            }
        }