Tile.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * From supertux/tools/tilemanager
  3. */
  4. using System;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Drawing;
  8. using Lisp;
  9. public class ImageRegion {
  10. public String ImageFile = null;
  11. public Rectangle Region = Rectangle.Empty;
  12. public RectangleF RelativeRegion = RectangleF.Empty;
  13. }
  14. public class Tile {
  15. public int ID;
  16. public bool Solid;
  17. public bool UniSolid;
  18. public bool Ice;
  19. public bool Water;
  20. public bool Slope;
  21. public bool Hidden;
  22. public bool Hurts;
  23. public bool FullBox;
  24. public bool Brick;
  25. public bool Coin;
  26. public bool Goal;
  27. public int NextTile;
  28. public int Data;
  29. public float AnimFps;
  30. public string EditorImage;
  31. public ArrayList Images = new ArrayList();
  32. public Tile() {
  33. ID = -1;
  34. NextTile = -1;
  35. AnimFps = 1;
  36. }
  37. public void Write(LispWriter writer) {
  38. writer.StartList("tile");
  39. writer.Write("id", ID);
  40. if(Images.Count > 0) {
  41. writer.StartList("images");
  42. foreach(ImageRegion region in Images) {
  43. if(region.Region.Width != 0) {
  44. writer.WriteVerbatimLine(
  45. String.Format("(region \"{0}\" {1} {2} {3} {4})",
  46. region.ImageFile, region.Region.Left,
  47. region.Region.Top, region.Region.Width,
  48. region.Region.Height));
  49. } else {
  50. writer.WriteVerbatimLine(
  51. "\"" + region.ImageFile + "\"");
  52. }
  53. }
  54. writer.EndList("images");
  55. } else {
  56. Console.WriteLine("no images on tile " + ID);
  57. }
  58. if(Solid)
  59. writer.Write("solid", true);
  60. if(UniSolid)
  61. writer.Write("unisolid", true);
  62. if(Ice)
  63. writer.Write("ice", true);
  64. if(Water)
  65. writer.Write("water", true);
  66. if(Slope)
  67. writer.Write("slope-type", Data);
  68. if(Hurts)
  69. writer.Write("hurts", true);
  70. if(Hidden)
  71. writer.Write("hidden", true);
  72. if(Coin)
  73. writer.Write("coin", true);
  74. if(FullBox)
  75. writer.Write("fullbox", true);
  76. if(Brick)
  77. writer.Write("brick", true);
  78. if(NextTile >= 0)
  79. writer.Write("next-tile", NextTile);
  80. if(Goal)
  81. writer.Write("goal", true);
  82. if(EditorImage != null)
  83. writer.Write("editor-images", EditorImage);
  84. if(Data != 0)
  85. writer.Write("data", Data);
  86. if(Images.Count > 1) {
  87. if(AnimFps == 1.0)
  88. AnimFps = 40;
  89. writer.Write("anim-fps", AnimFps);
  90. }
  91. writer.EndList("tile");
  92. }
  93. public void Parse(Lisp.Parser parser) {
  94. int d = parser.Depth;
  95. while(parser.Parse() && parser.Depth >= d) {
  96. if(parser.Depth == d+1) {
  97. if(parser.Type != Parser.LispType.SYMBOL)
  98. throw new Exception("expected SYMBOL");
  99. string symbol = parser.SymbolValue;
  100. parser.Parse();
  101. switch(symbol) {
  102. case "id":
  103. ID = parser.IntegerValue;
  104. break;
  105. case "images":
  106. ParseTileImages(parser);
  107. break;
  108. case "editor-images":
  109. EditorImage = parser.StringValue;
  110. break;
  111. case "solid":
  112. Solid = parser.BoolValue;
  113. break;
  114. case "unisolid":
  115. UniSolid = parser.BoolValue;
  116. break;
  117. case "ice":
  118. Ice = parser.BoolValue;
  119. break;
  120. case "water":
  121. Water = parser.BoolValue;
  122. break;
  123. case "slope-type":
  124. Slope = true;
  125. Data = parser.IntegerValue;
  126. break;
  127. case "anim-fps":
  128. AnimFps = parser.FloatValue;
  129. break;
  130. case "hurts":
  131. Hurts = parser.BoolValue;
  132. break;
  133. case "hidden":
  134. Hidden = parser.BoolValue;
  135. break;
  136. case "data":
  137. Data = parser.IntegerValue;
  138. break;
  139. case "next-tile":
  140. NextTile = parser.IntegerValue;
  141. break;
  142. case "brick":
  143. Brick = parser.BoolValue;
  144. break;
  145. case "fullbox":
  146. FullBox = parser.BoolValue;
  147. break;
  148. case "coin":
  149. Coin = parser.BoolValue;
  150. break;
  151. case "goal":
  152. Goal = parser.BoolValue;
  153. break;
  154. default:
  155. Console.WriteLine("Unknown tile element " + symbol);
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. private void ParseTileImages(Lisp.Parser parser) {
  162. if(parser.Type == Parser.LispType.END_LIST)
  163. return;
  164. int d = parser.Depth;
  165. do {
  166. ImageRegion region = new ImageRegion();
  167. if(parser.Type == Parser.LispType.STRING) {
  168. region.ImageFile = parser.StringValue;
  169. } else if(parser.Type == Parser.LispType.START_LIST) {
  170. ParseImageRegion(parser, region);
  171. } else {
  172. throw new Exception("unexpected lisp data: " + parser.Type);
  173. }
  174. Images.Add(region);
  175. } while(parser.Parse() && parser.Depth >= d);
  176. }
  177. private void ParseImageRegion(Lisp.Parser parser, ImageRegion region) {
  178. parser.Parse();
  179. if(parser.Type != Parser.LispType.SYMBOL)
  180. throw new Exception("expected symbol");
  181. if(parser.SymbolValue != "region")
  182. throw new Exception("expected region symbol");
  183. parser.Parse();
  184. if(parser.Type != Parser.LispType.STRING)
  185. throw new Exception("expected string");
  186. region.ImageFile = parser.StringValue;
  187. parser.Parse();
  188. if(parser.Type != Parser.LispType.INTEGER)
  189. throw new Exception("expected integer");
  190. region.Region.X = parser.IntegerValue;
  191. parser.Parse();
  192. if(parser.Type != Parser.LispType.INTEGER)
  193. throw new Exception("expected integer");
  194. region.Region.Y = parser.IntegerValue;
  195. parser.Parse();
  196. if(parser.Type != Parser.LispType.INTEGER)
  197. throw new Exception("expected integer");
  198. region.Region.Width = parser.IntegerValue;
  199. parser.Parse();
  200. if(parser.Type != Parser.LispType.INTEGER)
  201. throw new Exception("expected integer");
  202. region.Region.Height = parser.IntegerValue;
  203. parser.Parse();
  204. if(parser.Type != Parser.LispType.END_LIST)
  205. throw new Exception("expected END_LIST");
  206. }
  207. }