Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace PlatFormer
- {
- class TPlayer : Entity
- {
- //Collision
- public bool _OnGround;
- private float _PreviousBottom;
- private Rectangle _Collbox;
- private Texture2D _Outline;
- //Vertical movement
- private const float _JumpingPower = 650f;
- private bool _IsJumping;
- //Horizontal movement
- private const float _Friction = 0.9f;
- private const float _AccelerationRate = 0.5f;
- private Vector2 _Acceleration;
- public Rectangle CollisionRectangle
- {
- get
- {
- Vector2 origin = new Vector2(Animation.FrameWidth / 2.0f, Animation.FrameHeight);
- int left = (int)Math.Round(Position.X - origin.X) + _Collbox.X;
- int top = (int)Math.Round(Position.Y - origin.Y) + _Collbox.Y;
- return new Rectangle(left, top, _Collbox.Width, _Collbox.Height);
- }
- }
- public override void LoadContent(ContentManager content, InputManager input)
- {
- base.LoadContent(content, input);
- _Outline = content.Load<Texture2D>("Outlining");
- _FileManager = new FileManager();
- _MoveAnimation = new SpriteSheetAnimation();
- Vector2 tempFrames = Vector2.Zero;
- int offset = 0;
- _FileManager.LoadContent("Load/Player.txt", _Attributes, _Contents);
- for (int i = 0; i < _Attributes.Count; i++)
- {
- for (int j = 0; j < _Attributes[i].Count; j++)
- {
- switch (_Attributes[i][j])
- {
- case "Health":
- _Health = int.Parse(_Contents[i][j]);
- break;
- case "Frames":
- string[] frames = _Contents[i][j].Split(' ');
- tempFrames = new Vector2(int.Parse(frames[0]), int.Parse(frames[1]));
- break;
- case "Image":
- _Image = this._Content.Load<Texture2D>(_Contents[i][j]);
- break;
- case "Position":
- frames = _Contents[i][j].Split(' ');
- Position = new Vector2(int.Parse(frames[0]), int.Parse(frames[1]));
- break;
- case "Offset":
- offset = int.Parse(_Contents[i][j]);
- break;
- }
- }
- }
- _Gravity = 0.25f;
- _MoveSpeed = 0.2f;
- _Velocity = Vector2.Zero;
- _TilePositionSync = false;
- _ActivateGravity = true;
- _MoveAnimation.LoadContent(content, _Image, "", Position, tempFrames, offset);
- }
- public override void UnloadContent()
- {
- base.UnloadContent();
- _MoveAnimation.UnloadContent();
- }
- private void GetInput(GameTime gameTime, InputManager input)
- {
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- _PrevPosition = Position;
- _MoveAnimation.IsActive = true;
- if (input.KeyDown(Keys.Right, Keys.D))
- {
- _MoveAnimation.CurrentFrame = new Vector2(_MoveAnimation.CurrentFrame.X, 0);
- _Acceleration.X += _AccelerationRate;
- }
- if (input.KeyDown(Keys.Left, Keys.A))
- {
- _MoveAnimation.CurrentFrame = new Vector2(_MoveAnimation.CurrentFrame.X, 1);
- _Acceleration.X -= _AccelerationRate;
- }
- if (Math.Abs(_Acceleration.X) >= _MoveSpeed)
- {
- _Acceleration.X = MathHelper.Clamp(_Acceleration.X, -_MoveSpeed, _MoveSpeed);
- }
- if (input.KeyDown(Keys.Up, Keys.W) && _OnGround)
- {
- _MoveAnimation.CurrentFrame = new Vector2(_MoveAnimation.CurrentFrame.X, 3);
- _Velocity.Y = -12f;
- _OnGround = false;
- _IsJumping = true;
- //activateGravity = true;
- }
- if (input.KeyDown(Keys.Q))
- {
- //position.X = 0;
- Position.Y = 0;
- _Velocity.Y = 0;
- }
- if (input.KeyDown(Keys.E))
- {
- Position.X = 0;
- Position.Y = 0;
- _Acceleration = Vector2.Zero;
- _Velocity = Vector2.Zero;
- }
- _Velocity.Y = MathHelper.Clamp(_Velocity.Y + _Gravity, -40f, 100f);
- if (_Velocity.Y > 0.0f && _Velocity.Y < 0.5f)
- _Velocity.Y = 0.6f;
- if (!_OnGround) { }
- //velocity.Y += gravity * elapsed;
- else
- _Velocity.Y = 0;
- }
- private void Jump()
- {
- _Velocity.Y = -12.0f;
- }
- private void UpdatePhysics(GameTime gameTime, InputManager input, Layer layer)
- {
- _Acceleration *= _Friction;
- _Velocity *= _Friction;
- //if (velocity.X != 0)
- // velocity.X = velocity.X > 0 ? (float)Math.Ceiling(velocity.X) : (float)Math.Floor(velocity.X);
- _Velocity += _Acceleration;
- Position += _Velocity;
- if (Math.Abs(_Acceleration.X) < 0.001f)
- {
- _MoveAnimation.IsActive = false;
- }
- UpdateCollBox();
- CollisionHandle(layer);
- if (Position.X == _PrevPosition.X)
- _Velocity.X = 0;
- if (Position.Y == _PrevPosition.Y)
- _Velocity.Y = 0;
- }
- private void CollisionHandle(Layer layer)
- {
- int leftTile = (int)Math.Floor((float)_Collbox.Left / WorldTile.Width);
- int rightTile = (int)Math.Ceiling(((float)_Collbox.Right / WorldTile.Width)) - 1;
- int topTile = (int)Math.Floor((float)_Collbox.Top / WorldTile.Height);
- int bottomTile = (int)Math.Ceiling(((float)_Collbox.Bottom / WorldTile.Height)) - 1;
- _OnGround = false;
- for (int y = topTile; y <= bottomTile; y++)
- {
- for (int x = leftTile; x <= rightTile; x++)
- {
- WorldTile.TileCollision collision = layer.GetCollision(y, x);
- if (collision != WorldTile.TileCollision.Empty)
- {
- Rectangle tileBounds = layer.GetBounds(x, y);
- Vector2 depth = IntersectDepth(_Collbox, tileBounds);
- if (depth != Vector2.Zero)
- {
- float absDepthX = Math.Abs(depth.X);
- float absDepthY = Math.Abs(depth.Y);
- if (absDepthY < absDepthX || collision == WorldTile.TileCollision.OneWay)
- {
- if (_PreviousBottom <= tileBounds.Top)
- _OnGround = true;
- if ((collision == WorldTile.TileCollision.Solid) || _OnGround)
- {
- //position.Y = (float)Math.Round(position.Y);
- //position.X = (float)Math.Round(position.X);
- Position = new Vector2((int)Math.Round(Position.X), (int)Math.Round(Position.Y + depth.Y));
- UpdateCollBox();
- //pBounds = CollisionRectangle;
- }
- }
- else if (collision == WorldTile.TileCollision.Solid)
- {
- Position = new Vector2((int)Math.Round(Position.X + depth.X), (int)Math.Round(Position.Y));
- _Velocity.Y = 0;
- UpdateCollBox();
- //pBounds = CollisionRectangle;
- }
- }
- }
- }
- }
- _PreviousBottom = _Collbox.Bottom;
- }
- private void UpdateCollBox()
- {
- _Collbox = new Rectangle((int)Math.Round(Position.X), (int)Math.Round(Position.Y), Animation.FrameWidth, Animation.FrameHeight);
- }
- private Vector2 IntersectDepth(Rectangle rectangleA, Rectangle rectangleB)
- {
- float halfWidthA = rectangleA.Width / 2.0f;
- float halfHeightA = rectangleA.Height / 2.0f;
- float halfWidthB = rectangleB.Width / 2.0f;
- float halfHeightB = rectangleB.Height / 2.0f;
- Vector2 centerA = new Vector2(rectangleA.Left + halfWidthA, rectangleA.Top + halfHeightA);
- Vector2 centerB = new Vector2(rectangleB.Left + halfWidthB, rectangleB.Top + halfHeightB);
- float distanceX = centerA.X - centerB.X;
- float distanceY = centerA.Y - centerB.Y;
- float minDistanceX = halfWidthA + halfWidthB;
- float minDistanceY = halfHeightA + halfHeightB;
- // If no intersection is happening, return Vector2.Zero
- if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
- return Vector2.Zero;
- // Calculate instersection depth
- float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
- float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
- return new Vector2(depthX, depthY);
- }
- public override void Update(GameTime gameTime, InputManager input, Layer layer)
- {
- _PrevPosition = Position;
- GetInput(gameTime, input);
- UpdatePhysics(gameTime, input, layer);
- _MoveAnimation.Position = Position;
- _MoveAnimation.Update(gameTime);
- }
- public override void Draw(SpriteBatch spriteBatch)
- {
- _MoveAnimation.Draw(spriteBatch);
- spriteBatch.Draw(_Outline, _Collbox, Color.White);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement