/// <summary> /// Ajoute les tunnels donc la liste. /// </summary> private void PlaceTunnels() { Pathfinding p = new Pathfinding(GetSimpleMap(), Vector2.zero); List <ChildRoom> closedChildRoomList = new List <ChildRoom>(); ChildRoom currentChildRoom = GetRandomChildRoom(closedChildRoomList); //Start if (startPoint != null) { Vector2[] path = p.RequestPath(((Coord)startPoint).ToVect2(), currentChildRoom.rect.center); tunnels.Add(path); } for (int i = 0; i < childs.Count; i++) { currentChildRoom = GetRandomChildRoom(closedChildRoomList); if (currentChildRoom != null) { Vector2[] path = p.RequestPath(closedChildRoomList[closedChildRoomList.Count - 2].rect.center, currentChildRoom.rect.center); tunnels.Add(path); } } //End if (exitPoint != null) { Vector2[] path = p.RequestPath(closedChildRoomList[closedChildRoomList.Count - 1].rect.center, ((Coord)exitPoint).ToVect2()); tunnels.Add(path); } }
/// <summary> /// Affecte les childs room, leur nombre dépend de la taille de la room. /// </summary> private void AddChildRooms() { int padding = Random.Range(3, 5); int childMaxWidth = this.width / (childQuantity / 2); int childMinWidth = this.width / childQuantity; int childMaxHeight = this.height / (childQuantity / 2); int childMinHeight = this.height / childQuantity; //add rooms int placed = 0; int count = 0; while (placed < childQuantity) { int width = Random.Range(childMinWidth, childMaxWidth); int height = Random.Range(childMinHeight, childMaxHeight); int x = Random.Range(padding, (int)(GetRect().width - width - padding * 2)); int y = Random.Range(padding, (int)(GetRect().height - height - padding * 2)); ChildRoom childRoom = new ChildRoom(this, new Rect(x, y, width, height)); if (PlaceChildsRoom(childRoom)) { placed++; } // this is for debug stuff - shouldn't ever happen count++; if (count > 250) { Debug.LogWarning("Impossible de place toutes les childs room : " + placed + "/" + childQuantity); break; } } }
private ChildRoom GetRandomChildRoom(List <ChildRoom> closedList) { if (closedList.Count == childs.Count) { return(null); } ChildRoom currentChildRoom = childs[Random.Range(0, childs.Count)]; while (closedList.Contains(currentChildRoom)) { currentChildRoom = childs[Random.Range(0, childs.Count)]; } closedList.Add(currentChildRoom); return(currentChildRoom); }
/// <summary> /// Verifie que l'emplacement d'une child room est correct. Si correct l'ajoute dans la collection. /// </summary> /// <param name="testedChildRoom"></param> /// <returns></returns> private bool PlaceChildsRoom(ChildRoom testedChildRoom) { //check overlap bool overlap = false; foreach (var childRoom in childs) { if (childRoom.rect.Overlaps(testedChildRoom.rect)) { overlap = true; } } if (!overlap) { childs.Add(testedChildRoom); return(true); } return(false); }