| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace Tutorial021.Sprites
- {
- public class Player : Sprite
- {
- private bool _onGround;
- private bool _jumping;
- public Player(Texture2D texture)
- : base(texture)
- {
- }
- public override void Update(GameTime gameTime)
- {
- if (Keyboard.GetState().IsKeyDown(Keys.A))
- _velocity.X = -2f;
- else if (Keyboard.GetState().IsKeyDown(Keys.D))
- _velocity.X = 2f;
- else _velocity.X = 0f;
- if (Keyboard.GetState().IsKeyDown(Keys.Space))
- _jumping = true;
- }
- public override void OnCollide(Sprite sprite)
- {
- var test = sprite.Centre - (this.Centre);
- var onTop = this.WillIntersectTop(sprite);
- var onLeft = this.WillIntersectLeft(sprite);
- var onRight = this.WillIntersectRight(sprite);
- var onBotton = this.WillIntersectBottom(sprite);
- if (onTop)
- {
- _onGround = true;
- _velocity.Y = sprite.Rectangle.Top - this.Rectangle.Bottom;
- //this.Position = new Vector2(this.Position.X, sprite.Rectangle.Top - this.Origin.Y);
- //this._velocity.Y = 0;
- }
- else if (onLeft && _velocity.X > 0)
- {
- _velocity.X = 0;
- }
- else if (onRight && _velocity.X < 0)
- {
- _velocity.X = 0;
- }
- else if (onBotton)
- {
- _velocity.Y = 1;
- }
- }
- public override void ApplyPhysics(GameTime gameTime)
- {
- if (!_onGround)
- _velocity.Y += 0.2f;
- if (_onGround && _jumping)
- {
- _velocity.Y = -5f;
- }
- _onGround = false;
- _jumping = false;
- Position += _velocity;
- }
- }
- }
|