GameObject.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // $Id: foo.cpp 2979 2006-01-10 00:00:04Z sommer $
  2. //
  3. // Cobble - A simple SuperTux level editor
  4. // Copyright (C) 2006 Christoph Sommer <supertux@2006.expires.deltadevelopment.de>
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. // 02111-1307, USA.
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Text;
  23. using Lisp;
  24. namespace Cobble {
  25. public class GameObject : IComparable {
  26. private string typename;
  27. private static Dictionary<string, GameObject> exponents = new Dictionary<string,GameObject>(); /**< table of all known typename-to-GameObject mappings */
  28. public GameObject(string name) {
  29. this.typename = name;
  30. }
  31. static GameObject() {
  32. exponents["spawnpoint"] = new Spawnpoint("main", -1, -1);
  33. exponents["bell"] = new SpatialGameObject("bell", -1, -1);
  34. exponents["powerup"] = new PowerUp(-1, -1);
  35. foreach (string name in new string[] { "angrystone", "bouncingsnowball", "dispenser", "fish", "flame", "flyingsnowball", "jumpy", "kugelblitz", "mrbomb", "mriceblock", "mrrocket", "mrtree", "plant", "poisonivy", "skullyhop", "snowball", "snowsnail", "spidermite", "spiky", "sspiky", "stalactite", "yeti", "yeti_stalactite", "zeekling" }) {
  36. exponents[name] = new Badguy(name, -1, -1);
  37. }
  38. }
  39. public static List<GameObject> getExponents() {
  40. List<GameObject> gameObjects = new List<GameObject>();
  41. foreach (GameObject gameObject in exponents.Values) gameObjects.Add(gameObject);
  42. return gameObjects;
  43. }
  44. public static GameObject Parse(string name, Parser parser) {
  45. foreach (GameObject gameObject in exponents.Values) {
  46. if (gameObject.Type == name) {
  47. return gameObject.ParseAsThis(name, parser);
  48. }
  49. }
  50. return new SpatialGameObject(name, parser);
  51. }
  52. public virtual GameObject ParseAsThis(string name, Parser parser) {
  53. return new GameObject(name, parser);
  54. }
  55. public GameObject(string name, Parser parser) {
  56. this.typename = name;
  57. int d = parser.Depth;
  58. while (parser.Parse() && parser.Depth >= d) {
  59. if (parser.Depth == d + 1) {
  60. if (parser.Type != Parser.LispType.SYMBOL)
  61. throw new Exception("expected SYMBOL");
  62. string symbol = parser.SymbolValue;
  63. parser.Parse();
  64. if (!tryParse(symbol, parser)) {
  65. Console.WriteLine("WARNING: Unknown object property: \"" + symbol + "\", skipping");
  66. SkipList(parser);
  67. }
  68. }
  69. }
  70. }
  71. public virtual GameObject Clone() {
  72. GameObject clone = new GameObject(this.typename);
  73. return clone;
  74. }
  75. public virtual string DescriptiveText() {
  76. return "Game Object \"" + this.Type + "\"";
  77. }
  78. /**
  79. * <returns>true if token was successfully consumed</returns>
  80. */
  81. protected virtual bool tryParse(string symbol, Parser parser) {
  82. return false;
  83. }
  84. public void Write(LispWriter writer) {
  85. writer.StartList(this.typename);
  86. this.WriteProperties(writer);
  87. writer.EndList(this.typename);
  88. }
  89. protected virtual void WriteProperties(LispWriter writer) {
  90. }
  91. private static void SkipList(Lisp.Parser parser) {
  92. int d = parser.Depth;
  93. while (parser.Parse() && parser.Depth >= d)
  94. ;
  95. }
  96. public string Type {
  97. get {
  98. return typename;
  99. }
  100. }
  101. public override string ToString() {
  102. return typename;
  103. }
  104. public int CompareTo(object o) {
  105. if (!(o is GameObject)) throw new ArgumentException("Cannot compare GameObject to something else");
  106. GameObject go = (GameObject)o;
  107. return this.typename.CompareTo(go.typename);
  108. }
  109. }
  110. public class SpatialGameObject : GameObject {
  111. private float x = -1;
  112. private float y = -1;
  113. public SpatialGameObject(string name, float x, float y) : base(name) {
  114. this.x = x;
  115. this.y = y;
  116. }
  117. public SpatialGameObject(string name, Parser parser)
  118. : base(name, parser) {
  119. }
  120. public override GameObject ParseAsThis(string name, Parser parser) {
  121. return new SpatialGameObject(name, parser);
  122. }
  123. public override GameObject Clone() {
  124. SpatialGameObject clone = new SpatialGameObject(Type, X, Y);
  125. return clone;
  126. }
  127. protected override bool tryParse(string symbol, Parser parser) {
  128. switch (symbol) {
  129. case "x":
  130. this.x = parser.FloatValue;
  131. return true;
  132. case "y":
  133. this.y = parser.FloatValue;
  134. return true;
  135. default:
  136. return base.tryParse(symbol, parser);
  137. }
  138. }
  139. protected override void WriteProperties(LispWriter writer) {
  140. base.WriteProperties(writer);
  141. if (x != -1) writer.Write("x", this.x);
  142. if (y != -1) writer.Write("y", this.y);
  143. }
  144. public float X {
  145. get {
  146. return x;
  147. }
  148. set {
  149. this.x = value;
  150. }
  151. }
  152. public float Y {
  153. get {
  154. return y;
  155. }
  156. set {
  157. this.y = value;
  158. }
  159. }
  160. public override string DescriptiveText() {
  161. return "Spatial Game Object \"" + this.Type + "\"";
  162. }
  163. }
  164. public class Badguy : SpatialGameObject {
  165. //FIXME: "stay-on-platform" flag
  166. //FIXME: direction?
  167. private bool stayonplatform = false;
  168. public Badguy(string name, float x, float y)
  169. : base(name, x, y) {
  170. }
  171. public Badguy(string name, Parser parser)
  172. : base(name, parser) {
  173. }
  174. public override GameObject ParseAsThis(string name, Parser parser) {
  175. return new Badguy(name, parser);
  176. }
  177. public override GameObject Clone() {
  178. Badguy clone = new Badguy(Type, X, Y);
  179. return clone;
  180. }
  181. public override string DescriptiveText() {
  182. return "Badguy \"" + this.Type + "\"";
  183. }
  184. protected override bool tryParse(string symbol, Parser parser) {
  185. switch (symbol) {
  186. case "stay-on-platform":
  187. this.stayonplatform = parser.BoolValue;
  188. return true;
  189. default:
  190. return base.tryParse(symbol, parser);
  191. }
  192. }
  193. protected override void WriteProperties(LispWriter writer) {
  194. base.WriteProperties(writer);
  195. if (stayonplatform) writer.Write("stay-on-platform", this.stayonplatform);
  196. }
  197. public bool Stayonplatform {
  198. get {
  199. return stayonplatform;
  200. }
  201. set {
  202. this.stayonplatform = value;
  203. }
  204. }
  205. }
  206. public class Spawnpoint : SpatialGameObject {
  207. private string name = "main";
  208. /**
  209. * <param name="spawnname">Name of the spawnpoint, use "main" for default spawnpoint</param>
  210. */
  211. public Spawnpoint(string name, float x, float y)
  212. : base("spawnpoint", x, y) {
  213. this.name = name;
  214. }
  215. public Spawnpoint(string name, Parser parser)
  216. : base(name, parser) {
  217. }
  218. public override GameObject ParseAsThis(string name, Parser parser) {
  219. return new Spawnpoint(name, parser);
  220. }
  221. public override GameObject Clone() {
  222. Spawnpoint clone = new Spawnpoint(Name, X, Y);
  223. return clone;
  224. }
  225. public override string DescriptiveText() {
  226. return "Spawnpoint \""+this.name+"\"";
  227. }
  228. protected override bool tryParse(string symbol, Parser parser) {
  229. switch (symbol) {
  230. case "name":
  231. this.name = parser.StringValue;
  232. return true;
  233. default:
  234. return base.tryParse(symbol, parser);
  235. }
  236. }
  237. protected override void WriteProperties(LispWriter writer) {
  238. base.WriteProperties(writer);
  239. writer.Write("name", this.name);
  240. }
  241. public string Name {
  242. get {
  243. return name;
  244. }
  245. set {
  246. this.name = value;
  247. }
  248. }
  249. }
  250. public class PowerUp : SpatialGameObject {
  251. private string sprite = "images/powerups/potions/red-potion.sprite";
  252. private string script = "levelflip();";
  253. public PowerUp(float x, float y)
  254. : base("powerup", x, y) {
  255. }
  256. public PowerUp(string name, Parser parser)
  257. : base(name, parser) {
  258. }
  259. public override GameObject ParseAsThis(string name, Parser parser) {
  260. return new PowerUp(name, parser);
  261. }
  262. public override GameObject Clone() {
  263. PowerUp clone = new PowerUp(X, Y);
  264. return clone;
  265. }
  266. public override string DescriptiveText() {
  267. return "PowerUp \"" + this.sprite + "\", \"" + this.script + "\"";
  268. }
  269. protected override bool tryParse(string symbol, Parser parser) {
  270. switch (symbol) {
  271. case "sprite":
  272. this.sprite = parser.StringValue;
  273. return true;
  274. case "script":
  275. this.script = parser.StringValue;
  276. return true;
  277. default:
  278. return base.tryParse(symbol, parser);
  279. }
  280. }
  281. protected override void WriteProperties(LispWriter writer) {
  282. base.WriteProperties(writer);
  283. writer.Write("sprite", this.sprite);
  284. writer.Write("script", this.script);
  285. }
  286. public string Sprite {
  287. get {
  288. return sprite;
  289. }
  290. set {
  291. this.sprite = value;
  292. }
  293. }
  294. public string Script {
  295. get {
  296. return script;
  297. }
  298. set {
  299. this.script = value;
  300. }
  301. }
  302. }
  303. }