diff options
author | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
---|---|---|
committer | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
commit | ced3d03bdb3ce866d832e03fb212865140905a9a (patch) | |
tree | 2a16c2063a46d3c354ce1585029dda3124f6ad93 /V3/Objects/Arrow.cs | |
parent | 0394dccaf06e1009e591a6ff4d645895574724c1 (diff) | |
download | V3-ced3d03bdb3ce866d832e03fb212865140905a9a.tar.gz V3-ced3d03bdb3ce866d832e03fb212865140905a9a.tar.bz2 |
Diffstat (limited to 'V3/Objects/Arrow.cs')
-rw-r--r-- | V3/Objects/Arrow.cs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/V3/Objects/Arrow.cs b/V3/Objects/Arrow.cs new file mode 100644 index 0000000..57200b2 --- /dev/null +++ b/V3/Objects/Arrow.cs @@ -0,0 +1,109 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using V3.Objects.Sprite; + +namespace V3.Objects +{ + public sealed class Arrow + { + //Drawing an arrow + private const int SpeedModifier = 10; + private readonly ArrowSprite mArrow; + private Vector2 mArrowPosition; + private Vector2 mArrowGoal; + private Vector2 mDirection; + private bool mArrowDraw; + private readonly MovementDirection mMovementDirection; + + public Arrow(Vector2 start, Vector2 goal) + { + mArrowPosition = start; + mArrowGoal = goal; + mDirection = goal - start; + mDirection.Normalize(); + mMovementDirection = GiveMovementDirection(mDirection); + mArrow = new ArrowSprite(); + mArrowDraw = true; + } + + public void LoadArrow(ContentManager contentManager) + { + mArrow.Load(contentManager); + } + + public void DrawArrow(SpriteBatch spriteBatch) + { + if (mArrowDraw) + mArrow.Draw(spriteBatch, mArrowPosition, MovementState.Idle, mMovementDirection); + } + + /// <summary> + /// The moving for arrow + /// </summary> + public void UpdateArrow() + { + if (mArrowDraw) + mArrowPosition += mDirection * SpeedModifier; + + if (Vector2.Distance(mArrowPosition, mArrowGoal) < 1f * SpeedModifier) + mArrowDraw = false; + } + + /// <summary> + /// Calculates the direction the creature is looking when moving. + /// Method copied from PlayerMovement class. + /// </summary> + private MovementDirection GiveMovementDirection(Vector2 direction) + { + // |\ + // |α\ α == 22.5° + // b| \ 1 β == 67.5° + // | β\ + // ────── + // a + const float b = 0.92f; // b == sin β + const float a = 0.38f; // a == sin α + MovementDirection movementDirection; + if (direction.X < -b) + { + movementDirection = MovementDirection.W; + } + else if (direction.X > b) + { + movementDirection = MovementDirection.O; + } + else if (direction.Y > 0) + { + if (direction.X < -a) + { + movementDirection = MovementDirection.SW; + } + else if (direction.X > a) + { + movementDirection = MovementDirection.SO; + } + else + { + movementDirection = MovementDirection.S; + } + } + else + { + if (direction.X < -a) + { + movementDirection = MovementDirection.NW; + } + else if (direction.X > a) + { + movementDirection = MovementDirection.NO; + } + else + { + movementDirection = MovementDirection.N; + } + } + return movementDirection; + } + } +} |