aboutsummaryrefslogtreecommitdiff
path: root/V3/Objects/Arrow.cs
blob: 57200b2469cc4d020baaab93eac4649691978e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
        }
    }
}