public RoomOpeningLever(Game game, Point Tile, MapRoom Target, bool flipped = false) : base(game)
        {
            Ani            = new Animation(AnimationLoader._this.GetAnimation("images/misc/lever"));
            Ani.ImageSpeed = 0;
            Block          = Tile;
            var T = game.TM.GetTile(Tile.X, Tile.Y);

            TEST = this;
            if (T != null && T.solid)
            {
                if (Target != null)
                {
                    this.Room = Target;
                }
                T.Breakable = false;
                T.CanSlope  = false;
                var R = T.GetHitbox();
                if (flipped)
                {
                    Ani.Position.X = R.left - 16;
                    Ani.Flipped    = true;
                }
                else
                {
                    Ani.Position.X = R.right;
                }
                Ani.Position.Y = R.top;
            }
            else
            {
                Alive = false;
            }
        }
        public void finishGeneration()
        {
            if (!generated)
            {
                Helper.Log("finishgeneration() was called before basic generation was completed, aborting...");
                return;
            }
            var player = game.player;//player character

            var RR = game.stageBounds - game.TM.position;

            RR.width  -= game.TM.tilesize;
            RR.height -= game.TM.tilesize;
            game.TM.DrawRect(RR);
            game.TM.ApplyBreakable();
            var secrets = Math.Random() < 0.3 ? 1 : 0;

            if (secrets > 0 && Math.Random() < 0.3)
            {
                secrets++;
            }
            while (secrets > 0)
            {
                var L = MapRoom.FindValidUnplacedRooms().Pick();
                if (L.PlaceAndExpand())
                {
                    L.MakeSecret();
                    var lever = AttemptCreateLever(game, L);
                    game.AddEntity(lever);
                    Helper.Log("Placed secret room at:" + L.SX + "," + L.SY);
                }
                secrets--;
            }
            var V = FindEmptySpace(game);

            keyroom = null;
            if (V != null)
            {
                game.Door.Position.CopyFrom(V);
                game.Door.DropToGround();
                doorroom = MapRoom.FindRoom(game.Door.Position);
                if (doorroom == null)
                {
                    Helper.Log("Door room could not be determined...");
                }

                var PC = (PlayerCharacter)player;
                //PC.MoveToNewSpawn(V);
                PC.MoveToNewSpawn(game.Door.Position);
                Helper.Log("spawning at:" + (int)V.X + "," + (int)V.Y);
            }
            else
            {
                Helper.Log("cannot locate a spawn point...");
            }
            finished = true;
        }
        /*public static void BoxyGenerate(Game game)
         * {
         *  Helper.Log("boxy generate");
         *  var player = game.player;//player character
         *  var map = game.TM;//the tilemap to generate
         *  var bounds = game.stageBounds;//the bounds to stay within
         *  MapRoom.PlacedRooms = new List<MapRoom>();
         *  MapRoom.OpenRooms = new List<MapRoom>();
         *
         *  var SX = map.columns / 2;
         *  var SY = map.rows / 3;
         *  var root = new MapRoom();
         *  root.SX = SX;
         *  root.SY = SY;
         *  root.EX = SX + 6 + (int)(Math.Random() * 10);
         *  root.EY = SY + 6 + (int)(Math.Random() * 10);
         *
         *  root.game = game;
         *  rootroom = root;
         *
         *  //var roomtotal = 12+(int)(Math.Random() * 10);
         *  //var roomtotal = 16 + (int)(Math.Random() * 16);
         *
         *  //var roomMinimum = 16;
         *  var roomMinimum = 10+Math.Min(game.level,4);
         *  var roomtotal = roomMinimum + (int)(Math.Random() * (roomMinimum));
         *  //var rooms = 0;
         *
         *  var attempts = 400;
         *  var R = root;
         *  if (!root.PlaceAndExpand())
         *  {
         *      Helper.Log("Couldn't generate root room.");
         *      return;
         *  }
         *  while (MapRoom.OpenRooms.Count < roomtotal && attempts > 0)
         *  {
         *      var L = MapRoom.FindValidUnplacedRooms().Pick();
         *      if (L.PlaceAndExpand())
         *      {
         *          //rooms++;
         *      }
         *      attempts--;
         *  }
         *  var RR = game.stageBounds - game.TM.position;
         *  RR.width -= game.TM.tilesize;
         *  RR.height -= game.TM.tilesize;
         *  game.TM.DrawRect(RR);
         *  game.TM.ApplyBreakable();
         *  var secrets = Math.Random()<0.3 ? 1 : 0;
         *  if (secrets > 0 && Math.Random() < 0.3)
         *  {
         *      secrets++;
         *  }
         *  while (secrets > 0)
         *  {
         *      var L = MapRoom.FindValidUnplacedRooms().Pick();
         *      if (L.PlaceAndExpand())
         *      {
         *          L.MakeSecret();
         *          var lever = AttemptCreateLever(game, L);
         *          game.AddEntity(lever);
         *          Helper.Log("Placed secret room at:" + L.SX + "," + L.SY);
         *      }
         *      secrets--;
         *  }
         *  var V = FindEmptySpace(game);
         *  keyroom = null;
         *  if (V != null)
         *  {
         *      game.Door.Position.CopyFrom(V);
         *      game.Door.DropToGround();
         *      doorroom = MapRoom.FindRoom(game.Door.Position);
         *      if (doorroom == null)
         *      {
         *          Helper.Log("Door room could not be determined...");
         *      }
         *
         *      var PC = (PlayerCharacter)player;
         *      //PC.MoveToNewSpawn(V);
         *      PC.MoveToNewSpawn(game.Door.Position);
         *      Helper.Log("spawning at:" + (int)V.X + "," + (int)V.Y);
         *  }
         *  else
         *  {
         *      Helper.Log("cannot locate a spawn point...");
         *  }
         *
         * }*/
        public static RoomOpeningLever AttemptCreateLever(Game game, MapRoom Target)
        {
            var i = 0;

            while (i < 20)
            {
                var lever = RoomOpeningLever.FindAndPlaceOnWall(game, MapRoom.FindAnyEmptySpot(), Target);
                if (lever != null)
                {
                    return(lever);
                }
            }
            return(null);
        }
        public static RoomOpeningLever FindAndPlaceOnWall(Game game, Vector2 P, MapRoom Target)
        {
            var T = game.TM.CheckForTile(P);

            if (T != null)
            {
                var X = T.column;
                var Y = T.row;

                var XD = Math.Random() < 0.5 ? -1 : 1;
                while (true)
                {
                    X += XD;
                    var T2 = game.TM.GetTile(X, Y);
                    if (T2 != null)
                    {
                        if (T2.enabled && T2.solid)
                        {
                            var T3 = game.TM.GetTile(X - XD, Y + 1);
                            if (!(T3 != null && T3.enabled && T3.solid))//too close to the floor, the lever could become obscured...
                            {
                                var ret = new RoomOpeningLever(game, new Point(X, Y), Target, XD == 1);
                                return(ret.Alive ? ret : null);
                            }
                            else
                            {
                                return(null);
                            }
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            return(null);
        }
        public void Generate(int rooms = 1)
        {
            if (generated)
            {
                return;
            }
            //var player = game.player;//player character
            var map    = game.TM;          //the tilemap to generate
            var bounds = game.stageBounds; //the bounds to stay within


            var SX = map.columns / 2;
            var SY = map.rows / 3;

            if (!started)
            {
                Helper.Log("started boxy generate");

                root    = new MapRoom();
                root.SX = SX;
                root.SY = SY;
                root.EX = SX + 6 + (int)(Math.Random() * 10);
                root.EY = SY + 6 + (int)(Math.Random() * 10);

                root.game = game;
                rootroom  = root;

                MapRoom.PlacedRooms = new List <MapRoom>();
                MapRoom.OpenRooms   = new List <MapRoom>();

                var roomMinimum = 10 + Math.Min(game.level, 4);
                roomtotal = roomMinimum + (int)(Math.Random() * (roomMinimum));

                if (!root.PlaceAndExpand())
                {
                    Helper.Log("Couldn't generate root room.");
                    generated = true;
                    return;
                }
                attempts = 400;

                started = true;
            }

            //var roomtotal = 12+(int)(Math.Random() * 10);
            //var roomtotal = 16 + (int)(Math.Random() * 16);

            //var roomMinimum = 16;

            //var rooms = 0;

            var R = root;

            //while (MapRoom.OpenRooms.Count < roomtotal && attempts > 0)
            if (MapRoom.OpenRooms.Count < roomtotal && attempts > 0)
            {
                //number of rooms to attempt to do per
                while (rooms > 0 && (MapRoom.OpenRooms.Count < roomtotal && attempts > 0))
                {
                    var L = MapRoom.FindValidUnplacedRooms().Pick();
                    if (L.PlaceAndExpand())
                    {
                        //rooms++;
                    }
                    attempts--;
                    rooms--;
                }
            }
            else
            {
                //finishGeneration();
                generated = true;
                Helper.Log("basic generation completed");
            }
        }
Пример #6
0
        private MapRoom GenerateAdjacentRoom(int Xdir, int Ydir)
        {
            /*var min = 6;
             * var max = 18;*/
            var min = 5;
            var max = 13;
            var dif = (max - min);
            int W   = (int)(min + (Math.Random() * dif));

            //min += 1;
            min -= 1;
            max -= 4;
            dif  = (max - min);
            int H = (int)(min + (Math.Random() * dif));

            var X = -1;
            var Y = -1;

            //var maxstagger = (int)(min / 2);
            //make safely editable bounds that allow more easily staggered room placement.
            var SX = this.SX;
            var SY = this.SY;

            var EX = this.EX;
            var EY = this.EY;

            if (Xdir != 0)
            {
                //SY -= maxstagger;
                //EY += maxstagger;
                SY -= H - 2;

                Y = (int)(SY + (Math.Random() * (EY - SY)));
                if (Xdir < 0)
                {
                    X = SX - W;
                }
                else
                {
                    X = EX;
                }
            }
            else if (Ydir != 0)
            {
                SX -= W - 2;
                //SX -= maxstagger;
                //EX += maxstagger;

                X = (int)(SX + Math.Random() * ((EX - SX)));
                if (Ydir < 0)
                {
                    Y = SY - H;
                }
                else
                {
                    Y = EY;
                }
            }

            if (X >= 0 && Y >= 0)
            {
                var M = new MapRoom();
                M.SX     = X;
                M.SY     = Y;
                M.EX     = X + W;
                M.EY     = Y + H;
                M.parent = this;
                M.game   = game;
                if (M.CanBePlaced())
                {
                    return(M);
                }
            }
            return(null);
        }