Advertisement
bekovski

sg04_complicatedCpy

Jun 3rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 22.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. using System.Text.RegularExpressions;
  6.  
  7. public class LevelManager : MonoBehaviour {
  8.  
  9.     // singleton
  10.     public static LevelManager instance;
  11.  
  12.     // file prefix
  13.     private readonly string prefix = "Assets/Levels/";
  14.  
  15.     // player prefab
  16.     public Transform pacman;
  17.  
  18.     // player coods
  19.     int pacmanX;
  20.     int pacmanZ;
  21.  
  22.     // enemy prefabs
  23.     public Transform blinky;
  24.     public Transform pinky;
  25.     public Transform inky;
  26.     public Transform clyde;
  27.  
  28.     // enemy coords
  29.     int blinkyX;
  30.     int blinkyZ;
  31.     int pinkyX;
  32.     int pinkyZ;
  33.     int inkyX;
  34.     int inkyZ;
  35.     int clydeX;
  36.     int clydeZ;
  37.  
  38.     // tiles
  39.     public Transform corridor;
  40.     public Transform corner;
  41.     public Transform cross;
  42.     public Transform tCross;
  43.     public Transform deadEnd;
  44.  
  45.     // corresponding strings
  46.     readonly char sCorridorHorizontal = '-';
  47.     readonly char sCorridorVertical = '|';
  48.     readonly char sCross = '+';             // could be cross or tCross or corner
  49.  
  50.     // the level as a string
  51.     private List<string> level = new List<string> ();
  52.     private bool isReady;
  53.  
  54.     // a struct that helps setting the wayPoints
  55.     struct WayPointData {
  56.         public Transform tile;
  57.         public bool goUp;
  58.         public bool goDown;
  59.         public bool goLeft;
  60.         public bool goRight;
  61.     };
  62.     WayPointData[,] wayPointDataArray;  // 2D array
  63.     int rows = 0;                       // final # of rows
  64.     int cols = -1;                      // final # of columns
  65.     int r = 0;                          // row idx  (needed when filling in the array)
  66.     int c = 0;                          // col idx  (needed when filling in the array)
  67.  
  68.     // instantiated objects
  69.     List<Transform> createdTiles = new List<Transform>();
  70.     List<Transform> createdCharacters = new List<Transform>();
  71.  
  72.     void Awake() {
  73.         instance = this;
  74.     }
  75.  
  76.     void Update() {
  77.         if(isReady) {
  78.             wayPointDataArray = new WayPointData[rows, cols];
  79.             CreateLevel ();
  80.             SetWayPoints ();
  81.             SetAndInstantiateCharacters ();
  82.             isReady = false;
  83.         }
  84.     }
  85.  
  86.     /// <summary>
  87.     /// Reads the file. In doing so, it also destroys the current level and creates the data necessary for the new level.
  88.     /// While reading the file, syntax and semantic checks are done as well.
  89.     /// </summary>
  90.     /// <param name="fileName">File name.</param>
  91.     public void ReadFile(string fileName) {
  92.         Destroy ();
  93.  
  94.         string text = System.IO.File.ReadAllText (prefix + fileName);
  95.         SyntaxChecker.instance.errorMsg.text = "";
  96.         SemanticsChecker.instance.errorMsg.text = "";
  97.  
  98.         if(!SyntaxChecker.instance.IsSyntacticallyCorrect (text)) {
  99.             SyntaxChecker.instance.errorMsg.text = "Syntax Error!";
  100.             return;
  101.         }
  102.  
  103.         level.Clear ();
  104.         List<string> lines = new List<string>(Regex.Split (text, "\n"));
  105.  
  106.         rows = 0;
  107.         cols = -1;
  108.         r = 0;
  109.         c = 0;
  110.  
  111.         for(int i=0; i < lines.Count; ++i) {
  112.             string currentLine = lines[i].Trim();
  113.             string[] currentLineSplit = Regex.Split (currentLine, " ");
  114.  
  115.             // important note #1: we have to invert the provided z-value as the level grows downward but the z-axis points upward
  116.             // important note #2: the x-value is to be used in the 2nd dimension of the level-array (-> columns!)
  117.             // important note #3: as the z-value will be negative, we have to invert it again when accessing the level-array later on
  118.  
  119.             switch(currentLineSplit[0]) {
  120.             case "Pacman":
  121.                 pacmanX = int.Parse (currentLineSplit [1]);
  122.                 pacmanZ = -int.Parse (currentLineSplit [2]);
  123.                 break;
  124.             case "Blinky":
  125.                 blinkyX = int.Parse (currentLineSplit [1]);
  126.                 blinkyZ = -int.Parse (currentLineSplit [2]);
  127.                 break;
  128.             case "Pinky":
  129.                 pinkyX = int.Parse (currentLineSplit [1]);
  130.                 pinkyZ = -int.Parse (currentLineSplit [2]);
  131.                 break;
  132.             case "Inky":
  133.                 inkyX = int.Parse (currentLineSplit [1]);
  134.                 inkyZ = -int.Parse (currentLineSplit [2]);
  135.                 break;
  136.             case "Clyde":
  137.                 clydeX = int.Parse (currentLineSplit [1]);
  138.                 clydeZ = -int.Parse (currentLineSplit [2]);
  139.                 break;
  140.             default:
  141.                 // else it must be some level object
  142.                 level.Add (currentLine);
  143.                 rows++;
  144.                 if(cols == -1) {
  145.                     // assumes that each row is of the same length
  146.                     cols = currentLine.Length;
  147.                 }
  148.                 break;
  149.             } // switch()
  150.         } // for()
  151.            
  152.         if(!SemanticsChecker.instance.IsSemanticallyCorrect (lines, rows, cols)) {
  153.             SemanticsChecker.instance.errorMsg.text = "Semantics Error!";
  154.             return;
  155.         }
  156.  
  157.         isReady = true;
  158.     } // ReadFile()
  159.        
  160.  
  161.     /// <summary>
  162.     /// Destroys the level and the characters.
  163.     /// </summary>
  164.     void Destroy() {
  165.         foreach(Transform character in createdCharacters) {
  166.             if(character != null) {
  167.                 Destroy (character.gameObject);
  168.             }
  169.         }
  170.  
  171.         foreach(Transform tile in createdTiles) {
  172.             if(tile != null) {
  173.                 Destroy (tile.gameObject);
  174.             }
  175.         }
  176.  
  177.         createdCharacters.Clear ();
  178.         createdTiles.Clear ();
  179.     }
  180.        
  181.  
  182.     /// <summary>
  183.     /// Creates the level by instantiating the tiles in the correct position and rotation.
  184.     /// </summary>
  185.     void CreateLevel() {
  186.         float startX = 0f;
  187.  
  188.         float x = startX;
  189.         float y = 0f;
  190.         float z = 0f;
  191.  
  192.         Transform tile;
  193.  
  194.         for(int i=0; i < level.Count; ++i) {
  195.             for(int j=0; j < level[i].Length; ++j) {
  196.                 Vector3 vec = new Vector3 (x, y, z);
  197.  
  198.  
  199.                 if(level[i][j] == sCorridorHorizontal) {
  200.                     bool isDeadEndLeft  = (j == 0) || level[i][j-1] == sCorridorVertical;
  201.                     bool isDeadEndRight = (j == level[i].Length-1) || level[i][j+1] == sCorridorVertical;
  202.  
  203.                     if(isDeadEndLeft) {
  204.                         tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, -90f, false, false, false, true);
  205.                     }
  206.                     else if(isDeadEndRight) {
  207.                         tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 90f, false, false, true, false);
  208.                     }
  209.                     else {
  210.                         tile = InstantiateWithRotationAndCreateWayPointData(corridor, vec, 90f, false, false, true, true);
  211.                     }
  212.  
  213.                     createdTiles.Add (tile);
  214.                 }
  215.                 else if(level[i][j] == sCorridorVertical) {
  216.                     bool isDeadEndUp    = (i == 0) || level[i-1][j] == sCorridorHorizontal;
  217.                     bool isDeadEndDown  = (i == level.Count-1) || level[i+1][j] == sCorridorHorizontal;
  218.  
  219.                     if(isDeadEndUp) {
  220.                         tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 0f, false, true, false, false);
  221.                     }
  222.                     else if(isDeadEndDown) {
  223.                         tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 180f, true, false, false, false);
  224.                     }
  225.                     else {
  226.                         tile = InstantiateWithRotationAndCreateWayPointData(corridor, vec, 0f, true, true, false, false);
  227.                     }
  228.  
  229.                     createdTiles.Add (tile);
  230.                 }
  231.  
  232.                 char empty = ' ';
  233.                 if(level[i][j] == sCross) {
  234.                     char up     = (i > 0)                       ? level [i - 1] [j] : empty;
  235.                     char down   = ((i + 1) < level.Count)       ? level [i + 1] [j] : empty;
  236.                     char left   = (j > 0)                       ? level [i] [j - 1] : empty;
  237.                     char right  = ((j + 1) < level [i].Length)  ? level [i] [j + 1] : empty;
  238.  
  239.                     bool isLeftTopCorner        = (up == empty && left == empty && right != empty && down != empty);
  240.                     bool isLeftBottomCorner     = (up != empty && left == empty && right != empty && down == empty);
  241.                     bool isRightTopCorner       = (up == empty && left != empty && right == empty && down != empty);
  242.                     bool isRightBottomCorner    = (up != empty && left != empty && right == empty && down == empty);
  243.  
  244.                     bool isLeftTCross           = (up != empty && left == empty && right != empty && down != empty);
  245.                     bool isRightTCross          = (up != empty && left != empty && right == empty && down != empty);
  246.                     bool isTopTCross            = (up == empty && left != empty && right != empty && down != empty);
  247.                     bool isBottomTCross         = (up != empty && left != empty && right != empty && down == empty);
  248.  
  249.                     bool isCenterCross          = (up != empty && left != empty && right != empty && down != empty);
  250.  
  251.                     bool isDeadEndUp    = (i > 0)                       && (level [i - 1] [j] == sCorridorHorizontal);
  252.                     bool isDeadEndDown  = ((i + 1) < level.Count)       && (level [i + 1] [j] == sCorridorHorizontal);
  253.                     bool isDeadEndLeft  = (j > 0)                       && (level [i] [j - 1] == sCorridorVertical);
  254.                     bool isDeadEndRight = ((j + 1) < level[i].Length)   && (level [i] [j + 1] == sCorridorVertical);
  255.  
  256.                     // long if-sequence:
  257.                     // first the corners
  258.  
  259.                     if(isLeftTopCorner) {
  260.                         if(isDeadEndDown) {
  261.                             // only way: right
  262.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, -90f, false, false, false, true);
  263.                         }
  264.                         else if(isDeadEndRight) {
  265.                             // only way: down
  266.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 0f, false, true, false, false);
  267.                         }
  268.                         else {
  269.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, -90f, false, true, false, true);
  270.                         }
  271.  
  272.                         createdTiles.Add (tile);
  273.                     }
  274.                     else if(isLeftBottomCorner) {
  275.                         if(isDeadEndUp) {
  276.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, -90f, false, false, false, true);
  277.                         }
  278.                         else if(isDeadEndRight) {
  279.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 180f, true, false, false, false);
  280.                         }
  281.                         else {
  282.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 180f, true, false, false, true);
  283.                         }
  284.  
  285.                         createdTiles.Add (tile);
  286.                     }
  287.                     else if(isRightTopCorner) {
  288.                         if(isDeadEndDown) {
  289.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 90f, false, false, true, false);
  290.                         }
  291.                         else if(isDeadEndLeft) {
  292.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 90f, false, true, false, false);
  293.                         }
  294.                         else {
  295.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 0f, false, true, true, false);
  296.                         }
  297.  
  298.                         createdTiles.Add (tile);
  299.                     }
  300.                     else if(isRightBottomCorner) {
  301.                         if(isDeadEndUp) {
  302.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 90f, false, false, true, false);
  303.                         }
  304.                         else if(isDeadEndLeft) {
  305.                             tile = InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 90f, true, false, false, false);
  306.                         }
  307.                         else {
  308.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 90f, true, false, true, false);
  309.                         }
  310.  
  311.                         createdTiles.Add (tile);
  312.                     }
  313.  
  314.                     // still same if case
  315.                     // now TCrosses
  316.  
  317.                     else if(isLeftTCross) {
  318.                         if(isDeadEndUp && isDeadEndDown) {
  319.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, -90f, false, false, false, true);
  320.                         }
  321.                         else if(isDeadEndUp && isDeadEndRight) {
  322.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 0f, false, true, false, false);
  323.                         }
  324.                         else if(isDeadEndDown && isDeadEndRight) {
  325.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 180f, true, false, false, false);
  326.                         }
  327.                         else if(isDeadEndUp) {
  328.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, -90f, false, true, false, true);
  329.                         }
  330.                         else if(isDeadEndDown) {
  331.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 180f, true, false, false, true);
  332.                         }
  333.                         else if(isDeadEndRight) {
  334.                             tile = InstantiateWithRotationAndCreateWayPointData(corridor, vec, 0f, true, true, false, false);
  335.                         }
  336.                         else {
  337.                             tile = InstantiateWithRotationAndCreateWayPointData(tCross, vec, 0f, true, true, false, true);
  338.                         }
  339.  
  340.                         createdTiles.Add (tile);
  341.                     }
  342.                     else if(isRightTCross) {
  343.                         if(isDeadEndUp && isDeadEndDown) {
  344.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 90f, false, false, true, false);
  345.                         }
  346.                         else if(isDeadEndUp && isDeadEndLeft) {
  347.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 0f, false, true, false, false);
  348.                         }
  349.                         else if(isDeadEndDown && isDeadEndLeft) {
  350.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 180f, true, false, false, false);
  351.                         }
  352.                         else if(isDeadEndUp) {
  353.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 0f, false, true, true, false);
  354.                         }
  355.                         else if(isDeadEndDown) {
  356.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 90f, true, false, true, false);
  357.                         }
  358.                         else if(isDeadEndLeft) {
  359.                             tile = InstantiateWithRotationAndCreateWayPointData(corridor, vec, 0f, true, true, false, false);
  360.                         }
  361.                         else {
  362.                             tile = InstantiateWithRotationAndCreateWayPointData(tCross, vec, 180f, true, true, true, false);
  363.                         }
  364.  
  365.                         createdTiles.Add (tile);
  366.                     }
  367.                     else if(isTopTCross) {
  368.                         if(isDeadEndDown && isDeadEndLeft) {
  369.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, -90f, false, false, false, true);
  370.                         }
  371.                         else if(isDeadEndDown && isDeadEndRight) {
  372.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 90f, false, false, true, false);
  373.                         }
  374.                         else if(isDeadEndLeft && isDeadEndRight) {
  375.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 0f, false, true, false, false);
  376.                         }
  377.                         else if(isDeadEndDown) {
  378.                             tile = InstantiateWithRotationAndCreateWayPointData(corridor, vec, 90f, false, false, true, true);
  379.                         }
  380.                         else if(isDeadEndLeft) {
  381.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, -90f, false, true, false, true);
  382.                         }
  383.                         else if(isDeadEndRight) {
  384.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 0f, false, true, true, false);
  385.                         }
  386.                         else {
  387.                             tile = InstantiateWithRotationAndCreateWayPointData(tCross, vec, 90f, false, true, true, true);
  388.                         }
  389.  
  390.                         createdTiles.Add (tile);
  391.                     }
  392.                     else if(isBottomTCross) {
  393.                         if(isDeadEndUp && isDeadEndLeft) {
  394.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, -90f, false, false, false, true);
  395.                         }
  396.                         else if(isDeadEndUp && isDeadEndRight) {
  397.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 90f, false, false, true, false);
  398.                         }
  399.                         else if(isDeadEndLeft && isDeadEndRight) {
  400.                             tile = InstantiateWithRotationAndCreateWayPointData(deadEnd, vec, 180f, true, false, false, false);
  401.                         }
  402.                         else if(isDeadEndUp) {
  403.                             tile = InstantiateWithRotationAndCreateWayPointData(corridor, vec, 90f, false, false, true, true);
  404.                         }
  405.                         else if(isDeadEndLeft) {
  406.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 180f, true, false, false, true);
  407.                         }
  408.                         else if(isDeadEndRight) {
  409.                             tile = InstantiateWithRotationAndCreateWayPointData(corner, vec, 90f, true, false, true, false);
  410.                         }
  411.                         else {
  412.                             tile = InstantiateWithRotationAndCreateWayPointData(tCross, vec, -90f, true, false, true, true);
  413.                         }
  414.  
  415.                         createdTiles.Add (tile);
  416.                     }
  417.  
  418.                     // still same if case
  419.                     // now full cross
  420.  
  421.                     else if(isCenterCross) {
  422.                         if(isDeadEndUp && isDeadEndDown && isDeadEndLeft) {
  423.                             tile = CreateDeadEndLeft (vec);
  424.                         }
  425.                         else if(isDeadEndUp && isDeadEndDown && isDeadEndRight) {
  426.                             tile = CreateDeadEndRight (vec);
  427.                         }
  428.                         else if(isDeadEndUp && isDeadEndLeft && isDeadEndRight) {
  429.                             tile = CreateDeadEndUp (vec);
  430.                         }
  431.                         else if(isDeadEndDown && isDeadEndLeft && isDeadEndRight) {
  432.                             tile = CreateDeadEndDown (vec);
  433.                         }
  434.                         else if(isDeadEndUp && isDeadEndDown) {
  435.                             tile = CreateCorridorHorizontal (vec);
  436.                         }
  437.                         else if(isDeadEndUp && isDeadEndLeft) {
  438.                             tile = CreateCornerTopRight (vec);
  439.                         }
  440.                         else if(isDeadEndUp && isDeadEndRight) {
  441.                             tile = CreateCornerTopLeft (vec);
  442.                         }
  443.                         else if(isDeadEndDown && isDeadEndLeft) {
  444.                             tile = CreateCornerBottomLeft (vec);
  445.                         }
  446.                         else if(isDeadEndDown && isDeadEndRight) {
  447.                             tile = CreateCornerBottomRight (vec);
  448.                         }
  449.                         else if(isDeadEndLeft && isDeadEndRight) {
  450.                             tile = CreateCorridorVertical (vec);
  451.                         }
  452.                         else if(isDeadEndUp) {
  453.                             tile = CreateTCrossDownLeftRight (vec);
  454.                         }
  455.                         else if(isDeadEndDown) {
  456.                             tile = CreateTCrossUpLeftRight (vec);
  457.                         }
  458.                         else if(isDeadEndLeft) {
  459.                             tile = CreateTCrossUpDownRight (vec);
  460.                         }
  461.                         else if(isDeadEndRight) {
  462.                             tile = CreateTCrossUpDownLeft (vec);
  463.                         }
  464.                         else {
  465.                             tile = CreateCross (vec);
  466.                         }
  467.  
  468.                         createdTiles.Add (tile);
  469.                     }
  470.  
  471.                     // end of long if-sequence
  472.                     // end of long if-sequence
  473.                     // end of long if-sequence
  474.                 }
  475.  
  476.                 x += 1f;    // advance to the right
  477.             } // for(j)
  478.             z -= 1f;        // advance down
  479.             x = startX;     // start from the left
  480.         } // for(i)
  481.     } // CreateLevel()
  482.  
  483.  
  484.     // ---
  485.     // | |
  486.     // | |
  487.     Transform CreateDeadEndUp(Vector3 vec) {
  488.         return InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 0f, false, true, false, false);
  489.     }
  490.  
  491.     // | |
  492.     // | |
  493.     // ---
  494.     Transform CreateDeadEndDown(Vector3 vec) {
  495.         return InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 180f, true, false, false, false);
  496.     }
  497.  
  498.     // |-----
  499.     // |
  500.     // |-----
  501.     Transform CreateDeadEndLeft(Vector3 vec) {
  502.         return InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, -90f, false, false, false, true);
  503.     }
  504.  
  505.     // -----|
  506.     //      |
  507.     // -----|
  508.     Transform CreateDeadEndRight(Vector3 vec) {
  509.         InstantiateWithRotationAndCreateWayPointData (deadEnd, vec, 90f, false, false, true, false);
  510.     }
  511.  
  512.     //
  513.     // --------
  514.     // --------
  515.     //
  516.     Transform CreateCorridorHorizontal(Vector3 vec) {
  517.         return InstantiateWithRotationAndCreateWayPointData(corridor, vec, 90f, false, false, true, true);
  518.     }
  519.  
  520.     // ||
  521.     // ||
  522.     // ||
  523.     Transform CreateCorridorVertical(Vector3 vec) {
  524.         return InstantiateWithRotationAndCreateWayPointData(corridor, vec, 0f, true, true, false, false);
  525.     }
  526.  
  527.     // --------
  528.     // |  -----
  529.     // | |
  530.     // | |
  531.     Transform CreateCornerTopLeft(Vector3 vec) {
  532.         return InstantiateWithRotationAndCreateWayPointData(corner, vec, -90f, false, true, false, true);
  533.     }
  534.  
  535.     // --------
  536.     // -----   |
  537.     //      |  |
  538.     //      |  |
  539.     Transform CreateCornerTopRight(Vector3 vec) {
  540.         return InstantiateWithRotationAndCreateWayPointData(corner, vec, 0f, false, true, true, false);
  541.     }
  542.  
  543.  
  544.     // |  |
  545.     // |  |
  546.     // |   -----
  547.     //  --------
  548.     Transform CreateCornerBottomLeft(Vector3 vec) {
  549.         return InstantiateWithRotationAndCreateWayPointData(corner, vec, 180f, true, false, false, true);
  550.     }
  551.  
  552.     //      |  |
  553.     //      |  |
  554.     // -----   |
  555.     // --------
  556.     Transform CreateCornerBottomRight(Vector3 vec) {
  557.         return InstantiateWithRotationAndCreateWayPointData(corner, vec, 90f, true, false, true, false);
  558.     }
  559.  
  560.     //      ||
  561.     // -----||
  562.     //      ||
  563.     Transform CreateTCrossUpDownLeft(Vector3 vec) {
  564.         return InstantiateWithRotationAndCreateWayPointData (tCross, vec, 180f, true, true, true, false);
  565.     }
  566.  
  567.     // ||
  568.     // ||----
  569.     // ||
  570.     Transform CreateTCrossUpDownRight(Vector3 vec) {
  571.         return InstantiateWithRotationAndCreateWayPointData (tCross, vec, 0f, true, true, false, true);
  572.     }
  573.  
  574.     //    ||
  575.     //    ||
  576.     // --------
  577.     Transform CreateTCrossUpLeftRight(Vector3 vec) {
  578.         return InstantiateWithRotationAndCreateWayPointData (tCross, vec, -90f, true, false, true, true);
  579.     }
  580.  
  581.     // --------
  582.     //    ||
  583.     //    ||
  584.     Transform CreateTCrossDownLeftRight(Vector3 vec) {
  585.         return InstantiateWithRotationAndCreateWayPointData (tCross, vec, 90f, false, true, true, true);
  586.     }
  587.  
  588.     //    ||
  589.     // --------
  590.     // --------
  591.     //    ||
  592.     Transform CreateCross(Vector3 vec) {
  593.         return InstantiateWithRotationAndCreateWayPointData(cross, vec, 0f, true, true, true, true);
  594.     }
  595.  
  596.  
  597.     /// <summary>
  598.     /// Instantiates the given Transform object (a tile) at position 'vec', rotated by 'deg' degrees.
  599.     /// The boolean parameters are used to store the necessary information to create the WayPoint data for each tile later on.
  600.     /// </summary>
  601.     /// <returns>The instantiated object at position 'vec', rotated by 'deg' degrees.</returns>
  602.     /// <param name="obj">The Transform to be instantiated.</param>
  603.     /// <param name="vec">The Position at which the object is instantiated.</param>
  604.     /// <param name="deg">The degree by which the object is rotated.</param>
  605.     /// <param name="goUp">If set to <c>true</c> it's possible to go up.</param>
  606.     /// <param name="goDown">If set to <c>true</c> it's possible to go down.</param>
  607.     /// <param name="goLeft">If set to <c>true</c> it's possible to go left.</param>
  608.     /// <param name="goRight">If set to <c>true</c> it's possible to go right.</param>
  609.     Transform InstantiateWithRotationAndCreateWayPointData(Transform obj, Vector3 vec, float deg, bool goUp, bool goDown, bool goLeft, bool goRight) {
  610.         Vector3 rot = obj.transform.eulerAngles;
  611.         rot = new Vector3 (rot.x, rot.y + deg, rot.z);
  612.         Transform result = Instantiate (obj, vec, Quaternion.Euler (rot));
  613.  
  614.         WayPointData temp;
  615.         temp.tile = result;
  616.         temp.goUp = goUp;
  617.         temp.goDown = goDown;
  618.         temp.goLeft = goLeft;
  619.         temp.goRight = goRight;
  620.  
  621.         if(r < rows && c < cols) {
  622.             wayPointDataArray [r, c++] = temp;
  623.  
  624.             if(c == cols) {
  625.                 r++;
  626.                 c = 0;
  627.             }
  628.         }
  629.  
  630.         return result;
  631.     } // InstantiateWithRotationAndCreateWayPointData()
  632.  
  633.     /// <summary>
  634.     /// Sets the way points of each tile in the level.
  635.     /// </summary>
  636.     void SetWayPoints() {
  637.         // map
  638.         for(int i=0; i < rows; ++i) {
  639.             for(int j=0; j < cols; ++j) {
  640.                 WayPointData temp = wayPointDataArray [i, j];
  641.                 Transform obj = temp.tile;
  642.  
  643.                 if(obj != null) {
  644.                     WayPoint wp = obj.GetComponent<WayPoint> ();
  645.                     if(temp.goUp) {
  646.                         wp.upWaypoint = wayPointDataArray [i - 1, j].tile.GetComponent<WayPoint> ();
  647.                     }
  648.                     if(temp.goDown) {
  649.                         wp.downWaypoint = wayPointDataArray [i + 1, j].tile.GetComponent<WayPoint> ();
  650.                     }
  651.                     if(temp.goLeft) {
  652.                         wp.leftWaypoint = wayPointDataArray [i, j - 1].tile.GetComponent<WayPoint> ();
  653.                     }
  654.                     if(temp.goRight) {
  655.                         wp.rightWaypoint = wayPointDataArray [i, j + 1].tile.GetComponent<WayPoint> ();
  656.                     }
  657.                 }
  658.             } // for(j)
  659.         } // for(i)
  660.     } // SetWayPoints()
  661.  
  662.  
  663.     /// <summary>
  664.     /// Sets the necessary data (currentWayPoint and position) of each character and instantiates them.
  665.     /// </summary>
  666.     void SetAndInstantiateCharacters() {
  667.         InstantiateCharacter (ref pacman, pacmanX, pacmanZ);
  668.         InstantiateCharacter (ref blinky, blinkyX, blinkyZ);
  669.         InstantiateCharacter (ref pinky,  pinkyX,  pinkyZ);
  670.         InstantiateCharacter (ref inky,   inkyX,   inkyZ);
  671.         InstantiateCharacter (ref clyde,  clydeX,  clydeZ);
  672.     } // SetAndInstantiateCharacters()
  673.  
  674.  
  675.     /// <summary>
  676.     /// Instantiates the given character at position (x, z).
  677.     /// </summary>
  678.     /// <param name="character">The character to instantiate.</param>
  679.     /// <param name="x">The x coordinate.</param>
  680.     /// <param name="z">The z coordinate.</param>
  681.     void InstantiateCharacter(ref Transform character, int x, int z) {
  682.         // note the negation of the z-value!!
  683.         Transform tile = wayPointDataArray [-z, x].tile;
  684.         if(tile != null) {
  685.             WayPoint wp = tile.GetComponent<WayPoint> ();
  686.             if(character.CompareTag ("Pacman")) {
  687.                 character.GetComponent<PlayerControlScript> ().currentWaypoint = wp;
  688.             }
  689.             else{
  690.                 character.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp;
  691.             }
  692.  
  693.             Transform instantiatedCharacter;
  694.             instantiatedCharacter = Instantiate (character, new Vector3 (x, character.transform.position.y, z), Quaternion.identity);
  695.             createdCharacters.Add (instantiatedCharacter);
  696.         }
  697.     } // InstantiateCharacter()
  698. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement