Advertisement
Bcadren

All LD Code

Apr 19th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //LEVELLORD (File0): Controls pretty much everything.
  2.  
  3. private var newVertices : Vector3[];
  4. private var newUV : Vector2[];
  5. private var newTriangles : int[];
  6.  
  7. //local
  8. private var verts : Array;
  9. private var UVs : Array;
  10. private var triangles : Array;
  11. private var currentLength : int;
  12.  
  13. //universal
  14. private var totalVerts : Array;
  15. private var totalUVs : Array;
  16. private var totalTriangles : Array;
  17.  
  18. private var mesh : Mesh;
  19. var lines : GameObject;
  20. private var toggle : boolean;
  21. private var b : float;
  22. private var enumeration : int;
  23. var player : GameObject;
  24. var level : Texture2D;
  25.  
  26. function Start () {
  27.     enumeration = 0;
  28.  
  29.     totalVerts = new Array();
  30.     totalUVs = new Array();
  31.     totalTriangles = new Array();
  32.    
  33.     loadLevel ();
  34.     mesh = new Mesh ();
  35.    
  36.     /*flat (Vector2(1,1),3);
  37.     halfPipe(Vector2(0,0), Dir.Right, 2);*/
  38.    
  39.     newVertices = totalVerts.ToBuiltin(Vector3);
  40.     newUV = totalUVs.ToBuiltin(Vector2);
  41.     newTriangles = totalTriangles.ToBuiltin(int);
  42.    
  43.     mesh.vertices = newVertices;
  44.     mesh.uv = newUV;
  45.     mesh.triangles = newTriangles;
  46.    
  47.     mesh.RecalculateNormals();
  48.    
  49.     GetComponent.<MeshFilter>().mesh = mesh;
  50.     var Caylan = Instantiate (lines, Vector3(0,0,0),Quaternion.identity);
  51.     Caylan.SendMessage("setMesh",mesh);
  52.     Caylan.transform.parent = transform;
  53.     characterGraphics = Instantiate (player, Vector3(playerStart.x,levelData[playerStart.x,playerStart.y].height/2+.3, playerStart.y+.5),Quaternion.identity);
  54.     currentDirection = Dir.Up;
  55.     currentTile = levelData[playerStart.x,playerStart.y];
  56.    
  57.     yield WaitForSeconds (0.1);
  58.     animHandle = GameObject.FindGameObjectWithTag("animHandle");
  59. }
  60.  
  61. var animHandle : GameObject;
  62. var currentDirection : Dir;
  63. var baseSpeed : float; //(tiles/second)
  64. var maxSpeed : int; //baseSpeeds
  65. var currentSpeed : float; //baseSpeeds
  66. var characterGraphics : GameObject;
  67. var mode : modes;
  68. var currentTile : tile;
  69. var nextTile : tile;
  70.  
  71. /*public class index {
  72.     public var x : int;
  73.     public var y : int;
  74.     public function index (a:float, b:float) {
  75.         x = Mathf.FloorToInt(a);
  76.         y = Mathf.FloorToInt(b);
  77. }   public function index (a: Vector2) {
  78.         x = Mathf.FloorToInt(a.x);
  79.         y = Mathf.FloorToInt(a.y);
  80. }} */
  81.  
  82. enum modes {grounded, pipe, falling};
  83.  
  84. //type; dir; height
  85.  
  86. var expose : float;
  87. var up : boolean;
  88. var sendAnim : boolean = false;
  89.  
  90. function FixedUpdate () {
  91.     nextTile = levelData[Mathf.FloorToInt(characterGraphics.transform.position.x+0.24),Mathf.FloorToInt(characterGraphics.transform.position.z)];
  92.     if (currentTile.type == Type.normal) {
  93.         if (nextTile.type == Type.normal) {
  94.             if (nextTile.height == currentTile.height) {
  95.                 currentTile = nextTile;
  96. }}      else if (nextTile.type == Type.pipe) {
  97.             mode = modes.pipe;
  98.             animHandle.SendMessage("upRamp", baseSpeed*currentSpeed);
  99.             currentTile = nextTile;
  100.             up = true;
  101. }}  if (mode == modes.grounded) {
  102.         characterGraphics.transform.Translate(currentSpeed*baseSpeed, 0,0);
  103. }   if (mode == modes.pipe) {
  104.         expose = characterGraphics.transform.position.x+0.24-Mathf.Floor(characterGraphics.transform.position.x+0.24);
  105.         if ((nextTile.type == currentTile.type) && up) {
  106.             characterGraphics.transform.Translate(currentSpeed*baseSpeed, a*expose*expose*currentSpeed*baseSpeed,0);
  107. }       else if ((characterGraphics.transform.position.y < (currentTile.height/2+1.2+(currentSpeed/2)))&& (up)) {
  108.             characterGraphics.transform.Translate(0, currentSpeed*baseSpeed,0);
  109.                
  110.             sendAnim = true;
  111. }       else if (characterGraphics.transform.position.y > (currentTile.height/2+0.65)) {
  112.             up = false;
  113.             characterGraphics.transform.Translate(0, 0-(currentSpeed*baseSpeed),0);
  114.             if ((characterGraphics.transform.position.y < (currentTile.height/2+0.65+baseSpeed*currentSpeed*5.2))&&sendAnim){
  115.                 sendAnim = false;
  116.                 animHandle.SendMessage("downRamp", baseSpeed*currentSpeed);
  117. }}      else if ((characterGraphics.transform.position.y > (currentTile.height/2)+.2)) {
  118.             characterGraphics.transform.Translate(0-currentSpeed*baseSpeed, 0-a*expose*expose*currentSpeed*baseSpeed,0);
  119. }}}
  120.  
  121. function move () {
  122.  
  123. }
  124.  
  125. var direct : Dir;
  126. var levelLoad : Color[];
  127. var playerStart : Vector2;
  128. var levelData : tile[,];
  129.  
  130. function loadLevel () {
  131.     debugArray = new Array();
  132.     levelLoad = level.GetPixels(0,0,level.width,level.height);
  133.     levelData = new tile[level.width,level.height];
  134.     for (var i=0; i<level.width;i++){
  135.         for (var j=0; j< level.height; j++){
  136.             if (levelLoad[j*level.width+i].r < 0.1)
  137.                 direct = Dir.Right;
  138.             else if (levelLoad[j*level.width+i].r < 0.3)
  139.                 direct = Dir.Up;
  140.             else if (levelLoad[j*level.width+i].r < 0.6)
  141.                 direct = Dir.Left;
  142.             else
  143.                 direct = Dir.Down;
  144.             if (levelLoad[j*level.width+i].g == 1) {
  145.                 levelData [i,j] = tile(direct, Type.normal,Mathf.RoundToInt(levelLoad[j*level.width+i].b*255));
  146.                 flat (Vector2(i,j), Mathf.RoundToInt(levelLoad[j*level.width+i].b*255));
  147. }           else if (levelLoad[j*level.width+i].g == (128.0/255.0)) {
  148.                 levelData [i,j] = tile(direct, Type.pipe,Mathf.RoundToInt(levelLoad[j*level.width+i].b*255));
  149.                 halfPipe (Vector2(i,j), direct, Mathf.RoundToInt(levelLoad[j*level.width+i].b*255));
  150. }           else if (levelLoad[j*level.width+i].g == (91.0/255.0)){
  151.                 levelData [i,j] = tile(direct, Type.normal,Mathf.RoundToInt(levelLoad[j*level.width+i].b*255));
  152.                 playerStart = Vector2(i,j);
  153.                 flat (Vector2(i,j), Mathf.RoundToInt(levelLoad[j*level.width+i].b*255));
  154. }           else
  155.                 levelData [i,j] = tile(direct, Type.pit,Mathf.RoundToInt(levelLoad[j*level.width+i].b*255));
  156. }}}
  157.  
  158. public class tile {
  159.     public var direction : Dir;
  160.     public var type : Type;
  161.     public var height : int;
  162.     public function tile (a :Dir, b: Type, c:int) {
  163.         direction = a;
  164.         type = b;
  165.         height = c;
  166. }}
  167.  
  168. enum Dir {Up, Left, Right, Down};
  169. enum Type {normal, pipe, pit};
  170. var curvature : int; //must be even
  171. var a : float;
  172.  
  173. function flat (root:Vector2, height:int){
  174.  
  175.     triangles = new Array();
  176.     height = height/2;
  177.  
  178.     verts = Array(
  179.         Vector3(root.x,height,root.y),
  180.         Vector3(root.x+1,height,root.y),
  181.         Vector3(root.x, height,root.y+1),
  182.         Vector3(root.x+1, height,root.y+1),
  183.        
  184.         Vector3(root.x, 0,root.y),
  185.         Vector3(root.x+1, 0,root.y),
  186.         Vector3(root.x, 0,root.y+1),
  187.         Vector3(root.x+1, 0,root.y+1));
  188.        
  189.     UVs = Array(Vector2(0,0), Vector2(0,2), Vector2(2,0), Vector2(2,2), Vector2 (2,2), Vector2(2,0),Vector2(0,2),Vector2(0,0));  
  190.     triangles = Array(0,3,1, 0,2,3, 0,1,4, 4,1,5, 1,7,5, 1,3,7, 3,6,7, 2,6,3, 0,4,6, 0,6,2);
  191.    
  192.     currentLength = triangles.length;
  193.     for (i=0; i<currentLength;i++)
  194.         triangles[i] += enumeration;
  195.    
  196.     totalVerts = totalVerts.concat(verts);
  197.     enumeration = totalVerts.length;
  198.     totalTriangles = totalTriangles.concat(triangles);
  199.     totalUVs = totalUVs.concat(UVs);
  200. }
  201.  
  202. function halfPipe (root:Vector2, direction:Dir, height:int) {
  203.     toggle = true;
  204.    
  205.     verts = new Array();
  206.     UVs = new Array();
  207.     triangles = new Array();
  208.     height = height/2;
  209.  
  210.     GetComponent.<MeshFilter>().mesh = mesh;
  211.     for (var i =0; i< curvature; i++) {
  212.         b = i* 1.0/curvature;
  213.         if (direction == Dir.Up) {
  214.             verts.Add(Vector3(root.x+b, a*b*b+height, root.y+0));
  215.             verts.Add(Vector3(root.x+b, a*b*b+height, root.y+1));
  216. }       else if (direction == Dir.Left) {
  217.             verts.Add(Vector3(root.x+1, a*b*b+height, root.y+b));
  218.             verts.Add(Vector3(root.x+0, a*b*b+height, root.y+b));
  219. }       if (direction == Dir.Down) {
  220.             verts.Add(Vector3(root.x+1-b, a*b*b+height, root.y+1));
  221.             verts.Add(Vector3(root.x+1-b, a*b*b+height, root.y+0));
  222. }       else if (direction == Dir.Right) {
  223.             verts.Add(Vector3(root.x+0, a*b*b+height, root.y+1-b));
  224.             verts.Add(Vector3(root.x+1, a*b*b+height, root.y+1-b));
  225. }       if (toggle) {
  226.             UVs.Add (Vector2(0,0));
  227.             UVs.Add (Vector2(2,0));
  228.             UVs.Add (Vector2(0,1));
  229.             UVs.Add (Vector2(2,1));
  230.             triangles.Add (enumeration + i*2);
  231.             triangles.Add (enumeration + i*2+1);
  232.             triangles.Add (enumeration + i*2+2);
  233.            
  234.             triangles.Add (enumeration + i*2+2);
  235.             triangles.Add (enumeration + i*2+1);
  236.             triangles.Add (enumeration + i*2+3);
  237.            
  238.             if (i < (curvature-2)) {
  239.            
  240.                 triangles.Add (enumeration + i*2+2);
  241.                 triangles.Add (enumeration + i*2+3);
  242.                 triangles.Add (enumeration + i*2+5);
  243.                
  244.                 triangles.Add (enumeration + i*2+2);
  245.                 triangles.Add (enumeration + i*2+5);
  246.                 triangles.Add (enumeration + i*2+4);
  247.            
  248. }}      toggle = !toggle;
  249. }   currentLength = verts.length;
  250.    
  251.     for (j= 0; j< currentLength; j++) {
  252.         verts.Add(Vector3(verts[j].x, 0, verts[j].z));
  253.        
  254.         if (toggle) {
  255.             triangles.Add (enumeration + j, enumeration + j+2, enumeration + j+12);
  256.             triangles.Add (enumeration + j, enumeration + j+12, enumeration + j+10);
  257. }       else {
  258.             triangles.Add (enumeration + j, enumeration + j+12, enumeration + j+2);
  259.             triangles.Add (enumeration + j, enumeration + j+10, enumeration + j+12);
  260. }   toggle=!toggle;
  261. }   triangles.RemoveAt(93); triangles.RemoveAt(93); triangles.RemoveAt(93); triangles.RemoveAt(90); triangles.RemoveAt(90); triangles.RemoveAt(90);
  262.     triangles.RemoveAt(39); triangles.RemoveAt(39); triangles.RemoveAt(39); triangles.RemoveAt(33); triangles.RemoveAt(33); triangles.RemoveAt(33);
  263.     triangles.Add(enumeration + 20,enumeration + 10,enumeration + 22);
  264.    
  265.     currentLength = UVs.length;
  266.    
  267.     for (k = 0; k< currentLength; k++){
  268.         if (UVs[k]==Vector2(0,0))
  269.             UVs.Add(Vector2(2,0));
  270.         else if (UVs[k]==Vector2(2,0))
  271.             UVs.Add(Vector2(0,0));
  272.         else if (UVs[k]==Vector2(0,1))
  273.             UVs.Add(Vector2(2,1));
  274.         else if (UVs[k]==Vector2(2,1))
  275.             UVs.Add(Vector2(0,1)); 
  276. }
  277.     verts.Add (verts[0], verts[1], verts[12], verts[13], verts[10], verts[11],verts[22],verts[23]);
  278.     triangles.Add(enumeration + 25,enumeration + 24,enumeration + 26, enumeration + 25,enumeration + 26,enumeration + 27, enumeration + 28,enumeration + 29,enumeration + 30, enumeration + 30,enumeration + 29,enumeration + 31);
  279.     UVs.Add(Vector2(0,0),Vector2(0,1),Vector2(2,0),Vector2(2,1)); UVs.Add(Vector2(0,0),Vector2(0,1),Vector2(2,0),Vector2(2,1));
  280.    
  281.     totalVerts = totalVerts.concat(verts);
  282.     enumeration = totalVerts.length;
  283.     totalTriangles = totalTriangles.concat(triangles);
  284.     totalUVs = totalUVs.concat(UVs);   
  285. }
  286.  
  287. //y = ax^2
  288.  
  289. //Animation Behavior (File 1) controls the rotation of the board and it's relative position compared to the camera.
  290.  
  291. function Start () {
  292.     transform.localScale = Vector3(.15,.15,.15);
  293.     transform.localPosition = Vector3(0,0,0);
  294. }
  295.  
  296. function upRamp (a:float) {
  297.     baldGuy = 0;
  298.     yield WaitForSeconds (a*2);
  299.     up = true;
  300.     slerpTime = a;
  301. }
  302.  
  303. function downRamp (a:float) {
  304.     baldGuy = 0;
  305.     down = true;
  306.     slerpTime = a;
  307. }
  308.  
  309. var slerpTime : float;
  310. var up : boolean = false;
  311. var down : boolean = false;
  312. var baldGuy : float;
  313.  
  314. function FixedUpdate () {
  315.     if (up) {
  316.         //baldGuy ++;
  317.         if (transform.rotation.eulerAngles.z < 90)
  318.             transform.Rotate(0,0,90*slerpTime);
  319.         else up = false;
  320. }   else if (down) {
  321.         //baldGuy ++;
  322.         if (transform.rotation.eulerAngles.z > 3)
  323.             transform.Rotate(0,0,-90*slerpTime);
  324.         else down = false;
  325. }}
  326.  
  327. //Lines File 02 (Used to pass a mesh to a second object.) [the mesh with the lines and the solid color one are separate. it's (eventually) to allow customization of gameplay area without too much hassle).
  328.  
  329. function setMesh (a : Mesh) {
  330.     GetComponent.<MeshFilter>().mesh = a;
  331. }
  332.  
  333. //Skateboard File 03 (This is literally a 3D model of a skateboard)
  334.  
  335. var newVertices : Vector3[];
  336. var newUV : Vector2[];
  337. var newTriangles : int[];
  338. var triangles : Array;
  339. var UVs : Array;
  340. var vertices : Array;
  341. var player: GameObject;
  342. var width : float;
  343. var length : float;
  344. var depth : float;
  345. var duckHeight : float;
  346. private var currentLength : int;
  347.  
  348. function Start () {
  349.     UVs = new Array ();
  350.     newVertices = [Vector3(0,0,0), Vector3(0,0,width), Vector3(length,0,0), Vector3(length,0,width), Vector3(0,0,width/2),
  351.    
  352.         Vector3((width/2)*(0+Mathf.Sin(Mathf.Deg2Rad*(0-30))),duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(0-30)))),
  353.         Vector3((width/2)*(0+Mathf.Sin(Mathf.Deg2Rad*(0-60))),2*duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(0-60)))),
  354.         Vector3((width/2)*(0+Mathf.Sin(Mathf.Deg2Rad*(0-90))),duckHeight,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(0-90)))),
  355.         Vector3((width/2)*(0+Mathf.Sin(Mathf.Deg2Rad*(0-120))),2*duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(0-120)))),
  356.         Vector3((width/2)*(0+Mathf.Sin(Mathf.Deg2Rad*(0-150))),duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(0-150)))),
  357.        
  358.         Vector3(length,0,width/2),
  359.         Vector3(length+(width/2)*(Mathf.Sin(Mathf.Deg2Rad*(30))),duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(30)))),
  360.         Vector3(length+(width/2)*(Mathf.Sin(Mathf.Deg2Rad*(60))),2*duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(60)))),
  361.         Vector3(length+(width/2)*(Mathf.Sin(Mathf.Deg2Rad*(90))),duckHeight,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(90)))),
  362.         Vector3(length+(width/2)*(Mathf.Sin(Mathf.Deg2Rad*(120))),2*duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(120)))),
  363.         Vector3(length+(width/2)*(Mathf.Sin(Mathf.Deg2Rad*(150))),duckHeight/3,(width/2)*(1+Mathf.Cos(Mathf.Deg2Rad*(150))))
  364.        
  365. ];  currentLength = newVertices.Length;
  366.     vertices = Array(newVertices);
  367.     for (j=0; j<currentLength; j++)
  368.         vertices.Add(Vector3(newVertices[j].x,newVertices[j].y-depth,newVertices[j].z));
  369.     newVertices = vertices.ToBuiltin(Vector3);
  370.     newTriangles = [0,1,3, 0,3,2, 0,9,4, 5,4,6, 6,4,7, 7,4,8, 8,4,9, 5,1,4,
  371.         2,10,15, 15,10,14, 14,10,13, 13,10,12, 12,10,11, 11,10,3 ];
  372.     currentLength = (newTriangles.Length/3);
  373.     triangles = Array(newTriangles);
  374.     for (k=0; k< currentLength; k++){
  375.         triangles.Add(newTriangles[k*3]+16);   
  376.         triangles.Add(newTriangles[k*3+2]+16);
  377.         triangles.Add(newTriangles[k*3+1]+16); 
  378. }   triangles.Add(0,2,16, 18,16,2,
  379.         2,15,18, 18,15,31,
  380.         15,14,31, 31,14,30,
  381.         14,13,30, 30,13,29,
  382.         13,12,28, 13,28,29,
  383.         12,11,27, 12,27,28,
  384.         11,3,19, 11,19,27,
  385.         3,1,19, 1,17,19,
  386.         1,5,21, 1,21,17,
  387.         5,6,22, 21,5,22,
  388.         6,23,22, 23,6,7,
  389.         23,7,24, 24,7,8,
  390.         8,25,24, 25,8,9,
  391.         16,25,9, 16,9,0);
  392.     newTriangles = triangles.ToBuiltin(int);
  393.     for (i=0; i< newVertices.length; i++)
  394.         UVs.Add (Vector2(0,0));
  395.     newUV = UVs.ToBuiltin(Vector2);
  396.     var mesh : Mesh = new Mesh ();
  397.     GetComponent.<MeshFilter>().mesh = mesh;
  398.     mesh.vertices = newVertices;
  399.     mesh.uv = newUV;
  400.     mesh.triangles = newTriangles;
  401.     mesh.RecalculateNormals();
  402.     var tyler = Instantiate (player, Vector3 (transform.position.x+ length/2, transform.position.y+ 0 - depth/2, transform.position.z+ width/2), Quaternion.identity);
  403.     tyler.transform.parent = transform.parent;
  404.     transform.parent = tyler.transform;
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement