Advertisement
bekovski

sg04_safeCpy

May 31st, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.45 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.     // walls
  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.     private bool isLevelLoaded;
  54.  
  55.     // the waypoints
  56.     private List<WayPoint> wayPoints = new List<WayPoint>();
  57.  
  58.     // a struct that helps setting the wayPoints
  59.     struct AuxStruct {
  60.         public Transform tile;
  61.         public bool goUp;
  62.         public bool goDown;
  63.         public bool goLeft;
  64.         public bool goRight;
  65.     };
  66.     List<AuxStruct> auxList = new List<AuxStruct>();
  67.     AuxStruct[,] auxArray;
  68.     int rows = 0;
  69.     int cols = -1;
  70.     int r = 0;
  71.     int c = 0;
  72.  
  73.     void Awake() {
  74.         instance = this;
  75.     }
  76.  
  77.     void Start() {
  78.        
  79.     }
  80.  
  81.  
  82.     void Update() {
  83.         if(isReady) {
  84.             auxArray = new AuxStruct[rows, cols];
  85.             CreateLevel ();
  86.             SetWayPoints ();
  87.             isReady = false;
  88.             isLevelLoaded = true;
  89.         }
  90.     }
  91.  
  92.  
  93.     public void ReadFile(string fileName) {
  94.         string text = System.IO.File.ReadAllText (prefix + fileName);
  95.         level.Clear ();
  96.         List<string> lines = new List<string>(Regex.Split (text, "\n"));
  97.  
  98.         rows = 0;
  99.         cols = -1;
  100.         r = 0;
  101.         c = 0;
  102.  
  103.         for(int i=0; i < lines.Count; ++i) {
  104.             string currentLine = lines[i].Trim();
  105.             string[] currentLineSplit = Regex.Split (currentLine, " ");
  106.  
  107.             switch(currentLineSplit[0]) {
  108.             case "Pacman":
  109.                 pacman.transform.position = StringCoordsToVec (
  110.                     currentLineSplit [1],
  111.                     pacman.transform.position.y.ToString (),
  112.                     currentLineSplit [2]);
  113.                 pacmanX = int.Parse (currentLineSplit [1]);
  114.                 pacmanZ = -int.Parse (currentLineSplit [2]);
  115.                 //Instantiate (pacman, new Vector3 (pacmanX, pacman.transform.position.y, pacmanZ), Quaternion.identity);
  116.                 break;
  117.             case "Blinky":
  118.                 blinky.transform.position = StringCoordsToVec (
  119.                     currentLineSplit [1],
  120.                     blinky.transform.position.y.ToString (),
  121.                     currentLineSplit [2]);
  122.                 blinkyX = int.Parse (currentLineSplit [1]);
  123.                 blinkyZ = -int.Parse (currentLineSplit [2]);
  124.                 //Instantiate (blinky, new Vector3 (blinkyX, blinky.transform.position.y, blinkyZ), Quaternion.identity);
  125.                 break;
  126.             case "Pinky":
  127.                 pinky.transform.position = StringCoordsToVec (
  128.                     currentLineSplit [1],
  129.                     pinky.transform.position.y.ToString (),
  130.                     currentLineSplit [2]);
  131.                 pinkyX = int.Parse (currentLineSplit [1]);
  132.                 pinkyZ = -int.Parse (currentLineSplit [2]);
  133.                 //Instantiate (pinky, new Vector3 (pinkyX, pinky.transform.position.y, pinkyZ), Quaternion.identity);
  134.                 break;
  135.             case "Inky":
  136.                 inky.transform.position = StringCoordsToVec (
  137.                     currentLineSplit [1],
  138.                     inky.transform.position.y.ToString (),
  139.                     currentLineSplit [2]);
  140.                 inkyX = int.Parse (currentLineSplit [1]);
  141.                 inkyZ = -int.Parse (currentLineSplit [2]);
  142.                 //Instantiate (inky, new Vector3 (inkyX, inky.transform.position.y, inkyZ), Quaternion.identity);
  143.                 break;
  144.             case "Clyde":
  145.                 clyde.transform.position = StringCoordsToVec (
  146.                     currentLineSplit [1],
  147.                     clyde.transform.position.y.ToString (),
  148.                     currentLineSplit [2]);
  149.                 clydeX = int.Parse (currentLineSplit [1]);
  150.                 clydeZ = -int.Parse (currentLineSplit [2]);
  151.                 //Instantiate (clyde, new Vector3 (clydeX, clyde.transform.position.y, clydeZ), Quaternion.identity);
  152.                 break;
  153.             default:
  154.                 // else it must be some level object
  155.                 level.Add (currentLine);
  156.                 rows++;
  157.                 if(cols == -1) {
  158.                     cols = currentLine.Length;
  159.                 }
  160.                 break;
  161.             } // switch()
  162.         } // for()
  163.  
  164.         isReady = true;
  165.     } // ReadFile()
  166.        
  167.  
  168.  
  169.     Vector3 StringCoordsToVec(string x, string y, string z) {
  170.         float newX = float.Parse (x);
  171.         Debug.Log (y);
  172.         float newY = float.Parse (y);
  173.         float newZ = float.Parse (z);
  174.  
  175.         return new Vector3 (newX, newY, newZ);
  176.     }
  177.  
  178.  
  179.     void CreateLevel() {
  180.         float startX = 0f;
  181.  
  182.         float x = startX;
  183.         float y = 0f;
  184.         float z = 0f;
  185.  
  186.         for(int i=0; i < level.Count; ++i) {
  187.             for(int j=0; j < level[i].Length; ++j) {
  188.                 Vector3 vec = new Vector3 (x, y, z);
  189.  
  190.  
  191.                 if(level[i][j] == sCorridorHorizontal) {
  192.                     bool isDeadEndLeft  = (j == 0);
  193.                     bool isDeadEndRight = (j == level[i].Length-1);
  194.  
  195.                     if(isDeadEndLeft) {
  196.                         // InstantiateWithRotation (deadEnd, vec, -90f);
  197.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, -90f, false, false, false, true);
  198.                     }
  199.                     else if(isDeadEndRight) {
  200.                         // InstantiateWithRotation (deadEnd, vec, 90f);
  201.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 90f, false, false, true, false);
  202.                     }
  203.                     else {
  204.                         // InstantiateWithRotation (corridor, vec, 90f);
  205.                         InstantiateWithRotationAndCreateWayPoint(corridor, vec, 90f, false, false, true, true);
  206.                     }
  207.  
  208.                 }
  209.                 else if(level[i][j] == sCorridorVertical) {
  210.                     bool isDeadEndUp    = (i == 0);
  211.                     bool isDeadEndDown  = (i == level.Count-1);
  212.  
  213.                     if(isDeadEndUp) {
  214.                         // InstantiateWithRotation (deadEnd, vec, 0f);
  215.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 0f, false, true, false, false);
  216.                     }
  217.                     else if(isDeadEndDown) {
  218.                         // InstantiateWithRotation (deadEnd, vec, 180f);
  219.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 180f, true, false, false, false);
  220.                     }
  221.                     else {
  222.                         // InstantiateWithRotation (corridor, vec, 0f);
  223.                         InstantiateWithRotationAndCreateWayPoint(corridor, vec, 0f, true, true, false, false);
  224.                     }
  225.                 }
  226.  
  227.                 char empty = ' ';
  228.                 if(level[i][j] == sCross) {
  229.                     // Debug.Log (level [i][level[i].Length-1]);
  230.                     char up     = ((i - 1) >= 0)                ? level [i - 1] [j] : empty;
  231.                     char down   = ((i + 1) < level.Count)       ? level [i + 1] [j] : empty;
  232.                     char left   = ((j - 1) >= 0)                ? level [i] [j - 1] : empty;
  233.                     char right  = ((j + 1) < level [i].Length)  ? level [i] [j + 1] : empty;
  234.  
  235.                     // important: assumes that loaded level is syntactically correct! (@see SyntaxChecker)
  236.  
  237.                     bool isLeftTopCorner        = (up == empty && left == empty && right != empty && down != empty);
  238.                     bool isLeftBottomCorner     = (up != empty && left == empty && right != empty && down == empty);
  239.                     bool isRightTopCorner       = (up == empty && left != empty && right == empty && down != empty);
  240.                     bool isRightBottomCorner    = (up != empty && left != empty && right == empty && down == empty);
  241.  
  242.                     bool isLeftTCross           = (up != empty && left == empty && right != empty && down != empty);
  243.                     bool isRightTCross          = (up != empty && left != empty && right == empty && down != empty);
  244.                     bool isTopTCross            = (up == empty && left != empty && right != empty && down != empty);
  245.                     bool isBottomTCross         = (up != empty && left != empty && right != empty && down == empty);
  246.  
  247.                     bool isCenterCross          = (up != empty && left != empty && right != empty && down != empty);
  248.  
  249.                     if(isLeftTopCorner) {
  250.                         // InstantiateWithRotation (corner, vec, -90f);
  251.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, -90f, false, true, false, true);
  252.                     }
  253.                     else if(isLeftBottomCorner) {
  254.                         // InstantiateWithRotation (corner, vec, 180f);
  255.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, 180f, true, false, false, true);
  256.                     }
  257.                     else if(isRightTopCorner) {
  258.                         // InstantiateWithRotation (corner, vec, 0f);
  259.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, 0f, false, true, true, false);
  260.                     }
  261.                     else if(isRightBottomCorner) {
  262.                         // InstantiateWithRotation (corner, vec, 90f);
  263.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, 90f, true, false, true, false);
  264.                     }
  265.                     else if(isLeftTCross) {
  266.                         // InstantiateWithRotation (tCross, vec, 0f);
  267.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, 0f, true, true, false, true);
  268.                     }
  269.                     else if(isRightTCross) {
  270.                         // InstantiateWithRotation (tCross, vec, 180f);
  271.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, 180f, true, true, true, false);
  272.                     }
  273.                     else if(isTopTCross) {
  274.                         // InstantiateWithRotation (tCross, vec, 90f);
  275.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, 90f, false, true, true, true);
  276.                     }
  277.                     else if(isBottomTCross) {
  278.                         // InstantiateWithRotation (tCross, vec, -90f);
  279.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, -90f, true, false, true, true);
  280.                     }
  281.                     else if(isCenterCross) {
  282.                         // InstantiateWithRotation (cross, vec, 0f);
  283.                         InstantiateWithRotationAndCreateWayPoint(cross, vec, 0f, true, true, true, true);
  284.                     }
  285.                 }
  286.  
  287.                 x += 1f;    // advance to the right
  288.             } // for(j)
  289.             z -= 1f;        // advance down
  290.             x = startX;     // start from the left
  291.         } // for(i)
  292.     } // CreateLevel()
  293.  
  294.     void InstantiateWithRotation(Transform obj, Vector3 vec, float deg) {
  295.         Vector3 rot = obj.transform.eulerAngles;
  296.         rot = new Vector3 (rot.x, rot.y + deg, rot.z);
  297.         Instantiate (obj, vec, Quaternion.Euler (rot));
  298.     }
  299.  
  300.  
  301.     void InstantiateWithRotationAndCreateWayPoint(Transform obj, Vector3 vec, float deg, bool goUp, bool goDown, bool goLeft, bool goRight) {
  302.         Vector3 rot = obj.transform.eulerAngles;
  303.         rot = new Vector3 (rot.x, rot.y + deg, rot.z);
  304.         Transform result = Instantiate (obj, vec, Quaternion.Euler (rot));
  305.  
  306.         AuxStruct temp;
  307.         temp.tile = result;
  308.         temp.goUp = goUp;
  309.         temp.goDown = goDown;
  310.         temp.goLeft = goLeft;
  311.         temp.goRight = goRight;
  312.  
  313.         if(r < rows && c < cols) {
  314.             auxArray [r, c++] = temp;
  315.  
  316.             if(c == cols) {
  317.                 r++;
  318.                 c = 0;
  319.             }
  320.         }
  321.  
  322.     }
  323.  
  324.     void SetWayPoints() {
  325.         // map
  326.         for(int i=0; i < rows; ++i) {
  327.             for(int j=0; j < cols; ++j) {
  328.                 AuxStruct temp = auxArray [i, j];
  329.                 Transform obj = temp.tile;
  330.  
  331.                 if(obj != null) {
  332.                     WayPoint wp = obj.GetComponent<WayPoint> ();
  333.                     if(temp.goUp) {
  334.                         wp.upWaypoint = auxArray [i - 1, j].tile.GetComponent<WayPoint> ();
  335.                     }
  336.                     if(temp.goDown) {
  337.                         wp.downWaypoint = auxArray [i + 1, j].tile.GetComponent<WayPoint> ();
  338.                     }
  339.                     if(temp.goLeft) {
  340.                         wp.leftWaypoint = auxArray [i, j - 1].tile.GetComponent<WayPoint> ();
  341.                     }
  342.                     if(temp.goRight) {
  343.                         wp.rightWaypoint = auxArray [i, j + 1].tile.GetComponent<WayPoint> ();
  344.                     }
  345.                 }
  346.             } // for(j)
  347.         } // for(i)
  348.  
  349.         Debug.Log (pacman);
  350.         Debug.Log ("pacman = (" + pacmanX + ", " + pacmanZ + ")");
  351.         Debug.Log ("blinky = (" + blinkyX + ", " + blinkyZ + ")");
  352.         Debug.Log ("pinky = (" + pinkyX + ", " + pinkyZ + ")");
  353.         Debug.Log ("inky = (" + inkyX + ", " + inkyZ + ")");
  354.         Debug.Log ("clyde = (" + clydeX + ", " + clydeZ + ")");
  355.         Debug.Log ("(r, c) = (" + rows + ", " + cols + ")");
  356.         Debug.Log ("auxArray 1st dim length = " + auxArray.GetLength (0));
  357.         Debug.Log ("auxArray 2nd dim length = " + auxArray.GetLength (1));
  358.  
  359.         Transform tile;
  360.         WayPoint wp2;
  361.  
  362.         int x = (int)pacman.transform.position.x;
  363.         int z = (int)pacman.transform.position.z;
  364.         x = pacmanX;
  365.         z = -pacmanZ;
  366.         tile = auxArray [z, x].tile;
  367.         if(tile != null) {
  368.             wp2 = tile.GetComponent<WayPoint> ();
  369.             if(wp2 != null) {
  370.                 pacman.GetComponent<PlayerControlScript> ().currentWaypoint = wp2;
  371.                 Instantiate (pacman, new Vector3 (pacmanX, pacman.transform.position.y, pacmanZ), Quaternion.identity);
  372.             }
  373.         }
  374.  
  375.  
  376.         x = (int)blinky.transform.position.x;
  377.         z = (int)blinky.transform.position.z;
  378.         x = blinkyX;
  379.         z = -blinkyZ;
  380.         tile = auxArray [z, x].tile;
  381.         if(tile != null) {
  382.             wp2 = tile.GetComponent<WayPoint> ();
  383.             if(wp2 != null) {
  384.                 blinky.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
  385.                 Instantiate (blinky, new Vector3 (blinkyX, blinky.transform.position.y, blinkyZ), Quaternion.identity);
  386.             }
  387.         }
  388.  
  389.  
  390.  
  391.         x = (int)pinky.transform.position.x;
  392.         z = (int)pinky.transform.position.z;
  393.         x = pinkyX;
  394.         z = -pinkyZ;
  395.         tile = auxArray [z, x].tile;
  396.         if(tile != null) {
  397.             wp2 = tile.GetComponent<WayPoint> ();
  398.             if(wp2 != null) {
  399.                 pinky.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
  400.                 Instantiate (pinky, new Vector3 (pinkyX, pinky.transform.position.y, pinkyZ), Quaternion.identity);
  401.             }
  402.         }
  403.  
  404.  
  405.  
  406.         x = (int)inky.transform.position.x;
  407.         z = (int)inky.transform.position.z;
  408.         x = inkyX;
  409.         z = -inkyZ;
  410.         tile = auxArray [z, x].tile;
  411.         if(tile != null) {
  412.             wp2 = tile.GetComponent<WayPoint> ();
  413.             if(wp2 != null) {
  414.                 inky.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
  415.                 Instantiate (inky, new Vector3 (inkyX, inky.transform.position.y, inkyZ), Quaternion.identity);
  416.             }
  417.         }
  418.  
  419.  
  420.         x = (int)clyde.transform.position.x;
  421.         z = (int)clyde.transform.position.z;
  422.         x = clydeX;
  423.         z = -clydeZ;
  424.         tile = auxArray [z, x].tile;
  425.         if(tile != null) {
  426.             wp2 = tile.GetComponent<WayPoint> ();
  427.             if(wp2 != null) {
  428.                 clyde.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
  429.                 Instantiate (clyde, new Vector3 (clydeX, clyde.transform.position.y, clydeZ), Quaternion.identity);
  430.             }
  431.         }
  432.  
  433.     } // SetWayPoints()
  434.  
  435.  
  436.     public bool IsLevelLoaded() {
  437.         return isLevelLoaded;
  438.     }
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement