123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- using System;
- using System.Xml.Serialization;
- using OpenTK.Graphics.OpenGL;
- namespace RaahnSimulation
- {
- [XmlRoot("Entity")]
- public class EntityConfig
- {
- [XmlElement("X")]
- public double x;
- [XmlElement("Y")]
- public double y;
- [XmlElement("RelX")]
- public double relX;
- [XmlElement("RelY")]
- public double relY;
- [XmlElement("Angle")]
- public double angle;
- [XmlAttribute("Type")]
- public string type;
- }
- public abstract class Entity
- {
- public enum EntityType
- {
- NONE = -2,
- GENERIC = -1,
- WALL = 0,
- POINT = 1,
- CAR = 2,
- };
- public static readonly string[] ENTITY_TYPE_STRINGS =
- {
- "Wall", "Point", "Car"
- };
- public const double DEFAULT_COLOR_R = 1.0;
- public const double DEFAULT_COLOR_G = 1.0;
- public const double DEFAULT_COLOR_B = 1.0;
- public const double DEFAULT_COLOR_A = 1.0;
- public bool visible;
- public double angle;
- public AABB aabb;
- protected double width;
- protected double height;
- protected double transparency;
- protected Utils.Vector2 drawingVec;
- protected Simulator context;
- protected State currentState;
- protected TextureManager texMan;
- protected Camera camera;
- protected EntityType type;
- protected TextureManager.TextureType texture;
- protected Mesh mesh;
- protected Utils.Vector2 center;
- protected Utils.Vector2 velocity;
- protected Utils.Vector2 speed;
- protected Utils.Vector3 color;
- private bool moved;
- private double previousAngle;
- private Mesh lineRect;
- private Mesh quad;
- private Utils.Vector2 previousPos;
- private Utils.Vector2 transformedWorldPos;
- private Utils.Vector2 worldPos;
- protected Entity(Simulator sim)
- {
- context = sim;
- currentState = context.GetState();
- texMan = currentState.GetTexMan();
- camera = currentState.GetCamera();
- visible = true;
- type = EntityType.GENERIC;
- texture = TextureManager.TextureType.NONE;
- //Default to quad mesh.
- mesh = State.GetQuad();
- lineRect = State.GetLineRect();
- quad = State.GetQuad();
-
- width = 1.0;
- height = 1.0;
- angle = 0.0;
- moved = false;
- previousAngle = 0.0;
- //Initially opaque.
- transparency = DEFAULT_COLOR_A;
- aabb = new AABB();
- aabb.SetSize(width, height);
- worldPos = new Utils.Vector2(0.0, 0.0);
- transformedWorldPos = new Utils.Vector2(0.0, 0.0);
- velocity = new Utils.Vector2(0.0, 0.0);
- speed = new Utils.Vector2(0.0, 0.0);
- center = new Utils.Vector2(0.0, 0.0);
- previousPos = new Utils.Vector2(0.0, 0.0);
- //Initially no change on color.
- color = new Utils.Vector3(DEFAULT_COLOR_R, DEFAULT_COLOR_G, DEFAULT_COLOR_B);
- //Default drawing vector is transformedWorldPos.
- drawingVec = transformedWorldPos;
- velocity.x = 0.0;
- velocity.y = 0.0;
- speed.x = 1.0;
- speed.y = 1.0;
- }
- ~Entity()
- {
- }
- public static string GetStringFromType(EntityType entityType)
- {
- int typeInt = (int)entityType;
- if (typeInt >= 0 && typeInt < ENTITY_TYPE_STRINGS.Length)
- return ENTITY_TYPE_STRINGS[(int)entityType];
- else
- return null;
- }
- public static EntityType GetTypeFromString(string typeString)
- {
- for (int i = 0; i < ENTITY_TYPE_STRINGS.Length; i++)
- {
- if (typeString.Equals(ENTITY_TYPE_STRINGS[i]))
- return (EntityType)i;
- }
- return EntityType.NONE;
- }
- protected virtual void SetTransformUsage(bool usage)
- {
- if (usage)
- drawingVec = transformedWorldPos;
- else
- drawingVec = worldPos;
- }
- public virtual void Update()
- {
- UpdateCoordinates();
- double deltaAngle = angle - previousAngle;
- if (deltaAngle != 0.0)
- {
- aabb.Rotate(deltaAngle);
- previousAngle = angle;
- }
- double deltaX = drawingVec.x - previousPos.x;
- double deltaY = drawingVec.y - previousPos.y;
- if (deltaX != 0.0 || deltaY != 0.0)
- {
- aabb.Translate(deltaX, deltaY);
- previousPos.x = drawingVec.x;
- previousPos.y = drawingVec.y;
- moved = true;
- }
- else
- moved = false;
- }
- public virtual void UpdateEvent(Event e)
- {
- }
- public virtual void Draw()
- {
- if (texMan.GetCurrentTexture() != texture)
- texMan.SetTexture(texture);
- if (!mesh.IsCurrent())
- mesh.MakeCurrent();
- }
- //Allow entities with a default color to reset their color.
- public virtual void ResetColor()
- {
- color.x = DEFAULT_COLOR_R;
- color.y = DEFAULT_COLOR_G;
- color.z = DEFAULT_COLOR_B;
- transparency = DEFAULT_COLOR_A;
- }
- public void UpdateCoordinates()
- {
- if (drawingVec == worldPos)
- {
- Utils.Vector2 transform = camera.TransformWorld(worldPos);
- transformedWorldPos.x = transform.x;
- transformedWorldPos.y = transform.y;
- }
- else
- {
- Utils.Vector2 transform = camera.UntransformWorld(transformedWorldPos);
- worldPos.x = transform.x;
- worldPos.y = transform.y;
- }
- center.x = drawingVec.x + (width / 2.0);
- center.y = drawingVec.y + (height / 2.0);
- //OpenGL uses degress, standard math uses radians.
- velocity.x = Math.Cos(Utils.DegToRad(angle)) * speed.x;
- velocity.y = Math.Sin(Utils.DegToRad(angle)) * speed.y;
- }
- public bool Moved()
- {
- return moved;
- }
- public bool UsesTransform()
- {
- return drawingVec == transformedWorldPos;
- }
- public double GetAngle()
- {
- return angle;
- }
- public double GetWidth()
- {
- return width;
- }
- public double GetHeight()
- {
- return height;
- }
- public double GetWorldX()
- {
- return worldPos.x;
- }
- public double GetWorldY()
- {
- return worldPos.y;
- }
- public double GetTransformedX()
- {
- return transformedWorldPos.x;
- }
- public double GetTransformedY()
- {
- return transformedWorldPos.y;
- }
- public double GetTransparency()
- {
- return transparency;
- }
- public TextureManager.TextureType GetTexture()
- {
- return texture;
- }
- public Utils.Vector2 GetCenter()
- {
- return center;
- }
- public Utils.Vector3 GetColor()
- {
- return color;
- }
- public EntityType GetEntityType()
- {
- return type;
- }
- public virtual void SetPosition(double x, double y)
- {
- drawingVec.x = x;
- drawingVec.y = y;
- }
- public void SetColor(double r, double g, double b, double t)
- {
- color.x = r;
- color.y = g;
- color.z = b;
- transparency = t;
- }
- public virtual void SetWidth(double w)
- {
- width = w;
- aabb.SetSize(width, height);
- }
- public virtual void SetHeight(double h)
- {
- height = h;
- aabb.SetSize(width, height);
- }
- public void SetTexture(TextureManager.TextureType t)
- {
- texture = t;
- }
- public void SetMesh(Mesh newMesh)
- {
- mesh = newMesh;
- }
- public virtual void DebugDraw()
- {
- //Handle all generic debug drawing here.
- lineRect.MakeCurrent();
- aabb.DebugDraw();
- quad.MakeCurrent();
- }
- //If any clean operations are needed,
- //they can be added by overriding Clean()
- //Not abstract to avoid being forced to
- //Override Clean().
- public virtual void Clean()
- {
- }
- }
- }
|