GameSprite2D.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameSprite2D.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using RobotGameData.Render;
  17. using RobotGameData.Resource;
  18. using RobotGameData.Collision;
  19. using RobotGameData.GameInterface;
  20. #endregion
  21. namespace RobotGameData.GameObject
  22. {
  23. #region Sprite2DObject
  24. /// <summary>
  25. /// this structure stores an 2D sprite object’s information.
  26. /// </summary>
  27. public class Sprite2DObject : INamed
  28. {
  29. #region Fields
  30. String name = String.Empty;
  31. Rectangle screenRectangle = Rectangle.Empty;
  32. Rectangle sourceRectangle = Rectangle.Empty;
  33. Color color = new Color(0xFF, 0xFF, 0xFF);
  34. float rotation = 0.0f;
  35. float scale = 1.0f;
  36. bool visible = true;
  37. #endregion
  38. #region Properties
  39. public string Name
  40. {
  41. get { return name; }
  42. set { name = value; }
  43. }
  44. public bool Visible
  45. {
  46. get { return visible; }
  47. set { visible = value; }
  48. }
  49. public Rectangle ScreenRectangle
  50. {
  51. get { return screenRectangle; }
  52. set { screenRectangle = value; }
  53. }
  54. public Vector2 ScreenPosition
  55. {
  56. get
  57. {
  58. return new Vector2(screenRectangle.X, screenRectangle.Y);
  59. }
  60. set
  61. {
  62. screenRectangle.X = (int)value.X;
  63. screenRectangle.Y = (int)value.Y;
  64. }
  65. }
  66. public Vector2 ScreenSize
  67. {
  68. get
  69. {
  70. return new Vector2(screenRectangle.Width, screenRectangle.Height);
  71. }
  72. set
  73. {
  74. screenRectangle.Width = (int)value.X;
  75. screenRectangle.Height = (int)value.Y;
  76. }
  77. }
  78. public Rectangle SourceRectangle
  79. {
  80. get { return sourceRectangle; }
  81. set { sourceRectangle = value; }
  82. }
  83. public Vector2 SourcePosition
  84. {
  85. get
  86. {
  87. return new Vector2(sourceRectangle.X, sourceRectangle.Y);
  88. }
  89. set
  90. {
  91. sourceRectangle.X = (int)value.X;
  92. sourceRectangle.Y = (int)value.Y;
  93. }
  94. }
  95. public Vector2 SourceSize
  96. {
  97. get
  98. {
  99. return new Vector2(sourceRectangle.Width, sourceRectangle.Height);
  100. }
  101. set
  102. {
  103. sourceRectangle.Width = (int)value.X;
  104. sourceRectangle.Height = (int)value.Y;
  105. }
  106. }
  107. public Color Color
  108. {
  109. get { return color; }
  110. set { color = value; }
  111. }
  112. public byte Alpha
  113. {
  114. get { return color.A; }
  115. set { color = new Color(color.R, color.R, color.B, value); }
  116. }
  117. public float Rotation
  118. {
  119. get { return rotation; }
  120. set { rotation = value; }
  121. }
  122. public float Scale
  123. {
  124. get { return scale; }
  125. set { scale = value; }
  126. }
  127. #endregion
  128. #region Constructors
  129. public Sprite2DObject(string name, Texture2D tex)
  130. {
  131. Name = name;
  132. ScreenRectangle = new Rectangle(0, 0, tex.Width, tex.Height);
  133. SourceRectangle = new Rectangle(0, 0, tex.Width, tex.Height);
  134. }
  135. public Sprite2DObject(string name, Texture2D tex, Vector2 screenPosition)
  136. {
  137. Name = name;
  138. ScreenRectangle = new Rectangle(
  139. (int)screenPosition.X, (int)screenPosition.Y,
  140. tex.Width, tex.Height);
  141. SourceRectangle = new Rectangle(0, 0, tex.Width, tex.Height);
  142. }
  143. public Sprite2DObject(string name, Texture2D tex,
  144. Vector2 screenPosition, Vector2 screenSize)
  145. {
  146. Name = name;
  147. ScreenRectangle = new Rectangle(
  148. (int)screenPosition.X, (int)screenPosition.Y,
  149. (int)screenSize.X, (int)screenSize.Y);
  150. SourceRectangle = new Rectangle(0, 0, tex.Width, tex.Height);
  151. }
  152. public Sprite2DObject(string name,
  153. Vector2 screenPosition, Vector2 screenSize,
  154. Vector2 sourcePosition, Vector2 sourceSize)
  155. {
  156. Name = name;
  157. ScreenRectangle = new Rectangle(
  158. (int)screenPosition.X, (int)screenPosition.Y,
  159. (int)screenSize.X, (int)screenSize.Y);
  160. SourceRectangle = new Rectangle(
  161. (int)sourcePosition.X, (int)sourcePosition.Y,
  162. (int)sourceSize.X, (int)sourceSize.Y);
  163. }
  164. public Sprite2DObject(string name, Texture2D tex,
  165. Vector2 screenPosition, Rectangle sourceRectangle)
  166. {
  167. Name = name;
  168. ScreenRectangle = new Rectangle(
  169. (int)screenPosition.X, (int)screenPosition.Y,
  170. tex.Width, tex.Height);
  171. SourceRectangle = sourceRectangle;
  172. }
  173. public Sprite2DObject(string name, Rectangle screenRectangle,
  174. Rectangle sourceRectangle)
  175. {
  176. Name = name;
  177. ScreenRectangle = screenRectangle;
  178. SourceRectangle = sourceRectangle;
  179. }
  180. #endregion
  181. }
  182. #endregion
  183. /// <summary>
  184. /// this sprite draws an image to the 2D screen.
  185. /// </summary>
  186. public class GameSprite2D : GameSceneNode
  187. {
  188. #region Fields
  189. Texture2D texture2D = null;
  190. List<Sprite2DObject> spriteList = new List<Sprite2DObject>();
  191. #endregion
  192. #region Properties
  193. public Texture2D TextureResource { get { return texture2D; } }
  194. #endregion
  195. /// <summary>
  196. /// draws the registered sprite objects by using the sprite batch.
  197. /// </summary>
  198. /// <param name="renderTracer"></param>
  199. protected override void OnDraw(RenderTracer renderTracer)
  200. {
  201. if (TextureResource == null)
  202. throw new InvalidOperationException("The texture is empty");
  203. else if (TextureResource.IsDisposed )
  204. throw new ObjectDisposedException("TextureResource");
  205. for (int i = 0; i < spriteList.Count; i++)
  206. {
  207. Sprite2DObject sprite = spriteList[i];
  208. if (sprite != null)
  209. {
  210. // If active visible
  211. if (sprite.Visible )
  212. {
  213. renderTracer.SpriteBatch.Draw(TextureResource,
  214. sprite.ScreenRectangle, sprite.SourceRectangle,
  215. sprite.Color);
  216. }
  217. }
  218. }
  219. }
  220. protected override void UnloadContent()
  221. {
  222. spriteList.Clear();
  223. base.UnloadContent();
  224. }
  225. /// <summary>
  226. /// creates 2D sprite objects using the texture.
  227. /// </summary>
  228. /// <param name="count">sprite objects count</param>
  229. /// <param name="fileName">texture file name</param>
  230. public void Create(int count, string fileName)
  231. {
  232. for (int i = 0; i < count; i++)
  233. spriteList.Add(null);
  234. GameResourceTexture2D resource =
  235. FrameworkCore.ResourceManager.LoadTexture(fileName);
  236. texture2D = resource.Texture2D;
  237. }
  238. /// <summary>
  239. /// add a sprite object.
  240. /// </summary>
  241. /// <param name="index">an index of sprite object</param>
  242. /// <param name="name">sprite object name</param>
  243. /// <returns></returns>
  244. public Sprite2DObject AddSprite(int index, string name)
  245. {
  246. if (TextureResource == null)
  247. throw new InvalidOperationException("The texture is empty");
  248. Sprite2DObject newSprite = new Sprite2DObject(name, TextureResource);
  249. AddSprite(index, newSprite);
  250. return newSprite;
  251. }
  252. /// <summary>
  253. /// add a sprite object.
  254. /// </summary>
  255. /// <param name="index">an index of sprite object</param>
  256. /// <param name="name">sprite object name</param>
  257. /// <param name="screenPosition">
  258. /// 2D screen position of sprite object (pixel)
  259. /// </param>
  260. /// <returns></returns>
  261. public Sprite2DObject AddSprite(int index, string name, Vector2 screenPosition)
  262. {
  263. if (TextureResource == null)
  264. throw new InvalidOperationException("The texture is empty");
  265. Sprite2DObject newSprite =
  266. new Sprite2DObject(name, TextureResource, screenPosition);
  267. AddSprite(index, newSprite);
  268. return newSprite;
  269. }
  270. /// <summary>
  271. /// add a sprite object.
  272. /// </summary>
  273. /// <param name="index">an index of sprite object</param>
  274. /// <param name="name">sprite object name</param>
  275. /// <param name="screenPosition">
  276. /// 2D screen position of sprite object (pixel)
  277. /// </param>
  278. /// <param name="screenSize">2D screen size of sprite object (pixel)</param>
  279. /// <returns></returns>
  280. public Sprite2DObject AddSprite(int index, string name,
  281. Vector2 screenPosition, Vector2 screenSize)
  282. {
  283. if (TextureResource == null)
  284. throw new InvalidOperationException("The texture is empty");
  285. Sprite2DObject newSprite =
  286. new Sprite2DObject(name, TextureResource, screenPosition, screenSize);
  287. AddSprite(index, newSprite);
  288. return newSprite;
  289. }
  290. /// <summary>
  291. /// add a sprite object.
  292. /// </summary>
  293. /// <param name="index">an index of sprite object</param>
  294. /// <param name="name">sprite object name</param>
  295. /// <param name="screenPosition">
  296. /// 2D screen position of sprite object (pixel)
  297. /// </param>
  298. /// <param name="screenSize">2D screen size of sprite object (pixel)</param>
  299. /// <param name="sourcePosition">position of the source image (pixel)</param>
  300. /// <param name="sourceSize">size of the source image (pixel)</param>
  301. /// <returns></returns>
  302. public Sprite2DObject AddSprite(int index, string name,
  303. Vector2 screenPosition, Vector2 screenSize,
  304. Vector2 sourcePosition, Vector2 sourceSize)
  305. {
  306. if (TextureResource == null)
  307. throw new InvalidOperationException("The texture is empty");
  308. Sprite2DObject newSprite = new Sprite2DObject(name,
  309. screenPosition, screenSize,
  310. sourcePosition, sourceSize);
  311. AddSprite(index, newSprite);
  312. return newSprite;
  313. }
  314. /// <summary>
  315. /// add a sprite object.
  316. /// </summary>
  317. /// <param name="index">an index of sprite object</param>
  318. /// <param name="name">sprite object name</param>
  319. /// <param name="screenPosition">
  320. /// 2D screen position of sprite object (pixel)
  321. /// </param>
  322. /// <param name="sourceRectangle">a rectangle of the source image (pixel)</param>
  323. /// <returns></returns>
  324. public Sprite2DObject AddSprite(int index, string name,
  325. Vector2 screenPosition,
  326. Rectangle sourceRectangle)
  327. {
  328. if (TextureResource == null)
  329. throw new InvalidOperationException("The texture is empty");
  330. Sprite2DObject newSprite = new Sprite2DObject(name, TextureResource,
  331. screenPosition, sourceRectangle);
  332. AddSprite(index, newSprite);
  333. return newSprite;
  334. }
  335. /// <summary>
  336. /// add a sprite object.
  337. /// </summary>
  338. /// <param name="index">an index of sprite object</param>
  339. /// <param name="name">sprite object name</param>
  340. /// <param name="screenRectangle">a rectangle of sprite object (pixel)</param>
  341. /// <param name="sourceRectangle">a rectangle of the source image (pixel)</param>
  342. /// <returns></returns>
  343. public Sprite2DObject AddSprite(int index, string name,
  344. Rectangle screenRectangle,
  345. Rectangle sourceRectangle)
  346. {
  347. if (TextureResource == null)
  348. throw new InvalidOperationException("The texture is empty");
  349. Sprite2DObject newSprite =
  350. new Sprite2DObject(name, screenRectangle, sourceRectangle);
  351. AddSprite(index, newSprite);
  352. return newSprite;
  353. }
  354. /// <summary>
  355. /// add a sprite object.
  356. /// </summary>
  357. /// <param name="index">an index of sprite object</param>
  358. /// <param name="spriteObject">source sprite object</param>
  359. public void AddSprite(int index, Sprite2DObject spriteObject)
  360. {
  361. if (TextureResource == null)
  362. throw new InvalidOperationException("The texture is empty");
  363. if (spriteList.Count <= index || index < 0)
  364. throw new ArgumentException(
  365. "Cannot add sprite. Invalid index : " + index.ToString());
  366. if( spriteList[index] != null)
  367. throw new ArgumentException(
  368. "Cannot add sprite. already exist other sprite : " +
  369. index.ToString());
  370. spriteList[index] = spriteObject;
  371. }
  372. /// <summary>
  373. /// gets the sprite object.
  374. /// </summary>
  375. /// <param name="index">an index of sprite object</param>
  376. /// <returns>sprite object</returns>
  377. public Sprite2DObject GetSprite(int index)
  378. {
  379. if( spriteList.Count >= index || index < 0)
  380. throw new ArgumentException( "Invalid index : " +
  381. index.ToString());
  382. return spriteList[index];
  383. }
  384. /// <summary>
  385. /// configures visibility of all sprite objects.
  386. /// </summary>
  387. /// <param name="visible">visibility flag</param>
  388. public void VisibleSprite(bool visible)
  389. {
  390. for (int i = 0; i < spriteList.Count; i++)
  391. spriteList[i].Visible = visible;
  392. }
  393. /// <summary>
  394. /// configures a visibility of each sprite object.
  395. /// </summary>
  396. /// <param name="index">an index of sprite object</param>
  397. /// <param name="visible">visibility flag</param>
  398. public void VisibleSprite(int index, bool visible)
  399. {
  400. GetSprite(index).Visible = visible;
  401. }
  402. /// <summary>
  403. /// finds an index of sprite object by object
  404. /// </summary>
  405. /// <param name="sprite"></param>
  406. /// <returns></returns>
  407. public int FindSpriteIndex(Sprite2DObject sprite)
  408. {
  409. return spriteList.IndexOf(sprite);
  410. }
  411. /// <summary>
  412. /// swaps two sprite objects and changes the priority and the
  413. /// position in the list.
  414. /// </summary>
  415. /// <param name="tex1"></param>
  416. /// <param name="tex2"></param>
  417. public void SwapSprite(Sprite2DObject tex1, Sprite2DObject tex2)
  418. {
  419. int index1 = FindSpriteIndex(tex1);
  420. // Cannot find sprite
  421. if( index1 < 0)
  422. throw new ArgumentException("Cannot find tex1");
  423. int index2 = FindSpriteIndex(tex2);
  424. // Cannot find sprite
  425. if (index2 < 0)
  426. throw new ArgumentException("Cannot find tex2");
  427. spriteList[index1] = tex2;
  428. spriteList[index2] = tex1;
  429. }
  430. }
  431. }