Advertisement
Guest User

Untitled

a guest
Mar 28th, 2012
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 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 Microsoft.Xna.Framework.Net;
  12. using Microsoft.Xna.Framework.Storage;
  13.  
  14. namespace Pressure
  15. {
  16.  
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19.  
  20.  
  21. GraphicsDeviceManager graphics;
  22. SpriteBatch spriteBatch;
  23. Texture2D player;
  24. Vector2 pos = Vector2.Zero;
  25. Vector2 playersp = new Vector2(50.0f, 50.0f);
  26. Texture2D road1;
  27. Texture2D road2;
  28. Texture2D brickwall;
  29. Texture2D floor1;
  30. Texture2D floor2;
  31. Texture2D floor3;
  32. Texture2D grass;
  33. Texture2D sidewalk1;
  34. Texture2D wood;
  35. Texture2D road3;
  36. private Vector2 origin;
  37. KeyboardState currentState;
  38. Camera camera = new Camera();
  39.  
  40. Vector2 motion;
  41.  
  42. List<Texture2D> tiles = new List<Texture2D>();
  43.  
  44. static int tileWidth = 64;
  45. static int tileHeight = 64;
  46.  
  47. int tileMapWidth;
  48. int tileMapHeight;
  49.  
  50. static int screenWidth;
  51. static int screenHeight;
  52.  
  53. static int mapWidthInPixels;
  54. static int mapHeightInPixels;
  55. int[,] map = {
  56. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  57. {0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  70. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  71. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  72. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  73. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  74. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
  75. };
  76. public static int ScreenWidth
  77. {
  78. get { return screenWidth; }
  79. }
  80.  
  81. public static int ScreenHeight
  82. {
  83. get { return screenHeight; }
  84. }
  85.  
  86. public static int MapWidthInPixels
  87. {
  88. get { return mapWidthInPixels; }
  89. }
  90.  
  91. public static int MapHeightInPixels
  92. {
  93. get { return mapHeightInPixels; }
  94. }
  95. public Game1()
  96. {
  97. graphics = new GraphicsDeviceManager(this);
  98. Content.RootDirectory = "Content";
  99. }
  100.  
  101.  
  102. protected override void Initialize()
  103. {
  104.  
  105. base.Initialize();
  106. }
  107.  
  108. protected override void LoadContent()
  109. {
  110. spriteBatch = new SpriteBatch(GraphicsDevice);
  111.  
  112. player = Content.Load<Texture2D>("haz");
  113. origin.X = player.Width /2;
  114. origin.Y = player.Height /2;
  115. grass = Content.Load<Texture2D>("grass");
  116. floor1 = Content.Load<Texture2D>("floor1");
  117. floor2 = Content.Load<Texture2D>("floor2");
  118. floor3 = Content.Load<Texture2D>("floor3");
  119. wood = Content.Load<Texture2D>("wood");
  120. road1 = Content.Load<Texture2D>("road1");
  121. road2 = Content.Load<Texture2D>("road2");
  122. sidewalk1 = Content.Load<Texture2D>("sidewalk1");
  123. brickwall = Content.Load<Texture2D>("brickwall");
  124. road3 = Content.Load<Texture2D>("road3");
  125. tiles.Add(grass); //0
  126. tiles.Add(floor1);//1
  127. tiles.Add(floor2);//2
  128. tiles.Add(floor3);//3
  129. tiles.Add(wood);//4
  130. tiles.Add(road1);//5
  131. tiles.Add(road2);//6
  132. tiles.Add(sidewalk1);//7
  133. tiles.Add(brickwall);//8
  134. tiles.Add(road3);//9
  135.  
  136. tileMapWidth = map.GetLength(1);
  137. tileMapHeight = map.GetLength(0);
  138.  
  139. mapWidthInPixels = tileMapWidth * tileWidth;
  140. mapHeightInPixels = tileMapHeight * tileHeight;
  141.  
  142. screenWidth = GraphicsDevice.Viewport.Width;
  143. screenHeight = GraphicsDevice.Viewport.Height;
  144. }
  145.  
  146.  
  147. protected override void UnloadContent()
  148. {
  149. }
  150. private float RotationAngle;
  151. private float oldx;
  152. private float oldy;
  153.  
  154. protected override void Update(GameTime gameTime)
  155. {
  156. oldx = playersp.X;
  157. oldy = playersp.Y;
  158. currentState = Keyboard.GetState();
  159. pos = playersp;
  160.  
  161. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  162. this.Exit();
  163.  
  164. IsMouseVisible = true;
  165.  
  166.  
  167. MouseState Mouses = Mouse.GetState();
  168. Vector2 mouseLook = new Vector2(Mouses.X, Mouses.Y);
  169.  
  170. motion = Vector2.Zero;
  171. Vector2 direction = (playersp ) - mouseLook;
  172. float angle = (float)(Math.Atan2(direction.Y, direction.X));
  173.  
  174. RotationAngle = angle ;
  175. if (currentState.IsKeyDown(Keys.W))
  176. {
  177. if (playersp.Y > screenHeight /4)
  178. {
  179. playersp.Y = playersp.Y - 1;
  180.  
  181. }
  182. else
  183. {
  184. ScrollUp();
  185.  
  186. }
  187. }
  188. if (currentState.IsKeyDown(Keys.A))
  189. {
  190. if (playersp.X > screenWidth / 4)
  191. {
  192.  
  193. playersp.X = playersp.X - 1;
  194.  
  195. }
  196. else
  197. {
  198. ScrollLeft();
  199.  
  200.  
  201. }
  202. }
  203. if (currentState.IsKeyDown(Keys.S))
  204. {
  205. if (playersp.Y > screenHeight / 1.5f)
  206. {
  207. ScrollDown();
  208. }else{
  209. playersp.Y = playersp.Y + 1;
  210. }
  211.  
  212. }
  213. if (currentState.IsKeyDown( Keys.D))
  214. {
  215.  
  216. if (playersp.X > screenWidth / 1.5f)
  217. {
  218.  
  219. ScrollRight();
  220. }
  221. else
  222. {
  223. playersp.X = playersp.X + 1;
  224.  
  225. }
  226.  
  227. }
  228. if (motion != Vector2.Zero)
  229. {
  230. motion.Normalize();
  231.  
  232. camera.Position += motion * camera.Speed;
  233. }
  234.  
  235. base.Update(gameTime);
  236. }
  237.  
  238. private void ScrollUp()
  239. {
  240. motion.Y = -0.5f;
  241. }
  242.  
  243. private void ScrollRight()
  244. {
  245. motion.X = 0.5f;
  246. }
  247.  
  248. private void ScrollDown()
  249. {
  250. motion.Y = 0.5f;
  251. }
  252.  
  253. private void ScrollLeft()
  254. {
  255. motion.X = -0.5f;
  256. }
  257.  
  258. private Point VectorToCell(Vector2 vector)
  259. {
  260. return new Point(
  261. (int)(vector.X / tileWidth),
  262. (int)(vector.Y / tileHeight));
  263. }
  264.  
  265. private Vector2 ViewPortVector()
  266. {
  267. return new Vector2(
  268. screenWidth + tileWidth,
  269. screenHeight + tileHeight);
  270. }
  271.  
  272.  
  273. protected override void Draw(GameTime gameTime)
  274. {
  275.  
  276. GraphicsDevice.Clear(Color.CornflowerBlue);
  277. DrawMap();
  278.  
  279.  
  280.  
  281.  
  282. base.Draw(gameTime);
  283.  
  284. }
  285.  
  286. private void DrawMap()
  287. {
  288. Point cameraPoint = VectorToCell(camera.Position);
  289. Point viewPoint = VectorToCell(camera.Position +
  290. ViewPortVector());
  291.  
  292. Point min = new Point();
  293. Point max = new Point();
  294.  
  295. min.X = cameraPoint.X;
  296. min.Y = cameraPoint.Y;
  297. max.X = (int)Math.Min(viewPoint.X, map.GetLength(1));
  298. max.Y = (int)Math.Min(viewPoint.Y, map.GetLength(0));
  299.  
  300. Rectangle tileRectangle = new Rectangle(
  301. 0,
  302. 0,
  303. tileWidth,
  304. tileHeight);
  305.  
  306. spriteBatch.Begin();
  307.  
  308. for (int y = min.Y; y < max.Y; y++)
  309. {
  310. for (int x = min.X; x < max.X; x++)
  311. {
  312. tileRectangle.X = x * tileWidth - (int)camera.Position.X;
  313. tileRectangle.Y = y * tileHeight - (int)camera.Position.Y;
  314.  
  315. spriteBatch.Draw(tiles[map[y, x]],
  316. tileRectangle,
  317. Color.White);
  318. spriteBatch.Draw(player, pos, null, Color.White, RotationAngle,
  319. origin, 1.0f, SpriteEffects.None, 0f);
  320. }
  321. }
  322.  
  323. spriteBatch.End();
  324. }
  325.  
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement