Advertisement
JJCUBER

updated

Aug 25th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 48.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5.  
  6. class Tile
  7. {
  8.     public string type;
  9.     public int pos;
  10.    
  11.     // public bool grassAbove = false; // Implementing grass connectivity
  12.     // public bool grassBelow = false;
  13.     // public bool grassLeft = false;
  14.     // public bool grassRight = false;
  15.    
  16.     // public bool waterAbove = false;  // Implementing water connectivity // instead of checking for water above, you can check for grass above water instead, the output would need to be inversed in programming it
  17.    
  18.     public int tileType = 0; // If this is 0, then this is "other", if it is 1 or 2 then it is grass (1) or water (2)
  19.     public int grassSprite = 0; // Make sure to check in the code if spriteUpdated is true, then check if grassSprite and/or waterSprite are 0.  If they are then it is an "other" type **** THIS IS NOW DONE with the tileType above!! ****
  20.     public int waterSprite = 0;
  21.  
  22.     public bool spriteUpdated = false; // This turns true once I check if all four tiles around it are loaded, and then if they are, I update the sprite and set it to true
  23.    
  24.     // public bool isWaterDeterminedYet;
  25.     // public GameObject objRef;
  26.  
  27.     public Tile (string Type, int Pos/*, bool IsWaterDeterminedYet = false*//*, GameObject ObjRef = null*/)
  28.     {
  29.         this.type = Type;
  30.         this.pos = Pos;
  31.  
  32.         this.tileType = pos > 1 ? 0 : pos + 1; // if position is greater than one, then we set it to 0 to indicate other, otherwise we set it to 1 if it is 0 for grass or we set it to 2 if it is 1 for water!
  33.         // this.isWaterDeterminedYet = IsWaterDeterminedYet;
  34.         // this.objRef = ObjRef;
  35.     }
  36. }
  37.  
  38. public class TryGeneration4:MonoBehaviour
  39. {
  40.     public Camera cam;
  41.     public GameObject parent;
  42.     public GameObject[] items;
  43.     [Space]
  44.     public GameObject waterTypeTwo;
  45.     [Space]
  46.     public GameObject player;
  47.     [Space]
  48.     public Sprite[] grassSprites;
  49.     [Space]
  50.     public Sprite[] waterSprites;
  51.     [Space]
  52.    
  53.     // multiply this 4 and the 3 below by a number since we are now trying individual tiles!
  54.     public int halfRenderDistanceX = 16;
  55.     public int halfRenderDistanceY = 12;
  56.  
  57.     private Dictionary < Vector2, Tile > tiles = new Dictionary < Vector2, Tile > ();
  58.    
  59.     // private List < Vector2 > tilesLoaded = new List < Vector2 > (); // likely cause of lag spikes
  60.     private Dictionary<Vector2, bool> tilesLoaded = new Dictionary<Vector2, bool>();
  61.    
  62.     private Dictionary<Vector2, bool> waterTilesWaiting = new Dictionary<Vector2, bool>();
  63.  
  64.     private Dictionary<Vector2, bool> tilesWithCoroutines = new Dictionary<Vector2, bool>();
  65.  
  66.     private Dictionary<Vector2, bool> tilesToUpdate = new Dictionary<Vector2, bool>();
  67.  
  68.     private Dictionary<Vector2, bool> objectsWaitingToLoad = new Dictionary<Vector2, bool>();
  69.  
  70.     // private ArrayList negativeTilesCreatedX = new ArrayList();
  71.     // private ArrayList negativeTilesCreatedY = new ArrayList();
  72.     // private ArrayList positiveTilesCreatedX = new ArrayList();
  73.     // private ArrayList positiveTilesCreatedY = new ArrayList();
  74.  
  75.     // private List < Vector2 > previousSpots = new List<Vector2> ();
  76.    
  77.     private IntRange
  78.     grassIntRange = new IntRange (0, 0, 10000f),
  79.         // waterIntRange = new IntRange(1, 1, 10f), // NORMAL value!
  80.         // **********************  TEMPORARY VALUE FOR DEBUGING!!  **********************
  81.         waterIntRange = new IntRange (1, 1, 100f),
  82.         treeIntRange = new IntRange (2, 2, 200f),
  83.         rockIntRange = new IntRange (3, 3, 200f),
  84.         coalIntRange = new IntRange (4, 4, 150f),
  85.         ironIntRange = new IntRange (5, 5, 50f),
  86.         copperIntRange = new IntRange (6, 6, 25f),
  87.         silverIntRange = new IntRange (7, 7, 25f),
  88.         saltIntRange = new IntRange (8, 8, 50f);
  89.  
  90.     private List < IntRange > tileTypesIntRangeList;
  91.     private IntRange[] tileTypesIntRangeArray;
  92.     private string[] tileTypeNames;
  93.  
  94.     private Vector2 previousPlayerPos = new Vector2(0,0);
  95.     private void Start ()
  96.     {
  97.         Vector2 playerPos = player.transform.position;
  98.         // Set up List of Ranges for Random Generation (might want to use a seed instead in the future because this might be what is lagging the game when spawning parts)
  99.         tileTypesIntRangeList = new List < IntRange >
  100.         {
  101.             grassIntRange, waterIntRange, treeIntRange, rockIntRange, coalIntRange,
  102.             ironIntRange, copperIntRange, silverIntRange, saltIntRange
  103.         };
  104.  
  105.         // Copies the List of Ranges to an array to be put into functions easily
  106.         tileTypesIntRangeArray = tileTypesIntRangeList.ToArray ();
  107.  
  108.         // Set up List of Names for each type of tile
  109.         tileTypeNames = new string[]
  110.         {
  111.             "grass", "water", "tree", "rock", "coal",
  112.             "iron", "copper", "silver", "salt"
  113.         };
  114.  
  115.         // Goes through all spots within FULL Render Distance
  116.         for (int x =  -halfRenderDistanceX + (int)playerPos.x; x <= halfRenderDistanceX + (int)playerPos.x; x++)
  117.         {
  118.             for (int y =  -halfRenderDistanceY + (int)playerPos.y; y <= halfRenderDistanceY + (int)playerPos.y; y++)
  119.             {
  120.                 // if(x < 0) negativeTilesCreatedX[-x] = "exists";
  121.                 // else positiveTilesCreatedX[x] = "exists";
  122.                 // if(y < 0) negativeTilesCreatedY[-y] = "exists";
  123.                 // else positiveTilesCreatedY[y] = "exists";
  124.  
  125.  
  126.                 // previousSpots.Add(new Vector2(x, y));
  127.                
  128.                 Vector2 currentVectorPos = new Vector2 (x, y);
  129.  
  130.                 if (tiles.ContainsKey (currentVectorPos))goto end;
  131.                
  132.                 int tileTypePos = RandomRange.Range (tileTypesIntRangeArray);
  133.                 string tileTypePosName = tileTypeNames[tileTypePos];
  134.                 Tile tile = new Tile(tileTypePosName, tileTypePos);
  135.  
  136.                 tiles[currentVectorPos] = tile;
  137.  
  138.                 // wip
  139.                 // Tile newTile = new Tile("temp", 1, );
  140.                 // tiles.Add(new Vector2(x, y), newChunk);
  141.                 // loadedTileSpots.Add(new Vector2(x, y));
  142.                 // chunks[new Vector2(x, y)].loadChunk();
  143.                 // print(x + " " + y);
  144.  
  145.                
  146.                 if (tileTypePosName == "water")
  147.                 {
  148.                     waterTilesWaiting.Add(currentVectorPos, true);
  149.                     // Do stuff
  150.                 }
  151.  
  152.  
  153.                 end:;
  154.                 int tileTypePosition = tiles[currentVectorPos].pos;
  155.                 tilesLoaded.Add(currentVectorPos, true);
  156.  
  157.                 // GameObject objLoaded = Instantiate(items[tileTypePosition], currentVectorPos, Quaternion.identity, parent.transform);
  158.                 // objLoaded.transform.name = (x + " , " + y);
  159.  
  160.                 loadObj(currentVectorPos, tileTypePosition);
  161.             }
  162.         }
  163.         implementingWater();
  164.     }
  165.  
  166.     private void Update()
  167.     {
  168.         Vector2 playerPos = player.transform.position;
  169.  
  170.         // List<Vector2> itemsToRemove = new List<Vector2>();
  171.        
  172.         Dictionary<Vector2, bool> itemsToRemove = new Dictionary<Vector2, bool>();
  173.  
  174.         /*foreach (Vector2 item in tilesLoaded)
  175.         {
  176.             if( item.x > halfRenderDistanceX + (int)playerPos.x ||
  177.                 item.x < -halfRenderDistanceX + (int)playerPos.x ||
  178.                 item.y > halfRenderDistanceY + (int)playerPos.y ||
  179.                 item.y < -halfRenderDistanceY + (int)playerPos.y )
  180.                 itemsToRemove.Add(item);*/
  181.         foreach (KeyValuePair<Vector2, bool> item in tilesLoaded)
  182.         {
  183.             if( item.Key.x > halfRenderDistanceX + (int)playerPos.x ||
  184.                 item.Key.x < -halfRenderDistanceX + (int)playerPos.x ||
  185.                 item.Key.y > halfRenderDistanceY + (int)playerPos.y ||
  186.                 item.Key.y < -halfRenderDistanceY + (int)playerPos.y )
  187.                 // itemsToRemove.Add(item.Key);
  188.                 itemsToRemove.Add(item.Key, true);
  189.            
  190.             /*if( item.x > (int)cam.transform.position.x + (3 * cam.orthographicSize) || // *************** THIS IS TO TRY AND UNLOAD items not in the camera's view, but is interfering with the updating when moving around :/
  191.                 item.x < -(int)cam.transform.position.x - (3 * cam.orthographicSize) ||
  192.                 item.y > (int)cam.transform.position.y + (3 * cam.orthographicSize) ||
  193.                 item.y < -(int)cam.transform.position.y - (3 * cam.orthographicSize) )
  194.                 itemsToRemove.Add(item);*/
  195.         }
  196.        
  197.         if(itemsToRemove.Count > 0)
  198.         {
  199.             foreach(KeyValuePair<Vector2,bool> item in itemsToRemove)
  200.             {
  201.                 tilesLoaded.Remove(item.Key);
  202.                 // print("REMOVED!");
  203.                 // Destroy(GameObject.Find(item.Key.x + " , " + item.Key.y));
  204.                 destroyObj(item.Key);
  205.             }
  206.         }
  207.        
  208.         // List<Vector2> spots = new List<Vector2>();
  209.         if(previousPlayerPos != playerPos)
  210.         {
  211.             /* // Old way of going through all tiles in area, is REALLY laggy, so has been re-implemented into a function and called by for loops going through only the edges, and only the sides that the player has moved towards
  212.             for (int x =  -halfRenderDistanceX + (int)playerPos.x; x <= halfRenderDistanceX + (int)playerPos.x; x++)
  213.             {
  214.                 for (int y =  -halfRenderDistanceY + (int)playerPos.y; y <= halfRenderDistanceY + (int)playerPos.y; y++)
  215.                 {
  216.                     Vector2 currentVectorPos = new Vector2 (x, y);
  217.  
  218.                     // spots.Add(currentVectorPos);
  219.                     // if(previousSpots.Contains(currentVectorPos)) goto skip;
  220.  
  221.  
  222.                     // bool xExists = false;
  223.                     // bool yExists = false;
  224.                     // if(x < 0) xExists = (string)negativeTilesCreatedX[-x] == "exists" ? true : false;
  225.                     // else xExists = (string)positiveTilesCreatedX[x] == "exists" ? true : false;
  226.                     // if(y < 0) yExists = (string)negativeTilesCreatedY[-y] == "exists" ? true : false;
  227.                     // else yExists = (string)positiveTilesCreatedY[y] == "exists" ? true : false;
  228.  
  229.  
  230.                     // if((string)tilesCreatedX[x] == "exists" && (string)tilesCreatedX[y] == "exists") goto end;
  231.                     // if(xExists && yExists) goto end;
  232.  
  233.                     if (tiles.ContainsKey (currentVectorPos))goto end;
  234.                    
  235.                     int tileTypePos = RandomRange.Range (tileTypesIntRangeArray);
  236.                     string tileTypePosName = tileTypeNames[tileTypePos];
  237.                     Tile tile = new Tile(tileTypePosName, tileTypePos);
  238.                    
  239.                     tiles[currentVectorPos] = tile;
  240.  
  241.                     // wip
  242.                     // Tile newTile = new Tile("temp", 1, );
  243.                     // tiles.Add(new Vector2(x, y), newChunk);
  244.                     // loadedTileSpots.Add(new Vector2(x, y));
  245.                     // chunks[new Vector2(x, y)].loadChunk();
  246.                     // print(x + " " + y);
  247.  
  248.                    
  249.                     if (tileTypePosName == "water")
  250.                     {
  251.                         // Do stuff
  252.                     }
  253.  
  254.  
  255.                     // ****************** remove ALL stuff related to the tilesCreated arraylist, since it didn't work how I had wanted it to
  256.                    
  257.                     // if(x < 0) negativeTilesCreatedX[-x] = "exists";
  258.                     // else positiveTilesCreatedX[x] = "exists";
  259.                     // if(y < 0) negativeTilesCreatedY[-y] = "exists";
  260.                     // else positiveTilesCreatedY[y] = "exists";
  261.                    
  262.  
  263.  
  264.  
  265.                     end:;
  266.                     if(!tilesLoaded.Contains(currentVectorPos))
  267.                     {
  268.                         int tileTypePosition = tiles[currentVectorPos].pos;
  269.                         tilesLoaded.Add(currentVectorPos);
  270.  
  271.                         GameObject objLoaded = Instantiate(items[tileTypePosition], currentVectorPos, Quaternion.identity, parent.transform);
  272.                         objLoaded.transform.name = (x + " , " + y);
  273.                     }
  274.  
  275.                     // skip:;
  276.  
  277.                 }
  278.             }
  279.             */
  280.  
  281.  
  282.  
  283.  
  284.  
  285.            // ************************************** this loading/generating is taking up too much fps!! ***********************************
  286.             tilesToUpdate = new Dictionary<Vector2, bool>();
  287.  
  288.             if((int)playerPos.x > (int)previousPlayerPos.x) // moved right
  289.             {
  290.                 // print("moved RIGHT!");
  291.                 int x = halfRenderDistanceX + (int)playerPos.x; // locked x
  292.                 for(int y = -halfRenderDistanceY + (int)playerPos.y; y <= halfRenderDistanceY + (int)playerPos.y; y++) codeToRun(new Vector2(x, y)); // , -1, 0);
  293.  
  294.             }
  295.  
  296.             if((int)playerPos.x < (int)previousPlayerPos.x) // moved left
  297.             {
  298.                 // print("moved LEFT!");
  299.                 int x = -halfRenderDistanceX + (int)playerPos.x; // locked x
  300.                 for(int y = -halfRenderDistanceY + (int)playerPos.y; y <= halfRenderDistanceY + (int)playerPos.y; y++) codeToRun(new Vector2(x, y)); // , 1, 0);
  301.             }
  302.  
  303.             if((int)playerPos.y > (int)previousPlayerPos.y) // moved up
  304.             {
  305.                 // print("moved UP!");
  306.                 int y = halfRenderDistanceY + (int)playerPos.y;
  307.                 for(int x = -halfRenderDistanceX + (int)playerPos.x; x <= halfRenderDistanceX + (int)playerPos.x; x++) codeToRun(new Vector2(x, y)); // , 0, -1);
  308.             }
  309.  
  310.             if((int)playerPos.y < (int)previousPlayerPos.y) // moved down
  311.             {
  312.                 // print("moved DOWN!");
  313.                 int y = -halfRenderDistanceY + (int)playerPos.y;
  314.                 for(int x = -halfRenderDistanceX + (int)playerPos.x; x <= halfRenderDistanceX + (int)playerPos.x; x++) codeToRun(new Vector2(x, y)); // , 0, 1);
  315.             }
  316.  
  317.         }
  318.         implementingWater(); // Updates all water positions (adds spread)
  319.        
  320.         previousPlayerPos = player.transform.position;
  321.         // previousSpots = spots;
  322.        
  323.         Vector2 downOne = (Vector2)player.transform.position - new Vector2(0, 1);
  324.         Vector2 downTwo = (Vector2)player.transform.position - new Vector2(0, 2);
  325.  
  326.         downOne = new Vector2(Mathf.Round(downOne.x), Mathf.Round(downOne.y));
  327.         downTwo = new Vector2(Mathf.Round(downTwo.x), Mathf.Round(downTwo.y));
  328.  
  329.         if(tiles.ContainsKey(downOne) && tiles[downOne].pos == 2 && !tilesWithCoroutines.ContainsKey(downOne))
  330.         {
  331.             tilesWithCoroutines.Add(downOne, true);
  332.             StartCoroutine(changeTree(downOne));
  333.         }
  334.         if(tiles.ContainsKey(downTwo) && tiles[downTwo].pos == 2 && !tilesWithCoroutines.ContainsKey(downTwo))
  335.         {
  336.             tilesWithCoroutines.Add(downTwo, true);
  337.             StartCoroutine(changeTree(downTwo));
  338.         }
  339.  
  340.         foreach(KeyValuePair<Vector2, bool> item in tilesToUpdate) updateSprite(item.Key);
  341.  
  342.  
  343.         workOnObjectsWaitingToLoad(); // MIGHT NOT BE RIGHT SPOT!! ****
  344.     }
  345.  
  346.     private void codeToRun(Vector2 currentVectorPos)// , int xChangeCloser, int yChangeCloser)
  347.     {
  348.         // tilesToUpdate.Add(currentVectorPos + new Vector2(xChangeCloser, yChangeCloser), true);
  349.         tilesToUpdate.Add(currentVectorPos, true);
  350.        
  351.         if (tiles.ContainsKey (currentVectorPos))goto end;
  352.        
  353.  
  354.         int tileTypePos = RandomRange.Range (tileTypesIntRangeArray);
  355.         string tileTypePosName = tileTypeNames[tileTypePos];
  356.         Tile tile = new Tile(tileTypePosName, tileTypePos);
  357.        
  358.         tiles[currentVectorPos] = tile;
  359.  
  360.         // wip
  361.         // Tile newTile = new Tile("temp", 1, );
  362.         // tiles.Add(new Vector2(x, y), newChunk);
  363.         // loadedTileSpots.Add(new Vector2(x, y));
  364.         // chunks[new Vector2(x, y)].loadChunk();
  365.         // print(x + " " + y);
  366.  
  367.        
  368.         if (tileTypePosName == "water") waterTilesWaiting.Add(currentVectorPos, true);
  369.         /*{
  370.             // Do stuff
  371.             // implementingWater(); // decided to run this after whole loop is done to go through a dictionary of all vector spot with water
  372.             waterTilesWaiting.Add(currentVectorPos, true);
  373.            
  374.         }*/
  375.  
  376.  
  377.         // ****************** remove ALL stuff related to the tilesCreated arraylist, since it didn't work how I had wanted it to
  378.        
  379.         // if(x < 0) negativeTilesCreatedX[-x] = "exists";
  380.         // else positiveTilesCreatedX[x] = "exists";
  381.         // if(y < 0) negativeTilesCreatedY[-y] = "exists";
  382.         // else positiveTilesCreatedY[y] = "exists";
  383.        
  384.  
  385.  
  386.  
  387.         end:;
  388.         /*if(!tilesLoaded.Contains(currentVectorPos))
  389.         {
  390.             int tileTypePosition = tiles[currentVectorPos].pos;
  391.             tilesLoaded.Add(currentVectorPos);
  392.  
  393.             GameObject objLoaded = Instantiate(items[tileTypePosition], currentVectorPos, Quaternion.identity, parent.transform);
  394.             objLoaded.transform.name = (currentVectorPos.x + " , " + currentVectorPos.y);
  395.         }*/
  396.        
  397.         /*
  398.         int willUpdateSprite = 0;
  399.         // Check for grass
  400.         if(tiles[currentVectorPos].pos == 0)
  401.         {
  402.             Vector2 above = currentVectorPos + new Vector2(0,1);
  403.             Vector2 below = currentVectorPos + new Vector2(0,-1);
  404.             Vector2 left = currentVectorPos + new Vector2(-1,0);
  405.             Vector2 right = currentVectorPos + new Vector2(1,0);
  406.            
  407.             // if(tiles.ContainsKey(above) if(tiles[above].pos == 0) tiles[currentVectorPos].grassAbove = true;
  408.             // if(tiles.ContainsKey(below) if(tiles[below].pos == 0) tiles[currentVectorPos].grassBelow = true;
  409.             // if(tiles.ContainsKey(left) if(tiles[left].pos == 0) tiles[currentVectorPos].grassLeft = true;
  410.             // if(tiles.ContainsKey(right) if(tiles[right].pos == 0) tiles[currentVectorPos].grassRight = true;
  411.  
  412.             /*tiles[currentVectorPos].grassAbove = tiles.ContainsKey(above) ? tiles[above].pos == 0 ? true : false : false;
  413.             tiles[currentVectorPos].grassBelow = tiles.ContainsKey(below) ? tiles[below].pos == 0 ? true : false : false;
  414.             tiles[currentVectorPos].grassLeft = tiles.ContainsKey(left) ? tiles[left].pos == 0 ? true : false : false;
  415.             tiles[currentVectorPos].grassRight = tiles.ContainsKey(right) ? tiles[right].pos == 0 ? true : false : false;
  416.             * /
  417.            
  418.             int total = 0;
  419.            
  420.             total += tiles.ContainsKey(above) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  421.             total += tiles.ContainsKey(below) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  422.             total += tiles.ContainsKey(left) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  423.             total += tiles.ContainsKey(right) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  424.            
  425.             total = 15 - total;
  426.  
  427.             tiles[currentVectorPos].grassSprite = total;
  428.            
  429.             // updateSprite(currentVectorPos, true); // each bool defaults to false, so if it is the first one, you only need to put the first one
  430.             willUpdateSprite = 1;
  431.         }
  432.        
  433.         //
  434.        
  435.         // Check for water
  436.         else if(tiles[currentVectorPos].pos == 1)
  437.         {
  438.             Vector2 above = currentVectorPos + new Vector2(0, 1);
  439.            
  440.             // if(tiles.ContainsKey(above) if(tiles[above].pos == 1) tiles[currentVectorPos].waterAbove = true;
  441.            
  442.             // tiles[currentVectorPos].waterAbove = tiles.ContainsKey(above) ? tiles[above].pos == 1 ? true : false : false;
  443.        
  444.             tiles[currentVectorPos].waterSprite = tiles.ContainsKey(above) ? tiles[above].pos == 1 ? 1 : 0 : 0;
  445.            
  446.             // updateSprite(currentVectorPos, false, true);
  447.             willUpdateSprite = 2;
  448.         }
  449.         */
  450.         //
  451.        
  452.         // workOnObjectsWaitingToLoad(); // **** TEMP MOVING THIS!!
  453.  
  454.         if(!tilesLoaded.ContainsKey(currentVectorPos))
  455.         {
  456.             int tileTypePosition = tiles[currentVectorPos].pos;
  457.             tilesLoaded.Add(currentVectorPos, true);
  458.  
  459.             // GameObject objLoaded = Instantiate(items[tileTypePosition], currentVectorPos, Quaternion.identity, parent.transform);
  460.             // objLoaded.transform.name = (currentVectorPos.x + " , " + currentVectorPos.y);
  461.             loadObj(currentVectorPos, tileTypePosition);
  462.         }
  463.  
  464.         // if(willUpdateSprite == 1) updateSprite(currentVectorPos, true);
  465.         // else if(willUpdateSprite == 2) updateSprite(currentVectorPos, false, true);
  466.         // skip:;
  467.        
  468.         // updateSprite(currentVectorPos);
  469.     }
  470.  
  471.     private void implementingWater() // **** MAKE SURE that you unload a gameobject if it is loaded BEFORE changing the tile, otherwise there will be random permanently loaded gameobjects
  472.     {
  473.         // waterTilesWaiting // dict with all water Tiles that are waiting
  474.        
  475.         // Need multiplier, random chance, etc.
  476.        
  477.         foreach(KeyValuePair<Vector2, bool> item in waterTilesWaiting)
  478.         {
  479.             Dictionary<Vector2, bool> previousWaterTiles = new Dictionary<Vector2, bool>(); // Might need to change bool to (class Tile)
  480.             previousWaterTiles.Add(item.Key, true); // test in compiler if this works
  481.            
  482.             float chance = 1000f;
  483.             float notChance = 1000f - chance;
  484.             // int iterationNum = 0;
  485.            
  486.             // Dictionary<Vector2, bool> tilesToLoadAgain = new Dictionary<Vector2, bool>();
  487.  
  488.             while(previousWaterTiles.Count > 0)
  489.             {
  490.                 chance /= 1.5f;
  491.                 notChance = 1000f - chance;
  492.                
  493.                 Dictionary<Vector2, bool> tempHolder = new Dictionary<Vector2, bool>();
  494.                
  495.                 foreach(KeyValuePair<Vector2, bool> item2 in previousWaterTiles)
  496.                 {
  497.                     Vector2 currentVectorPos = item2.Key;
  498.  
  499.                     if(GameObject.Find(currentVectorPos.x + " , " + currentVectorPos.y) && tiles[currentVectorPos].pos != 1) // YES I FIXED THE MULTIPLE WATER ISSUE!  I first put this check before the change, and made sure that a water tile wasn't already there before trying to change it, since it decided to be like that for some reason!
  500.                     {
  501.                         // Destroy(GameObject.Find(currentVectorPos.x + " , " + currentVectorPos.y));
  502.                         destroyObj(currentVectorPos);
  503.  
  504.                         // GameObject objLoaded = Instantiate(items[1], currentVectorPos, Quaternion.identity, parent.transform);
  505.                         // objLoaded.transform.name = (currentVectorPos.x + " , " + currentVectorPos.y);
  506.                         loadObj(currentVectorPos, 1);
  507.                     }
  508.                    
  509.                     if(tiles.ContainsKey(currentVectorPos)) tiles[currentVectorPos] = new Tile(tileTypeNames[1], 1);
  510.                     else tiles.Add(currentVectorPos, new Tile(tileTypeNames[1], 1));
  511.                     /*if(GameObject.Find(currentVectorPos.x + " , " + currentVectorPos.y))
  512.                     {
  513.                         // while(GameObject.Find(currentVectorPos.x + " , " + currentVectorPos.y)) Destroy(GameObject.Find(currentVectorPos.x + " , " + currentVectorPos.y));
  514.                         // Destroy(GameObject.FindGameObjectsWithTag(currentVectorPos.x + " , " + currentVectorPos.y));
  515.                         // GameObject[] allOfThem = GameObject.Find(currentVectorPos.x + " , " + currentVectorPos.y);
  516.                         // foreach(GameObject obj in Object.FindObjectOfType(typeof(GameObject)) as GameObject[]) Destroy(obj);
  517.                         // List<GameObject> objects = Resources.FindObjectsOfTypeAll<GameObject>().Where(obj => obj.name == (currentVectorPos.x + " , " + currentVectorPos.y)) as List<GameObject>;
  518.                         // foreach(GameObject obj in objects) Destroy(obj);
  519.                         // foreach(GameObject obj in objects) Destroy(obj);
  520.                         // objects = new List<GameObject>();
  521.  
  522.                         // if(!tilesToLoadAgain.ContainsKey(currentVectorPos)) tilesToLoadAgain.Add(currentVectorPos, true);
  523.  
  524.                         Destroy(GameObject.Find(currentVectorPos.x + " , " + currentVectorPos.y));
  525.  
  526.                     }*/
  527.                    
  528.                    
  529.                     bool up = RandomRange.Range(new IntRange(0, 0, notChance), new IntRange(1, 1, chance)) == 1 ? true : false;
  530.                     bool down = RandomRange.Range(new IntRange(0, 0, notChance), new IntRange(1, 1, chance)) == 1 ? true : false;
  531.                     bool left = RandomRange.Range(new IntRange(0, 0, notChance), new IntRange(1, 1, chance)) == 1 ? true : false;
  532.                     bool right = RandomRange.Range(new IntRange(0, 0, notChance), new IntRange(1, 1, chance)) == 1 ? true : false;
  533.                    
  534.                    
  535.                     // if (up && currentVectorPos.y + 1 < 16) tempHolder.Add(new Vector2((currentVectorPos.x), (currentVectorPos.y + 1)), true);
  536.                     // if (down && currentVectorPos.y - 1 >= 0) tempHolder.Add(new Vector2((currentVectorPos.x), (currentVectorPos.y - 1)), true);
  537.                     // if (left && currentVectorPos.x - 1 >= 0) tempHolder.Add(new Vector2((currentVectorPos.x - 1), (currentVectorPos.y)), true);
  538.                     // if (right && currentVectorPos.x + 1 < 16) tempHolder.Add(new Vector2((currentVectorPos.x + 1), (currentVectorPos.y)), true);
  539.  
  540.                     if(up && !tempHolder.ContainsKey(new Vector2((currentVectorPos.x), (currentVectorPos.y + 1)))) tempHolder.Add(new Vector2((currentVectorPos.x), (currentVectorPos.y + 1)), true);
  541.                     if(down && !tempHolder.ContainsKey(new Vector2((currentVectorPos.x), (currentVectorPos.y - 1)))) tempHolder.Add(new Vector2((currentVectorPos.x), (currentVectorPos.y - 1)), true);
  542.                     if(left && !tempHolder.ContainsKey(new Vector2((currentVectorPos.x - 1), (currentVectorPos.y)))) tempHolder.Add(new Vector2((currentVectorPos.x - 1), (currentVectorPos.y)), true);
  543.                     if(right && !tempHolder.ContainsKey(new Vector2((currentVectorPos.x + 1), (currentVectorPos.y)))) tempHolder.Add(new Vector2((currentVectorPos.x + 1), (currentVectorPos.y)), true);
  544.                    
  545.                 }
  546.                 previousWaterTiles = tempHolder;
  547.                
  548.             }
  549.            
  550.             /*foreach(KeyValuePair<Vector2, bool> tile in tilesToLoadAgain) // ****************************
  551.             {
  552.                 GameObject objLoaded = Instantiate(items[1], tile.Key, Quaternion.identity, parent.transform);
  553.                 objLoaded.transform.name = (tile.Key.x + " , " + tile.Key.y);
  554.             }*/
  555.         }
  556.         waterTilesWaiting = new Dictionary<Vector2, bool>(); // resets waterTilesWaiting to and empty dictionary after working on all of the water parts
  557.        
  558.     }
  559.    
  560.     /* private int determineGrassTexture(Vector2 vec) // 16 aka 15 spots with 0 base // Found a better way of doing this, but don't understand it: bitmasking, I think I understand it.  Each position is a step in a power of 2, and each output is unique, so you create a set that if you get a certain output num, then it equals a certain number corresponding to something in a list of sprites! https://connect.unity.com/p/bitmasks-and-enum-flags
  561.     {
  562.         bool above = tiles[vec].grassAbove;
  563.         bool below = tiles[vec].grassBelow;
  564.         bool left = tiles[vec].grassLeft;
  565.         bool right = tiles[vec].grassRight;
  566.        
  567.         /*
  568.         // going in order like using binary (0000, 0001, 0010, 0011, etc.)
  569.         // MIGHT be able to optimize this if you group it into two sections, above being true and above being false, then left or right section in each, it would be more code but certain code will have to go through less code which should make it slightly faster since this iterates over all new stuff
  570.         if(!above && !below && !left && !right) return 0;
  571.         if(!above && !below && !left && right) return 1;
  572.         if(!above && !below && left && !right) return 2;
  573.         if(!above && !below && left && right) return 3;
  574.         if(!above && below && !left && !right) return 4;
  575.         if(!above && below && !left && right) return 5;
  576.         if(!above && below && left && !right) return 6;
  577.         if(!above && below && left && right) return 7;
  578.         if(above && !below && !left && !right) return 8;
  579.         if(above && !below && !left && right) return 9;
  580.         if(above && !below && left && !right) return 10;
  581.         if(above && !below && left && right) return 11;
  582.         if(above && below && !left && !right) return 12;
  583.         if(above && below && !left && right) return 13;
  584.         if(above && below && left && !right) return 14;
  585.         if(above && below && left && right) return 15;
  586.         * /
  587.        
  588.        
  589.         // try 2 with bitmasking - SAME THING AS ABOVE BUT SO MUCH FASTER!!!!!
  590.         int total = 0;
  591.        
  592.         total += above ? 1 : 0;
  593.         total += below ? 2 : 0;
  594.         total += left ? 4 : 0;
  595.         total += right ? 8 : 0;
  596.        
  597.         return total;
  598.        
  599.     }
  600.    
  601.     private int determineWaterTexture(Vector2 vec) return tiles[vec].waterAbove ? 1 : 0;
  602.     */
  603.    
  604.     /*
  605.     private void updateSprite(Vector2 vec, bool isGrass = false, bool isWater = false)
  606.     {
  607.         // Unsure if | GetComponent(SpriteRenderer) | is faster, slower, or the same as what I did below! check!
  608.         // if(isGrass) tiles[vec].GetComponent<SpriteRenderer>().sprite = grassSprites[tiles[vec].grassSprite];
  609.         // else if(isWater) tiles[vec].GetComponent<SpriteRenderer>().sprite = waterSprites[tiles[vec].waterSprite];
  610.         /*
  611.         if(isGrass) GameObject.Find(vec.x + " , " + vec.y).GetComponent<SpriteRenderer>().sprite = grassSprites[tiles[vec].grassSprite];
  612.         else if(isWater && tiles[vec].waterSprite == 1)
  613.         {
  614.             Destroy(GameObject.Find(vec.x + " , " + vec.y));
  615.                        
  616.             GameObject objLoaded = Instantiate(waterTypeTwo, vec, Quaternion.identity, parent.transform);
  617.             objLoaded.transform.name = (vec.x + " , " + vec.y);
  618.             // GameObject.Find(vec.x + " , " + vec.y).GetComponent<Image>().sprite = waterSprites[tiles[vec].waterSprite]; // changed this to only change water if it is going to change to whole tile, since I don't want to have to recode the startup of the animation!
  619.         }
  620.         * /
  621.        
  622.         if(isGrass)
  623.         {
  624.             // GameObject.Find(vec.x + " , " + vec.y).GetComponent<SpriteRenderer>().sprite = grassSprites[tiles[vec].grassSprite];
  625.             bool aboveb = tiles.ContainsKey(vec + new Vector2(0, 1));
  626.             bool belowb = tiles.ContainsKey(vec + new Vector2(0, -1));
  627.             bool leftb = tiles.ContainsKey(vec + new Vector2(-1, 0));
  628.             bool rightb = tiles.ContainsKey(vec + new Vector2(1, 0));
  629.            
  630.             Vector2 abovev = vec + new Vector2(0, 1);
  631.             Vector2 belowv = vec + new Vector2(0, -1);
  632.             Vector2 leftv = vec + new Vector2(-1, 0);
  633.             Vector2 rightv = vec + new Vector2(1, 0);
  634.            
  635.             int total;
  636.            
  637.             if(aboveb && tiles[abovev].pos == 0)
  638.             {
  639.                 total = 0;
  640.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  641.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  642.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  643.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  644.                
  645.                 total = 15 - total;
  646.                 tiles[abovev].grassSprite = total;
  647.             }
  648.             if(belowb && tiles[belowv].pos == 0)
  649.             {
  650.                 total = 0;
  651.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  652.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  653.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  654.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  655.                
  656.                 total = 15 - total;
  657.                 tiles[abovev].grassSprite = total;
  658.             }
  659.             if(leftb && tiles[leftv].pos == 0)
  660.             {
  661.                 total = 0;
  662.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  663.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  664.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  665.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  666.                
  667.                 total = 15 - total;
  668.                 tiles[abovev].grassSprite = total;
  669.             }
  670.             if(rightb && tiles[rightv].pos == 0)
  671.             {
  672.                 total = 0;
  673.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  674.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  675.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  676.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  677.                
  678.                 total = 15 - total;
  679.                 tiles[abovev].grassSprite = total;
  680.             }
  681.            
  682.            
  683.            
  684.         }
  685.        
  686.         else if(isWater && tiles[vec].waterSprite == 1)
  687.         {
  688.             /*
  689.             Destroy(GameObject.Find(vec.x + " , " + vec.y));
  690.                        
  691.             GameObject objLoaded = Instantiate(waterTypeTwo, vec, Quaternion.identity, parent.transform);
  692.             objLoaded.transform.name = (vec.x + " , " + vec.y);
  693.             // GameObject.Find(vec.x + " , " + vec.y).GetComponent<Image>().sprite = waterSprites[tiles[vec].waterSprite]; // changed this to only change water if it is going to change to whole tile, since I don't want to have to recode the startup of the animation!
  694.             * /
  695.            
  696.             bool aboveb = tiles.ContainsKey(vec + new Vector2(0, 1));
  697.             bool belowb = tiles.ContainsKey(vec + new Vector2(0, -1));
  698.             bool leftb = tiles.ContainsKey(vec + new Vector2(-1, 0));
  699.             bool rightb = tiles.ContainsKey(vec + new Vector2(1, 0));
  700.            
  701.             Vector2 abovev = vec + new Vector2(0, 1);
  702.             Vector2 belowv = vec + new Vector2(0, -1);
  703.             Vector2 leftv = vec + new Vector2(-1, 0);
  704.             Vector2 rightv = vec + new Vector2(1, 0);
  705.            
  706.             int total;
  707.            
  708.             if(aboveb && tiles[abovev].pos == 0)
  709.             {
  710.                 total = 0;
  711.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 1 ? 1 : 0 : 0;
  712.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 1 ? 2 : 0 : 0;
  713.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 1 ? 4 : 0 : 0;
  714.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 1 ? 8 : 0 : 0;
  715.                
  716.                 total = 15 - total;
  717.                 tiles[abovev].grassSprite = total;
  718.             }
  719.             if(belowb && tiles[belowv].pos == 0)
  720.             {
  721.                 total = 0;
  722.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  723.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  724.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  725.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  726.                
  727.                 total = 15 - total;
  728.                 tiles[abovev].grassSprite = total;
  729.             }
  730.             if(leftb && tiles[leftv].pos == 0)
  731.             {
  732.                 total = 0;
  733.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  734.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  735.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  736.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  737.                
  738.                 total = 15 - total;
  739.                 tiles[abovev].grassSprite = total;
  740.             }
  741.             if(rightb && tiles[rightv].pos == 0)
  742.             {
  743.                 total = 0;
  744.                 total += tiles.ContainsKey(abovev + new Vector2(0, 1)) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  745.                 total += tiles.ContainsKey(abovev + new Vector2(0, -1)) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  746.                 total += tiles.ContainsKey(abovev + new Vector2(-1, 0)) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  747.                 total += tiles.ContainsKey(abovev + new Vector2(1, 0)) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  748.                
  749.                 total = 15 - total;
  750.                 tiles[abovev].grassSprite = total;
  751.             }
  752.         }
  753.     }
  754.     */
  755.    
  756.     private void updateSprite(Vector2 vec) // ********* WIP *********
  757.     {
  758.         /* bool aboveb = tiles.ContainsKey(vec + new Vector2(0, 1));
  759.         bool belowb = tiles.ContainsKey(vec + new Vector2(0, -1));
  760.         bool leftb = tiles.ContainsKey(vec + new Vector2(-1, 0));
  761.         bool rightb = tiles.ContainsKey(vec + new Vector2(1, 0));
  762.        
  763.         Vector2 abovev = vec + new Vector2(0, 1);
  764.         Vector2 belowv = vec + new Vector2(0, -1);
  765.         Vector2 leftv = vec + new Vector2(-1, 0);
  766.         Vector2 rightv = vec + new Vector2(1, 0);
  767.        
  768.         if(aboveb && tiles[abovev].pos == 0) updateSpriteGrass(abovev); // Might not work
  769.         else if(aboveb && tiles[abovev].pos == 1) updateSpriteWater(abovev); // Might not work
  770.        
  771.         if(belowb && tiles[belowv].pos == 0) updateSpriteGrass(belowv); // Might not work
  772.         else if(belowb && tiles[belowv].pos == 1) updateSpriteWater(belowv); // Might not work
  773.        
  774.         if(leftb && tiles[leftv].pos == 0) updateSpriteGrass(leftv); // Might not work
  775.         else if(leftb && tiles[leftv].pos == 1) updateSpriteWater(leftv); // Might not work
  776.        
  777.         if(rightb && tiles[rightv].pos == 0) updateSpriteGrass(rightv); // Might not work
  778.         else if(rightb && tiles[rightv].pos == 1) updateSpriteWater(rightv); // Might not work
  779.         */
  780.        
  781.        
  782.        
  783.         // if(tiles[vec].pos == 0) updateSpriteGrass(vec);
  784.         // else if(tiles[vec].pos == 1) updateSpriteWater(vec);
  785.  
  786.         if(!tiles[vec].spriteUpdated)
  787.         {
  788.             if(tiles[vec].tileType == 0) tiles[vec].spriteUpdated = true;
  789.             else if(tiles[vec].tileType == 1) updateSpriteGrass(vec);
  790.             else if(tiles[vec].tileType == 2) updateSpriteWater(vec);
  791.  
  792.             /*Vector2 above = vec + new Vector2(0, 1);
  793.             Vector2 below = vec + new Vector2(0, -1);
  794.             Vector2 left = vec + new Vector2(-1, 0);
  795.             Vector2 right = vec + new Vector2(1, 0);
  796.  
  797.             if(tiles.ContainsKey(above) && tiles.ContainsKey(below) && tiles.ContainsKey(left) && tiles.ContainsKey(right))
  798.             {
  799.  
  800.             }*/
  801.  
  802.  
  803.         }
  804.         /*if(tiles[vec].spriteUpdated)
  805.         {
  806.  
  807.         }*/
  808.     }
  809.    
  810.     private void updateSpriteGrass(Vector2 vec)
  811.     {
  812.         Vector2 above = vec + new Vector2(0, 1);
  813.         Vector2 below = vec + new Vector2(0, -1);
  814.         Vector2 left = vec + new Vector2(-1, 0);
  815.         Vector2 right = vec + new Vector2(1, 0);
  816.        
  817.         if(tiles.ContainsKey(above) && tiles.ContainsKey(below) && tiles.ContainsKey(left) && tiles.ContainsKey(right))
  818.         {
  819.             int total = 0;
  820.            
  821.             // total += tiles.ContainsKey(above) ? tiles[above].pos == 0 ? 1 : 0 : 0;
  822.             // total += tiles.ContainsKey(below) ? tiles[below].pos == 0 ? 2 : 0 : 0;
  823.             // total += tiles.ContainsKey(left) ? tiles[left].pos == 0 ? 4 : 0 : 0;
  824.             // total += tiles.ContainsKey(right) ? tiles[right].pos == 0 ? 8 : 0 : 0;
  825.            
  826.             total += tiles[above].pos == 0 ? 1 : 0;
  827.             total += tiles[below].pos == 0 ? 2 : 0;
  828.             total += tiles[left].pos == 0 ? 4 : 0;
  829.             total += tiles[right].pos == 0 ? 8 : 0;
  830.  
  831.  
  832.             total = 15 - total;
  833.  
  834.             tiles[vec].grassSprite = total;
  835.  
  836.             tiles[vec].spriteUpdated = true;
  837.            
  838.            
  839.            
  840.             // if(GameObject.Find(vec.x + " , " + vec.y) && tiles[vec].pos == 0) GameObject.Find(vec.x + " , " + vec.y).GetComponent<SpriteRenderer>().sprite = grassSprites[tiles[vec].grassSprite];
  841.  
  842.         }
  843.        
  844.        
  845.        
  846.        
  847.        
  848.         //  **** how to update this type of sprite!
  849.         // if(GameObject.Find(vec.x + " , " + vec.y) && tiles[vec].pos == 0) GameObject.Find(vec.x + " , " + vec.y).GetComponent<SpriteRenderer>().sprite = grassSprites[tiles[vec].grassSprite];
  850.     }
  851.    
  852.     private void updateSpriteWater(Vector2 vec)
  853.     {
  854.         Vector2 above = vec + new Vector2(0, 1);
  855.        
  856.         // tiles[vec].waterSprite = tiles.ContainsKey(above) ? tiles[above].pos == 1 ? 1 : 0 : 0;
  857.         if(tiles.ContainsKey(above))
  858.         {
  859.             tiles[vec].waterSprite = tiles[above].pos == 1 ? 1 : 0;
  860.             tiles[vec].spriteUpdated = true;
  861.            
  862.            
  863.            
  864.             /*if(GameObject.Find(vec.x + " , " + vec.y))
  865.             {
  866.        
  867.             Destroy(GameObject.Find(vec.x + " , " + vec.y));
  868.            
  869.             GameObject objLoaded = Instantiate(waterTypeTwo, vec, Quaternion.identity, parent.transform);
  870.             objLoaded.transform.name = (vec.x + " , " + vec.y);
  871.        
  872.             }*/
  873.         }
  874.        
  875.        
  876.        
  877.        
  878.         /* **** THIS IS HOW you update this type!
  879.         if(GameObject.Find(vec.x + " , " + vec.y))
  880.         {
  881.        
  882.             Destroy(GameObject.Find(vec.x + " , " + vec.y));
  883.            
  884.             GameObject objLoaded = Instantiate(waterTypeTwo, vec, Quaternion.identity, parent.transform);
  885.             objLoaded.transform.name = (vec.x + " , " + vec.y);
  886.        
  887.         }
  888.         */
  889.     }
  890.    
  891.     public void loadObj(Vector2 vec, int pos)
  892.     {
  893.         if(tiles[vec].spriteUpdated)
  894.         {
  895.             // GameObject objLoaded = Instantiate(items[pos], vec, Quaternion.identity, parent.transform);
  896.             // objLoaded.transform.name = (vec.x + " , " + vec.y); // Add the change of sprites stuff
  897.             if(tiles[vec].tileType == 2)
  898.             {
  899.                 GameObject objLoaded = Instantiate(waterTypeTwo, vec, Quaternion.identity, parent.transform);
  900.                 objLoaded.transform.name = (vec.x + " , " + vec.y);
  901.                 return;
  902.             }
  903.  
  904.             GameObject objLoaded2 = Instantiate(items[pos], vec, Quaternion.identity, parent.transform);
  905.             objLoaded2.transform.name = (vec.x + " , " + vec.y);
  906.             if(tiles[vec].tileType == 1) objLoaded2.GetComponent<SpriteRenderer>().sprite = grassSprites[tiles[vec].grassSprite];
  907.         }
  908.         else
  909.         {
  910.             objectsWaitingToLoad.Add(vec, true);
  911.         }
  912.     }
  913.  
  914.     public void workOnObjectsWaitingToLoad()
  915.     {
  916.         Vector2 playerPos = (Vector2)player.transform.position;
  917.         Dictionary<Vector2, bool> toRemove = new Dictionary<Vector2, bool>();
  918.         foreach(KeyValuePair<Vector2, bool> item in objectsWaitingToLoad)
  919.         {
  920.             if(tiles[item.Key].spriteUpdated)
  921.             {
  922.                 loadObj(item.Key, tiles[item.Key].pos);
  923.                 toRemove.Add(item.Key, true);
  924.             }
  925.             else
  926.             {
  927.                 // find all the or statements for being outside of render distance then add to toRemove dict
  928.                 if( item.Key.x > halfRenderDistanceX + (int)playerPos.x ||
  929.                 item.Key.x < -halfRenderDistanceX + (int)playerPos.x ||
  930.                 item.Key.y > halfRenderDistanceY + (int)playerPos.y ||
  931.                 item.Key.y < -halfRenderDistanceY + (int)playerPos.y )
  932.                     toRemove.Add(item.Key, true);
  933.             }
  934.         }
  935.        
  936.         foreach(KeyValuePair<Vector2, bool> item in toRemove) objectsWaitingToLoad.Remove(item.Key);
  937.         // print(objectsWaitingToLoad.Count);
  938.     }
  939.  
  940.     public void destroyObj(Vector2 vec)
  941.     {
  942.         Destroy(GameObject.Find(vec.x + " , " + vec.y));
  943.     }
  944.    
  945.     private IEnumerator changeTree(Vector2 vec) // Has to yield return something, even if you return null
  946.     {
  947.         print("IENUMERATOR STARTED!");
  948.         Vector2 playerPos = (Vector2)player.transform.position;
  949.         GameObject prefab = GameObject.Find(vec.x + " , " + vec.y);
  950.         playerPos = new Vector2(Mathf.Round(playerPos.x), Mathf.Round(playerPos.y));
  951.         while(playerPos - new Vector2(0,1) == vec || playerPos - new Vector2(0,2) == vec)
  952.         {
  953.             playerPos = (Vector2)player.transform.position;
  954.             playerPos = new Vector2(Mathf.Round(playerPos.x), Mathf.Round(playerPos.y));
  955.             yield return new WaitForSeconds(0.1f);
  956.             // Renderer renderer = GameObject.Find(vec.x + " , " + vec.y).GetComponent<Renderer>();
  957.             // float alpha = GameObject.Find(vec.x + " , " + vec.y).GetComponent<Renderer>().material.color.a;
  958.             //GameObject prefab = GameObject.Find(vec.x + " , " + vec.y);
  959.  
  960.             // Dictionary<int, Transform> children = new Dictionary<int, Transform>();
  961.  
  962.             // int count = 0;
  963.  
  964.             foreach(Transform child in prefab.transform)
  965.             {
  966.                 // children.Add(count, child);
  967.                 // count++;
  968.  
  969.                 // child.GetComponent<SpriteRenderer>().color = new Color(0.1f, 0.1f, 0.1f);
  970.                 float alpha = child.GetComponent<SpriteRenderer>().color.a;
  971.                 // print("alpha one: " + alpha);
  972.                 if(alpha > 0.2f) child.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, Mathf.Lerp(alpha, 0.2f, 20 * Time.deltaTime));
  973.             }
  974.  
  975.             // float alpha = children[0].GetComponent<Renderer>().material.color.a;
  976.             /*
  977.             if(alpha > 0.2f)
  978.             {
  979.                 // float newAlpha = Mathf.Lerp(alpha, 0.2f, 2 * Time.deltaTime);
  980.                 // children[0].GetComponent<Renderer>().material.color.a = newAlpha;
  981.                 // Renderer renderer = children[2].GetComponent<Renderer>();
  982.                 // renderer.material.color.a = Mathf.Lerp(alpha, 0.2f, 2 * Time.deltaTime);
  983.                 //children[2].alpha =
  984. ]           }*/
  985.         }
  986.  
  987.         float alpha2 = 0.0f;
  988.         while(alpha2 < 0.95f)
  989.         {
  990.             yield return new WaitForSeconds(0.1f);
  991.             // GameObject prefab = GameObject.Find(vec.x + " , " + vec.y);
  992.             foreach(Transform child in prefab.transform)
  993.             {
  994.                 alpha2 = child.GetComponent<SpriteRenderer>().color.a;
  995.                 // print("alpha two: " + alpha2);
  996.                 child.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, Mathf.Lerp(alpha2, 1.0f, 20 * Time.deltaTime));
  997.             }
  998.             // float alpha = GameObject.Find(vec.x + " , " + vec.y).GetComponent<Renderer>().material.color.a;
  999.             // alpha =
  1000.            
  1001.             // if(alpha < 1.0f)
  1002.             // {
  1003.             //  alpha =
  1004.             // }
  1005.             // else yield return null;
  1006.         }
  1007.        
  1008.         // GameObject prefab = GameObject.Find(vec.x + " , " + vec.y);
  1009.         foreach(Transform child in prefab.transform) child.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
  1010.        
  1011.         tilesWithCoroutines.Remove(vec);
  1012.         print("COMPLETE!");
  1013.         yield return null;
  1014.     }
  1015. }
  1016.  
  1017. /* ==========================================
  1018.  * |[-] = not started, [*] = WIP, [+] = done|
  1019.  * ------------------------------------------
  1020.  * stuff to add:
  1021.  * ------------------------------------------
  1022.  * ******** Grass textures aren't updating right, because most of the time tile to the left and above it haven't loaded, so I need to make them wait until the next row loads!
  1023.  * [*+] **** need to fix the fact that tiles get assigned the wrong sprite when loading, because stuff to the left or right and below of them aren't loaded yet, so they need to wait to update sprite until other sprites aroun them load![(code seems to be almost complete, BUT it needs to be tested for glitches / to see if it is working!!)] tile connectivity (grass connecting, water connecting (there are two tiles for water, whole or wave) )
  1024.  * [*+] [(doubled speed, BUT need to test if it is still too slow/fast)] increase player movement speed
  1025.  * [-] make sure grass tiles start showing behind all stuff (trees, etc) - instead of making other grass tile with rocks and trees, make a unique grass or stone behind them that stay there after you destroy the tree/rock, and will regen within time
  1026.  * [+] [(code seems to be complete, BUT it needs to be tested for glitches / to see if it is working!!)] re-implement the spreading of water tiles
  1027.  * [-] add optimization, like re-implementing object pool script, which is more efficient than Instantiating and Deleting objects
  1028.  * [**] make sure to NOT use lists, especially when trying to find a term inside of it, ALWAYS use a dict in that case (a dict uses a hash table to optimize speed of finding information
  1029.  * [-] make the graphics look better, like changing the green of the grass and trees and 60+ fps to contrast from each other more
  1030.  * [*+] [(needs to be tested, also only done to trees so far!)] make obstacles, buildings, trees, etc. go semi-transparent when player goes behind them *** might want to use sprite renderer component .color and use alpha and lerp from 1.0f to 0.2f or something like that! ***
  1031.  * *********** MAKE SURE TO change how I fixed the updating of tile sprites from updating all four around it to putting it into a list, once that side is stepped over to even further, then update the sprites since there will now be stuff all tiles loaded in all four directions!! **************
  1032.  * [-] make sure to FIX trees, as in down one and two can each run a coroutine on the same tile, so create a list of tiles already being affected by a coroutine I guess.  Also find a way to fix sudden snaps to low or high opacity of trees, most likely because of two coroutines running at the same time?
  1033.  * [*] FIX water tiles loading, also FIX water not being unloaded, and tiles of water overlapping!!
  1034.  * [*] MIGHT have to revert code to the way that was working but you'd have to leave and re-enter the area for the sprites to update and retry coding!! (***Make sure if you revert, to re-implement the coroutines for trees!***)
  1035.  *
  1036.  * ***************************** WAIT what if I check if something connected to each tile is null, if it is, if all tiles around it are instantiated, then update the sprite?!?!?!?!
  1037.  * *** NOW THAT I HAVE FIXED THE SPRITE UPDATING PART, make sure that I create a list of tiles that are waiting to get loaded once tilesUpdated == true, and make sure to in the main code running script that loads objects to remove all the ones it is able to load from the list!
  1038.  * ==========================================
  1039.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement