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;
- // player coods
- int pacmanX;
- int pacmanZ;
- // enemy prefabs
- public Transform blinky;
- public Transform pinky;
- public Transform inky;
- public Transform clyde;
- // enemy coords
- int blinkyX;
- int blinkyZ;
- int pinkyX;
- int pinkyZ;
- int inkyX;
- int inkyZ;
- int clydeX;
- int clydeZ;
- // 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;
- private bool isLevelLoaded;
- // 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>();
- AuxStruct[,] auxArray;
- int rows = 0;
- int cols = -1;
- int r = 0;
- int c = 0;
- void Awake() {
- instance = this;
- }
- void Start() {
- }
- void Update() {
- if(isReady) {
- auxArray = new AuxStruct[rows, cols];
- CreateLevel ();
- SetWayPoints ();
- isReady = false;
- isLevelLoaded = true;
- }
- }
- 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"));
- rows = 0;
- cols = -1;
- r = 0;
- c = 0;
- 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]);
- pacmanX = int.Parse (currentLineSplit [1]);
- pacmanZ = -int.Parse (currentLineSplit [2]);
- //Instantiate (pacman, new Vector3 (pacmanX, pacman.transform.position.y, pacmanZ), Quaternion.identity);
- break;
- case "Blinky":
- blinky.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- blinky.transform.position.y.ToString (),
- currentLineSplit [2]);
- blinkyX = int.Parse (currentLineSplit [1]);
- blinkyZ = -int.Parse (currentLineSplit [2]);
- //Instantiate (blinky, new Vector3 (blinkyX, blinky.transform.position.y, blinkyZ), Quaternion.identity);
- break;
- case "Pinky":
- pinky.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- pinky.transform.position.y.ToString (),
- currentLineSplit [2]);
- pinkyX = int.Parse (currentLineSplit [1]);
- pinkyZ = -int.Parse (currentLineSplit [2]);
- //Instantiate (pinky, new Vector3 (pinkyX, pinky.transform.position.y, pinkyZ), Quaternion.identity);
- break;
- case "Inky":
- inky.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- inky.transform.position.y.ToString (),
- currentLineSplit [2]);
- inkyX = int.Parse (currentLineSplit [1]);
- inkyZ = -int.Parse (currentLineSplit [2]);
- //Instantiate (inky, new Vector3 (inkyX, inky.transform.position.y, inkyZ), Quaternion.identity);
- break;
- case "Clyde":
- clyde.transform.position = StringCoordsToVec (
- currentLineSplit [1],
- clyde.transform.position.y.ToString (),
- currentLineSplit [2]);
- clydeX = int.Parse (currentLineSplit [1]);
- clydeZ = -int.Parse (currentLineSplit [2]);
- //Instantiate (clyde, new Vector3 (clydeX, clyde.transform.position.y, clydeZ), Quaternion.identity);
- break;
- default:
- // else it must be some level object
- level.Add (currentLine);
- rows++;
- if(cols == -1) {
- cols = currentLine.Length;
- }
- 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, true);
- }
- else if(isRightTCross) {
- // InstantiateWithRotation (tCross, vec, 180f);
- InstantiateWithRotationAndCreateWayPoint(tCross, vec, 180f, true, true, true, false);
- }
- else if(isTopTCross) {
- // InstantiateWithRotation (tCross, vec, 90f);
- InstantiateWithRotationAndCreateWayPoint(tCross, vec, 90f, false, true, true, true);
- }
- else if(isBottomTCross) {
- // InstantiateWithRotation (tCross, vec, -90f);
- InstantiateWithRotationAndCreateWayPoint(tCross, vec, -90f, true, 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()
- void InstantiateWithRotation(Transform obj, Vector3 vec, float deg) {
- Vector3 rot = obj.transform.eulerAngles;
- rot = new Vector3 (rot.x, rot.y + deg, rot.z);
- Instantiate (obj, vec, Quaternion.Euler (rot));
- }
- 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;
- if(r < rows && c < cols) {
- auxArray [r, c++] = temp;
- if(c == cols) {
- r++;
- c = 0;
- }
- }
- }
- void SetWayPoints() {
- // map
- for(int i=0; i < rows; ++i) {
- for(int j=0; j < cols; ++j) {
- AuxStruct temp = auxArray [i, j];
- Transform obj = temp.tile;
- if(obj != null) {
- WayPoint wp = obj.GetComponent<WayPoint> ();
- if(temp.goUp) {
- wp.upWaypoint = auxArray [i - 1, j].tile.GetComponent<WayPoint> ();
- }
- if(temp.goDown) {
- wp.downWaypoint = auxArray [i + 1, j].tile.GetComponent<WayPoint> ();
- }
- if(temp.goLeft) {
- wp.leftWaypoint = auxArray [i, j - 1].tile.GetComponent<WayPoint> ();
- }
- if(temp.goRight) {
- wp.rightWaypoint = auxArray [i, j + 1].tile.GetComponent<WayPoint> ();
- }
- }
- } // for(j)
- } // for(i)
- Debug.Log (pacman);
- Debug.Log ("pacman = (" + pacmanX + ", " + pacmanZ + ")");
- Debug.Log ("blinky = (" + blinkyX + ", " + blinkyZ + ")");
- Debug.Log ("pinky = (" + pinkyX + ", " + pinkyZ + ")");
- Debug.Log ("inky = (" + inkyX + ", " + inkyZ + ")");
- Debug.Log ("clyde = (" + clydeX + ", " + clydeZ + ")");
- Debug.Log ("(r, c) = (" + rows + ", " + cols + ")");
- Debug.Log ("auxArray 1st dim length = " + auxArray.GetLength (0));
- Debug.Log ("auxArray 2nd dim length = " + auxArray.GetLength (1));
- Transform tile;
- WayPoint wp2;
- int x = (int)pacman.transform.position.x;
- int z = (int)pacman.transform.position.z;
- x = pacmanX;
- z = -pacmanZ;
- tile = auxArray [z, x].tile;
- if(tile != null) {
- wp2 = tile.GetComponent<WayPoint> ();
- if(wp2 != null) {
- pacman.GetComponent<PlayerControlScript> ().currentWaypoint = wp2;
- Instantiate (pacman, new Vector3 (pacmanX, pacman.transform.position.y, pacmanZ), Quaternion.identity);
- }
- }
- x = (int)blinky.transform.position.x;
- z = (int)blinky.transform.position.z;
- x = blinkyX;
- z = -blinkyZ;
- tile = auxArray [z, x].tile;
- if(tile != null) {
- wp2 = tile.GetComponent<WayPoint> ();
- if(wp2 != null) {
- blinky.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
- Instantiate (blinky, new Vector3 (blinkyX, blinky.transform.position.y, blinkyZ), Quaternion.identity);
- }
- }
- x = (int)pinky.transform.position.x;
- z = (int)pinky.transform.position.z;
- x = pinkyX;
- z = -pinkyZ;
- tile = auxArray [z, x].tile;
- if(tile != null) {
- wp2 = tile.GetComponent<WayPoint> ();
- if(wp2 != null) {
- pinky.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
- Instantiate (pinky, new Vector3 (pinkyX, pinky.transform.position.y, pinkyZ), Quaternion.identity);
- }
- }
- x = (int)inky.transform.position.x;
- z = (int)inky.transform.position.z;
- x = inkyX;
- z = -inkyZ;
- tile = auxArray [z, x].tile;
- if(tile != null) {
- wp2 = tile.GetComponent<WayPoint> ();
- if(wp2 != null) {
- inky.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
- Instantiate (inky, new Vector3 (inkyX, inky.transform.position.y, inkyZ), Quaternion.identity);
- }
- }
- x = (int)clyde.transform.position.x;
- z = (int)clyde.transform.position.z;
- x = clydeX;
- z = -clydeZ;
- tile = auxArray [z, x].tile;
- if(tile != null) {
- wp2 = tile.GetComponent<WayPoint> ();
- if(wp2 != null) {
- clyde.GetComponent<EnemyBehaviourScript> ().currentWaypoint = wp2;
- Instantiate (clyde, new Vector3 (clydeX, clyde.transform.position.y, clydeZ), Quaternion.identity);
- }
- }
- } // SetWayPoints()
- public bool IsLevelLoaded() {
- return isLevelLoaded;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement