Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- //@HideInInspector
- var verts:Vector3[]= new Vector3[0];
- //@HideInInspector
- var tris:int[]= new int[0];
- //@HideInInspector
- var uvs:Vector2[] = new Vector2[0];
- var size:int;
- var tilesetSize:float;//4,8,16
- var epsilon: float;
- var emptyObject:Transform;
- function Start ()
- {
- CreateRectangularStarterRoom();
- }
- function Update ()
- {
- }
- var vertsTier:int;
- var trisTier:int;
- function CreateRectangularStarterRoom()
- {
- var roomSize:Vector3=Vector3(Mathf.RoundToInt(Random.Range(6,12)),Mathf.RoundToInt(Random.Range(2,2)),Mathf.RoundToInt(Random.Range(6,12)));
- print("roomSize= "+roomSize);
- var numberOfDoors:int=Random.Range(4,5);
- var numberOfVerts:int=(roomSize.x*roomSize.z*2*4)+(roomSize.x*roomSize.y*2*4)+(roomSize.z*roomSize.y*2*4);//-(numberOfDoors*2*4);
- verts=new Vector3[numberOfVerts];
- var numberOfTris:int=(roomSize.x*roomSize.z*2*2*3)+(roomSize.x*roomSize.y*2*2*3)+(roomSize.z*roomSize.y*2*2*3);//-(numberOfDoors*2*2*3);
- tris=new int[numberOfTris];
- uvs=new Vector2[numberOfVerts];
- var doorLocations:Vector3[]=new Vector3[numberOfDoors];
- var doorsSet:int;
- while(doorsSet<numberOfDoors)
- {
- var farOrNear:int=Random.Range(0,2);
- var xorz:int=Random.Range(0,2);
- var newDoor:Vector3=new Vector3();
- if(xorz==0) //along x axis
- {
- var doNotSetA:boolean=false;
- newDoor=Vector3(Mathf.RoundToInt(Random.Range(1,roomSize.x-2)),0,Mathf.RoundToInt(Random.Range(62,64)));
- for(var i=0;i<doorsSet;i++)
- {
- if(doorLocations[i].z==newDoor.z)
- {
- print("1A= "+newDoor);
- if(Mathf.Abs(doorLocations[i].x-newDoor.x)<=1)
- {
- print("2A= "+newDoor);
- doNotSetA=true;
- }
- }
- }
- if(!doNotSetA)
- {
- doorLocations[doorsSet]=newDoor;
- print("door "+doorsSet+" "+doorLocations[doorsSet]);
- doorsSet++;
- }
- }
- else
- {
- var doNotSetB:boolean=false;
- newDoor=Vector3(Mathf.RoundToInt(Random.Range(62,64)),0,Mathf.RoundToInt(Random.Range(1,roomSize.z-2)));
- for(var j=0;j<doorsSet;j++)
- {
- if(doorLocations[j].x==newDoor.x)
- {
- print("1B= "+newDoor);
- if(Mathf.Abs(doorLocations[j].z-newDoor.z)<=1)
- {
- print("2B= "+newDoor);
- doNotSetB=true;
- }
- }
- }
- if(!doNotSetB)
- {
- doorLocations[doorsSet]=newDoor;
- print("door "+doorsSet+" "+doorLocations[doorsSet]);
- doorsSet++;
- }
- }
- }
- CreateWall(0,Mathf.FloorToInt(roomSize.x*roomSize.z*4),size,0,0,0,size,0,0,0,0,[4,17,0],[2,1,0,3,2,0],[true,false,false,false,false,true],Mathf.FloorToInt(roomSize.x));
- CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.x*roomSize.z*4),size,0,0,0,size,0,0,roomSize.y,0,[0,16,0],[0,1,2,0,2,3],[true,false,false,false,false,true],Mathf.FloorToInt(roomSize.x));
- var xStart1:int=vertsTier;
- CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.x*roomSize.y*4),0,size,size,0,0,0,0,0,0,[6,4,0],[0,1,2,0,2,3],[true,false,false,false,true,false],Mathf.FloorToInt(roomSize.x));
- var xStart2:int=vertsTier;
- CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.x*roomSize.y*4),0,size,size,0,0,0,0,0,roomSize.z,[6,4,0],[2,1,0,3,2,0],[true,false,false,false,true,false],Mathf.FloorToInt(roomSize.x));
- var zStart1:int=vertsTier;
- CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.y*roomSize.z*4),0,0,0,size,0,size,0,0,0,[6,4,1],[0,1,2,0,2,3],[false,false,true,false,true,false],Mathf.FloorToInt(roomSize.z));
- var zStart2:int=vertsTier;
- CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+roomSize.y*roomSize.z*4),0,0,0,size,0,size,roomSize.x,0,0,[6,4,1],[2,1,0,3,2,0],[false,false,true,false,true,false],Mathf.FloorToInt(roomSize.z));
- var hallwayStarts:Vector3[]=new Vector3[doorLocations.length];
- for(var z=0;z<doorLocations.length;z++)//make doors
- {
- if(doorLocations[z].x>0&&doorLocations[z].x<60)
- {
- if(doorLocations[z].z==62)
- {
- hallwayStarts[z]=verts[xStart1+doorLocations[z].x*4];
- print("hallwayStarts[z]= "+hallwayStarts[z]);
- verts[xStart1+doorLocations[z].x*4]=Vector3.zero;
- verts[xStart1+doorLocations[z].x*4+1]=Vector3.zero;
- verts[xStart1+doorLocations[z].x*4+2]=Vector3.zero;
- verts[xStart1+doorLocations[z].x*4+3]=Vector3.zero;
- /* verts[xStart1+doorLocations[z].x*4+roomSize.x*4]=Vector3.zero;
- verts[xStart1+doorLocations[z].x*4+roomSize.x*4+1]=Vector3.zero;
- verts[xStart1+doorLocations[z].x*4+roomSize.x*4+2]=Vector3.zero;
- verts[xStart1+doorLocations[z].x*4+roomSize.x*4+3]=Vector3.zero; */
- }
- if(doorLocations[z].z==63)
- {
- hallwayStarts[z]=verts[xStart2+doorLocations[z].x*4];
- verts[xStart2+doorLocations[z].x*4]=Vector3.zero;
- verts[xStart2+doorLocations[z].x*4+1]=Vector3.zero;
- verts[xStart2+doorLocations[z].x*4+2]=Vector3.zero;
- verts[xStart2+doorLocations[z].x*4+3]=Vector3.zero;
- /* verts[xStart2+doorLocations[z].x*4+roomSize.x*4]=Vector3.zero;
- verts[xStart2+doorLocations[z].x*4+roomSize.x*4+1]=Vector3.zero;
- verts[xStart2+doorLocations[z].x*4+roomSize.x*4+2]=Vector3.zero;
- verts[xStart2+doorLocations[z].x*4+roomSize.x*4+3]=Vector3.zero; */
- }
- }
- if(doorLocations[z].z>0&&doorLocations[z].z<60)
- {
- if(doorLocations[z].x==62)
- {
- hallwayStarts[z]=verts[zStart1+doorLocations[z].z*4];
- verts[zStart1+doorLocations[z].z*4]=Vector3.zero;
- verts[zStart1+doorLocations[z].z*4+1]=Vector3.zero;
- verts[zStart1+doorLocations[z].z*4+2]=Vector3.zero;
- verts[zStart1+doorLocations[z].z*4+3]=Vector3.zero;
- /* verts[zStart1+doorLocations[z].z*4+roomSize.z*4]=Vector3.zero;
- verts[zStart1+doorLocations[z].z*4+roomSize.z*4+1]=Vector3.zero;
- verts[zStart1+doorLocations[z].z*4+roomSize.z*4+2]=Vector3.zero;
- verts[zStart1+doorLocations[z].z*4+roomSize.z*4+3]=Vector3.zero; */
- }
- if(doorLocations[z].x==63)
- {
- hallwayStarts[z]=verts[zStart2+doorLocations[z].z*4];
- verts[zStart2+doorLocations[z].z*4]=Vector3.zero;
- verts[zStart2+doorLocations[z].z*4+1]=Vector3.zero;
- verts[zStart2+doorLocations[z].z*4+2]=Vector3.zero;
- verts[zStart2+doorLocations[z].z*4+3]=Vector3.zero;
- /* verts[zStart2+doorLocations[z].z*4+roomSize.z*4]=Vector3.zero;
- verts[zStart2+doorLocations[z].z*4+roomSize.z*4+1]=Vector3.zero;
- verts[zStart2+doorLocations[z].z*4+roomSize.z*4+2]=Vector3.zero;
- verts[zStart2+doorLocations[z].z*4+roomSize.z*4+3]=Vector3.zero; */
- }
- }
- }
- var newMesh:Mesh=transform.GetComponent(MeshFilter).mesh;
- newMesh.Clear();
- newMesh.vertices=verts;
- newMesh.triangles=tris;
- newMesh.uv=uvs;
- newMesh.RecalculateNormals();
- transform.GetComponent(MeshCollider).sharedMesh=newMesh;
- for(var a=0;a<doorLocations.length;a++)
- {
- if(doorLocations[a].z==62)
- {
- CreateHallway(hallwayStarts[a],0);
- }
- if(doorLocations[a].z==63)
- {
- CreateHallway(hallwayStarts[a],1);
- }
- if(doorLocations[a].x==62)
- {
- }
- if(doorLocations[a].x==63)
- {
- }
- }
- }
- var slow:boolean;
- function CreateHallway(hallStart:Vector3,dir:int)
- {
- print("hallStart= "+hallStart+" dir= "+dir);
- var hall:Transform=Instantiate(emptyObject,Vector3(0,0,0),Quaternion.identity);
- var hallLength:int=Random.Range(2,8);
- var numberOfVerts:int=(hallLength*16);
- verts=new Vector3[numberOfVerts];
- var numberOfTris:int=(hallLength*8*3);
- tris=new int[numberOfTris];
- uvs=new Vector2[numberOfVerts];
- vertsTier=0;
- trisTier=0;
- switch (dir)
- {
- case 0:
- CreateWall(0,Mathf.FloorToInt(hallLength*4),size,0,0,0,size,0,hallStart.x/size,hallStart.y/size,hallStart.z/size-hallLength,[4,17,0],[2,1,0,3,2,0],[true,false,false,false,false,true],1);
- print("side hall starts at "+vertsTier);
- CreateWall(vertsTier,Mathf.FloorToInt(vertsTier+hallLength*4),0,0,size,0,size,0,hallStart.x/size+1,hallStart.y/size,hallStart.z/size-hallLength,[6,4,0],[0,1,2,0,2,3],[false,true,false,false,false,true],1);
- print("side hall ends at "+vertsTier);
- break;
- case 1:
- CreateWall(0,Mathf.FloorToInt(hallLength*4),size,0,0,0,size,0,hallStart.x/size,hallStart.y/size,hallStart.z/size,[4,17,0],[2,1,0,3,2,0],[true,false,false,false,false,true],1);
- break;
- }
- var newMesh:Mesh=hall.GetComponent(MeshFilter).mesh;
- newMesh.Clear();
- newMesh.vertices=verts;
- newMesh.triangles=tris;
- newMesh.uv=uvs;
- newMesh.RecalculateNormals();
- hall.GetComponent(MeshCollider).sharedMesh=newMesh;
- print("hallways done");
- }
- function CreateWall(vertStart:int,vertEnd:int,incXa:int,incXb:int,incYa:int,incYb:int,incZa:int,incZb:int,xStart:int,yStart:int,zStart:int,uv:int[],trisIncrease:int[],along:boolean[],width:int)
- {
- var uvX:Vector4;
- var uvY:Vector4;
- if(uv[2]==0)//bottom left clockwise
- {
- uvX=Vector4((uv[0]+0)/tilesetSize+epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+1)/tilesetSize-epsilon);
- uvY=Vector4((uv[1]+0)/tilesetSize+epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+0)/tilesetSize+epsilon);
- }
- if(uv[2]==1)//top left clockwise
- {
- uvX=Vector4((uv[0]+0)/tilesetSize+epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+0)/tilesetSize+epsilon);
- uvY=Vector4((uv[1]+1)/tilesetSize-epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+0)/tilesetSize+epsilon);
- }
- if(uv[2]==2)//top right clockwise
- {
- uvX=Vector4((uv[0]+1)/tilesetSize-epsilon,(uv[0]+1)/tilesetSize-epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+0)/tilesetSize+epsilon);
- uvY=Vector4((uv[1]+1)/tilesetSize-epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+1)/tilesetSize-epsilon);
- }
- if(uv[2]==3)//bottom right clockwise
- {
- uvX=Vector4((uv[0]+1)/tilesetSize-epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+0)/tilesetSize+epsilon,(uv[0]+1)/tilesetSize-epsilon);
- uvY=Vector4((uv[1]+0)/tilesetSize+epsilon,(uv[1]+0)/tilesetSize+epsilon,(uv[1]+1)/tilesetSize-epsilon,(uv[1]+1)/tilesetSize-epsilon);
- }
- var xTier:int;
- var yTier:int;
- var zTier:int;
- for(var i=vertStart;i<vertEnd;i+=4)
- {
- verts[i+0]=Vector3(xTier*size+xStart*size+incXa, yTier*size+yStart*size, zTier*size+zStart*size);
- verts[i+1]=Vector3(xTier*size+xStart*size+incXa+incXb, yTier*size+yStart*size+incYb, zTier*size+zStart*size+incZa);
- verts[i+2]=Vector3(xTier*size+xStart*size+incXb, yTier*size+yStart*size+incYa+incYb, zTier*size+zStart*size+incZa+incZb);
- verts[i+3]=Vector3(xTier*size+xStart*size, yTier*size+yStart*size+incYa, zTier*size+zStart*size+incZb);
- uvs[i+0]=Vector2(uvX.w,uvY.w);
- uvs[i+1]=Vector2(uvX.x,uvY.x);
- uvs[i+2]=Vector2(uvX.y,uvY.y);
- uvs[i+3]=Vector2(uvX.z,uvY.z);
- tris[trisTier+0]=i+trisIncrease[0]; tris[trisTier+1]=i+trisIncrease[1]; tris[trisTier+2]=i+trisIncrease[2];
- tris[trisTier+3]=i+trisIncrease[3]; tris[trisTier+4]=i+trisIncrease[4]; tris[trisTier+5]=i+trisIncrease[5];
- trisTier+=6;
- vertsTier+=4;
- if(along[0])
- {
- xTier++;
- if(xTier>=width)
- {
- xTier=0;
- if(along[4])
- {
- yTier++;
- }
- if(along[5])
- {
- zTier++;
- }
- }
- }
- if(along[1])
- {
- yTier++;
- if(yTier>=width)
- {
- yTier=0;
- if(along[3])
- {
- xTier++;
- }
- if(along[5])
- {
- zTier++;
- }
- }
- }
- if(along[2])
- {
- zTier++;
- if(zTier>=width)
- {
- zTier=0;
- if(along[3])
- {
- xTier++;
- }
- if(along[4])
- {
- yTier++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment