Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace TinkerWorX.Legeria
- {
- public class Entity
- {
- public Boolean IsAffectedByGravity = true;
- public Boolean IsAffectedByAirdrag = true;
- public Boolean IsAffectedByFriction = true;
- public Boolean IsAffectedByElasticity = true;
- public Boolean IsPushingDown { get; protected set; }
- public Boolean IsPushingUp { get; protected set; }
- public Boolean IsPushingLeft { get; protected set; }
- public Boolean IsPushingRight { get; protected set; }
- protected Single ActiveFriction = Single.NaN;
- protected Single ActiveElasticity = Single.NaN;
- private TileRealm realm;
- public TileRealm Realm
- {
- get { return realm; }
- set
- {
- if (value == this.realm)
- return;
- if (this.realm != null)
- this.realm.RemoveEntity(this);
- this.realm = value;
- if (this.realm != null)
- this.realm.AddEntity(this);
- }
- }
- protected Vector2 VelocityVector = Vector2.Zero;
- public Point Velocity
- {
- get { return new Point((Int32)this.VelocityVector.X, (Int32)this.VelocityVector.Y); }
- set { this.VelocityVector = new Vector2(value.X, value.Y); }
- }
- protected Vector2 PositionVector = Vector2.Zero;
- public Point Position
- {
- get { return new Point((Int32)Math.Floor(this.PositionVector.X), (Int32)Math.Floor(this.PositionVector.Y)); }
- set { this.PositionVector = new Vector2(value.X, value.Y); }
- }
- public Point Size = Point.Zero;
- 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); } }
- public Boolean IsMovingLeft { get { return (this.VelocityVector.X < 0); } }
- public Boolean IsMovingRight { get { return (this.VelocityVector.X > 0); } }
- public Boolean IsMovingUp { get { return (this.VelocityVector.Y < 0); } }
- public Boolean IsMovingDown { get { return (this.VelocityVector.Y > 0); } }
- public Int32 TileStartX { get { return (Int32)Math.Floor(this.BoundingBox.Left / (Single)TileWorld.TILE_SIZE); } }
- public Int32 TileEndX { get { return (Int32)Math.Floor((this.BoundingBox.Right - 0.001f) / TileWorld.TILE_SIZE); } }
- public Int32 TileStartY { get { return (Int32)Math.Floor(this.BoundingBox.Top / (Single)TileWorld.TILE_SIZE); } }
- public Int32 TileEndY { get { return (Int32)Math.Floor((this.BoundingBox.Bottom - 0.001f) / TileWorld.TILE_SIZE); } }
- 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)); } }
- public virtual void Update(GameTime gameTime)
- {
- if(this.realm == null)
- return;
- if (this.IsAffectedByGravity)
- this.VelocityVector += this.realm.Gravity;
- //TODO: The friction system needs work.
- if (this.IsPushingDown)
- {
- if (this.IsAffectedByFriction && !Single.IsNaN(this.ActiveFriction))
- {
- if (Math.Abs(this.VelocityVector.X) > Math.Abs(this.ActiveFriction))
- this.VelocityVector.X -= this.ActiveFriction * Math.Sign(this.VelocityVector.X);
- else
- this.VelocityVector.X = 0;
- }
- }
- else
- {
- if (this.IsAffectedByAirdrag && !Single.IsNaN(this.realm.Airdrag))
- {
- if (Math.Abs(this.VelocityVector.X) > this.realm.Airdrag)
- this.VelocityVector.X -= this.realm.Airdrag * Math.Sign(this.VelocityVector.X);
- else
- this.VelocityVector.X = 0;
- }
- }
- //TODO: Remove the speed limit.
- this.VelocityVector.Y = MathHelper.Clamp(this.VelocityVector.Y, -TileWorld.ENTITY_SPEED_LIMIT, TileWorld.ENTITY_SPEED_LIMIT);
- this.VelocityVector.X = MathHelper.Clamp(this.VelocityVector.X, -TileWorld.ENTITY_SPEED_LIMIT, TileWorld.ENTITY_SPEED_LIMIT);
- var velocity = this.ApplyTileCollision(this.VelocityVector * (Single)gameTime.ElapsedGameTime.TotalSeconds);
- //TODO: The elasticity system needs work.
- if (this.IsAffectedByElasticity && !Single.IsNaN(this.ActiveElasticity))
- {
- this.VelocityVector.Y *= -this.ActiveElasticity;
- }
- else
- {
- // If our vertical deltaVelocity is zero, we stop.
- if (Math.Abs(velocity.Y) < Single.Epsilon)
- this.VelocityVector.Y = 0;
- }
- //TODO: Add horizontal elasticity.
- // If our horizontal deltaVelocity is zero, we stop.
- if (Math.Abs(velocity.X) < Single.Epsilon)
- this.VelocityVector.X = 0;
- this.PositionVector += velocity;
- }
- private Vector2 ApplyTileCollision(Vector2 deltaVelocity)
- {
- var deltaPosition = this.PositionVector + deltaVelocity;
- // Reset active values, as they are recalculated.
- this.ActiveFriction = Single.NaN;
- this.ActiveElasticity = Single.NaN;
- var startx = this.TileStartX - 1;
- var endx = this.TileEndX + 1;
- var starty = this.TileStartY - 1;
- var endy = this.TileEndY + 1;
- TileRegion? horizontalWall = null;
- TileRegion? verticalWall = null;
- if (this.IsMovingRight)
- {
- verticalWall = this.realm.GetTileRegion(endx, starty + 1, endx, endy - 1);
- }
- else if (this.IsMovingLeft)
- {
- verticalWall = this.realm.GetTileRegion(startx, starty + 1, startx, endy - 1);
- }
- if (this.IsMovingDown)
- {
- horizontalWall = this.realm.GetTileRegion(startx + 1, endy, endx - 1, endy);
- }
- else if (this.IsMovingUp)
- {
- horizontalWall = this.realm.GetTileRegion(startx + 1, starty, endx - 1, starty);
- }
- this.IsPushingLeft = false;
- this.IsPushingRight = false;
- if (verticalWall.HasValue && verticalWall.Value.IsSolid)
- {
- if (this.IsMovingRight && deltaPosition.X + this.BoundingBox.Width >= verticalWall.Value.BoundingBox.Left)
- {
- this.IsPushingRight = true;
- deltaVelocity.X = verticalWall.Value.BoundingBox.Left - this.BoundingBox.Right;
- }
- else if (this.IsMovingLeft && deltaPosition.X < verticalWall.Value.BoundingBox.Right)
- {
- this.IsPushingLeft = true;
- deltaVelocity.X = verticalWall.Value.BoundingBox.Right - this.BoundingBox.Left;
- }
- }
- this.IsPushingDown = false;
- this.IsPushingUp = false;
- if (horizontalWall.HasValue && horizontalWall.Value.IsSolid)
- {
- if (this.IsMovingDown && deltaPosition.Y + this.BoundingBox.Height >= horizontalWall.Value.BoundingBox.Top)
- {
- this.ActiveFriction = horizontalWall.Value.Friction;
- this.IsPushingDown = true;
- if (deltaVelocity.Y > 4.00f)
- {
- this.ActiveElasticity = horizontalWall.Value.Elasticity;
- deltaVelocity.Y = horizontalWall.Value.BoundingBox.Top - this.BoundingBox.Bottom;
- }
- else
- {
- deltaVelocity.Y = horizontalWall.Value.BoundingBox.Top - this.BoundingBox.Bottom;
- }
- }
- else if (this.IsMovingUp && deltaPosition.Y < horizontalWall.Value.BoundingBox.Bottom)
- {
- this.IsPushingUp = true;
- deltaVelocity.Y = horizontalWall.Value.BoundingBox.Bottom - this.BoundingBox.Top;
- }
- }
- return deltaVelocity;
- }
- public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch) { }
- public virtual void DrawLight(GameTime gameTime, SpriteBatch spriteBatch) { }
- public virtual Boolean Input(GameTime gameTime) { return false; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement