Advertisement
Alan468

XNA 001

Oct 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using GrafikaRuchoma_00.CustomClasses;
  12.  
  13. namespace GrafikaRuchoma_00
  14. {
  15. public class Game1 : Microsoft.Xna.Framework.Game
  16. {
  17. GraphicsDeviceManager graphics;
  18. SpriteBatch spriteBatch;
  19. Texture2D background;
  20.  
  21. List<List<Tile>> map;
  22. List<Tile> tools;
  23. Tile selectedTile = null;
  24. bool clickLock = false;
  25.  
  26. int mapX = 10, mapY = 10;
  27. int mapLeftOffset = 150;
  28.  
  29. public Game1()
  30. {
  31. graphics = new GraphicsDeviceManager(this);
  32. Content.RootDirectory = "Content";
  33.  
  34. graphics.PreferredBackBufferWidth = 800;
  35. graphics.PreferredBackBufferHeight = 600;
  36. this.Window.AllowUserResizing = true;
  37. this.IsMouseVisible = true;
  38. }
  39.  
  40. protected override void Initialize()
  41. {
  42. int tileScaleX = (GraphicsDevice.Viewport.Width - mapLeftOffset) / mapX;
  43. int tileScaleY = GraphicsDevice.Viewport.Height / mapY;
  44.  
  45. #region Mapa Gry
  46. map = new List<List<Tile>>();
  47. for (int x = 0; x < mapX; x++)
  48. {
  49. map.Add(new List<Tile>());
  50. for (int y = 0; y < mapY; y++)
  51. {
  52. map[x].Add(new Tile(Content.Load<Texture2D>("puste"), new Rectangle((x * tileScaleX) + mapLeftOffset, y * tileScaleY, tileScaleX, tileScaleY), 0));
  53. }
  54. }
  55. #endregion
  56.  
  57. #region Tool box
  58. tools = new List<Tile>();
  59. Texture2D gameSprites = Content.Load<Texture2D>("kafelki"); // 12 => 100x100
  60. Texture2D gameSpritesQ = Content.Load<Texture2D>("puste"); // 200x200
  61.  
  62. tools.Add(new Tile(gameSpritesQ, new Rectangle(50, 150, 100, 100), 0));
  63.  
  64. int i = 0;
  65. for (int sy = 0; sy < 2; sy++)
  66. {
  67. for (int sx = 0; sx < 4; sx++)
  68. {
  69. tools.Add(new Tile(gameSprites, new Rectangle(50, 100 + (50 * i), 50, 50), new Rectangle(sx * 100, sy * 100, 100, 100), 0));
  70. i++;
  71. }
  72. }
  73. #endregion
  74.  
  75. base.Initialize();
  76. }
  77.  
  78. protected override void LoadContent()
  79. {
  80. // Create a new SpriteBatch, which can be used to draw textures.
  81. spriteBatch = new SpriteBatch(GraphicsDevice);
  82.  
  83. background = Content.Load<Texture2D>("background");
  84.  
  85. // TODO: use this.Content to load your game content here
  86. }
  87.  
  88. protected override void UnloadContent()
  89. {
  90. // TODO: Unload any non ContentManager content here
  91. }
  92.  
  93. protected override void Update(GameTime gameTime)
  94. {
  95. // Allows the game to exit
  96. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  97. this.Exit();
  98.  
  99. #region Mapa Gry
  100. int tileScaleX = (GraphicsDevice.Viewport.Width - mapLeftOffset) / mapX;
  101. int tileScaleY = GraphicsDevice.Viewport.Height / mapY;
  102.  
  103. for (int x = 0; x < mapX; x++)
  104. {
  105. for (int y = 0; y < mapY; y++)
  106. {
  107. map[x][y].Position = new Rectangle((x * tileScaleX) + mapLeftOffset, y * tileScaleY, tileScaleX, tileScaleY);
  108. }
  109. }
  110. #endregion
  111.  
  112. var mouseState = Mouse.GetState();
  113. var mousePosition = new Point(mouseState.X, mouseState.Y);
  114.  
  115. if (mouseState.LeftButton == ButtonState.Pressed && !clickLock)
  116. {
  117. clickLock = true;
  118. foreach (var item in tools)
  119. {
  120. if (item.Position.Contains(mousePosition))
  121. {
  122. selectedTile = item;
  123. selectedTile.Rotation += 1.57F;
  124. break;
  125. }
  126. }
  127. }
  128. else if (mouseState.LeftButton == ButtonState.Released)
  129. {
  130. clickLock = false;
  131. }
  132.  
  133. base.Update(gameTime);
  134. }
  135.  
  136. protected override void Draw(GameTime gameTime)
  137. {
  138. GraphicsDevice.Clear(Color.CornflowerBlue);
  139.  
  140. spriteBatch.Begin();
  141.  
  142. spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.Wheat); // Wyrysowanie tła
  143.  
  144. #region Mapa Gry
  145. for (int x = 0; x < mapX; x++)
  146. {
  147. for (int y = 0; y < mapY; y++)
  148. {
  149. if (map[x][y].IsPart)
  150. spriteBatch.Draw(map[x][y].Sprite, map[x][y].Position, map[x][y].CutPart, Color.Wheat, map[x][y].Rotation,
  151. new Vector2(map[x][y].Position.Width / 2, map[x][y].Position.Height / 2), new SpriteEffects(), 0);
  152. else
  153. spriteBatch.Draw(map[x][y].Sprite, map[x][y].Position,
  154. new Rectangle(0, 0, map[x][y].Sprite.Width, map[x][y].Sprite.Height), Color.Wheat,
  155. map[x][y].Rotation,
  156. new Vector2(map[x][y].CutPart.X, map[x][y].CutPart.Y/2), new SpriteEffects(), 0);
  157. }
  158. }
  159. #endregion
  160.  
  161. #region Tool box
  162. for (int i = 0; i < tools.Count; i++)
  163. {
  164. if (tools[i].IsPart)
  165. spriteBatch.Draw(tools[i].Sprite, tools[i].Position, tools[i].CutPart, Color.Wheat, tools[i].Rotation,
  166. new Vector2(tools[i].Position.Width / 2, tools[i].Position.Height/2), new SpriteEffects(), 0);
  167. else
  168. spriteBatch.Draw(tools[i].Sprite, tools[i].Position,
  169. new Rectangle(0, 0, tools[i].Sprite.Width, tools[i].Sprite.Height), Color.Wheat,
  170. tools[i].Rotation,
  171. new Vector2(tools[i].CutPart.X, tools[i].CutPart.Y / 2), new SpriteEffects(), 0);
  172. }
  173. #endregion
  174.  
  175. spriteBatch.End();
  176.  
  177. base.Draw(gameTime);
  178. }
  179. }
  180.  
  181. public class Tile
  182. {
  183. public Texture2D Sprite { get; set; }
  184. public Rectangle Position { get; set; }
  185. public float Rotation { get; set; }
  186.  
  187. public bool IsPart { get; set; }
  188. public Rectangle CutPart { get; set; }
  189.  
  190. public Tile(Texture2D sprite, Rectangle position, int rotation)
  191. {
  192. Sprite = sprite;
  193. Position = position;
  194. Rotation = rotation;
  195.  
  196. IsPart = false;
  197. CutPart = new Rectangle(0, 0, 0, 0);
  198. }
  199.  
  200. public Tile(Texture2D sprite, Rectangle position, Rectangle cutPart, int rotation)
  201. {
  202. Sprite = sprite;
  203. Position = position;
  204. Rotation = rotation;
  205.  
  206. IsPart = true;
  207. CutPart = cutPart;
  208. }
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement