Advertisement
Guest User

Entity.cs

a guest
Jun 30th, 2019
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. namespace EngineTest
  11. {
  12. public class Entity : ICloneable
  13. {
  14. protected Texture2D _texture;
  15. protected float _rotation;
  16.  
  17. public Vector2 Position;
  18. public Vector2 Origin;
  19. public Vector2 Velocity;
  20. public Vector2 Speed;
  21. public Color Color = Color.White;
  22. public Rectangle Rectangle
  23. {
  24. get
  25. {
  26. return new Rectangle((int)Position.X, (int)Position.Y, _texture.Width, _texture.Height);
  27. }
  28.  
  29. }
  30.  
  31. public Entity (Texture2D texture)
  32. {
  33. _texture = texture;
  34. //Origin = new Vector2(_texture.Width / 2, _texture.Height / 2);
  35. }
  36.  
  37. public virtual void Update(GameTime gameTime, List<Entity> entities)
  38. {
  39.  
  40. }
  41. public virtual void Draw (SpriteBatch spriteBatch)
  42. {
  43. spriteBatch.Draw(_texture, Position, null, Color.White, _rotation, Origin, 1, SpriteEffects.None, 0);
  44. }
  45. public object Clone()
  46. {
  47. return this.MemberwiseClone();
  48. }
  49.  
  50. #region Collision
  51. protected bool IsTouchingLeft(Vector2 pos, Entity entity)
  52. {
  53. return pos.X + _texture.Width >= entity.Rectangle.Left &&
  54. pos.X <= entity.Rectangle.Left &&
  55. pos.Y + _texture.Height >= entity.Rectangle.Top &&
  56. pos.Y <= entity.Rectangle.Bottom;
  57. }
  58. protected bool IsTouchingRight(Vector2 pos, Entity entity)
  59. {
  60. return pos.X >= entity.Rectangle.Right &&
  61. pos.X + _texture.Width >= entity.Rectangle.Right &&
  62. pos.Y + _texture.Height >= entity.Rectangle.Top &&
  63. pos.Y <= entity.Rectangle.Bottom;
  64. }
  65. protected bool IsTouchingTop(Vector2 pos, Entity entity)
  66. {
  67. return pos.X <= entity.Rectangle.Right &&
  68. pos.X + _texture.Width >= entity.Rectangle.Left &&
  69. pos.Y + _texture.Height >= entity.Rectangle.Top &&
  70. pos.Y <= entity.Rectangle.Top;
  71. }
  72. protected bool IsTouchingBottom(Vector2 pos, Entity entity)
  73. {
  74. return pos.X <= entity.Rectangle.Right &&
  75. pos.X + _texture.Width >= entity.Rectangle.Left &&
  76. pos.Y <= entity.Rectangle.Bottom &&
  77. pos.Y + _texture.Height >= entity.Rectangle.Bottom;
  78. }
  79. protected bool PlaceMeeting(Vector2 pos, Entity entity)
  80. {
  81. return IsTouchingLeft(pos, entity) ||
  82. IsTouchingRight(pos, entity) ||
  83. IsTouchingTop(pos, entity) ||
  84. IsTouchingBottom(pos, entity);
  85. }
  86. #endregion
  87. }
  88. }
Advertisement
RAW Paste Data Copied
Advertisement