usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicabstractclassExWhy{publicExWhy(intxMx,intyMx,stringresName){xMax=xMx;yMax=yMx;resourceName=resName;gridCells=newExWhyCell[xMax,yMax];spriteSheet=newSprite[16,16];loadPrefabs();loadSpriteMap();}//The dimensions of the gridpublicint xMax;publicint yMax;//These hold the gameObject PRefabspublicGameObject[] tilePrefabs;publicGameObject[] iconPrefabs;//This variable holds the basic cell or "tile" data for a world. It's stored it chars, so to make it easy to create and edit maps with little to know coding knowledgepublicchar[,] worldData;//This variable holds events. Each event, such as non random battle, shops or story events have a number. 0 is used to denote no event. //This should be the same dimensions as world data. publicint[,] eventMask;//The this holds references to the grid cells one they are created.publicExWhyCell[,] gridCells;//These variables are used to find and load sprites.publicSprite[,] spriteSheet;publicstring resourceName;//Function to load the sprites of the grid from Unity's "resource" folder into memory//Each tile type should have 16 spritespublicvoidloadSpriteMap(){inty=0;foreach (GameObjecttpintilePrefabs){for (intx=0;x<16;x++){//GameObject.Find("globalGameController").GetComponent<globalGameController>().notPrint("OverworldTiles/sprites/" + resourceName + "/" + tp.name + (x + 1));//OverworldTiles / sprites / arena / floorSprites1.pngspriteSheet[y,x] =Resources.Load<Sprite>("MapTiles/Sprites/"+resourceName+"/"+tp.name+ (x+1));//+ (x + 1)); //OverworldTiles/sprites/arena/floorSpritesfloorSprites.png}y=y+1;}}publicvoidloadPrefabs(){tilePrefabs=Resources.LoadAll<GameObject>("MapTiles/Prefabs/"+resourceName);}//Place Holder. This should be overriden in the implementationpublicvirtualvoidinitiateCells(){for (intx=0;x<xMax;x=x+1){for (inty=0;y<yMax;y=y+1){gridCells[x,y] =newExWhyCell(x,y,worldData[x,y],0);//eventMask[y, x]);}}}protectedvoidinstantiateCell(ExWhyCellcell,boolwalkable,intprefabIndex,intspriteID){cell.cellGO=GameObject.Instantiate(tilePrefabs[prefabIndex],newVector3(cell.xPosition,cell.yPosition,1) *4,newQuaternion());cell.walkable=walkable;cell.spriteID=spriteID;}publicabstractvoidinstantiateCells();publicabstractvoidinstantiateEvents();//This is the script to instantiate the grid in the game world//It reads each cell in a grid, then "draws" it in the game worldpublicvoiddrawGrid(){instantiateCells();// instantiateEvents();//This section gets the consolidation ID. In other words, checks the cell type around each cell, and gives it a different sprite depending. //For instance, if you have a water cell next to a grass cell, it shows a shore line on the water sprite. foreach (ExWhyCellcellingridCells){cell.colID=getConsolidationID(this,cell.xPosition,cell.yPosition);consolidate(cell);}}//This function is used to trigger an event. publicabstractvoideventTest(inteventIndex);publicvoidconsolidate(ExWhyCellcell){cell.cellGO.GetComponent<SpriteRenderer>().sprite=spriteSheet[cell.spriteID,cell.colID];}//Now, fam, this isn't necessarily the best way. We could use a hypotenuse, but for now, we doing this. publicstaticintgetDistanceBetweenCells(ExWhyCellcell1,ExWhyCellcell2){return (Math.Abs(cell1.xPosition-cell2.xPosition) +Math.Abs(cell1.yPosition-cell2.yPosition));}//This bit of code may look complicated, but it's simple. It gets a cell in a grid, then checks the surrounding cells to see whether they're the same type. // If you want some grass next to some water, then it'll show a shoreline, instead of the two sprites next to each other. publicstaticintgetConsolidationID(ExWhygs,intx,inty){chartileChar=gs.worldData[x,y];//Likesboolup;boolright;booldown;boolleft;intoutput=0;if (x==0){left=true;}else{left= (gs.worldData[x-1,y] ==tileChar);}if (y==0){down=true;}else{down= (gs.worldData[x,y-1] ==tileChar);}if (x+1==gs.xMax){right=true;}else{right= (gs.worldData[x+1,y] ==tileChar);}if (y+1==gs.yMax){up=true;}else{up= (gs.worldData[x,y+1] ==tileChar);}if (!up){output+=1;}if (!down){output+=2;}if (!left){output+=4;}if (!right){output+=8;}/* //This is the old code, left here just in case. //Up Down Left Right //Centre if (up && down && left && right) { return 0; } //Up Edge if (!up && down && left && right) { return 1; } //Down Edge if (up && !down && left && right) { return 2; } //Left Edge if (up && down && !left && right) { return 3; } //Right Edge if (up && down && left && !right) { return 4; } //Upper Left Edge if (!up && down && !left && right) { return 5; } //Upper Right Edge if (!up && down && left && !right) { return 6; } //Down Left Edge if (up && !down && !left && right) { return 7; } //Down Right Edge if (up && !down && left && !right) { return 8; } //Down Pocket if (!up && down && !left && !right) { return 9; } //Down Pocket if (up && !down && !left && !right) { return 10; } //Left Pocket if (!up && !down && !left && right) { return 11; } //Right Pocket if (!up && !down && left && !right) { return 12; } //Up Hallways if (!up && !down && left && right) { return 13; } //Side Hallways if (up && down && !left && !right) { return 14; } if (!up && !down && !left && !right) { return 15; } */returnoutput;}}