Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using SimpleJSON;
  6. using UnityEngine;
  7.  
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11.  
  12. public static class LevelIndex
  13. {
  14. public const string LEVEL_RESOURCE = "levels";
  15. public const string LEVEL_FILENAME = "Resources/levels.json";
  16.  
  17. private static Dictionary<char, TileType> _charTileDict = new Dictionary<char, TileType>
  18. {
  19. {'F', TileType.Floor},
  20. {'W', TileType.Wall},
  21. {'P', TileType.Pit},
  22. {'E', TileType.Exit},
  23. {'S', TileType.Spawn}
  24. };
  25.  
  26. private static Dictionary<TileType, char> _tileCharDict = _charTileDict.ToDictionary(kv => kv.Value, kv => kv.Key);
  27.  
  28. private static readonly Dictionary<string, MirrorMode> _strMirrorModeDict = new Dictionary<string, MirrorMode>
  29. {
  30. {"none", MirrorMode.None},
  31. {"horizontal", MirrorMode.Horizontal},
  32. {"vertical", MirrorMode.Vertical},
  33. {"both", MirrorMode.Both}
  34. };
  35.  
  36. private static readonly Dictionary<MirrorMode, string> _mirrorModeStrDict = _strMirrorModeDict.ToDictionary(kv => kv.Value, kv => kv.Key);
  37.  
  38. public static Dictionary<string, LevelData> Levels = new Dictionary<string, LevelData>();
  39.  
  40. public static void LoadLevels()
  41. {
  42. var jsonString = Resources.Load<TextAsset>("levels").ToString();
  43.  
  44. var json = JSON.Parse(jsonString);
  45.  
  46. foreach (var level in json.Children)
  47. {
  48. var name = level["name"];
  49.  
  50. Levels[name] = new LevelData
  51. {
  52. Name = name,
  53. Width = level["width"].AsInt,
  54. Length = level["length"].AsInt,
  55. Tiles = StringToTileArray(level["tiles"]),
  56. Mode = _strMirrorModeDict[level["mirror_mode"]],
  57. MirrorOffset = level["mirror_offset"].AsInt,
  58. Seed = level["seed"].AsInt
  59. };
  60. }
  61. }
  62.  
  63. public static void SaveLevels()
  64. {
  65. var json = new JSONArray();
  66.  
  67. foreach (var data in Levels.Values)
  68. {
  69. var level = new JSONClass();
  70.  
  71. level["name"] = data.Name;
  72. level["width"].AsInt = data.Width;
  73. level["length"].AsInt = data.Length;
  74. level["tiles"] = TileArrayToString(data.Tiles);
  75. level["mirror_mode"] = _mirrorModeStrDict[data.Mode];
  76. level["mirror_offset"].AsInt = data.MirrorOffset;
  77. level["seed"].AsInt = data.Seed;
  78.  
  79. json.Add(level);
  80. }
  81.  
  82. #if UNITY_EDITOR
  83. File.WriteAllText(Application.dataPath + "/" + LEVEL_FILENAME, json.ToJSON(0));
  84. AssetDatabase.Refresh();
  85. #endif
  86. }
  87.  
  88. private static TileType[] StringToTileArray(string tileString)
  89. {
  90. var count = tileString.Length;
  91. var tiles = new TileType[count];
  92.  
  93. for (var i = 0; i < tileString.Length; i++)
  94. {
  95. tiles[i] = _charTileDict[tileString[i]];
  96. }
  97.  
  98. return tiles;
  99. }
  100.  
  101. private static string TileArrayToString(TileType[] tiles)
  102. {
  103. var tileString = new StringBuilder();
  104.  
  105. foreach (var tile in tiles)
  106. {
  107. tileString.Append(_tileCharDict[tile]);
  108. }
  109.  
  110. return tileString.ToString();
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement