Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Text.RegularExpressions;
- public class LevelManager : MonoBehaviour {
- // singleton
- public static LevelManager instance;
- // file prefix
- private readonly string prefix = "Assets/Levels/";
- // player prefab
- public Transform pacman;
- // enemy prefabs
- public Transform blinky;
- public Transform pinky;
- public Transform inky;
- public Transform clyde;
- // walls
- public Transform corridor;
- public Transform corner;
- public Transform cross;
- public Transform tCross;
- public Transform deadEnd;
- // corresponding strings
- readonly char sCorridorHorizontal = '-';
- readonly char sCorridorVertical = '|';
- readonly char sCross = '+'; // could be cross or tCross or corner
- // the level as a string
- private List<string> level = new List<string> ();
- private bool isReady;
- // the waypoints
- private List<WayPoint> wayPoints = new List<WayPoint>();
- // a struct that helps setting the wayPoints
- struct AuxStruct {
- public Transform tile;
- public bool goUp;
- public bool goDown;
- public bool goLeft;
- public bool goRight;
- };
- List<AuxStruct> auxList = new List<AuxStruct>();
- void Awake() {
- instance = this;
- }
- void Update() {
- if(isReady) {
- CreateLevel ();
- isReady = false;
- }
- }
- public void ReadFile(string fileName) {
- string text = System.IO.File.ReadAllText (prefix + fileName);
- level.Clear ();
- List<string> lines = new List<string>(Regex.Split (text, "\n"));
- for(int i=0; i < lines.Count; ++i) {
- string currentLine = lines[i].Trim();
- string[] currentLineSplit = Regex.Split (currentLine, " ");
- switch(currentLineSplit[0]) {
- case "Pacman":
- pacman.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- pacman.transform.position.y.ToString (),
- currentLineSplit [2]);
- break;
- case "Blinky":
- blinky.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- blinky.transform.position.y.ToString (),
- currentLineSplit [2]);
- break;
- case "Pinky":
- pinky.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- pinky.transform.position.y.ToString (),
- currentLineSplit [2]);
- break;
- case "Inky":
- inky.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- inky.transform.position.y.ToString (),
- currentLineSplit [2]);
- break;
- case "Clyde":
- clyde.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- clyde.transform.position.y.ToString (),
- currentLineSplit [2]);
- break;
- default:
- // else it must be some level object
- level.Add(currentLine);
- break;
- } // switch()
- } // for()
- isReady = true;
- } // ReadFile()
- Vector3 StringCoordsToVec(string x, string y, string z) {
- float newX = float.Parse (x);
- Debug.Log (y);
- float newY = float.Parse (y);
- float newZ = float.Parse (z);
- return new Vector3 (newX, newY, newZ);
- }
- void CreateLevel() {
- float startX = 0f;
- float x = startX;
- float y = 0f;
- float z = 0f;
- for(int i=0; i < level.Count; ++i) {
- for(int j=0; j < level[i].Length; ++j) {
- Vector3 vec = new Vector3 (x, y, z);
- if(level[i][j] == sCorridorHorizontal) {
- bool isDeadEndLeft = (j == 0);
- bool isDeadEndRight = (j == level[i].Length-1);
- if(isDeadEndLeft) {
- // InstantiateWithRotation (deadEnd, vec, -90f);
- InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, -90f, false, false, false, true);
- }
- else if(isDeadEndRight) {
- // InstantiateWithRotation (deadEnd, vec, 90f);
- InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 90f, false, false, true, false);
- }
- else {
- // InstantiateWithRotation (corridor, vec, 90f);
- InstantiateWithRotationAndCreateWayPoint(corridor, vec, 90f, false, false, true, true);
- }
- }
- else if(level[i][j] == sCorridorVertical) {
- bool isDeadEndUp = (i == 0);
- bool isDeadEndDown = (i == level.Count-1);
- if(isDeadEndUp) {
- // InstantiateWithRotation (deadEnd, vec, 0f);
- InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 0f, false, true, false, false);
- }
- else if(isDeadEndDown) {
- // InstantiateWithRotation (deadEnd, vec, 180f);
- InstantiateWithRotationAndCreateWayPoint(deadEnd, vec, 180f, true, false, false, false);
- }
- else {
- // InstantiateWithRotation (corridor, vec, 0f);
- InstantiateWithRotationAndCreateWayPoint(corridor, vec, 0f, true, true, false, false);
- }
- }
- char empty = ' ';
- if(level[i][j] == sCross) {
- Debug.Log (level [i][level[i].Length-1]);
- char up = ((i - 1) >= 0) ? level [i - 1] [j] : empty;
- char down = ((i + 1) < level.Count) ? level [i + 1] [j] : empty;
- char left = ((j - 1) >= 0) ? level [i] [j - 1] : empty;
- char right = ((j + 1) < level [i].Length) ? level [i] [j + 1] : empty;
- // important: assumes that loaded level is syntactically correct! (@see SyntaxChecker)
- bool isLeftTopCorner = (up == empty && left == empty && right != empty && down != empty);
- bool isLeftBottomCorner = (up != empty && left == empty && right != empty && down == empty);
- bool isRightTopCorner = (up == empty && left != empty && right == empty && down != empty);
- bool isRightBottomCorner = (up != empty && left != empty && right == empty && down == empty);
- bool isLeftTCross = (up != empty && left == empty && right != empty && down != empty);
- bool isRightTCross = (up != empty && left != empty && right == empty && down != empty);
- bool isTopTCross = (up == empty && left != empty && right != empty && down != empty);
- bool isBottomTCross = (up != empty && left != empty && right != empty && down == empty);
- bool isCenterCross = (up != empty && left != empty && right != empty && down != empty);
- if(isLeftTopCorner) {
- // InstantiateWithRotation (corner, vec, -90f);
- InstantiateWithRotationAndCreateWayPoint(corner, vec, -90f, false, true, false, true);
- }
- else if(isLeftBottomCorner) {
- // InstantiateWithRotation (corner, vec, 180f);
- InstantiateWithRotationAndCreateWayPoint(corner, vec, 180f, true, false, false, true);
- }
- else if(isRightTopCorner) {
- // InstantiateWithRotation (corner, vec, 0f);
- InstantiateWithRotationAndCreateWayPoint(corner, vec, 0f, false, true, true, false);
- }
- else if(isRightBottomCorner) {
- // InstantiateWithRotation (corner, vec, 90f);
- InstantiateWithRotationAndCreateWayPoint(corner, vec, 90f, true, false, true, false);
- }
- else if(isLeftTCross) {
- // InstantiateWithRotation (tCross, vec, 0f);
- InstantiateWithRotationAndCreateWayPoint(tCross, vec, 0f, true, true, false, false);
- }
- else if(isRightTCross) {
- // InstantiateWithRotation (tCross, vec, 180f);
- InstantiateWithRotationAndCreateWayPoint(tCross, vec, 180f, true, true, false, false);
- }
- else if(isTopTCross) {
- // InstantiateWithRotation (tCross, vec, 90f);
- InstantiateWithRotationAndCreateWayPoint(tCross, vec, 90f, false, false, true, true);
- }
- else if(isBottomTCross) {
- // InstantiateWithRotation (tCross, vec, -90f);
- InstantiateWithRotationAndCreateWayPoint(tCross, vec, -90f, false, false, true, true);
- }
- else if(isCenterCross) {
- // InstantiateWithRotation (cross, vec, 0f);
- InstantiateWithRotationAndCreateWayPoint(cross, vec, 0f, true, true, true, true);
- }
- }
- x += 1f; // advance to the right
- } // for(j)
- z -= 1f; // advance down
- x = startX; // start from the left
- } // for(i)
- } // CreateLevel()
- Transform InstantiateWithRotation(Transform obj, Vector3 vec, float deg) {
- Vector3 rot = obj.transform.eulerAngles;
- rot = new Vector3 (rot.x, rot.y + deg, rot.z);
- Transform result = Instantiate (obj, vec, Quaternion.Euler (rot));
- wayPoints.Add (result.GetComponent<WayPoint> ());
- return result;
- }
- void InstantiateWithRotationAndCreateWayPoint(Transform obj, Vector3 vec, float deg, bool goUp, bool goDown, bool goLeft, bool goRight) {
- Vector3 rot = obj.transform.eulerAngles;
- rot = new Vector3 (rot.x, rot.y + deg, rot.z);
- Transform result = Instantiate (obj, vec, Quaternion.Euler (rot));
- AuxStruct temp;
- temp.tile = result;
- temp.goUp = goUp;
- temp.goDown = goDown;
- temp.goLeft = goLeft;
- temp.goRight = goRight;
- // insert temporary placeholders to know which way we can go when assigning waypoints later
- /*WayPoint temp = result.GetComponent<WayPoint> ();
- if(goUp) {
- temp.upWaypoint = new WayPoint ();
- }
- if(goDown) {
- temp.downWaypoint = new WayPoint ();
- }
- if(goLeft) {
- temp.leftWaypoint = new WayPoint ();
- }
- if(goRight) {
- temp.rightWaypoint = new WayPoint ();
- }
- wayPoints.Add (temp);*/
- }
- void Wp() {
- int idx = 0;
- int rowLength = level [0].Length;
- for(int i=0; i < level.Count; ++i) {
- for(int j=0; j < level[i].Length; ++j) {
- AuxStruct temp = auxList [j + rowLength * i];
- WayPoint w = temp.tile.GetComponent<WayPoint> ();;
- if(temp.goUp) {
- w.upWaypoint = auxList [idx - level [i].Length].tile.GetComponent<WayPoint> ();
- }
- if(temp.goDown) {
- w.upWaypoint = auxList [idx + level [i].Length].tile.GetComponent<WayPoint> ();
- }
- if(temp.goLeft) {
- w.upWaypoint = auxList [idx - 1].tile.GetComponent<WayPoint> ();
- }
- if(temp.goRight) {
- w.upWaypoint = auxList [idx + 1].tile.GetComponent<WayPoint> ();
- }
- /*WayPoint wp = wayPoints [j + rowLength * i];
- if(wp.upWaypoint != null) {
- wp.upWaypoint = wayPoints [idx - level [i].Length];
- }
- if(wp.downWaypoint != null) {
- wp.downWaypoint = wayPoints [idx + level [i].Length];
- }
- if(wp.leftWaypoint != null) {
- wp.leftWaypoint = wayPoints [idx - 1];
- }
- if(wp.rightWaypoint != null) {
- wp.leftWaypoint = wayPoints [idx + 1];
- }*/
- idx++;
- }
- }
- }
- void AssignWayPoints() {
- GameObject[] gos = GameObject.FindGameObjectsWithTag ("Tile");
- List<List<WayPoint>> wps = new List<List<WayPoint>> ();
- List<WayPoint> temp = new List<WayPoint> ();
- foreach(GameObject go in gos) {
- WayPoint wp = go.GetComponent<WayPoint> ();
- temp.Add (wp);
- }
- // create a "2D array" of waypoints (that corresponds to the level format)
- for(int i=0; i < level.Count; ++i) {
- wps [i] = new List<WayPoint> ();
- for(int j=0; j < level[i].Length; ++j) {
- wps [i].Add (temp [j + level [i].Length * i]);
- }
- }
- for(int i=0; i < wps.Count; ++i) {
- for(int j=0; j < wps[i].Count; ++j) {
- WayPoint currentWayPoint = wps [i] [j];
- // TODO: need to consider type of object (e.g. cross or corridor or ...)
- // up
- if(i > 0 && level[i-1][j] != ' ' && level[i-1][j] != '-') {
- currentWayPoint.upWaypoint = wps [i - 1] [j];
- }
- // down
- if(i < wps.Count - 1 && level[i+1][j] != ' ' && level[i+1][j] != '-') {
- currentWayPoint.upWaypoint = wps [i - 1] [j];
- }
- // left
- if(j > 0 && level[i][j-1] != ' ' && level[i][j-1] != '|') {
- currentWayPoint.upWaypoint = wps [i - 1] [j];
- }
- // right
- if(j < wps[i].Count - 1 && level[i][j+1] != ' ' && level[i][j+1] != '|') {
- currentWayPoint.upWaypoint = wps [i - 1] [j];
- }
- }
- }
- } // AssignWayPoints()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement