Ejemplo n.º 1
0
        private void GenerateMaze(Player player)
        {
            var message         = GetComponents <DebugMessageBuilder>().First();
            var entireMaze      = BoundingBox.CreateMerged(new Cell(0, 0).GetBoundingBox(), new Cell(_width - 1, _height - 1).GetBoundingBox());
            var collisionEngine = new CollisionEngine(entireMaze);

            Add(collisionEngine);

            var chunkManager = new ChunkManager(RenderContext, message, collisionEngine, _width, _height);

            collisionEngine.Add(player);

            Add(chunkManager);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method will generate the mesh structure inside the provided boundaries. It will already reduce the vertex count by merging walls within a corridor.
        /// Note that this is not the most efficient algorithm (in regards to optimization). It is however very fast.
        /// It runs two steps: first only merging in x direction then in y direction, this will result in a mesh like this.
        /// <example>
        /// XXX
        /// X X
        /// XXX
        /// </example>
        /// into
        /// <example>
        /// 111
        /// 2 3
        /// 444
        /// </example>
        /// Note that the left side (124) could technically also be merged if the algorithm would consider faces, however since it is cell-based it will only merge each cell once.
        /// </summary>
        /// <param name="cells"></param>
        /// <param name="startXIndex"></param>
        /// <param name="startYIndex"></param>
        /// <param name="endX"></param>
        /// <param name="endY"></param>
        /// <param name="collisionEngine"></param>
        /// <returns></returns>
        public static IMeshDescription GenerateWallMesh(Cell[,] cells, int startXIndex, int startYIndex, int endX, int endY, CollisionEngine collisionEngine)
        {
            var skip            = MergeCells(cells, startXIndex, startYIndex, endX, endY);
            var wallMeshBuilder = new TexturedMeshDescriptionBuilder();

            for (int y = startYIndex; y < endY; y++)
            {
                for (int x = startXIndex; x < endX; x++)
                {
                    var c = cells[x, y];

                    var cellBox = c.GetBoundingBox();
                    if (c.Mode == CellMode.Wall)
                    {
                        if (skip[x - startXIndex, y - startYIndex] == 3)
                        {
                            // has already been merged with previous cells, do not add again
                            continue;
                        }
                        // look at the cells to the right if they can be merged
                        if (x + 1 < endX)
                        {
                            // walk to the right as long as possible
                            int count = 0;
                            int i     = 1;
                            while (x + i < endX && skip[x - startXIndex + i, y - startYIndex] == 1)
                            {
                                count = i;
                                i++;
                            }
                            // yes, merge
                            var lastCell = cells[x + count, y].GetBoundingBox();
                            cellBox = BoundingBox.CreateMerged(cellBox, lastCell);
                            // flag all as already merged
                            if (count > 0)
                            {
                                do
                                {
                                    skip[x - startXIndex + count, y - startYIndex] = 3;
                                    count--;
                                } while (count > 0);
                                wallMeshBuilder.AddBox(cellBox, TileSize);
                                collisionEngine.Add(new Wall(cellBox));
                                continue;
                            }
                        }
                        // look at the cells bellow if they can be merged
                        if (y + 1 < endY)
                        {
                            int count = 0;
                            int i     = 1;
                            while (y + i < endY && skip[x - startXIndex, y - startYIndex + i] == 2)
                            {
                                count = i;
                                i++;
                            }
                            // yes, merge
                            var lastCell = cells[x, y + count].GetBoundingBox();
                            cellBox = BoundingBox.CreateMerged(cellBox, lastCell);
                            // flag all as already merged
                            do
                            {
                                skip[x - startXIndex, y - startYIndex + count] = 3;
                                count--;
                            } while (count > 0);
                            wallMeshBuilder.AddBox(cellBox, TileSize);
                            collisionEngine.Add(new Wall(cellBox));
                            continue;
                        }

                        // no merging possible, just place default cell sized box
                        wallMeshBuilder.AddBox(cellBox, TileSize);
                        collisionEngine.Add(new Wall(cellBox));
                    }
                }
            }

            return(wallMeshBuilder);
        }