Advertisement
Guest User

Untitled

a guest
Jan 30th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.83 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4.  
  5. namespace TinkerWorX.Legeria
  6. {
  7.     public class Entity
  8.     {
  9.         public Boolean IsAffectedByGravity = true;
  10.         public Boolean IsAffectedByAirdrag = true;
  11.         public Boolean IsAffectedByFriction = true;
  12.         public Boolean IsAffectedByElasticity = true;
  13.  
  14.         public Boolean IsPushingDown { get; protected set; }
  15.         public Boolean IsPushingUp { get; protected set; }
  16.         public Boolean IsPushingLeft { get; protected set; }
  17.         public Boolean IsPushingRight { get; protected set; }
  18.  
  19.         protected Single ActiveFriction = Single.NaN;
  20.         protected Single ActiveElasticity = Single.NaN;
  21.  
  22.         private TileRealm realm;
  23.  
  24.         public TileRealm Realm
  25.         {
  26.             get { return realm; }
  27.             set
  28.             {
  29.                 if (value == this.realm)
  30.                     return;
  31.  
  32.                 if (this.realm != null)
  33.                     this.realm.RemoveEntity(this);
  34.  
  35.                 this.realm = value;
  36.  
  37.                 if (this.realm != null)
  38.                     this.realm.AddEntity(this);
  39.             }
  40.         }
  41.  
  42.         protected Vector2 VelocityVector = Vector2.Zero;
  43.  
  44.         public Point Velocity
  45.         {
  46.             get { return new Point((Int32)this.VelocityVector.X, (Int32)this.VelocityVector.Y); }
  47.             set { this.VelocityVector = new Vector2(value.X, value.Y); }
  48.         }
  49.  
  50.         protected Vector2 PositionVector = Vector2.Zero;
  51.  
  52.         public Point Position
  53.         {
  54.             get { return new Point((Int32)Math.Floor(this.PositionVector.X), (Int32)Math.Floor(this.PositionVector.Y)); }
  55.             set { this.PositionVector = new Vector2(value.X, value.Y); }
  56.         }
  57.  
  58.         public Point Size = Point.Zero;
  59.  
  60.         public Rectangle BoundingBox { get { return new Rectangle((Int32)Math.Floor(this.PositionVector.X), (Int32)Math.Floor(this.PositionVector.Y), this.Size.X, this.Size.Y); } }
  61.  
  62.         public Boolean IsMovingLeft { get { return (this.VelocityVector.X < 0); } }
  63.  
  64.         public Boolean IsMovingRight { get { return (this.VelocityVector.X > 0); } }
  65.  
  66.         public Boolean IsMovingUp { get { return (this.VelocityVector.Y < 0); } }
  67.  
  68.         public Boolean IsMovingDown { get { return (this.VelocityVector.Y > 0); } }
  69.  
  70.         public Int32 TileStartX { get { return (Int32)Math.Floor(this.BoundingBox.Left / (Single)TileWorld.TILE_SIZE); } }
  71.  
  72.         public Int32 TileEndX { get { return (Int32)Math.Floor((this.BoundingBox.Right - 0.001f) / TileWorld.TILE_SIZE); } }
  73.  
  74.         public Int32 TileStartY { get { return (Int32)Math.Floor(this.BoundingBox.Top / (Single)TileWorld.TILE_SIZE); } }
  75.  
  76.         public Int32 TileEndY { get { return (Int32)Math.Floor((this.BoundingBox.Bottom - 0.001f) / TileWorld.TILE_SIZE); } }
  77.  
  78.         public Point TileCenter { get { return new Point((Int32)Math.Floor(this.BoundingBox.Center.X / (Single)TileWorld.TILE_SIZE), (Int32)Math.Floor(this.BoundingBox.Center.Y / (Single)TileWorld.TILE_SIZE)); } }
  79.  
  80.         public virtual void Update(GameTime gameTime)
  81.         {
  82.             if(this.realm == null)
  83.                 return;
  84.  
  85.             if (this.IsAffectedByGravity)
  86.                 this.VelocityVector += this.realm.Gravity;
  87.  
  88.             //TODO: The friction system needs work.
  89.             if (this.IsPushingDown)
  90.             {
  91.                 if (this.IsAffectedByFriction && !Single.IsNaN(this.ActiveFriction))
  92.                 {
  93.                     if (Math.Abs(this.VelocityVector.X) > Math.Abs(this.ActiveFriction))
  94.                         this.VelocityVector.X -= this.ActiveFriction * Math.Sign(this.VelocityVector.X);
  95.                     else
  96.                         this.VelocityVector.X = 0;
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 if (this.IsAffectedByAirdrag && !Single.IsNaN(this.realm.Airdrag))
  102.                 {
  103.                     if (Math.Abs(this.VelocityVector.X) > this.realm.Airdrag)
  104.                         this.VelocityVector.X -= this.realm.Airdrag * Math.Sign(this.VelocityVector.X);
  105.                     else
  106.                         this.VelocityVector.X = 0;
  107.                 }
  108.             }
  109.  
  110.             //TODO: Remove the speed limit.
  111.             this.VelocityVector.Y = MathHelper.Clamp(this.VelocityVector.Y, -TileWorld.ENTITY_SPEED_LIMIT, TileWorld.ENTITY_SPEED_LIMIT);
  112.             this.VelocityVector.X = MathHelper.Clamp(this.VelocityVector.X, -TileWorld.ENTITY_SPEED_LIMIT, TileWorld.ENTITY_SPEED_LIMIT);
  113.  
  114.             var velocity = this.ApplyTileCollision(this.VelocityVector * (Single)gameTime.ElapsedGameTime.TotalSeconds);
  115.  
  116.             //TODO: The elasticity system needs work.
  117.             if (this.IsAffectedByElasticity && !Single.IsNaN(this.ActiveElasticity))
  118.             {
  119.                 this.VelocityVector.Y *= -this.ActiveElasticity;
  120.             }
  121.             else
  122.             {
  123.                 // If our vertical deltaVelocity is zero, we stop.
  124.                 if (Math.Abs(velocity.Y) < Single.Epsilon)
  125.                     this.VelocityVector.Y = 0;
  126.             }
  127.  
  128.             //TODO: Add horizontal elasticity.
  129.             // If our horizontal deltaVelocity is zero, we stop.
  130.             if (Math.Abs(velocity.X) < Single.Epsilon)
  131.                 this.VelocityVector.X = 0;
  132.  
  133.             this.PositionVector += velocity;
  134.         }
  135.  
  136.         private Vector2 ApplyTileCollision(Vector2 deltaVelocity)
  137.         {
  138.             var deltaPosition = this.PositionVector + deltaVelocity;
  139.  
  140.             // Reset active values, as they are recalculated.
  141.             this.ActiveFriction = Single.NaN;
  142.             this.ActiveElasticity = Single.NaN;
  143.  
  144.             var startx = this.TileStartX - 1;
  145.             var endx = this.TileEndX + 1;
  146.             var starty = this.TileStartY - 1;
  147.             var endy = this.TileEndY + 1;
  148.  
  149.             TileRegion? horizontalWall = null;
  150.             TileRegion? verticalWall = null;
  151.  
  152.             if (this.IsMovingRight)
  153.             {
  154.                 verticalWall = this.realm.GetTileRegion(endx, starty + 1, endx, endy - 1);
  155.             }
  156.             else if (this.IsMovingLeft)
  157.             {
  158.                 verticalWall = this.realm.GetTileRegion(startx, starty + 1, startx, endy - 1);
  159.             }
  160.  
  161.             if (this.IsMovingDown)
  162.             {
  163.                 horizontalWall = this.realm.GetTileRegion(startx + 1, endy, endx - 1, endy);
  164.             }
  165.             else if (this.IsMovingUp)
  166.             {
  167.                 horizontalWall = this.realm.GetTileRegion(startx + 1, starty, endx - 1, starty);
  168.             }
  169.  
  170.             this.IsPushingLeft = false;
  171.             this.IsPushingRight = false;
  172.             if (verticalWall.HasValue && verticalWall.Value.IsSolid)
  173.             {
  174.                 if (this.IsMovingRight && deltaPosition.X + this.BoundingBox.Width >= verticalWall.Value.BoundingBox.Left)
  175.                 {
  176.                     this.IsPushingRight = true;
  177.                     deltaVelocity.X = verticalWall.Value.BoundingBox.Left - this.BoundingBox.Right;
  178.                 }
  179.                 else if (this.IsMovingLeft && deltaPosition.X < verticalWall.Value.BoundingBox.Right)
  180.                 {
  181.                     this.IsPushingLeft = true;
  182.                     deltaVelocity.X = verticalWall.Value.BoundingBox.Right - this.BoundingBox.Left;
  183.                 }
  184.             }
  185.  
  186.             this.IsPushingDown = false;
  187.             this.IsPushingUp = false;
  188.             if (horizontalWall.HasValue && horizontalWall.Value.IsSolid)
  189.             {
  190.                 if (this.IsMovingDown && deltaPosition.Y + this.BoundingBox.Height >= horizontalWall.Value.BoundingBox.Top)
  191.                 {
  192.                     this.ActiveFriction = horizontalWall.Value.Friction;
  193.                     this.IsPushingDown = true;
  194.                     if (deltaVelocity.Y > 4.00f)
  195.                     {
  196.                         this.ActiveElasticity = horizontalWall.Value.Elasticity;
  197.                         deltaVelocity.Y = horizontalWall.Value.BoundingBox.Top - this.BoundingBox.Bottom;
  198.                     }
  199.                     else
  200.                     {
  201.                         deltaVelocity.Y = horizontalWall.Value.BoundingBox.Top - this.BoundingBox.Bottom;
  202.                     }
  203.                 }
  204.                 else if (this.IsMovingUp && deltaPosition.Y < horizontalWall.Value.BoundingBox.Bottom)
  205.                 {
  206.                     this.IsPushingUp = true;
  207.                     deltaVelocity.Y = horizontalWall.Value.BoundingBox.Bottom - this.BoundingBox.Top;
  208.                 }
  209.             }
  210.  
  211.             return deltaVelocity;
  212.         }
  213.  
  214.         public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch) { }
  215.  
  216.         public virtual void DrawLight(GameTime gameTime, SpriteBatch spriteBatch) { }
  217.  
  218.         public virtual Boolean Input(GameTime gameTime) { return false; }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement