Advertisement
Guest User

PlayerClass XNA Super Mario World 2

a guest
Mar 25th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10.  
  11. namespace PlatFormer
  12. {
  13. class TPlayer : Entity
  14. {
  15. //Collision
  16. public bool _OnGround;
  17. private float _PreviousBottom;
  18. private Rectangle _Collbox;
  19. private Texture2D _Outline;
  20.  
  21. //Vertical movement
  22. private const float _JumpingPower = 650f;
  23. private bool _IsJumping;
  24.  
  25. //Horizontal movement
  26. private const float _Friction = 0.9f;
  27. private const float _AccelerationRate = 0.5f;
  28. private Vector2 _Acceleration;
  29.  
  30. public Rectangle CollisionRectangle
  31. {
  32. get
  33. {
  34. Vector2 origin = new Vector2(Animation.FrameWidth / 2.0f, Animation.FrameHeight);
  35. int left = (int)Math.Round(Position.X - origin.X) + _Collbox.X;
  36. int top = (int)Math.Round(Position.Y - origin.Y) + _Collbox.Y;
  37.  
  38. return new Rectangle(left, top, _Collbox.Width, _Collbox.Height);
  39. }
  40. }
  41.  
  42. public override void LoadContent(ContentManager content, InputManager input)
  43. {
  44. base.LoadContent(content, input);
  45. _Outline = content.Load<Texture2D>("Outlining");
  46. _FileManager = new FileManager();
  47. _MoveAnimation = new SpriteSheetAnimation();
  48. Vector2 tempFrames = Vector2.Zero;
  49. int offset = 0;
  50.  
  51. _FileManager.LoadContent("Load/Player.txt", _Attributes, _Contents);
  52. for (int i = 0; i < _Attributes.Count; i++)
  53. {
  54. for (int j = 0; j < _Attributes[i].Count; j++)
  55. {
  56. switch (_Attributes[i][j])
  57. {
  58. case "Health":
  59. _Health = int.Parse(_Contents[i][j]);
  60. break;
  61. case "Frames":
  62. string[] frames = _Contents[i][j].Split(' ');
  63. tempFrames = new Vector2(int.Parse(frames[0]), int.Parse(frames[1]));
  64. break;
  65. case "Image":
  66. _Image = this._Content.Load<Texture2D>(_Contents[i][j]);
  67. break;
  68. case "Position":
  69. frames = _Contents[i][j].Split(' ');
  70. Position = new Vector2(int.Parse(frames[0]), int.Parse(frames[1]));
  71. break;
  72. case "Offset":
  73. offset = int.Parse(_Contents[i][j]);
  74. break;
  75. }
  76. }
  77. }
  78. _Gravity = 0.25f;
  79. _MoveSpeed = 0.2f;
  80. _Velocity = Vector2.Zero;
  81. _TilePositionSync = false;
  82. _ActivateGravity = true;
  83.  
  84. _MoveAnimation.LoadContent(content, _Image, "", Position, tempFrames, offset);
  85. }
  86.  
  87. public override void UnloadContent()
  88. {
  89. base.UnloadContent();
  90. _MoveAnimation.UnloadContent();
  91. }
  92.  
  93. private void GetInput(GameTime gameTime, InputManager input)
  94. {
  95. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  96. _PrevPosition = Position;
  97.  
  98. _MoveAnimation.IsActive = true;
  99. if (input.KeyDown(Keys.Right, Keys.D))
  100. {
  101. _MoveAnimation.CurrentFrame = new Vector2(_MoveAnimation.CurrentFrame.X, 0);
  102. _Acceleration.X += _AccelerationRate;
  103. }
  104. if (input.KeyDown(Keys.Left, Keys.A))
  105. {
  106. _MoveAnimation.CurrentFrame = new Vector2(_MoveAnimation.CurrentFrame.X, 1);
  107. _Acceleration.X -= _AccelerationRate;
  108. }
  109. if (Math.Abs(_Acceleration.X) >= _MoveSpeed)
  110. {
  111. _Acceleration.X = MathHelper.Clamp(_Acceleration.X, -_MoveSpeed, _MoveSpeed);
  112. }
  113.  
  114. if (input.KeyDown(Keys.Up, Keys.W) && _OnGround)
  115. {
  116. _MoveAnimation.CurrentFrame = new Vector2(_MoveAnimation.CurrentFrame.X, 3);
  117. _Velocity.Y = -12f;
  118. _OnGround = false;
  119. _IsJumping = true;
  120. //activateGravity = true;
  121. }
  122. if (input.KeyDown(Keys.Q))
  123. {
  124. //position.X = 0;
  125. Position.Y = 0;
  126. _Velocity.Y = 0;
  127. }
  128. if (input.KeyDown(Keys.E))
  129. {
  130. Position.X = 0;
  131. Position.Y = 0;
  132. _Acceleration = Vector2.Zero;
  133. _Velocity = Vector2.Zero;
  134. }
  135.  
  136. _Velocity.Y = MathHelper.Clamp(_Velocity.Y + _Gravity, -40f, 100f);
  137. if (_Velocity.Y > 0.0f && _Velocity.Y < 0.5f)
  138. _Velocity.Y = 0.6f;
  139. if (!_OnGround) { }
  140. //velocity.Y += gravity * elapsed;
  141. else
  142. _Velocity.Y = 0;
  143. }
  144. private void Jump()
  145. {
  146. _Velocity.Y = -12.0f;
  147. }
  148. private void UpdatePhysics(GameTime gameTime, InputManager input, Layer layer)
  149. {
  150. _Acceleration *= _Friction;
  151. _Velocity *= _Friction;
  152.  
  153. //if (velocity.X != 0)
  154. // velocity.X = velocity.X > 0 ? (float)Math.Ceiling(velocity.X) : (float)Math.Floor(velocity.X);
  155.  
  156. _Velocity += _Acceleration;
  157. Position += _Velocity;
  158.  
  159. if (Math.Abs(_Acceleration.X) < 0.001f)
  160. {
  161. _MoveAnimation.IsActive = false;
  162. }
  163.  
  164. UpdateCollBox();
  165.  
  166. CollisionHandle(layer);
  167.  
  168. if (Position.X == _PrevPosition.X)
  169. _Velocity.X = 0;
  170.  
  171. if (Position.Y == _PrevPosition.Y)
  172. _Velocity.Y = 0;
  173. }
  174.  
  175. private void CollisionHandle(Layer layer)
  176. {
  177. int leftTile = (int)Math.Floor((float)_Collbox.Left / WorldTile.Width);
  178. int rightTile = (int)Math.Ceiling(((float)_Collbox.Right / WorldTile.Width)) - 1;
  179. int topTile = (int)Math.Floor((float)_Collbox.Top / WorldTile.Height);
  180. int bottomTile = (int)Math.Ceiling(((float)_Collbox.Bottom / WorldTile.Height)) - 1;
  181.  
  182. _OnGround = false;
  183.  
  184. for (int y = topTile; y <= bottomTile; y++)
  185. {
  186. for (int x = leftTile; x <= rightTile; x++)
  187. {
  188. WorldTile.TileCollision collision = layer.GetCollision(y, x);
  189.  
  190. if (collision != WorldTile.TileCollision.Empty)
  191. {
  192. Rectangle tileBounds = layer.GetBounds(x, y);
  193. Vector2 depth = IntersectDepth(_Collbox, tileBounds);
  194. if (depth != Vector2.Zero)
  195. {
  196.  
  197. float absDepthX = Math.Abs(depth.X);
  198. float absDepthY = Math.Abs(depth.Y);
  199.  
  200. if (absDepthY < absDepthX || collision == WorldTile.TileCollision.OneWay)
  201. {
  202. if (_PreviousBottom <= tileBounds.Top)
  203. _OnGround = true;
  204.  
  205. if ((collision == WorldTile.TileCollision.Solid) || _OnGround)
  206. {
  207. //position.Y = (float)Math.Round(position.Y);
  208. //position.X = (float)Math.Round(position.X);
  209. Position = new Vector2((int)Math.Round(Position.X), (int)Math.Round(Position.Y + depth.Y));
  210.  
  211. UpdateCollBox();
  212. //pBounds = CollisionRectangle;
  213. }
  214. }
  215. else if (collision == WorldTile.TileCollision.Solid)
  216. {
  217. Position = new Vector2((int)Math.Round(Position.X + depth.X), (int)Math.Round(Position.Y));
  218. _Velocity.Y = 0;
  219. UpdateCollBox();
  220. //pBounds = CollisionRectangle;
  221. }
  222. }
  223. }
  224. }
  225. }
  226.  
  227. _PreviousBottom = _Collbox.Bottom;
  228. }
  229.  
  230. private void UpdateCollBox()
  231. {
  232. _Collbox = new Rectangle((int)Math.Round(Position.X), (int)Math.Round(Position.Y), Animation.FrameWidth, Animation.FrameHeight);
  233. }
  234.  
  235. private Vector2 IntersectDepth(Rectangle rectangleA, Rectangle rectangleB)
  236. {
  237. float halfWidthA = rectangleA.Width / 2.0f;
  238. float halfHeightA = rectangleA.Height / 2.0f;
  239. float halfWidthB = rectangleB.Width / 2.0f;
  240. float halfHeightB = rectangleB.Height / 2.0f;
  241.  
  242. Vector2 centerA = new Vector2(rectangleA.Left + halfWidthA, rectangleA.Top + halfHeightA);
  243. Vector2 centerB = new Vector2(rectangleB.Left + halfWidthB, rectangleB.Top + halfHeightB);
  244.  
  245. float distanceX = centerA.X - centerB.X;
  246. float distanceY = centerA.Y - centerB.Y;
  247. float minDistanceX = halfWidthA + halfWidthB;
  248. float minDistanceY = halfHeightA + halfHeightB;
  249.  
  250. // If no intersection is happening, return Vector2.Zero
  251. if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
  252. return Vector2.Zero;
  253.  
  254. // Calculate instersection depth
  255. float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
  256. float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
  257. return new Vector2(depthX, depthY);
  258. }
  259.  
  260. public override void Update(GameTime gameTime, InputManager input, Layer layer)
  261. {
  262. _PrevPosition = Position;
  263. GetInput(gameTime, input);
  264. UpdatePhysics(gameTime, input, layer);
  265.  
  266. _MoveAnimation.Position = Position;
  267. _MoveAnimation.Update(gameTime);
  268. }
  269.  
  270. public override void Draw(SpriteBatch spriteBatch)
  271. {
  272. _MoveAnimation.Draw(spriteBatch);
  273. spriteBatch.Draw(_Outline, _Collbox, Color.White);
  274. }
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement