Path.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. namespace Cobble {
  24. public class PathNode {
  25. public float x;
  26. public float y;
  27. public float time;
  28. public PathNode(float x, float y, float time) {
  29. this.x = x;
  30. this.y = y;
  31. this.time = time;
  32. }
  33. public PathNode(Lisp.Parser parser) {
  34. int d = parser.Depth;
  35. while (parser.Parse() && parser.Depth >= d) {
  36. if (parser.Depth == d + 1) {
  37. if (parser.Type != Lisp.Parser.LispType.SYMBOL) throw new Exception("expected SYMBOL");
  38. string symbol = parser.SymbolValue;
  39. parser.Parse();
  40. switch (symbol) {
  41. case "x":
  42. this.x = parser.FloatValue;
  43. break;
  44. case "y":
  45. this.x = parser.FloatValue;
  46. break;
  47. case "time":
  48. this.x = parser.FloatValue;
  49. break;
  50. default:
  51. throw new Exception("Unknown Token in Path Node");
  52. }
  53. }
  54. }
  55. }
  56. public void Write(LispWriter writer) {
  57. writer.StartList("node");
  58. writer.Write("x", x);
  59. writer.Write("y", y);
  60. writer.Write("time", time);
  61. writer.EndList("node");
  62. }
  63. }
  64. public class Path : GameObject {
  65. private string name;
  66. public List<PathNode> pathNodes;
  67. public Path(string name)
  68. : base(name) {
  69. }
  70. public Path(string name, List<PathNode> pathNodes)
  71. : base(name) {
  72. this.pathNodes.AddRange(pathNodes);
  73. }
  74. public Path(string name, Lisp.Parser parser)
  75. : base(name, parser) {
  76. }
  77. public override GameObject ParseAsThis(string name, Lisp.Parser parser) {
  78. return new SpatialGameObject(name, parser);
  79. }
  80. public override GameObject Clone() {
  81. Path clone = new Path(name);
  82. return clone;
  83. }
  84. protected override bool tryParse(string symbol, Lisp.Parser parser) {
  85. switch (symbol) {
  86. case "name":
  87. this.name = parser.StringValue;
  88. return true;
  89. case "node":
  90. this.pathNodes.Add(new PathNode(parser));
  91. return true;
  92. default:
  93. return base.tryParse(symbol, parser);
  94. }
  95. }
  96. protected override void WriteProperties(LispWriter writer) {
  97. base.WriteProperties(writer);
  98. writer.Write("name", this.name);
  99. foreach (PathNode pn in pathNodes) pn.Write(writer);
  100. }
  101. public string Name {
  102. get {
  103. return name;
  104. }
  105. set {
  106. this.name = value;
  107. }
  108. }
  109. public override string DescriptiveText() {
  110. return "Path \"" + this.name + "\"";
  111. }
  112. }
  113. }