Advertisement
bekovski

sg04

May 30th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.42 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.     // enemy prefabs
  19.     public Transform blinky;
  20.     public Transform pinky;
  21.     public Transform inky;
  22.     public Transform clyde;
  23.  
  24.     // walls
  25.     public Transform corridor;
  26.     public Transform corner;
  27.     public Transform cross;
  28.     public Transform tCross;
  29.     public Transform deadEnd;
  30.  
  31.     // corresponding strings
  32.     readonly char sCorridorHorizontal = '-';
  33.     readonly char sCorridorVertical = '|';
  34.     readonly char sCross = '+';             // could be cross or tCross or corner
  35.  
  36.     // the level as a string
  37.     private List<string> level = new List<string> ();
  38.     private bool isReady;
  39.  
  40.     // the waypoints
  41.     private List<WayPoint> wayPoints = new List<WayPoint>();
  42.  
  43.     // a struct that helps setting the wayPoints
  44.     struct AuxStruct {
  45.         public Transform tile;
  46.         public bool goUp;
  47.         public bool goDown;
  48.         public bool goLeft;
  49.         public bool goRight;
  50.     };
  51.     List<AuxStruct> auxList = new List<AuxStruct>();
  52.  
  53.     void Awake() {
  54.         instance = this;
  55.     }
  56.  
  57.  
  58.     void Update() {
  59.         if(isReady) {
  60.             CreateLevel ();
  61.             isReady = false;
  62.         }
  63.     }
  64.  
  65.  
  66.     public void ReadFile(string fileName) {
  67.         string text = System.IO.File.ReadAllText (prefix + fileName);
  68.         level.Clear ();
  69.         List<string> lines = new List<string>(Regex.Split (text, "\n"));
  70.  
  71.         for(int i=0; i < lines.Count; ++i) {
  72.             string currentLine = lines[i].Trim();
  73.             string[] currentLineSplit = Regex.Split (currentLine, " ");
  74.  
  75.             switch(currentLineSplit[0]) {
  76.             case "Pacman":
  77.                 pacman.transform.position = StringCoordsToVec (
  78.                     currentLineSplit [1],
  79.                     pacman.transform.position.y.ToString (),
  80.                     currentLineSplit [2]);
  81.                 break;
  82.             case "Blinky":
  83.                 blinky.transform.position = StringCoordsToVec (
  84.                     currentLineSplit [1],
  85.                     blinky.transform.position.y.ToString (),
  86.                     currentLineSplit [2]);
  87.                 break;
  88.             case "Pinky":
  89.                 pinky.transform.position = StringCoordsToVec (
  90.                     currentLineSplit [1],
  91.                     pinky.transform.position.y.ToString (),
  92.                     currentLineSplit [2]);
  93.                 break;
  94.             case "Inky":
  95.                 inky.transform.position = StringCoordsToVec (
  96.                     currentLineSplit [1],
  97.                     inky.transform.position.y.ToString (),
  98.                     currentLineSplit [2]);
  99.                 break;
  100.             case "Clyde":
  101.                 clyde.transform.position = StringCoordsToVec (
  102.                     currentLineSplit [1],
  103.                     clyde.transform.position.y.ToString (),
  104.                     currentLineSplit [2]);
  105.                 break;
  106.             default:
  107.                 // else it must be some level object
  108.                 level.Add(currentLine);
  109.                 break;
  110.             } // switch()
  111.         } // for()
  112.  
  113.         isReady = true;
  114.     } // ReadFile()
  115.        
  116.  
  117.  
  118.     Vector3 StringCoordsToVec(string x, string y, string z) {
  119.         float newX = float.Parse (x);
  120.         Debug.Log (y);
  121.         float newY = float.Parse (y);
  122.         float newZ = float.Parse (z);
  123.  
  124.         return new Vector3 (newX, newY, newZ);
  125.     }
  126.  
  127.  
  128.     void CreateLevel() {
  129.         float startX = 0f;
  130.  
  131.         float x = startX;
  132.         float y = 0f;
  133.         float z = 0f;
  134.  
  135.         for(int i=0; i < level.Count; ++i) {
  136.             for(int j=0; j < level[i].Length; ++j) {
  137.                 Vector3 vec = new Vector3 (x, y, z);
  138.  
  139.  
  140.                 if(level[i][j] == sCorridorHorizontal) {
  141.                     bool isDeadEndLeft  = (j == 0);
  142.                     bool isDeadEndRight = (j == level[i].Length-1);
  143.  
  144.                     if(isDeadEndLeft) {
  145.                         // InstantiateWithRotation (deadEnd, vec, -90f);
  146.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, -90f, false, false, false, true);
  147.                     }
  148.                     else if(isDeadEndRight) {
  149.                         // InstantiateWithRotation (deadEnd, vec, 90f);
  150.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 90f, false, false, true, false);
  151.                     }
  152.                     else {
  153.                         // InstantiateWithRotation (corridor, vec, 90f);
  154.                         InstantiateWithRotationAndCreateWayPoint(corridor, vec, 90f, false, false, true, true);
  155.                     }
  156.  
  157.                 }
  158.                 else if(level[i][j] == sCorridorVertical) {
  159.                     bool isDeadEndUp    = (i == 0);
  160.                     bool isDeadEndDown  = (i == level.Count-1);
  161.  
  162.                     if(isDeadEndUp) {
  163.                         // InstantiateWithRotation (deadEnd, vec, 0f);
  164.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 0f, false, true, false, false);
  165.                     }
  166.                     else if(isDeadEndDown) {
  167.                         // InstantiateWithRotation (deadEnd, vec, 180f);
  168.                         InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 180f, true, false, false, false);
  169.                     }
  170.                     else {
  171.                         // InstantiateWithRotation (corridor, vec, 0f);
  172.                         InstantiateWithRotationAndCreateWayPoint(corridor, vec, 0f, true, true, false, false);
  173.                     }
  174.                 }
  175.  
  176.                 char empty = ' ';
  177.                 if(level[i][j] == sCross) {
  178.                     Debug.Log (level [i][level[i].Length-1]);
  179.                     char up     = ((i - 1) >= 0)                ? level [i - 1] [j] : empty;
  180.                     char down   = ((i + 1) < level.Count)       ? level [i + 1] [j] : empty;
  181.                     char left   = ((j - 1) >= 0)                ? level [i] [j - 1] : empty;
  182.                     char right  = ((j + 1) < level [i].Length)  ? level [i] [j + 1] : empty;
  183.  
  184.                     // important: assumes that loaded level is syntactically correct! (@see SyntaxChecker)
  185.  
  186.                     bool isLeftTopCorner        = (up == empty && left == empty && right != empty && down != empty);
  187.                     bool isLeftBottomCorner     = (up != empty && left == empty && right != empty && down == empty);
  188.                     bool isRightTopCorner       = (up == empty && left != empty && right == empty && down != empty);
  189.                     bool isRightBottomCorner    = (up != empty && left != empty && right == empty && down == empty);
  190.  
  191.                     bool isLeftTCross           = (up != empty && left == empty && right != empty && down != empty);
  192.                     bool isRightTCross          = (up != empty && left != empty && right == empty && down != empty);
  193.                     bool isTopTCross            = (up == empty && left != empty && right != empty && down != empty);
  194.                     bool isBottomTCross         = (up != empty && left != empty && right != empty && down == empty);
  195.  
  196.                     bool isCenterCross          = (up != empty && left != empty && right != empty && down != empty);
  197.  
  198.                     if(isLeftTopCorner) {
  199.                         // InstantiateWithRotation (corner, vec, -90f);
  200.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, -90f, false, true, false, true);
  201.                     }
  202.                     else if(isLeftBottomCorner) {
  203.                         // InstantiateWithRotation (corner, vec, 180f);
  204.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, 180f, true, false, false, true);
  205.                     }
  206.                     else if(isRightTopCorner) {
  207.                         // InstantiateWithRotation (corner, vec, 0f);
  208.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, 0f, false, true, true, false);
  209.                     }
  210.                     else if(isRightBottomCorner) {
  211.                         // InstantiateWithRotation (corner, vec, 90f);
  212.                         InstantiateWithRotationAndCreateWayPoint(corner, vec, 90f, true, false, true, false);
  213.                     }
  214.                     else if(isLeftTCross) {
  215.                         // InstantiateWithRotation (tCross, vec, 0f);
  216.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, 0f, true, true, false, false);
  217.                     }
  218.                     else if(isRightTCross) {
  219.                         // InstantiateWithRotation (tCross, vec, 180f);
  220.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, 180f, true, true, false, false);
  221.                     }
  222.                     else if(isTopTCross) {
  223.                         // InstantiateWithRotation (tCross, vec, 90f);
  224.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, 90f, false, false, true, true);
  225.                     }
  226.                     else if(isBottomTCross) {
  227.                         // InstantiateWithRotation (tCross, vec, -90f);
  228.                         InstantiateWithRotationAndCreateWayPoint(tCross, vec, -90f, false, false, true, true);
  229.                     }
  230.                     else if(isCenterCross) {
  231.                         // InstantiateWithRotation (cross, vec, 0f);
  232.                         InstantiateWithRotationAndCreateWayPoint(cross, vec, 0f, true, true, true, true);
  233.                     }
  234.                 }
  235.  
  236.                 x += 1f;    // advance to the right
  237.             } // for(j)
  238.             z -= 1f;        // advance down
  239.             x = startX;     // start from the left
  240.         } // for(i)
  241.     } // CreateLevel()
  242.  
  243.     Transform InstantiateWithRotation(Transform obj, Vector3 vec, float deg) {
  244.         Vector3 rot = obj.transform.eulerAngles;
  245.         rot = new Vector3 (rot.x, rot.y + deg, rot.z);
  246.         Transform result = Instantiate (obj, vec, Quaternion.Euler (rot));
  247.         wayPoints.Add (result.GetComponent<WayPoint> ());
  248.         return result;
  249.     }
  250.  
  251.  
  252.     void InstantiateWithRotationAndCreateWayPoint(Transform obj, Vector3 vec, float deg, bool goUp, bool goDown, bool goLeft, bool goRight) {
  253.         Vector3 rot = obj.transform.eulerAngles;
  254.         rot = new Vector3 (rot.x, rot.y + deg, rot.z);
  255.         Transform result = Instantiate (obj, vec, Quaternion.Euler (rot));
  256.  
  257.         AuxStruct temp;
  258.         temp.tile       = result;
  259.         temp.goUp       = goUp;
  260.         temp.goDown     = goDown;
  261.         temp.goLeft     = goLeft;
  262.         temp.goRight    = goRight;
  263.  
  264.  
  265.         // insert temporary placeholders to know which way we can go when assigning waypoints later
  266.         /*WayPoint temp = result.GetComponent<WayPoint> ();
  267.         if(goUp) {
  268.             temp.upWaypoint = new WayPoint ();
  269.         }
  270.         if(goDown) {
  271.             temp.downWaypoint = new WayPoint ();
  272.         }
  273.         if(goLeft) {
  274.             temp.leftWaypoint = new WayPoint ();
  275.         }
  276.         if(goRight) {
  277.             temp.rightWaypoint = new WayPoint ();
  278.         }
  279.  
  280.         wayPoints.Add (temp);*/
  281.     }
  282.  
  283.  
  284.     void Wp() {
  285.         int idx = 0;
  286.         int rowLength = level [0].Length;
  287.  
  288.         for(int i=0; i < level.Count; ++i) {
  289.             for(int j=0; j < level[i].Length; ++j) {
  290.                 AuxStruct temp = auxList [j + rowLength * i];
  291.  
  292.                 WayPoint w = temp.tile.GetComponent<WayPoint> ();;
  293.                 if(temp.goUp) {
  294.                     w.upWaypoint = auxList [idx - level [i].Length].tile.GetComponent<WayPoint> ();
  295.                 }
  296.                 if(temp.goDown) {
  297.                     w.upWaypoint = auxList [idx + level [i].Length].tile.GetComponent<WayPoint> ();
  298.                 }
  299.                 if(temp.goLeft) {
  300.                     w.upWaypoint = auxList [idx - 1].tile.GetComponent<WayPoint> ();
  301.                 }
  302.                 if(temp.goRight) {
  303.                     w.upWaypoint = auxList [idx + 1].tile.GetComponent<WayPoint> ();
  304.                 }
  305.  
  306.  
  307.                 /*WayPoint wp = wayPoints [j + rowLength * i];
  308.  
  309.                 if(wp.upWaypoint != null) {
  310.                     wp.upWaypoint = wayPoints [idx - level [i].Length];
  311.                 }
  312.                 if(wp.downWaypoint != null) {
  313.                     wp.downWaypoint = wayPoints [idx + level [i].Length];
  314.                 }
  315.                 if(wp.leftWaypoint != null) {
  316.                     wp.leftWaypoint = wayPoints [idx - 1];
  317.                 }
  318.                 if(wp.rightWaypoint != null) {
  319.                     wp.leftWaypoint = wayPoints [idx + 1];
  320.                 }*/
  321.  
  322.                 idx++;
  323.             }
  324.         }
  325.     }
  326.  
  327.  
  328.     void AssignWayPoints() {
  329.         GameObject[] gos = GameObject.FindGameObjectsWithTag ("Tile");
  330.  
  331.         List<List<WayPoint>> wps = new List<List<WayPoint>> ();
  332.         List<WayPoint> temp = new List<WayPoint> ();
  333.         foreach(GameObject go in gos) {
  334.             WayPoint wp = go.GetComponent<WayPoint> ();
  335.             temp.Add (wp);
  336.         }
  337.  
  338.         // create a "2D array" of waypoints (that corresponds to the level format)
  339.         for(int i=0; i < level.Count; ++i) {
  340.             wps [i] = new List<WayPoint> ();
  341.             for(int j=0; j < level[i].Length; ++j) {
  342.                 wps [i].Add (temp [j + level [i].Length * i]);
  343.             }
  344.         }
  345.  
  346.         for(int i=0; i < wps.Count; ++i) {
  347.             for(int j=0; j < wps[i].Count; ++j) {
  348.                 WayPoint currentWayPoint = wps [i] [j];
  349.  
  350.                 // TODO: need to consider type of object (e.g. cross or corridor or ...)
  351.  
  352.                 // up
  353.                 if(i > 0 && level[i-1][j] != ' ' && level[i-1][j] != '-') {
  354.                     currentWayPoint.upWaypoint = wps [i - 1] [j];
  355.                 }
  356.  
  357.                 // down
  358.                 if(i < wps.Count - 1 && level[i+1][j] != ' ' && level[i+1][j] != '-') {
  359.                     currentWayPoint.upWaypoint = wps [i - 1] [j];
  360.                 }
  361.  
  362.                 // left
  363.                 if(j > 0 && level[i][j-1] != ' ' && level[i][j-1] != '|') {
  364.                     currentWayPoint.upWaypoint = wps [i - 1] [j];
  365.                 }
  366.  
  367.                 // right
  368.                 if(j < wps[i].Count - 1 && level[i][j+1] != ' ' && level[i][j+1] != '|') {
  369.                     currentWayPoint.upWaypoint = wps [i - 1] [j];
  370.                 }
  371.             }
  372.         }
  373.     } // AssignWayPoints()
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement