public PlayerMovement(MovingObject movingObject)
     : base(movingObject)
 {
     _game = GameLogic.GetInstance().GetGame();
     GetMovingObject().SetPosition(new Vector2(_game.GraphicsDevice.Viewport.Width / 2, _game.GraphicsDevice.Viewport.Height * 0.9f));
     SetSpeed(new Vector2(4.0f, 0.0f));
 }
Exemple #2
0
 /// <summary>
 /// Sets forth a sine movement of 'movingObject'
 /// along vector 'direction'
 /// </summary>
 /// <param name="movingObject"></param>
 /// <param name="direction"></param>
 public Wave(MovingObject movingObject, Vector2 direction)
     : base(movingObject)
 {
     _direction = direction;
     _modDirection = new Vector2(-_direction.X, _direction.Y);
     _speed = _direction.Length();
     _basePosition = GetMovingObject().GetPosition();
 }
Exemple #3
0
        public Follow(MovingObject movingObject, ArrayList targets, float speed)
            : base(movingObject)
        {
            // Use this for a list of objects that can be followed
            _targets = targets;
            _speed = speed;

            TargetValidation();

            // We want the initial direction to be straight up
            // in case that there are no enemies on screen
            // TODO: Make Follow initial vector dynamic
            SetSpeed(new Vector2(0.0f, -_speed));
        }
Exemple #4
0
        public Follow(MovingObject movingObject, MovingObject target, ArrayList targets, float speed)
            : base(movingObject)
        {
            // Use this for one object that should be followed,
            // the object should always exist (the player's Ship for example)
            _target = target;
            _speed = speed;

            // If the target ceases to exist, we need a fallback to point to
            _fallbackTargets = targets;

            TargetValidation();
            Vector2 v = GameHelper.PointToTarget(GetMovingObject(), _target);
            v = v * _speed;
            v = v * new Vector2(1.0f, -1.0f);
            SetSpeed(v);
        }
Exemple #5
0
 /// <summary>
 /// Makes 'movingObject' move with vector 'speed'
 /// </summary>
 /// <param name="movingObject"></param>
 /// <param name="speed"></param>
 public StraightLine(MovingObject movingObject, Vector2 speed)
     : base(movingObject)
 {
     SetSpeed(speed);
 }
 public MovingBehaviour(MovingObject movingObject)
 {
     _movingObject = movingObject;
 }
Exemple #7
0
        /// <summary>
        /// Checks for collisions, stores them in the stack
        /// and returns a boolean
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="movingObjectArray"></param>
        /// <param name="stack"></param>
        /// <returns></returns>
        public static bool CollisionHappened(MovingObject subject, ArrayList movingObjectArray, Stack stack)
        {
            Vector2 subjectOrigin = subject.GetOrigin();
            Vector2 subjectPosition = subject.GetPosition();
            Texture2D subjectTexture = subject.GetTexture();
            float _scale = GameLogic.GetInstance().GetScale();

            _subjectTextureData =
                new Color[subjectTexture.Width * subjectTexture.Height];
            subjectTexture.GetData(_subjectTextureData);

            // Update the subject's transform
            Matrix subjectTransform =
                Matrix.CreateTranslation(new Vector3(-subjectOrigin, 0.0f)) *
                Matrix.CreateScale(_scale) *
                Matrix.CreateTranslation(new Vector3(subjectPosition, 0.0f));

            // Get the bounding rectangle of the subject
            Rectangle subjectRectangle = CalculateBoundingRectangle(
                new Rectangle(0, 0,
                subjectTexture.Width, subjectTexture.Height), subjectTransform);

            bool collision = false;
            for (int i = movingObjectArray.Count - 1; i >= 0; i--)
            {
                MovingObject movingObject = (MovingObject)movingObjectArray[i];
                Vector2 movingObjectOrigin = movingObject.GetOrigin();
                Vector2 movingObjectPosition = movingObject.GetPosition();
                Texture2D movingObjectTexture = movingObject.GetTexture();

                _movingObjectTextureData =
                    new Color[movingObjectTexture.Width * movingObjectTexture.Height];
                movingObjectTexture.GetData(_movingObjectTextureData);

                // Build the movingObject's transform
                Matrix movingObjectTransform =
                    Matrix.CreateTranslation(new Vector3(-movingObjectOrigin, 0.0f)) *
                    Matrix.CreateScale(_scale) *
                    // Matrix.CreateRotationZ(movingObjectRotation) *
                    Matrix.CreateTranslation(new Vector3(movingObjectPosition, 0.0f));

                // Calculate the bounding rectangle of this movingObject in world space
                Rectangle movingObjectRectangle = CalculateBoundingRectangle(
                         new Rectangle(0, 0, movingObjectTexture.Width, movingObjectTexture.Height),
                         movingObjectTransform);

                // The per-pixel check is expensive, so check the bounding rectangles
                // first to prevent testing pixels when collisions are impossible.
                if (subjectRectangle.Intersects(movingObjectRectangle) || subjectRectangle.Contains(movingObjectRectangle))
                {
                    // Check collision with person
                    if (IntersectPixels(subjectTransform, subjectTexture.Width,
                                        subjectTexture.Height, _subjectTextureData,
                                        movingObjectTransform, movingObjectTexture.Width,
                                        movingObjectTexture.Height, _movingObjectTextureData))
                    {
                        collision = true;
                        stack.Push(movingObject);
                    }
                }
            }

            return collision;
        }
Exemple #8
0
 /// <summary>
 /// Returns a unit vector of the direction from
 /// subject to target
 /// </summary>
 /// <param name="subject"></param>
 /// <param name="target"></param>
 /// <returns></returns>
 public static Vector2 PointToTarget(MovingObject subject, MovingObject target)
 {
     Vector2 v = target.GetPosition() - subject.GetPosition();
     v.Normalize();
     return v;
 }
Exemple #9
0
 public RowByRow(MovingObject movingObject)
     : base(movingObject)
 {
 }
Exemple #10
0
 /// <summary>
 /// Checks if targets are still valid and takes
 /// appropriate action if they aren't.
 /// </summary>
 private void TargetValidation()
 {
     if (_targets != null)
     {
         // If target wasn't set or no longer exists
         if (_target == null || !_targets.Contains(_target))
         {
             // Choose a target at random
             Random random = new Random();
             if (_targets.Count != 0)
             {
                 int t = random.Next(0, _targets.Count);
                 _target = (MovingObject)_targets[t];
             }
             else
             {
                 // We've run out of targets, null everything
                 _target = null;
                 _targets = null;
                 _fallbackTargets = null;
             }
         }
     }
     else
     {
         if (_fallbackTargets != null)
         {
             _targets = _fallbackTargets;
         }
     }
 }