Entity.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Xml.Serialization;
  3. using OpenTK.Graphics.OpenGL;
  4. namespace RaahnSimulation
  5. {
  6. [XmlRoot("Entity")]
  7. public class EntityConfig
  8. {
  9. [XmlElement("X")]
  10. public double x;
  11. [XmlElement("Y")]
  12. public double y;
  13. [XmlElement("RelX")]
  14. public double relX;
  15. [XmlElement("RelY")]
  16. public double relY;
  17. [XmlElement("Angle")]
  18. public double angle;
  19. [XmlAttribute("Type")]
  20. public string type;
  21. }
  22. public abstract class Entity
  23. {
  24. public enum EntityType
  25. {
  26. NONE = -2,
  27. GENERIC = -1,
  28. WALL = 0,
  29. POINT = 1,
  30. CAR = 2,
  31. };
  32. public static readonly string[] ENTITY_TYPE_STRINGS =
  33. {
  34. "Wall", "Point", "Car"
  35. };
  36. public const double DEFAULT_COLOR_R = 1.0;
  37. public const double DEFAULT_COLOR_G = 1.0;
  38. public const double DEFAULT_COLOR_B = 1.0;
  39. public const double DEFAULT_COLOR_A = 1.0;
  40. public bool visible;
  41. public double angle;
  42. public AABB aabb;
  43. protected double width;
  44. protected double height;
  45. protected double transparency;
  46. protected Utils.Vector2 drawingVec;
  47. protected Simulator context;
  48. protected State currentState;
  49. protected TextureManager texMan;
  50. protected Camera camera;
  51. protected EntityType type;
  52. protected TextureManager.TextureType texture;
  53. protected Mesh mesh;
  54. protected Utils.Vector2 center;
  55. protected Utils.Vector2 velocity;
  56. protected Utils.Vector2 speed;
  57. protected Utils.Vector3 color;
  58. private bool moved;
  59. private double previousAngle;
  60. private Mesh lineRect;
  61. private Mesh quad;
  62. private Utils.Vector2 previousPos;
  63. private Utils.Vector2 transformedWorldPos;
  64. private Utils.Vector2 worldPos;
  65. protected Entity(Simulator sim)
  66. {
  67. context = sim;
  68. currentState = context.GetState();
  69. texMan = currentState.GetTexMan();
  70. camera = currentState.GetCamera();
  71. visible = true;
  72. type = EntityType.GENERIC;
  73. texture = TextureManager.TextureType.NONE;
  74. //Default to quad mesh.
  75. mesh = State.GetQuad();
  76. lineRect = State.GetLineRect();
  77. quad = State.GetQuad();
  78. width = 1.0;
  79. height = 1.0;
  80. angle = 0.0;
  81. moved = false;
  82. previousAngle = 0.0;
  83. //Initially opaque.
  84. transparency = DEFAULT_COLOR_A;
  85. aabb = new AABB();
  86. aabb.SetSize(width, height);
  87. worldPos = new Utils.Vector2(0.0, 0.0);
  88. transformedWorldPos = new Utils.Vector2(0.0, 0.0);
  89. velocity = new Utils.Vector2(0.0, 0.0);
  90. speed = new Utils.Vector2(0.0, 0.0);
  91. center = new Utils.Vector2(0.0, 0.0);
  92. previousPos = new Utils.Vector2(0.0, 0.0);
  93. //Initially no change on color.
  94. color = new Utils.Vector3(DEFAULT_COLOR_R, DEFAULT_COLOR_G, DEFAULT_COLOR_B);
  95. //Default drawing vector is transformedWorldPos.
  96. drawingVec = transformedWorldPos;
  97. velocity.x = 0.0;
  98. velocity.y = 0.0;
  99. speed.x = 1.0;
  100. speed.y = 1.0;
  101. }
  102. ~Entity()
  103. {
  104. }
  105. public static string GetStringFromType(EntityType entityType)
  106. {
  107. int typeInt = (int)entityType;
  108. if (typeInt >= 0 && typeInt < ENTITY_TYPE_STRINGS.Length)
  109. return ENTITY_TYPE_STRINGS[(int)entityType];
  110. else
  111. return null;
  112. }
  113. public static EntityType GetTypeFromString(string typeString)
  114. {
  115. for (int i = 0; i < ENTITY_TYPE_STRINGS.Length; i++)
  116. {
  117. if (typeString.Equals(ENTITY_TYPE_STRINGS[i]))
  118. return (EntityType)i;
  119. }
  120. return EntityType.NONE;
  121. }
  122. protected virtual void SetTransformUsage(bool usage)
  123. {
  124. if (usage)
  125. drawingVec = transformedWorldPos;
  126. else
  127. drawingVec = worldPos;
  128. }
  129. public virtual void Update()
  130. {
  131. UpdateCoordinates();
  132. double deltaAngle = angle - previousAngle;
  133. if (deltaAngle != 0.0)
  134. {
  135. aabb.Rotate(deltaAngle);
  136. previousAngle = angle;
  137. }
  138. double deltaX = drawingVec.x - previousPos.x;
  139. double deltaY = drawingVec.y - previousPos.y;
  140. if (deltaX != 0.0 || deltaY != 0.0)
  141. {
  142. aabb.Translate(deltaX, deltaY);
  143. previousPos.x = drawingVec.x;
  144. previousPos.y = drawingVec.y;
  145. moved = true;
  146. }
  147. else
  148. moved = false;
  149. }
  150. public virtual void UpdateEvent(Event e)
  151. {
  152. }
  153. public virtual void Draw()
  154. {
  155. if (texMan.GetCurrentTexture() != texture)
  156. texMan.SetTexture(texture);
  157. if (!mesh.IsCurrent())
  158. mesh.MakeCurrent();
  159. }
  160. //Allow entities with a default color to reset their color.
  161. public virtual void ResetColor()
  162. {
  163. color.x = DEFAULT_COLOR_R;
  164. color.y = DEFAULT_COLOR_G;
  165. color.z = DEFAULT_COLOR_B;
  166. transparency = DEFAULT_COLOR_A;
  167. }
  168. public void UpdateCoordinates()
  169. {
  170. if (drawingVec == worldPos)
  171. {
  172. Utils.Vector2 transform = camera.TransformWorld(worldPos);
  173. transformedWorldPos.x = transform.x;
  174. transformedWorldPos.y = transform.y;
  175. }
  176. else
  177. {
  178. Utils.Vector2 transform = camera.UntransformWorld(transformedWorldPos);
  179. worldPos.x = transform.x;
  180. worldPos.y = transform.y;
  181. }
  182. center.x = drawingVec.x + (width / 2.0);
  183. center.y = drawingVec.y + (height / 2.0);
  184. //OpenGL uses degress, standard math uses radians.
  185. velocity.x = Math.Cos(Utils.DegToRad(angle)) * speed.x;
  186. velocity.y = Math.Sin(Utils.DegToRad(angle)) * speed.y;
  187. }
  188. public bool Moved()
  189. {
  190. return moved;
  191. }
  192. public bool UsesTransform()
  193. {
  194. return drawingVec == transformedWorldPos;
  195. }
  196. public double GetAngle()
  197. {
  198. return angle;
  199. }
  200. public double GetWidth()
  201. {
  202. return width;
  203. }
  204. public double GetHeight()
  205. {
  206. return height;
  207. }
  208. public double GetWorldX()
  209. {
  210. return worldPos.x;
  211. }
  212. public double GetWorldY()
  213. {
  214. return worldPos.y;
  215. }
  216. public double GetTransformedX()
  217. {
  218. return transformedWorldPos.x;
  219. }
  220. public double GetTransformedY()
  221. {
  222. return transformedWorldPos.y;
  223. }
  224. public double GetTransparency()
  225. {
  226. return transparency;
  227. }
  228. public TextureManager.TextureType GetTexture()
  229. {
  230. return texture;
  231. }
  232. public Utils.Vector2 GetCenter()
  233. {
  234. return center;
  235. }
  236. public Utils.Vector3 GetColor()
  237. {
  238. return color;
  239. }
  240. public EntityType GetEntityType()
  241. {
  242. return type;
  243. }
  244. public virtual void SetPosition(double x, double y)
  245. {
  246. drawingVec.x = x;
  247. drawingVec.y = y;
  248. }
  249. public void SetColor(double r, double g, double b, double t)
  250. {
  251. color.x = r;
  252. color.y = g;
  253. color.z = b;
  254. transparency = t;
  255. }
  256. public virtual void SetWidth(double w)
  257. {
  258. width = w;
  259. aabb.SetSize(width, height);
  260. }
  261. public virtual void SetHeight(double h)
  262. {
  263. height = h;
  264. aabb.SetSize(width, height);
  265. }
  266. public void SetTexture(TextureManager.TextureType t)
  267. {
  268. texture = t;
  269. }
  270. public void SetMesh(Mesh newMesh)
  271. {
  272. mesh = newMesh;
  273. }
  274. public virtual void DebugDraw()
  275. {
  276. //Handle all generic debug drawing here.
  277. lineRect.MakeCurrent();
  278. aabb.DebugDraw();
  279. quad.MakeCurrent();
  280. }
  281. //If any clean operations are needed,
  282. //they can be added by overriding Clean()
  283. //Not abstract to avoid being forced to
  284. //Override Clean().
  285. public virtual void Clean()
  286. {
  287. }
  288. }
  289. }