// Take a normalized vector and return the direction it's facing.
        // There's a quite of lot of operations here I'm sure it could be made more efficent.
        private Direction VectorToDirection(Vector direction)
        {
            Vector up = new Vector(0, 1, 0);
            Vector down = new Vector(0, -1, 0);
            Vector left = new Vector(-1, 0, 0);
            Vector right = new Vector(1, 0, 0);

            double upDiff = Math.Acos(direction.DotProduct(up));
            double downDiff = Math.Acos(direction.DotProduct(down));
            double leftDiff = Math.Acos(direction.DotProduct(left));
            double rightDiff = Math.Acos(direction.DotProduct(right));

            double smallest = Math.Min(Math.Min(upDiff, downDiff), Math.Min(leftDiff, rightDiff));

            // yes there's a precidence if they're the same value, it doesn't matter
            if (smallest == upDiff)
            {
                return Direction.Up;
            }
            else if (smallest == downDiff)
            {
                return Direction.Down;
            }
            else if (smallest == leftDiff)
            {
                return Direction.Left;
            }
            else
            {
                return Direction.Right;
            }
        }
Example #2
0
 public Matrix(Vector x, Vector y, Vector z, Vector o)
 {
     _m11 = x.X; _m12 = x.Y; _m13 = x.Z;
     _m21 = y.X; _m22 = y.Y; _m23 = y.Z;
     _m31 = z.X; _m32 = z.Y; _m33 = z.Z;
     _m41 = o.X; _m42 = o.Y; _m43 = o.Z;
 }
Example #3
0
        public Vector NextMove(Vector currentPosition, double elapsedTime)
        {
            _elapsedTime += elapsedTime;
            var prng = new Random();
            double ranTime = (double) prng.Next(5, 25)/10;

            if (currentPosition.X > 600) _lastMove = new Vector(-1, 0, 0);
            else if (currentPosition.X < -600) _lastMove = new Vector(1, 0, 0);
            else if (currentPosition.Y > 370) _lastMove = new Vector(0, -1, 0);
            else if (currentPosition.Y < -370) _lastMove = new Vector(0, 1, 0);

            else if (_elapsedTime > ranTime)
            {
                _elapsedTime = 0;
                int ranNum = prng.Next(0, 3);

                switch (ranNum)
                {
                    case 0:
                        _lastMove = new Vector(0, 1, 0);
                        break;
                    case 1:
                        _lastMove = new Vector(1, 0, 0);
                        break;
                    case 2:
                        _lastMove = new Vector(0, -1, 0);
                        break;
                    case 3:
                        _lastMove = new Vector(-1, 0, 0);
                        break;
                }
            }
            return _lastMove;
        }
Example #4
0
 public Tile(string tileName, Texture texture, TileCollision tileCollision, Vector position)
 {
     TileName = tileName;
     TileCollision = tileCollision;
     Sprite.Texture = texture;
     Sprite.SetPosition(position);
 }
Example #5
0
 public Ship(Vector pos, Game game, ParticleEngine particleEngine, Arena arena)
 {
     position = pos;
     gameref = game;
     particles = particleEngine;
     arenaref = arena;
 }
Example #6
0
 public void AddExplosion(Vector position)
 {
     var explosion = new AnimatedSprite {Texture = _textureManager.Get("explosion")};
     explosion.SetAnimation(4, 4);
     explosion.SetPosition(position);
     _effects.Add(explosion);
 }
Example #7
0
        private void CreateText(double x, double y, double maxWidth)
        {
            _bitmapText.Clear();
            double currentX = 0;
            double currentY = 0;

            string[] words = _text.Split(' ');

            foreach (string word in words)
            {
                Vector nextWordLength = _font.MeasureFont(word);

                if (maxWidth != -1 &&
                    (currentX + nextWordLength.X) > maxWidth)
                {
                    currentX = 0;
                    currentY += nextWordLength.Y;
                }

                string wordWithSpace = word + " "; // add the space character that was removed.

                foreach (char c in wordWithSpace)
                {
                    CharacterSprite sprite = _font.CreateSprite(c);
                    float xOffset = ((float)sprite.Data.XOffset) / 2;
                    float yOffset = (((float)sprite.Data.Height) * 0.5f) + ((float)sprite.Data.YOffset);
                    sprite.Sprite.SetPosition(x + currentX + xOffset, y - currentY - yOffset);
                    currentX += sprite.Data.XAdvance;
                    _bitmapText.Add(sprite);
                }
            }
            _dimensions = _font.MeasureFont(_text, _maxWidth);
            _dimensions.Y = currentY;
            SetColor(_color);
        }
Example #8
0
        public void Move(Vector amount)
        {
            amount *= Speed;

            if (Math.Abs(amount.X) > Math.Abs(amount.Y))
            {
                if (amount.X > 0)
                {
                    MoveRight();
                }
                else
                {
                    MoveLeft();
                }
            }

            else
            {
                if (amount.Y > 0)
                {
                    MoveUp();
                }
                else
                {
                    MoveDown();
                }
            }

            Sprite.SetPosition(Sprite.GetPosition() + amount);
        }
Example #9
0
 /// <summary>
 ///     Constructs an enemy given the texture and AI (IEnemyBrain)
 /// </summary>
 /// <param name="texture">The texture for the enemy</param>
 /// <param name="enemyBrain">The AI for the enemy</param>
 /// <param name="position">The spawn point for this enemy</param>
 public Enemy(Texture texture, IEnemyBrain enemyBrain, Vector position)
 {
     _enemyBrain = enemyBrain;
     Sprite.Texture = texture;
     Sprite.SetPosition(position);
     RestartShootCountDown();
 }
Example #10
0
        /// <summary>
        ///     Calculates the signed depth of intersection between two rectangles.
        /// </summary>
        /// <returns>
        ///     The amount of overlap between two intersecting rectangles. These
        ///     depth values can be negative depending on which wides the rectangles
        ///     intersect. This allows callers to determine the correct direction
        ///     to push objects in order to resolve collisions.
        ///     If the rectangles are not intersecting, Vector2.Zero is returned.
        /// </returns>
        public static Vector GetIntersectionDepth(this RectangleF rectA, RectangleF rectB)
        {
            // Calculate half sizes.
            double halfWidthA = rectA.Width/2.0f;
            double halfHeightA = rectA.Height/2.0f;
            double halfWidthB = rectB.Width/2.0f;
            double halfHeightB = rectB.Height/2.0f;

            // Calculate centers.
            var centerA = new Vector(rectA.Left + halfWidthA, rectA.Top + halfHeightA, 0);
            var centerB = new Vector(rectB.Left + halfWidthB, rectB.Top + halfHeightB, 0);

            // Calculate current and minimum-non-intersecting distances between centers.
            double distanceX = centerA.X - centerB.X;
            double distanceY = centerA.Y - centerB.Y;
            double minDistanceX = halfWidthA + halfWidthB;
            double minDistanceY = halfHeightA + halfHeightB;

            // If we are not intersecting at all, return (0, 0).
            if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
                return Vector.Zero;

            // Calculate and return intersection depths.
            double depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
            double depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
            return new Vector(depthX, depthY, 0);
        }
Example #11
0
 //Spawn a new particle
 public void SpawnParticle(int timeToLive, Vector position, Vector velocity, Vector accelleration, double red, double green, double blue, double alpha, bool gradualFade, int size)
 {
     if (particles.Count < maxParticles)
     {
         particles.Add(new Particle(texture, timeToLive, position, velocity, accelleration, red, green, blue, alpha, gradualFade, size));
     }
 }
Example #12
0
        public void Update(double elapsedTime)
        {
            // Get controls and apply to player character
            double _x = _input.Controller.LeftControlStick.X;
            double _y = _input.Controller.LeftControlStick.Y * -1;
            Vector controlInput = new Vector(_x, _y, 0);

            if (Math.Abs(controlInput.Length()) < 0.0001)
            {
                // If the input is very small, then the player may not be using 
                // a controller;, they might be using the keyboard.
                if (_input.Keyboard.IsKeyHeld(Keys.Left))
                {
                    controlInput.X = -1;
                }

                if (_input.Keyboard.IsKeyHeld(Keys.Right))
                {
                    controlInput.X = 1;
                }

                if (_input.Keyboard.IsKeyHeld(Keys.Up))
                {
                    controlInput.Y = 1;
                }

                if (_input.Keyboard.IsKeyHeld(Keys.Down))
                {
                    controlInput.Y = -1;
                }
            }

            _playerCharacter.Move(controlInput * elapsedTime);

        }
Example #13
0
        //Character attributes
        public BasicGroundEnemy(Game game, Vector position, Vector velocity, Dictionary<string, Sprite> sprites, string defaultSprite, IController controller, //GameObject attributes
		               WorldPhysics worldPhysics, ObjectPhysics objectPhysics, Dictionary<string, BoundingPolygon> boundingPolygons,	//PhysicalObject attributes
		               double runSpeed, double maxSpeed)
            : base(game, position, velocity, sprites, defaultSprite, controller, worldPhysics, objectPhysics, boundingPolygons, runSpeed, maxSpeed)
        {
            Stompable = true;
        }
Example #14
0
 public EditorModel()
 {
     Running = true;
     MousePosition = new Vector();
     Objects = new List<MapObject>();
     ExtraProperties = new Dictionary<string, string>();
 }
Example #15
0
 public CollidableObject(double x, double y, Vector v)
 {
     position = new Vector(x, y);
     velocity = v;
     LastVelocity = new Vector(0,0);
     BoundingBox = new BoundingBox(position.X-width/2, position.Y+height/2, position.X+width/2, position.Y-height/2);
 }
Example #16
0
 public Block(TextureManager manager, string blockType)
 {
     _sprite.Texture = manager.Get(blockType);
     DropSpeed = 30.0;
     Direction = new Vector(0, -1, 0);
     KeepAlive = true;
     _moveDistance = _sprite.GetWidth();
 }
Example #17
0
 public VerticalMenu(double x, double y, Input.Input input, Color unfocused, Color focused)
 {
     this.input = input;
     position = new Vector(x, y, 0);
     Spacing = 50;
     SetUnfocusedColor(unfocused);
     SetFocusColor(focused);
 }
Example #18
0
 public Block(Texture texture)
 {
     _sprite.Texture = texture;
     DropSpeed = 30.0;
     Direction = new Vector(0, -1, 0);
     KeepAlive = true;
     _moveDistance = _sprite.GetWidth();
 }
Example #19
0
        public Bullet(Texture bulletTexture)
        {
            _sprite.Texture = bulletTexture;

            // Some default values
            Dead = false;
            Direction = new Vector(1, 0, 0);
            Speed = 512;// pixels per second
        }
Example #20
0
        public PhysicalObject(Game game, Vector position, Vector velocity, Dictionary<string, Sprite> sprites, string defaultSprite, IController controller, 
		                       WorldPhysics worldPhysics, ObjectPhysics objectPhysics, Dictionary<string, BoundingPolygon> boundingPolygons)
            : base(game, position, velocity, sprites, defaultSprite, controller, boundingPolygons)
        {
            this.worldPhysics = worldPhysics;
            this.objectPhysics = objectPhysics;
            this.friction = objectPhysics.Friction*worldPhysics.GroundFrictionFactor;
            inAirTimer.Start();
        }
Example #21
0
        //Just pass on the constructor stuff to base
        public Character(Game game, Vector position, Vector velocity, Dictionary<string, Sprite> sprites, string defaultSprite, IController controller, 
		                 WorldPhysics worldPhysics, ObjectPhysics objectPhysics, Dictionary<string, BoundingPolygon> boundingPolygons,
		                 double runSpeed, double maxSpeed)
            : base(game, position, velocity, sprites, defaultSprite, controller, worldPhysics, objectPhysics, boundingPolygons)
        {
            CurrentSprite.PlayAnimation("stand", true);
            MaxSpeed = maxSpeed;
            RunSpeed = runSpeed;
        }
Example #22
0
 public Block(Texture texture, Vector scaler, Vector position)
 {
     _sprite.Texture = texture;
     DropSpeed = 30.0;
     Direction = new Vector(0, -1, 0);
     KeepAlive = true;
     _sprite.SetScale(scaler.X, scaler.Y);
     _sprite.SetPosition(position.X, position.Y);
     _moveDistance = _sprite.GetWidth();
 }
Example #23
0
        public Missile(Texture missileTexture, Vector direction)
        {
            Sprite.Texture = missileTexture;

            // Some default values
            Dead = false;
            Direction = direction;
            Speed = 512; // pixels per second
            Damage = 10;
        }
Example #24
0
        public override void OnCollision(IEntity entity, Vector amount)
        {
            if (entity.GetType() == typeof (Eyeball))
            {
                return;
            }

            Dead = true;
            //Sprite.SetColor(new Color(0, 0, 1, 1));
        }
Example #25
0
 public Vector MeasureFont(string text, double maxWidth)
 {
     Vector dimensions = new Vector();
     foreach (char c in text)
     {
         CharacterData data = characterData[c];
         dimensions.X += data.XAdvance;
         dimensions.Y = Math.Max(dimensions.Y, data.Height + data.YOffset);
     }
     return dimensions;
 }
Example #26
0
        public Player(Game game, Vector position, Vector velocity, Dictionary<string, Sprite> sprites, string defaultSprite, IController controller, //GameObject attributes
		               WorldPhysics worldPhysics, ObjectPhysics objectPhysics, Dictionary<string, BoundingPolygon> boundingPolygons, //PhysicalObject attributes
		               double runSpeed, double maxSpeed, 	//Character attributes
		               PlayerState state)
            : base(game, position, velocity, sprites, defaultSprite, controller, worldPhysics, objectPhysics, boundingPolygons, runSpeed, maxSpeed)
        {
            oldFriction = objectPhysics.Friction;
            PlayerState = state;

            invincibleTimer.Start();
        }
Example #27
0
 /// <summary>
 ///     Constructs an enemy given the texture and AI (IEnemyBrain)
 /// </summary>
 /// <param name="texture">The texture for the enemy</param>
 /// <param name="enemyBrain">The AI for the enemy</param>
 /// <param name="position">The spawn point for this enemy</param>
 public EastWestSkeleton(Texture texture, IEnemyBrain enemyBrain, Vector position)
     : base(texture, enemyBrain, position)
 {
     Health = 15;
     Damage = 10;
     Sprite.SetScale(1.8, 1.8);
     Speed = 200;
     MaxTimeToShoot = 12;
     MinTimeToShoot = 1;
     RestartShootCountDown();
 }
Example #28
0
        public Mushroom(Game game, 
		                    Vector position, 
		                    Dictionary<string, Sprite> sprites, string defaultSprite, 
		                    WorldPhysics worldPhysics, ObjectPhysics objectPhysics, 
		                    Dictionary<string, BoundingPolygon> polygons, 
		                    ItemType itemType)
            : base(game, position, new Vector(0,0), sprites, defaultSprite, new DumbGroundAI(), worldPhysics, objectPhysics, polygons)
        {
            CurrentSprite.PlayAnimation(Sprite.DEFAULT_ANIMATION, true);
            MushroomType = itemType;
        }
Example #29
0
        private Vector CalculateCatmullRom(double t, Vector p1, Vector p2, Vector p3, Vector p4)
        {
            double t2 = t*t;
            double t3 = t2*t;

            double b1 = 0.5*(-t3 + 2*t2 - t);
            double b2 = 0.5*(3*t3 - 5*t2 + 2);
            double b3 = 0.5*(-3*t3 + 4*t2 + t);
            double b4 = 0.5*(t3 - t2);

            return (p1*b1 + p2*b2 + p3*b3 + p4*b4);
        }
        public void Move(Vector amount, bool _setPosition)
        {
            amount *= _speed;
            if (!_setPosition)
            {
                _sprite.SetPosition(_sprite.GetPosition() + amount);
            }

            else
            {
                _sprite.SetPosition(_sprite.GetPosition());
            }
        }