private void TestTile() { Debug.Log(nameof(TestTile)); Vector3 posA = new Vector3(1, 1, 2); HeightLevel heightLevelA = new HeightLevel(); Tile a = new Tile(posA, 0, 0, heightLevelA, 1); Vector3 posB = new Vector3(1, 1, 2); HeightLevel heightLevelB = new HeightLevel(); Tile b = new Tile(posB, 0, 0, heightLevelB, 1); Vector3 posC = new Vector3(0, 1, 2); HeightLevel heightLevelC = new HeightLevel(); Tile c = new Tile(posC, 0, 0, heightLevelC, 1); Vector3 posD = new Vector3(2, 1, 2); HeightLevel heightLevelD = new HeightLevel(); Tile d = new Tile(posD, 0, 0, heightLevelD, 1); Vector3 posE = new Vector3(1, 1, 1); HeightLevel heightLevelE = new HeightLevel(); Tile e = new Tile(posE, 0, 0, heightLevelE, 1); Vector3 posF = new Vector3(1, 1, 3); HeightLevel heightLevelF = new HeightLevel(); Tile f = new Tile(posF, 0, 0, heightLevelF, 1); Assert("a=" + a.WorldPos + " b=" + b.WorldPos + " a == b", a.CompareTo(b) == 0); Assert("a=" + a.WorldPos + " c=" + c.WorldPos + " a > c", a.CompareTo(c) == 1); Assert("a=" + a.WorldPos + " d=" + d.WorldPos + " a < d", a.CompareTo(d) == -1); Assert("a=" + a.WorldPos + " e=" + e.WorldPos + " a > e", a.CompareTo(e) == 1); Assert("a=" + a.WorldPos + " f=" + f.WorldPos + " a < f", a.CompareTo(d) == -1); }
/// <summary> /// Determine terrain type given the map height, temperature and rain /// </summary> private TerrainType DetermineTerrain(HeightLevel height /*, MoistureLevel rain, TemperatureLevel temperature*/) { switch (height) { case HeightLevel.Sea: return(TerrainType.Ocean); case HeightLevel.Land: return(TerrainType.Grassland); case HeightLevel.LowMountain: return(TerrainType.MountainsLow); case HeightLevel.MediumMountain: return(TerrainType.MountainsMed); case HeightLevel.HighMountain: return(TerrainType.MountainsHigh); case HeightLevel.MountainPeak: return(TerrainType.MountainPeak); default: return(TerrainType.Unknown); } }
static void Main(string[] args) { var bodyInfomationPublisher = new Publisher <BodyInformationDTO>(); var BMICheckup = new BMI(); var freeMealCheckup = new FreeMeal(); var heightLevelCheckup = new HeightLevel(); BMICheckup.Subscribe(bodyInfomationPublisher); freeMealCheckup.Subscribe(bodyInfomationPublisher); heightLevelCheckup.Subscribe(bodyInfomationPublisher); bodyInfomationPublisher.Notification(new BodyInformationDTO() { Name = "Leo", Age = 24, Height = 170, Weight = 70, IsMale = true }); bodyInfomationPublisher.Notification(new BodyInformationDTO() { Name = "Mendy", Age = 26, Height = 162, Weight = 50, IsMale = false }); bodyInfomationPublisher.Notification(new BodyInformationDTO() { Name = "Lucy", Age = 20, Height = 152, Weight = 42, IsMale = false }); bodyInfomationPublisher.Notification(new BodyInformationDTO() { Name = "Jack", Age = 25, Height = 165, Weight = 80, IsMale = true }); bodyInfomationPublisher.EndSpread(); }
/** * Sets the position of the room for the specified height level and column on that height level. * Also modifies the HeightLevel list to create new height level and returns the start of the list */ private HeightLevel FixateRoomOnHeightLevel(Room room, HeightLevel heightLevel, int column, HeightLevel start) { room.column = column; room.row = heightLevel.rowHeight; HeightLevel movingBack = heightLevel; for (int shift = heightLevel.columnStart; shift >= column; shift--) { if ((movingBack.previous != null) && (shift < (movingBack.previous.columnStart + movingBack.previous.width))) { movingBack = movingBack.previous; } } HeightLevel movingForward = heightLevel; for (int shift = heightLevel.columnStart + heightLevel.width; shift < (column + room.width); shift++) { if ((movingForward.next != null) && (shift > (movingForward.next.columnStart))) { movingForward = movingForward.next; } } // combine and divide moving backward and moving forward movingBack.DivideByIntroducingNewHeight(heightLevel.rowHeight + room.height, room.column, room.width, movingForward); return(start); //start will almost never change because we modify the start not change it. }
public Tile(Vector3 worldPos, int gridPosX, int gridPosZ, HeightLevel height, float size) { WorldPos = worldPos; GridPosX = gridPosX; GridPosZ = gridPosZ; HeightLevel = height; Size = size; }
private Room[][] FixPositionsForRoom(List <Room> roomList, int maxWidth) { //for maxHeight just compute the summation of height of all rooms int maxHeight = 0; foreach (Room room in roomList) { maxHeight += room.height; } //max height is only the upper limit of the layout //occupancy holds the information about slots that are occupied Room[][] occupancy = new Room[maxHeight][]; for (int i = 0; i < maxHeight; i++) { occupancy[i] = new Room[maxWidth]; for (int j = 0; j < maxWidth; j++) { occupancy[i][j] = null; } } HeightLevel start = new HeightLevel(0, 0, maxWidth); int maxHeightLevel = 0; foreach (Room room in roomList) { //try to fit this room in the lowest height level HeightLevel lowestFittingHeightLevel = FindHeightLevelForRoom(start, room); int columnStart = FindColumnStartOnHeightLevel(room, lowestFittingHeightLevel, occupancy, maxWidth); start = FixateRoomOnHeightLevel(room, lowestFittingHeightLevel, columnStart, start); int heightReached = room.row + room.height; if (heightReached > maxHeightLevel) { maxHeightLevel = heightReached; } FillOccupancyWithRoom(occupancy, room); room.MakeTileGrid(); int doorsMade = room.FindAndMakeOpeningsWithAdjacentRooms(occupancy, maxHeight, maxWidth); if (doorsMade == 0) { Debug.Log("No doors made"); } } return(occupancy); }
/** * Traverse the height list and find the appropriate height to fit this room, * Keep in mind that this might also turn the room */ private HeightLevel FindHeightLevelForRoom(HeightLevel start, Room room) { HeightLevel t = start; int smallestHeight = 9999; HeightLevel fittingHeightLevel = null; HeightLevel biggestHightLevel = null; int biggestHeight = 0; bool roomNeedsToTurnToFit = false; while (t != null) { if (t.rowHeight < smallestHeight) { if (t.width > room.width) { roomNeedsToTurnToFit = false; smallestHeight = t.rowHeight; fittingHeightLevel = t; } else if (t.width > room.height) { roomNeedsToTurnToFit = true; smallestHeight = t.rowHeight; fittingHeightLevel = t; } } if (t.rowHeight >= biggestHeight) { biggestHeight = t.rowHeight; biggestHightLevel = t; } t = t.next; } if (fittingHeightLevel == null) { Debug.Log("couldn't find smallest height for " + room.ToString()); fittingHeightLevel = biggestHightLevel; } if (roomNeedsToTurnToFit) { room.Turn(); } return(fittingHeightLevel); }
internal static HeightLevel GetFrom(List <HeightLevel> heights, float y) { HeightLevel height = new HeightLevel(); foreach (HeightLevel current in heights) { float upperBorder = current.upperBorder; float lowerBorder = current.lowerBorder; //lower border is inclusive, higher border is exclusive //heightLevel.Type is defined like [lowerBorder; upperBorder[ if (y >= lowerBorder && y < upperBorder) { height = current; } } return(height); }
public void Calculate() { //calculate tile size in meters from resolution TileLengthX = Vector3.Distance(borders.bottomLeft, borders.bottomRight) / ResolutionX; TileLengthZ = Vector3.Distance(borders.bottomLeft, borders.topLeft) / ResolutionZ; //initialize grid Tiles = new Tile[ResolutionX, ResolutionZ]; //offset to get the middle of a tile Vector3 offset = new Vector3(TileLengthX / 2, 0, TileLengthZ / 2); //begin bottom left of viewport Vector3 iterator = borders.bottomLeft - offset; //iterate over rows and within rows iterate over collumns for (int z = 0; z < ResolutionZ; z++) { for (int x = 0; x < ResolutionX; x++) { //get projection-point of current grid-tile // Does the ray intersect any objects excluding the player layer if (Physics.Raycast(iterator, -Vector3.up, out RaycastHit hit)) { Vector3 pos = hit.point; HeightLevel heightLevel = HeightLevel.GetFrom(heightLevels, pos.y); float size = Mathf.Max(TileLengthX, TileLengthZ); Tile tile = new Tile(pos, x, z, heightLevel, size); //tile.neighbours = CalculateNeighbours(tile, grid); Tiles[x, z] = tile; } else { //null if the ray did not intersect with anything (point lies beyond game world) Tiles[x, z] = null; } //create tile by position data (Vector3) and store it in 2D array iterator.x -= TileLengthX; } iterator.x = borders.bottomLeft.x - offset.x; iterator.z -= TileLengthZ; } //if (debugMode) DebugGrid(); }
/** * Finds the start column based on the height level and keeping in mind to allow maximum adjacency to * other rooms */ private int FindColumnStartOnHeightLevel(Room room, HeightLevel heightLevel, Room[][] grid, int maxWidth) { int columnStart = heightLevel.columnStart; while (columnStart + room.width + 1 < maxWidth && grid [heightLevel.rowHeight] [columnStart + room.width + 1] == null) { columnStart++; } //if it didn't touch an adjacent room, if (columnStart - 1 < 0 || grid[heightLevel.rowHeight][columnStart - 1] == null) { //go in the other direction columnStart = heightLevel.columnStart; while (columnStart - 1 >= 0 && grid[heightLevel.rowHeight][columnStart - 1] == null) { columnStart--; } } return(columnStart); }
private void TestArea() { Debug.Log(nameof(TestArea)); //create empty area Area area = new Area(); Assert("create empty area", area != null); int notFoundCount = 0; bool foundAllTiles = true; int bounds = 4; float max = 128; float min = -max; for (int i = 0; i < bounds; i++) { for (int j = 0; j < bounds; j++) { Tile tile = null; bool contains = true; while (contains) { float x = Random.Range(min, max); float y = Random.Range(min, max); float z = Random.Range(min, max); Vector3 pos = new Vector3(x, y, z); HeightLevel heightLevel = new HeightLevel(); tile = new Tile(pos, 0, 0, heightLevel, 1); contains = area.Contains(tile); if (contains) { Debug.LogWarning("collision"); } } area.Add(tile); if (!area.Contains(tile)) { notFoundCount++; foundAllTiles = false; } } } int count = area.GetTiles().Count; Assert("Added " + bounds * bounds + " tiles to area. Count=" + count, count == (bounds * bounds)); Assert("Searching for tiles. Number of Tiles not found=" + notFoundCount, foundAllTiles); area = new Area(); bounds = 2; float tileSize = 1; for (int i = 0; i < bounds; i++) { for (int j = 0; j < bounds; j++) { Tile tile = null; //bool contains = true; //while (contains) //{ float x = Random.Range(min, max); float y = Random.Range(min, max); float z = Random.Range(min, max); Vector3 pos = new Vector3(x, y, z); HeightLevel heightLevel = new HeightLevel(); tile = new Tile(pos, 0, 0, heightLevel, tileSize); // contains = area.Contains(tile); // if (contains) // Debug.LogWarning("collision"); //} area.Add(tile); } } float areaSize = area.GetTiles().Count *tileSize; Assert("Get size of area", area.SizeInSquareMeters() == areaSize); }
/// <summary> /// Gets the char value of a Heightlevel. /// </summary> /// <param name="level">The heightlevel to get the char value of</param> /// <returns>A string containing the char value of the item.</returns> public static string GetCharValue(HeightLevel level) { return(string.Empty + (int)level); }
public void DivideByIntroducingNewHeight(int newHeight, int newHeightColumn, int newHeightWidth, HeightLevel overlappingEnd) { //retain the old height and width before overwriting int oldHeight = rowHeight; int oldWidth = this.width; // if new height belongs to this height level if (newHeightColumn == this.columnStart) { //retain this object and increase its height this.rowHeight = newHeight; this.width = newHeightWidth; if (newHeightWidth < oldWidth) { if (overlappingEnd == this) //this will always be true in such a case //create a new height level and insert it in the next { HeightLevel remainingWidthLevel = new HeightLevel(oldHeight, newHeightColumn + newHeightWidth, oldWidth - newHeightWidth); remainingWidthLevel.previous = this; remainingWidthLevel.next = this.next; this.next = remainingWidthLevel; if (remainingWidthLevel.next != null) { remainingWidthLevel.next.previous = remainingWidthLevel; } } } else if (newHeightWidth > oldWidth) { if (overlappingEnd != this) //this assertion will always be true //make the overlapping end the next to this { int amountOverlapped = (newHeightColumn + newHeightWidth) - overlappingEnd.columnStart; overlappingEnd.columnStart = newHeightColumn + newHeightWidth; overlappingEnd.width = overlappingEnd.width - amountOverlapped; overlappingEnd.previous = this; this.next = overlappingEnd; } } } else if (newHeightColumn < this.columnStart) { int oldColumnStart = this.columnStart; this.columnStart = newHeightColumn; this.width = newHeightWidth; HeightLevel elevatedLevel = new HeightLevel(newHeight, newHeightColumn, newHeightWidth); elevatedLevel.previous = this; if (overlappingEnd == this) { int amountOverlapped = (newHeightColumn + newHeightWidth) - oldColumnStart; HeightLevel remainingWidthLevel = new HeightLevel(oldHeight, newHeightColumn + newHeightWidth, oldWidth - amountOverlapped); elevatedLevel.next = remainingWidthLevel; remainingWidthLevel.previous = elevatedLevel; remainingWidthLevel.next = this.next; if (this.next != null) { this.next.previous = remainingWidthLevel; } this.next = elevatedLevel; } else { int amountOverlapped = (newHeightColumn + newHeightWidth) - overlappingEnd.columnStart; overlappingEnd.columnStart = newHeightColumn + newHeightWidth; overlappingEnd.width = overlappingEnd.width - amountOverlapped; this.next = overlappingEnd; overlappingEnd.previous = this; } } else { this.width = newHeightColumn - this.columnStart; HeightLevel elevatedLevel = new HeightLevel(newHeight, newHeightColumn, newHeightWidth); elevatedLevel.previous = this; if (overlappingEnd == this) { elevatedLevel.next = this.next; if (this.next != null) { this.next.previous = elevatedLevel; } } else { int amountOverlapped = (newHeightColumn + newHeightWidth) - overlappingEnd.columnStart; overlappingEnd.columnStart = newHeightColumn + newHeightWidth; overlappingEnd.width = overlappingEnd.width - amountOverlapped; elevatedLevel.next = overlappingEnd; if (overlappingEnd.next != null) { overlappingEnd.next.previous = elevatedLevel; } } this.next = elevatedLevel; } }