public bool isOverlapping(PlanetBubble bubble) { // change xCenter, yCenter by factors of -1, 0, 1 int lx = bubble.getLeftX(); int rx = bubble.getRightX(); int ty = bubble.getTopY(); int by = bubble.getBottomY(); float tlx = getLeftX() - texture.width; float trx = getRightX() - texture.width; for (int x = 0; x < 2; x++) { float tby = yCenter - (height / 2f) - texture.height; float tty = yCenter + (height / 2f) - texture.height; for (int y = 0; y < 2; y++) { bool xContainsLeft = (tlx < lx && lx < trx); bool xContainsRight = (tlx < rx && rx < trx); bool yContainsTop = (tby < ty && ty < tty); bool yContainsBottom = (tby < by && by < tty); bool xBounded = xContainsLeft || xContainsRight; bool yBounded = yContainsTop || yContainsBottom; if (xBounded) { return(true); } tby += texture.height; tty += texture.height; } tlx += texture.width; trx += texture.width; } return(false); }
private void spawnBubble(Texture2D texture, float minHeight, float maxHeight, bool allowOverlap, int layer) { int numTries = 0; while (true) { bool shouldRedo = false; float xCenter = Random.Range(0, width); float yCenter = Random.Range(0, height); float bubbleHeight = Random.Range(minHeight, maxHeight); //float bubbleWidth = 0; float bubbleWidth = Random.Range(bubbleHeight * 1.5f, bubbleHeight * 3f); PlanetBubble planetBubble = new PlanetBubble(texture, xCenter, yCenter, bubbleWidth, bubbleHeight, layerOneSpeed, layerOneColor); switch (layer) { case 1: planetBubble = new PlanetBubble(texture, xCenter, yCenter, bubbleWidth, bubbleHeight, layerOneSpeed, layerOneColor); break; case 2: planetBubble = new PlanetBubble(texture, xCenter, yCenter, bubbleWidth, bubbleHeight, layerTwoSpeed, layerTwoColor); break; case 3: planetBubble = new PlanetBubble(texture, xCenter, yCenter, bubbleWidth, bubbleHeight, layerThreeSpeed, layerThreeColor); break; default: break; } if (!allowOverlap) { foreach (PlanetBubble bubble in bubbles) { if (bubble.isOverlapping(planetBubble) || planetBubble.isOverlapping(bubble)) { shouldRedo = true; break; } } if (shouldRedo) { planetBubble.draw(); numTries++; Debug.Log("Overlap detected"); continue; } else { bubbles.Add(planetBubble); break; } } else { bubbles.Add(planetBubble); break; } } }
public bool isXBounded(PlanetBubble bubble) { return(true); }