예제 #1
0
 public void AddExit(int x, int y, string direction, bool canBeClosed)
 {
     if (x < this.Width && x >= 0 && y < this.Height && y >= 0 &&
         (direction == "N" || direction == "S" || direction == "W" || direction == "E"))
     {
         var exit = new BlockExit {
             X = x, Y = y, Direction = direction, CanBeClosed = canBeClosed
         };
         this.exits.Add(exit);
     }
 }
예제 #2
0
 public void PlaceBlock(ConstructionBlock block, int x, int y)
 {
     for (int j = 0; j < block.Height; j++)
     {
         for (int i = 0; i < block.Width; i++)
         {
             //if (i + x >= 0 && i + x < maxX && j + y >= 0 && j + y < maxY && this.IsUsed [i + x, j + y] == false) {
             if (this.IsValid(i + x, j + y) && this.IsUsed [i + x, j + y] == false)
             {
                 this.Data [i + x, j + y]   = block.GetTile(i, j);
                 this.IsUsed [i + x, j + y] = true;
                 this.UsedArea++;
             }
         }
     }
     foreach (var exit in block.Exits)
     {
         var i = exit.X + x;
         var j = exit.Y + y;
         if (this.IsOpenExit(i, j, exit.Direction))
         {
             var nexit = new BlockExit {
                 X = i, Y = j, Direction = exit.Direction, CanBeClosed = exit.CanBeClosed
             };
             this.exits.Add(nexit);
         }
     }
     foreach (var obj in block.Objects)
     {
         var nobj = new BlockObject();
         nobj.ID          = obj.ID;
         nobj.Probability = obj.Probability;
         nobj.X           = obj.X + x;
         nobj.Y           = obj.Y + y;
         this.Objects.Add(nobj);
     }
     if (block.StartPoint != null)
     {
         this.StartCell.X = block.StartPoint.X + x;
         this.StartCell.Y = block.StartPoint.Y + y;
     }
     if (block.EndPoint != null)
     {
         this.EndCell.X = block.EndPoint.X + x;
         this.EndCell.Y = block.EndPoint.Y + y;
     }
     this.blockCount++;
     minX = (x < minX) ? (x > 0) ? x : 0 : minX;
     minY = (y < minY) ? (y > 0) ? y : 0 : minY;
     maxX = (x + block.Width > maxX) ? x + block.Width : maxX;
     maxY = (y + block.Height > maxY) ? y + block.Height : maxY;
 }
예제 #3
0
        /// <summary>
        /// Places a random block inside the level, using the still open exits. If <c>false</c> is returned
        /// it means that no blocks can be added animaore (within a tolerance level).
        /// </summary>
        /// <returns><c>true</c>, if random block was added, <c>false</c> otherwise.</returns>
        /// <param name="blocks">Blocks.</param>
        public bool AddRandomBlock(ProbabilityVector <ConstructionBlock> blocks)
        {
            var exit = exits.Random();
            var iter = 0;
            var x    = 0;
            var y    = 0;

            if (exit != null)
            {
                // search for a valid exit
                while (this.IsOpenExit(exit.X, exit.Y, exit.Direction) == false && iter++ < MAX_ITERATIONS)
                {
                    if (exit.CanBeClosed)
                    {
                        // the exit was closed, we can just close it and remove it
                        //this.Data [exit.X, exit.Y] = this.CloseTile;
                        //this.exits.Remove (exit);
                    }
                    exit = exits.Random();
                }
                // got one, now search for a block with a valid connection
                ConstructionBlock next   = null;
                BlockExit         access = null;
                do
                {
                    next   = blocks.Random();
                    access = next.GetAccess(exit.Direction);
                    if (access != null)
                    {
                        x  = exit.X - access.X;
                        y  = exit.Y - access.Y;
                        x += (access.Direction == "W") ? 1 : (access.Direction == "E") ? -1 : 0;
                        y += (access.Direction == "N") ? 1 : (access.Direction == "S") ? -1 : 0;
                        if (this.CanPlaceBlock(next, x, y) == false)
                        {
                            access = null;
                        }
                    }
                } while (access == null && iter++ < MAX_ITERATIONS);
                // found a good block? place it
                if (iter < MAX_ITERATIONS)
                {
                    // BEFORE placing the block remove the used exit
                    this.exits.Remove(exit);
                    this.PlaceBlock(next, x, y);
                    return(true);
                }
            }
            return(false);
        }