// Use this for initialization void Start() { ground = GameObject.Find("Ground"); player = GameObject.Find("Player"); WalkingGeneration.setValues(); if (map == null) { // Put the correct data in the map map = new Hashtable(); Random.seed = seed; Vector3 position = new Vector3(); // This is the starting position previousYPosR = 10; previousYPosL = 10; // Create map from -1000 to 1000 for (float i = minX; i < maxX; i += widthOfGroundPiece) { // Pick whether the block goes up or down previousYPosR += selectDirection(); for (float j = -20; j < previousYPosR; j += widthOfGroundPiece) { position = new Vector3(i, j, WALL_LAYER); map.Add(position, "Wall"); position.z = STANDARD_LAYER; map.Add(position, "Rock"); } Vector3 position1 = position; Vector3 position2 = position; Vector3 position3 = position; Vector3 position4 = position; position1.y = position.y - 1; position2.y = position.y - 2; position3.y = position.y - 3; position4.y = position.y - 4; // Put grassy block on top map.Remove(position); map.Remove(position1); map.Remove(position2); map.Remove(position3); map.Remove(position4); map.Add(position1, "Dirt"); map.Add(position2, "Dirt"); map.Add(position3, "Dirt"); map.Add(position4, "Dirt"); map.Add(position, "DirtWGrass"); position.z = WALL_LAYER; position1.z = WALL_LAYER; position2.z = WALL_LAYER; position3.z = WALL_LAYER; position4.z = WALL_LAYER; map.Remove(position); map.Remove(position1); map.Remove(position2); map.Remove(position3); map.Remove(position4); // Set minimum and maximum y value if (previousYPosR > maxY) { maxY = previousYPosR; } if (previousYPosR < minY) { minY = previousYPosR; } } } else { Debug.Log("Loaded map"); maxY = 100; } // Generate Caves for (int i = 0; i < 10; i++) { GenerateCaves.generateCave(); } GenerateTrees.treeLocation((int)maxY, 1.95f); player.transform.position = new Vector3(10, 26); // Creating the objects for around you when you spawn int loadingRight = Mathf.RoundToInt(player.transform.position.x + distToDestroyBox); int loadingLeft = Mathf.RoundToInt(player.transform.position.x - distanceBetweenLoads); int bottomLoad = Mathf.RoundToInt(player.transform.position.y - distToDestroyBox); int topLoad = Mathf.RoundToInt(player.transform.position.y + distToDestroyBox); for (float i = loadingLeft; i < loadingRight; i += widthOfGroundPiece) { for (float j = bottomLoad; j < topLoad; j += widthOfGroundPiece) { WalkingGeneration.loadPeice(i, j, STANDARD_LAYER); WalkingGeneration.loadPeice(i, j, WALL_LAYER); WalkingGeneration.loadPeice(i, j, GRASS_LAYER); } } }
// Should be called in an update public static void checkIfNeedsLoading() { int xPos = Mathf.RoundToInt(player.transform.position.x); int yPos = Mathf.RoundToInt(player.transform.position.y); bool goingRight = player.GetComponent <Rigidbody2D>().velocity.x > 0; bool goingUp = player.GetComponent <Rigidbody2D>().velocity.y > 0; // Check if player should load in the X direction if (xPos > lastXTransition + distanceBetweenLoads || xPos < lastXTransition - distanceBetweenLoads) { // Load peices up to your position on the right for (int i = lastXTransition; i < xPos; i += distanceBetweenLoads) { WalkingGeneration.loadPeices(i, yPos, goingRight, goingUp, true, false); lastXTransition = i; } // Load peices up to your position on the left for (int i = lastXTransition; i > xPos; i -= distanceBetweenLoads) { WalkingGeneration.loadPeices(i, yPos, goingRight, goingUp, true, false); lastXTransition = i; } } // Check if player should load in the Y direction if (yPos > lastYTransition + distanceBetweenLoads || yPos < lastYTransition - distanceBetweenLoads) { // Load peices above you for (int i = lastYTransition; i < yPos; i += distanceBetweenLoads) { WalkingGeneration.loadPeices(xPos, i, goingRight, goingUp, false, true); lastYTransition = i; } // Load peices below you for (int i = lastYTransition; i > yPos; i -= distanceBetweenLoads) { WalkingGeneration.loadPeices(xPos, i, goingRight, goingUp, false, true); lastYTransition = i; } } Collider2D[] collidersY; Collider2D[] collidersX; /* DEBUGGING * Debug.DrawLine (new Vector2 (xPos + heightDestroyBox / 2, yPos + distToDestroyBox + widthDestroyBox), new Vector2(xPos - heightDestroyBox/2, yPos + distToDestroyBox)); * Debug.DrawLine (new Vector2(xPos + heightDestroyBox/2, yPos - distToDestroyBox - widthDestroyBox), new Vector2(xPos - heightDestroyBox/2, yPos - distToDestroyBox)); * Debug.DrawLine (new Vector2(xPos + distToDestroyBox, yPos - heightDestroyBox/2), new Vector2(xPos + distToDestroyBox + widthDestroyBox, yPos + heightDestroyBox/2)); * Debug.DrawLine (new Vector2(xPos - distToDestroyBox, yPos - heightDestroyBox/2), new Vector2(xPos - distToDestroyBox - widthDestroyBox, yPos + heightDestroyBox/2)); */ if (!goingUp) { collidersY = Physics2D.OverlapAreaAll(new Vector2(xPos + heightDestroyBox / 2, yPos + distToDestroyBox + widthDestroyBox), new Vector2(xPos - heightDestroyBox / 2, yPos + distToDestroyBox)); } else { collidersY = Physics2D.OverlapAreaAll(new Vector2(xPos + heightDestroyBox / 2, yPos - distToDestroyBox - widthDestroyBox), new Vector2(xPos - heightDestroyBox / 2, yPos - distToDestroyBox)); } if (!goingRight) { collidersX = Physics2D.OverlapAreaAll(new Vector2(xPos + distToDestroyBox, yPos - heightDestroyBox / 2), new Vector2(xPos + distToDestroyBox + widthDestroyBox, yPos + heightDestroyBox / 2)); } else { collidersX = Physics2D.OverlapAreaAll(new Vector2(xPos - distToDestroyBox, yPos - heightDestroyBox / 2), new Vector2(xPos - distToDestroyBox - widthDestroyBox, yPos + heightDestroyBox / 2)); } foreach (Collider2D col in collidersX) { if (col.CompareTag("Tile")) { Dictionary.remove(col.gameObject); } } foreach (Collider2D col in collidersY) { if (col.CompareTag("Tile")) { Dictionary.remove(col.gameObject); } } }
// Update is called once per frame void Update() { WalkingGeneration.checkIfNeedsLoading(); }