예제 #1
0
 /**
  * @brief Create level parse from file.
  *
  * @param int levelNo
  * @return Level
  **/
 public void CreateLevel(int levelNo, List<Enemy> ignoreEnemies)
 {
     Dictionary<Vector2, char> charMap = new Dictionary<Vector2, char>();
     TextAsset asset = (TextAsset)Resources.Load ("Levels/Level" + levelNo.ToString (), typeof(TextAsset));
     levelObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
     levelObject.renderer.enabled = false;
     levelObject.name = "Level" + levelNo;
     string[] lines = asset.text.Split ('\n');
     int width = 0;
     int height = lines.Length;
     for (int y = 0; y < lines.Length; ++y) {
         string line = lines [y];
         for (int x = 0; x < line.Length; ++x) {
             if (line.Length > width)
                 width = line.Length;
             char c = line [x];
             charMap.Add (new Vector2 (x, y), c);
         }
     }
     Level level = new Level (levelNo, width, height);
     this.level = level;
     level.SetCharMap(charMap);
     foreach (Vector2 key in charMap.Keys) {
         Vector3 position = this.MatrixToPosition(key);
         int x = (int)key.x;
         int y = (int)key.y;
         Vector2 p = new Vector2(x, y);
         char c = charMap[p];
         if (level.IsFloor(x, y)) {
             GameObject floorPrefab = (GameObject)Resources.Load ("Prefabs/floorPrefab", typeof(GameObject));
             GameObject floor = (GameObject)Instantiate (floorPrefab, position, Quaternion.identity);
             floor.transform.parent = levelObject.transform;
             level.SetObject(p, floor);
         }
         if (level.IsRoute(x, y)) {
             GameObject routePrefab = null;
             if (this.level.IsFloor(x + 1, y) || this.level.IsFloor(x - 1, y) || this.level.IsFloor(x, y + 1) || this.level.IsFloor(x, y - 1)) {
                 routePrefab = (GameObject)Resources.Load ("Prefabs/routeEntrancePrefab", typeof(GameObject));
             } else {
                 routePrefab = (GameObject)Resources.Load ("Prefabs/routePrefab", typeof(GameObject));
             }
             GameObject route = (GameObject)Instantiate (routePrefab, position, Quaternion.identity);
             route.transform.parent = levelObject.transform;
             // if route is placed holizontally, rotate object.
             if (level.IsRoute(x - 1, y) || level.IsRoute(x + 1, y)) {
                 route.transform.Rotate (new Vector3 (0, 90, 0));
             }
             level.SetObject(p, route);
             // add Gate
             if (char.IsUpper(c)) {
                 GameObject gatePrefab = (GameObject)Resources.Load("Prefabs/gatePrefab", typeof(GameObject));
                 this.AddGate(route, gatePrefab);
             } else if (c == '|') {
                 GameObject gatePrefab = (GameObject)Resources.Load("Prefabs/autoGatePrefab", typeof(GameObject));
                 this.AddGate(route, gatePrefab);
             }
         } else if (level.IsWall(x, y)) {
             GameObject wallPrefab = null;
             GameObject wall = null;
             bool isHorizontal = level.IsFloor(x - 1, y) ^ level.IsFloor(x + 1, y);
             bool isCurve = false;
             bool isCorner = false;
             if (c == '#') {
                 if ((level.IsWall(x + 1, y) && level.IsWall(x, y + 1)) ||
                     (level.IsWall(x + 1, y) && level.IsWall(x, y - 1)) ||
                     (level.IsWall(x - 1, y) && level.IsWall(x, y + 1)) ||
                     (level.IsWall(x - 1, y) && level.IsWall(x, y - 1)) ) {
                     wallPrefab = (GameObject)Resources.Load ("Prefabs/cornerWallPrefab", typeof(GameObject));
                     isCorner = true;
                 } else if (isHorizontal) {
                     wallPrefab = (GameObject)Resources.Load ("Prefabs/curveWallPrefab", typeof(GameObject));
                     isCurve = true;
                 } else {
                     // Normal Wall
                     wallPrefab = (GameObject)Resources.Load ("Prefabs/wallPrefab", typeof(GameObject));
                 }
             } else if (char.IsLower(c)) {
                 // corner Wall
                 wallPrefab = (GameObject)Resources.Load ("Prefabs/switchWallPrefab", typeof(GameObject));
             } else if (c == '$') {
                 // Broken Wall
                 wallPrefab = (GameObject)Resources.Load ("Prefabs/brokenWallPrefab", typeof(GameObject));
             } else if (c == '%') {
                 // Window Wall
                 wallPrefab = (GameObject)Resources.Load ("Prefabs/windowWallPrefab", typeof(GameObject));
             }
             wall = (GameObject)Instantiate(wallPrefab, position, Quaternion.identity);
             if (isCurve) {
                 if (level.IsFloor(x - 1, y)) {
                     wall.transform.Rotate (new Vector3 (0, 180, 0));
                 }
             } else {
                 if (isCorner) {
                     // cornerWall
                     if (level.IsRoute(x, y - 1)) {
                         Destroy(wall.transform.Find("up").gameObject);
                     }
                     if (level.IsRoute(x, y + 1)) {
                         Destroy(wall.transform.Find("down").gameObject);
                     }
                     if (level.IsRoute(x - 1, y)) {
                         Destroy(wall.transform.Find("left").gameObject);
                     }
                     if (level.IsRoute(x + 1, y)) {
                         Destroy(wall.transform.Find("right").gameObject);
                     }
                 } else {
                     if (level.IsFloor(x, y - 1) || level.IsFloor(x - 1, y - 1) || level.IsFloor(x + 1, y - 1) ) {
                         wall.transform.Translate(Vector3.forward * (this.HEIGHT / 2.0f));
                     } else if (level.IsFloor(x, y + 1) || level.IsFloor(x - 1, y + 1) || level.IsFloor(x + 1, y + 1) ) {
                         wall.transform.Translate(Vector3.forward * -(this.HEIGHT / 2.0f));
                         wall.transform.Rotate(Vector3.up * 180);
                     }
                 }
             }
             wall.transform.parent = levelObject.transform;
             level.SetObject(p, wall);
         } else if (char.IsDigit(c)) {
             GameObject prefab = (GameObject)Resources.Load("Prefabs/playerPrefab");
             GameObject player = (GameObject)Instantiate(prefab, position + Vector3.up, Quaternion.identity);
             player.transform.parent = levelObject.transform;
             this.level.AddStartPoint(p);
         } else if (c == '!' || c == '?' || c == '^') {
             string prefabName = "enemyPrefab";
             if (c == '?') {
                 prefabName = "bombEnemyPrefab";
             } else if (c == '^') {
                 prefabName = "bossPrefab";
             }
             bool ignore = false;
             foreach (Enemy enemy in ignoreEnemies) {
                 if (enemy.GetInitialPosition() == p) {
                     ignore = true;
                     break;
                 }
             }
             if (!ignore) {
                 GameObject enemyPrefab = (GameObject)Resources.Load ("Prefabs/" + prefabName, typeof(GameObject));
                 GameObject enemy = (GameObject)Instantiate (enemyPrefab, position + Vector3.up * 1.5f, Quaternion.identity);
                 enemy.transform.parent = levelObject.transform;
             }
         }
     }
     this.CreateRooms();
     this.CreateRoutes();
     this.ConnectGates();
     AstarPath path = AstarPath.active;
     path.Initialize();
     PointGraph graph = path.graphs[0] as PointGraph;
     graph.Scan();
     path.FlushGraphUpdates();
     // Set warning floor Material
     Material warningMaterial = (Material)Resources.Load("Materials/warningFloorMaterial");
     foreach (Room room in this.GetLevel().GetRooms()) {
         if (room.IsProtect()) {
             foreach (Vector2 pos in room.GetFloors()) {
                 GameObject obj = this.GetLevel().GetObject(pos);
                 Destroy(obj.transform.Find("floor/Plane").renderer.material);
                 obj.transform.Find("floor/Plane").renderer.material = warningMaterial;
                 obj.transform.Find("floor/Plane").renderer.useLightProbes = false;
             }
         }
     }
     Resources.UnloadUnusedAssets();
 }