PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/MJS/Variables/Map.cs

https://bitbucket.org/realquaisaq/mcrevive
C# | 112 lines | 107 code | 5 blank | 0 comment | 14 complexity | 9d6c1c671d7ed0ccb4fea0d821f1c3b4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MJS
  6. {
  7. public class Map
  8. {
  9. public static List<Map> maps = new List<Map>();
  10. public static List<string> variableNames = new List<string>();
  11. public static List<string> variableTypes = new List<string>();
  12. public static List<object> variableDefaultValues = new List<object>();
  13. public string name;
  14. public string owner;
  15. public ushort width;
  16. public ushort height;
  17. public ushort depth;
  18. public ushort[] blocks;
  19. public List<Player> players = new List<Player>();
  20. public List<object> variableValues = new List<object>();
  21. public Map(string Name, ushort Width, ushort Height, ushort Depth, ushort[] Blocks, string Owner)
  22. {
  23. name = Name;
  24. width = Width;
  25. height = Height;
  26. depth = Depth;
  27. blocks = Blocks;
  28. owner = Owner;
  29. }
  30. public void Save()
  31. {
  32. Events.SaveMap(name);
  33. }
  34. public void Unload()
  35. {
  36. Events.UnloadMap(name);
  37. }
  38. public ushort GetBlock(ushort x, ushort y, ushort z)
  39. {
  40. if (x < 0) { return 255; }
  41. if (x >= width) { return 255; }
  42. if (y < 0) { return 255; }
  43. if (y >= depth) { return 255; }
  44. if (z < 0) { return 255; }
  45. if (z >= height) { return 255; }
  46. return blocks[x + z * width + y * width * height];
  47. }
  48. public void SetBlock(ushort x, ushort y, ushort z, ushort type)
  49. {
  50. blocks[x + width * z + width * height * y] = type;
  51. Events.SetBlock(name, x, y, z, type);
  52. }
  53. public static void SetBlock(string map, ushort x, ushort y, ushort z, ushort type)
  54. {
  55. Map.Find(map).SetBlock(x, y, z, type);
  56. }
  57. public static void UpdateBlocks(string map, ushort[] Blocks)
  58. {
  59. Map.Find(map).blocks = Blocks;
  60. }
  61. public static Map Find(string name)
  62. {
  63. foreach (Map m in maps)
  64. if (m.name == name)
  65. return m;
  66. return null;
  67. }
  68. public static bool varExists(string name)
  69. {
  70. return variableNames.Contains(name);
  71. }
  72. public object varValue(string name)
  73. {
  74. int i = 0;
  75. foreach (string varName in variableNames)
  76. {
  77. if (varName == name) return variableValues[i];
  78. i++;
  79. }
  80. return null;
  81. }
  82. public static bool addVar(string name, object defaultValue)
  83. {
  84. if (variableNames.Contains(name)) return false;
  85. variableNames.Add(name);
  86. variableTypes.Add(defaultValue.GetType().Name);
  87. variableDefaultValues.Add(defaultValue);
  88. foreach (Map m in maps) m.variableValues.Add(defaultValue);
  89. return true;
  90. }
  91. public static bool delVar(string name)
  92. {
  93. if (!variableNames.Contains(name)) return false;
  94. int i = 0;
  95. foreach (string varName in variableNames)
  96. {
  97. if (varName == name) goto foundIndex;
  98. i++;
  99. }
  100. foundIndex:
  101. variableNames.RemoveRange(i, 1);
  102. variableTypes.RemoveRange(i, 1);
  103. foreach (Map m in maps) m.variableValues.RemoveRange(i, 1);
  104. return true;
  105. }
  106. }
  107. }