public bool HitTest(DungRoom a, int dop) { if ((left + wid + dop > a.left && left < a.left + a.wid + dop) && (top + hei + dop > a.top && top < a.hei + a.top + dop)) { return(true); } return(false); }
public void Adventure() { Console.Clear(); List <ConsoleKey> availableKeys = new List <ConsoleKey>() { ConsoleKey.D1, ConsoleKey.D2, ConsoleKey.D3, ConsoleKey.D4, ConsoleKey.D5, ConsoleKey.D6, ConsoleKey.D7 }; nowIn = rooms[0]; while (true) { nowIn.DrawSelfCurrentRoom(); ConsoleKey k = ConsoleKey.A; List <ConsoleKey> canGo = new List <ConsoleKey>(); for (int i = 0; i < nowIn.near.Count; i++) { canGo.Add(availableKeys[i]); } canGo.Add(ConsoleKey.Q); do { k = Console.ReadKey().Key; if (k == ConsoleKey.Q) { Console.BackgroundColor = ConsoleColor.DarkRed; Console.Write("Are you sure to exit : y/n "); k = Console.ReadKey().Key; if (k == ConsoleKey.Y) { Console.ResetColor(); Console.Clear(); break; } } } while (canGo.IndexOf(k) < 0); if (k == ConsoleKey.Y) { break; } int choosenRoad = canGo.IndexOf(k); nowIn.DrawSelf('.'); if (nowIn == nowIn.near[choosenRoad].from) { nowIn = nowIn.near[choosenRoad].to; } else { nowIn = nowIn.near[choosenRoad].from; } } }
bool TryAdd(Random rnd) { if ((rooms.Count < maxRoomCount || rnd.Next(20) != 0) && (rnd.Next(rooms.Count * roomSpawnRegular / 2) == 0 || rooms.Count == 0)) { DungRoom r = new DungRoom(); r.SetParmas(rnd.Next(wid - 4), rnd.Next(hei - 4), 4, 4); rooms.Add(r); } foreach (DungRoom r in rooms) { if (rnd.Next(6) == 0 && r.S < maxRoomsSize) { r.Scale(rnd.Next(2), rnd.Next(2)); } //if (rnd.Next(5) == 0) // r.Move(rnd.Next(-1, 2), rnd.Next(-1, 2)); } bool anyCollides = false; do { anyCollides = false; foreach (DungRoom r in rooms) { foreach (DungRoom ra in rooms) { if (r != ra) { if (r.HitTest(ra, minDistance)) { anyCollides = true; r.MakeRed(ConsoleColor.Red); ra.MakeRed(ConsoleColor.Red); r.Move(rnd.Next(-1, 2), rnd.Next(-1, 2)); ra.Move(rnd.Next(-1, 2), rnd.Next(-1, 2)); //Draw(false); } } } } } while (anyCollides); return(rooms.Count >= maxRoomCount); }
public DungRoad makeRoad(DungRoom to, Random rnd) { DungRoad r = new DungRoad(this, to); { // finng road direction if (!(to.top + to.hei < top || to.top > top + hei)) { if (to.left > left + wid) { //A ----> B r.SetParams(specRnd(rnd, Math.Max(1, to.top - top), Math.Min(hei, to.top + to.hei - top) - 1), wid, 0, to.left - left - wid); } if (to.left + to.wid < left) { r.SetParams(specRnd(rnd, Math.Max(1, to.top - top), Math.Min(hei, to.top + to.hei - top) - 1), 0, 1, left - to.left - to.wid); } } else { if (to.top > top + hei) { // A v B r.SetParams(hei, specRnd(rnd, Math.Max(1, to.left - left), Math.Min(wid, to.left + to.wid - left)) - 1, 2, to.top - top - hei + 1); } if (to.top + to.hei < top) { r.SetParams(0, specRnd(rnd, Math.Max(1, to.left - left), Math.Min(wid, to.left + to.wid - left)) - 1, 3, top - to.top - to.hei + 1); } } } //r.Draw(); near.Add(r); to.near.Add(r); return(r); }
List <Tuple <int, DungRoom> > FindClosest(DungRoom dr, Random rnd) { //Draw(true); //dr.MakeRed(ConsoleColor.Red); //Draw(false); //List<> res = new List<Tuple<int, DungRoom>>(); List <Tuple <int, DungRoom> > closest = new List <Tuple <int, DungRoom> >(); int maxCount = maxRoadsFrom; for (int i = 0; i < maxCount; i++) { closest.Add(new Tuple <int, DungRoom>(100, null)); } //DungRoom dr = rooms[0]; for (int w = 0; w < dr.wid * 2 + dr.hei * 2; w++) { int dist = 0; while (dist < Math.Min(wid, hei)) { ++dist; bool found = false; foreach (DungRoom r in rooms) { if (r != dr) { if ((w < dr.wid && r.HitTest(dr.left + w, dr.top - dist)) || (w >= dr.wid && w < dr.wid * 2 && r.HitTest(dr.left + w - dr.wid, dr.top + dr.hei + dist)) || (w >= dr.wid * 2 && w < dr.wid * 2 + dr.hei && r.HitTest(dr.left - dist, dr.top + w - dr.wid * 2)) || (w >= dr.wid * 2 + dr.hei && r.HitTest(dr.left + dr.wid + dist, dr.top + w - dr.wid * 2 - dr.hei)) ) { found = true; Tuple <int, DungRoom> foundT = new Tuple <int, DungRoom>(dist, r); for (int i = 0; i < maxCount; i++) { if (foundT.Item1 < closest[i].Item1) { if (closest.IndexOf(foundT) < 0) { if (closest[i].Item2 != foundT.Item2) { for (int I = maxCount - 1; I > i; I--) { closest[I] = closest[I - 1]; } } closest[i] = foundT; break; } } } break; } } } if (found) { break; } } //res.Add(new Tuple<int, DungRoom>()); } for (int i = 0; i < maxCount; i++) { if (closest[i].Item2 != null) { closest[i].Item2.MakeRed(ConsoleColor.Green); if (dr.NearRooms().IndexOf(closest[i].Item2) < 0) { dr.makeRoad(closest[i].Item2, rnd); } } } //Draw(false); return(closest); }
public DungRoad(DungRoom from, DungRoom to) { this.from = from; this.to = to; }