/Ginga/Ginga/Entity.cs

https://bitbucket.org/KuroKaze/ginga · C# · 186 lines · 146 code · 32 blank · 8 comment · 4 complexity · 1268aa4375e6e277f9e5bfcca43a6726 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. class Entity
  7. {
  8. protected int rows, columns;
  9. public Texture2D spriteSheet;
  10. public Color[] textureData;
  11. public Vector2 position, origin;
  12. public Tuple<int, int, int> frame;
  13. public Rectangle srcRect, boundingBox;
  14. public float angle, scale;
  15. public Matrix transformM;
  16. /// <summary>
  17. /// Constructor
  18. /// </summary>
  19. /// <param name="texture">Sprite map</param>
  20. /// <param name="numRows/numColumns">Number of rows and columns of sprites on sprite map (one set of frames for animation per row)</param>
  21. /// <param name="startFrame/endFrame">Frame no. for starting and ending animation</param>
  22. /// <param name="loc">Location of object</param>
  23. /// <param name="ftl">If location is measured from top left corner</param>
  24. public Entity(Texture2D texture, int numRows, int numColumns, int startFrame, int endFrame, Vector2 loc, float scl = 1.0f, float deg = 0.0f)
  25. {
  26. spriteSheet = texture;
  27. rows = numRows;
  28. columns = numColumns;
  29. frame = new Tuple<int, int, int>(startFrame, startFrame, endFrame);
  30. position = new Vector2(loc.X, loc.Y);
  31. srcRect.Width = spriteSheet.Width / columns;
  32. srcRect.Height = spriteSheet.Height / rows;
  33. updateSrcRect();
  34. angle = MathHelper.ToRadians(deg);
  35. scale = scl;
  36. origin = new Vector2(srcRect.Width / 2, srcRect.Height / 2);
  37. textureData = new Color[srcRect.Width * srcRect.Height];
  38. spriteSheet.GetData(0, srcRect, textureData, srcRect.X * srcRect.Y, srcRect.Width * srcRect.Height);
  39. transformM = Matrix.Identity;
  40. transform(0.0f, 0.0f, 0.0f);
  41. }
  42. public Entity(Entity ent)
  43. {
  44. spriteSheet = ent.spriteSheet;
  45. rows = ent.rows;
  46. columns = ent.columns;
  47. position = ent.position;
  48. origin = ent.origin;
  49. frame = ent.frame;
  50. textureData = ent.textureData;
  51. srcRect = ent.srcRect;
  52. angle = ent.angle;
  53. }
  54. public virtual void doWhenCollide()
  55. {
  56. }
  57. public virtual void doWhenClicked()
  58. {
  59. }
  60. public virtual void transform(float x, float y, float degrees, float times = 0.0f)
  61. {
  62. angle += MathHelper.ToRadians(degrees);
  63. if (MathHelper.ToRadians(360) < angle)
  64. angle -= MathHelper.ToRadians(360);
  65. else if (0.0f > angle)
  66. angle += MathHelper.ToRadians(360);
  67. position.X += x;
  68. position.Y += y;
  69. scale += times;
  70. Matrix shiftToOriginM = Matrix.CreateTranslation(new Vector3(-origin, 0.0f));
  71. Matrix scaleM = Matrix.CreateScale(scale);
  72. Matrix rotateM = Matrix.CreateRotationZ(angle);
  73. Matrix shiftM = Matrix.CreateTranslation(new Vector3(position, 0.0f));
  74. transformM = shiftToOriginM * scaleM * rotateM * shiftM;
  75. updateBoundingBox();
  76. }
  77. public virtual void transformForward(float forward, float times = 0.0f)
  78. {
  79. position.X += (float)Math.Cos((double)angle - MathHelper.ToRadians(90)) * forward;
  80. position.Y += (float)Math.Sin((double)angle - MathHelper.ToRadians(90)) * forward;
  81. scale += times;
  82. Matrix shiftToOriginM = Matrix.CreateTranslation(new Vector3(-origin, 0.0f));
  83. Matrix scaleM = Matrix.CreateScale(scale);
  84. Matrix rotateM = Matrix.CreateRotationZ(angle);
  85. Matrix shiftM = Matrix.CreateTranslation(new Vector3(position, 0.0f));
  86. transformM = shiftToOriginM * scaleM * rotateM * shiftM;
  87. updateBoundingBox();
  88. }
  89. public virtual void transformToward(float forward, float radians, float times = 0.0f)
  90. {
  91. position.X += (float)Math.Cos((double)radians - MathHelper.ToRadians(90)) * forward;
  92. position.Y += (float)Math.Sin((double)radians - MathHelper.ToRadians(90)) * forward;
  93. scale += times;
  94. Matrix shiftToOriginM = Matrix.CreateTranslation(new Vector3(-origin, 0.0f));
  95. Matrix scaleM = Matrix.CreateScale(scale);
  96. Matrix rotateM = Matrix.CreateRotationZ(angle);
  97. Matrix shiftM = Matrix.CreateTranslation(new Vector3(position, 0.0f));
  98. transformM = shiftToOriginM * scaleM * rotateM * shiftM;
  99. updateBoundingBox();
  100. }
  101. protected void updateBoundingBox()
  102. {
  103. Rectangle rectangle = new Rectangle(0, 0, srcRect.Width, srcRect.Height);
  104. Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top);
  105. Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top);
  106. Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom);
  107. Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom);
  108. Vector2.Transform(ref leftTop, ref transformM, out leftTop);
  109. Vector2.Transform(ref rightTop, ref transformM, out rightTop);
  110. Vector2.Transform(ref leftBottom, ref transformM, out leftBottom);
  111. Vector2.Transform(ref rightBottom, ref transformM, out rightBottom);
  112. Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), Vector2.Min(leftBottom, rightBottom));
  113. Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), Vector2.Max(leftBottom, rightBottom));
  114. boundingBox = new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y));
  115. }
  116. public virtual void animate()
  117. {
  118. frame = new Tuple<int, int, int>(frame.Item1, frame.Item2 + 1, frame.Item3);
  119. if (frame.Item2 >= frame.Item3)
  120. frame = new Tuple<int, int, int>(frame.Item1, frame.Item1, frame.Item3);
  121. updateSrcRect();
  122. spriteSheet.GetData(0, srcRect, textureData, srcRect.X * srcRect.Y, srcRect.Width * srcRect.Height);
  123. }
  124. public virtual void newAnimation(int startFrame, int endFrame)
  125. {
  126. frame = new Tuple<int, int, int>(startFrame, startFrame, endFrame);
  127. }
  128. protected void updateSrcRect()
  129. {
  130. int row = frame.Item2 / columns;
  131. int column = frame.Item2 % columns;
  132. srcRect.X = srcRect.Width * column;
  133. srcRect.Y = srcRect.Height * row;
  134. }
  135. public virtual void Update(GameTime gameTime)
  136. {
  137. }
  138. public virtual void Draw(SpriteBatch spriteBatch)
  139. {
  140. spriteBatch.Draw(spriteSheet, position, srcRect, Color.White, angle, origin, scale, SpriteEffects.None, 1);
  141. }
  142. public void correctPosition()
  143. {
  144. position.X = (int)(position.X);
  145. position.Y = (int)(position.Y);
  146. }
  147. public void debug()
  148. {
  149. Console.WriteLine(boundingBox.ToString());
  150. Console.WriteLine("pos:(" + position.X + ", " + position.Y + ")");
  151. Console.WriteLine("ori:(" + origin.X + ", " + origin.Y + ")");
  152. Console.WriteLine("bou:(" + boundingBox.X + ", " + boundingBox.Y + ", " + boundingBox.Width + ", " + boundingBox.Height + ")");
  153. }
  154. }