Example #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public EnemyIdleState(Enemy enemy)
 {
     Enemy = enemy;
     Enemy.Range = 500;
     Enemy.Sprite.PlayAnimation("idle");
     Enemy.Velocity = new Vector2(0, 0);
 }
Example #2
0
        /// <summary>
        /// Constructors a new sprite.
        /// </summary>        
        public MagicItem(string filePath,string owner, string p_ItemType, int p_Duration, Enemy enemy)
        {
            Enemy = enemy;
            Owner = owner;
            sprite = new Sprite(GV.ContentManager, filePath);
            Sprite.Scale = 1.0f;
            if (Owner != "player")
            {
                if (p_ItemType == "arrow")
                {
                    this.state = new ArrowState(this);
                    Position = new Vector2(enemy.Position.X, enemy.Position.Y -9) ;
                    Direction = enemy.Direction;

                }
                else if (p_ItemType == "fireMage")
                {
                    //this.state = new fireMageState(this);
                }
            }
            else
            {
                this.state = new InAirState(this);
                Position = GV.Player.Position;

            }

            itemType = p_ItemType;
            Duration = p_Duration;
            active = true;
        }
Example #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public PatrolState(Enemy enemy)
 {
     Enemy = enemy;
     //Enemy.Range = 500;
     time = 0.0f;
     Enemy.Sprite.PlayAnimation("walking");
 }
Example #4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public PursueState(Enemy enemy)
 {
     Enemy = enemy;
     Enemy.Range = 50;
     Random random = new Random();
     randomNumber = random.Next(3, 10);
 }
Example #5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public FlyState(Enemy enemy)
 {
     Enemy = enemy;
     //Enemy.Range = 500;
     time = 0.0f;
     angle = 0;
     Enemy.Sprite.PlayAnimation("flying");
 }
Example #6
0
        public static void CreateMagicItem(string type, Enemy enemy)
        {
            switch (type)
            {
                //player magic spells
                case "fire":
                case "earth":
                case "water":
                case "wind":
                case "dark":
                case "light":
                    magicItemList.Add(new MagicItem("XML Documents/DanMagicAnimations", "player", type, 5, null));
                    if (type == "light")
                        lightCount++;
                    break;

                // enemy weapons
                case "arrow":
                    magicItemList.Add(new MagicItem("XML Documents/EnemyMagicAnimations", "enemy", type, 5, enemy));
                    break;

                // enemy weapons
                case "egg":
                    magicItemList.Add(new MagicItem("XML Documents/DanMagicAnimations", "enemy", type, 5, enemy));
                    lightCount++;
                    break;
                // enemy weapons
                case "fireRow":
                    MagicItem magicItem1 = (new MagicItem("XML Documents/EnemyMagicAnimations", "enemy", type, 5, enemy));
                    MagicItem magicItem2 = (new MagicItem("XML Documents/EnemyMagicAnimations", "enemy", type, 5, enemy));
                    MagicItem magicItem3 = (new MagicItem("XML Documents/EnemyMagicAnimations", "enemy", type, 5, enemy));
                    MagicItem magicItem4 = (new MagicItem("XML Documents/EnemyMagicAnimations", "enemy", type, 5, enemy));
                    MagicItem magicItem5 = (new MagicItem("XML Documents/EnemyMagicAnimations", "enemy", type, 5, enemy));
                    int direction = 1;
                    if (enemy.Direction == GV.LEFT)
                        direction = -1;

                    magicItem1.Position = new Vector2(enemy.Position.X + (direction * 20),enemy.Position.Y) ;
                    magicItem2.Position = new Vector2(enemy.Position.X + (direction * 60),enemy.Position.Y) ;
                    magicItem3.Position = new Vector2(enemy.Position.X + (direction * 100),enemy.Position.Y) ;
                    magicItem4.Position = new Vector2(enemy.Position.X + (direction * 140),enemy.Position.Y) ;
                    magicItem5.Position = new Vector2(enemy.Position.X + (direction * 180),enemy.Position.Y) ;

                    magicItemList.Add(magicItem1);
                    magicItemList.Add(magicItem2);
                    magicItemList.Add(magicItem3);
                    magicItemList.Add(magicItem4);
                    magicItemList.Add(magicItem5);
                    break;

                default:
                    Console.WriteLine("Invalid selection in Magic Item Manager");
                    break;
            }
        }
Example #7
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="player"></param>
        public AttackState(Enemy enemy)
        {
            Enemy = enemy;
            Enemy.Sprite.PlayAnimation("attack1");
            //CollisionManager.ResolveCollisions(Enemy);

            if (Enemy.IsHit)
            {
                Enemy.StateMachine.UpdateState("isHit");

            }
        }
Example #8
0
        public EnemyJumpingState(Enemy enemy)
        {
            Enemy = enemy;
            Enemy.Sprite.PlayAnimation("jumping");

            Enemy.JumpVelocity = new Vector2(0, 0);
            //Player.Velocity = new Vector2(Player.Velocity.X, -(Math.Abs(Player.AnalogState)) +JUMPVELOCITY);
            if (Enemy.Direction == GV.LEFT)
                Enemy.Velocity = new Vector2(-5, -20);
            else if (Enemy.Direction == GV.RIGHT)
                Enemy.Velocity = new Vector2(5, -20);

            Enemy.IsOnGround = false;
        }
Example #9
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="player"></param>
        public FlockPatrolState(Enemy enemy)
        {
            Enemy = enemy;
            //Enemy.Range = 500;
            time = 0.0f;
            angle = 0;
            turnaroundtime = 1.0f;

            if (Enemy.Direction == GV.LEFT)
            {
                Enemy.Velocity = new Vector2(-5, Enemy.Velocity.Y);
            }
            else
            {
                Enemy.Velocity = new Vector2(5, Enemy.Velocity.Y);
            }
        }
Example #10
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="player"></param>
        public PursueState(Enemy enemy)
        {
            Enemy = enemy;
            if(Enemy.GetType().ToString() == "KismetDataTypes.Goblin")
            {
                Enemy.Range = 50;
            }
            else if(Enemy.GetType().ToString() == "KismetDataTypes.FireMage")
            {
                 Enemy.Range = 200;
            }
            else if(Enemy.GetType().ToString() == "KismetDataTypes.DemonAracher")
            {
                     Enemy.Range = 500;
            }

            //Enemy.Range = 50;
            Random random = new Random();
            randomNumber = random.Next(6, 10);
        }
Example #11
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="player"></param>
        public KnockedDownState(Enemy enemy)
        {
            Enemy = enemy;
            Enemy.Sprite.PlayAnimation("knockedDown");

            if (Enemy.Direction == "left" && Enemy.BackStabber)
            {
                Enemy.Velocity = new Vector2(-2, -10);
                Enemy.ToggleDirections();
            }
            else if (Enemy.Direction == "right" && Enemy.BackStabber)
            {
                Enemy.Velocity = new Vector2(2, -10);
                Enemy.ToggleDirections();
            }
            else if (Enemy.Direction == "left" && Enemy.FaceOff)
            {
                Enemy.Velocity = new Vector2(2, -10);
            }
            else if (Enemy.Direction == "right" && Enemy.FaceOff)
            {
                Enemy.Velocity = new Vector2(-2, -10);
            }
        }
Example #12
0
        public static void CreateMagicItem(string type, Enemy enemy)
        {
            switch (type)
            {
                //player magic spells
                case "fire":
                case "earth":
                case "water":
                case "wind":
                case "dark":
                case "light":
                    magicItemList.Add(new MagicItem("XML Documents/DanMagicAnimations", "player", type, 5, null));
                    break;

                // enemy weapons
                case "arrow":
                    magicItemList.Add(new MagicItem("XML Documents/EnemyMagicAnimations", "enemy", type, 5, enemy));
                    break;

                default:
                    Console.WriteLine("Invalid selection");
                    break;
            }
        }
Example #13
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public LittleHitState(Enemy enemy)
 {
     Enemy = enemy;
     Enemy.Sprite.PlayAnimation("littleHit");
     Enemy.Velocity = new Vector2(0, 0);
 }
Example #14
0
        public static Vector2 ResolveCollisions(Enemy enemy, Vector2 nextPosition, Vector2 vel, List<MagicItem> magicItemList)
        {
            float xVel = vel.X, yVel = vel.Y;

            if (vel.Y >= 32)
                yVel = 31;
            if (Math.Abs(vel.X) >= 32)
            {
                if (vel.X < 0)
                    vel.X = -31;
                else
                    vel.X = 31;
            }

            // Get the player's bounding rectangle and find neighboring tiles.

            Rectangle nextBounds = enemy.getBounds(nextPosition);
            //Rectangle pBounds = GV.Player.Bounds;
            int leftTile = (int)Math.Floor((float)nextBounds.Left / Layer.TileWidth);
            int rightTile = (int)Math.Ceiling(((float)nextBounds.Right / Layer.TileWidth)) - 1;
            int topTile = (int)Math.Floor((float)nextBounds.Top / Layer.TileHeight);
            int bottomTile = (int)Math.Ceiling(((float)nextBounds.Bottom / Layer.TileHeight)) - 1;

            // Reset flag to search for ground collision.
            enemy.IsOnGround = false;
            enemy.WallDetected = false;
            bool BottomCollision = false;
            bool LeftLedgeDetected = false;
            bool RightLedgeDetected = false;
            bool LeftCollision = false;
            bool TopCollision = false;
            bool XCollision = false;
            Rectangle tileBounds;
            Vector2 depth;
            Vector2 depth1;

            //depth2 = new Vector2(0.0f, 0.0f);
            if (vel.Y < 0)
            {
                if (!enemy.IsOnGround)
                {
                    for (int x = leftTile; x <= rightTile; ++x)
                    {
                        int collision = GV.Level.GetCollision(x, topTile);
                        if (collision == Layer.Impassable)
                        {
                            TopCollision = true;
                        }

                    }
                }
            }
            // For each potentially colliding tile,
            for (int y = topTile; y < bottomTile; ++y)
            {

                int lCollision = GV.Level.GetCollision(leftTile, y);
                if (lCollision == Layer.Impassable)
                {
                    LeftCollision = true;
                    XCollision = true;
                }
                int rCollision = GV.Level.GetCollision(rightTile, y);

                if (rCollision == Layer.Impassable)
                {

                    XCollision = true;
                }
            }
            //handles collison in x axis
            if (XCollision)
            {
                if (LeftCollision)
                {
                    tileBounds = GV.Level.GetBounds(leftTile, bottomTile);
                    enemy.Direction = GV.RIGHT;
                }
                else
                {
                    tileBounds = GV.Level.GetBounds(rightTile, bottomTile);
                    enemy.Direction = GV.LEFT;
                }
                depth = RectangleExtensions.GetIntersectionDepth(nextBounds, tileBounds);

                xVel = vel.X + depth.X;
            }

            if (TopCollision)
            {
                tileBounds = GV.Level.GetBounds(leftTile, topTile);
                depth1 = RectangleExtensions.GetIntersectionDepth(nextBounds, tileBounds);
                float absDepthY = Math.Abs(depth1.Y);
                float absDepthX = Math.Abs(depth1.X);

                yVel = 0;// vel.Y + absDepthY;

                nextBounds = new Rectangle(nextBounds.X, nextBounds.Y + (int)depth1.Y, nextBounds.Width, nextBounds.Height);
                xVel = 0;
            }

            if (vel.Y > 0)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    int collision = GV.Level.GetCollision(x, bottomTile);
                    if (collision == Layer.Hole)
                    {
                        if (enemy.GetType().ToString() == "KismetDataTypes.Goblin" ||
                            enemy.GetType().ToString() == "KismetDataTypes.FireMage" ||
                            enemy.GetType().ToString() == "KismetDataTypes.DemonArcher")
                        { enemy.IsAlive = false; }
                    }
                    else if (collision != Layer.Passable)
                    {
                        BottomCollision = true;
                    }
                    else if (x == leftTile && collision == Layer.Passable)
                        LeftLedgeDetected = true;
                    else if (x == rightTile && collision == Layer.Passable)
                        RightLedgeDetected = true;

                }
            }
            if (BottomCollision)
            {
                tileBounds = GV.Level.GetBounds(leftTile, bottomTile);
                depth = RectangleExtensions.GetIntersectionDepth(nextBounds, tileBounds);
                float absDepthY = Math.Abs(depth.Y);
                if (enemy.PreviousBottom <= tileBounds.Top)
                    yVel = vel.Y + depth.Y;

                enemy.IsOnGround = true;

            }

            //GV.Player.IsHit = false;
            //enemy.IsHit = false;
            enemy.BackStabber = false;
            enemy.FaceOff = false;
            enemy.CollisionDetected = false;
            Rectangle enemySightBounds = enemy.SightRange;
            Rectangle playerBounds = GV.Player.Bounds;
            Rectangle enemyBounds = enemy.Bounds;
            Rectangle playerAttackBounds = GV.Player.AttackBounds;

            //Console.WriteLine("Enemy Health " + enemy.Health);
            Vector2 PlayerAttackDepth = RectangleExtensions.GetIntersectionDepth(enemyBounds, playerAttackBounds);
            if (PlayerAttackDepth != Vector2.Zero)
            {

                if (enemy.State.GetType().ToString() != "KismetDataTypes.KnockedDownState" && enemy.GetType().ToString() != "KismetDataTypes.Imp")
                {
                    if (GV.Player.State.GetType().ToString() == "KismetDataTypes.Attack1State" && GV.Player.Sprite.CurrentFrame == 7
                        || GV.Player.State.GetType().ToString() == "KismetDataTypes.JumpingAttackState" && GV.Player.Sprite.CurrentFrame == 2)
                    {

                        //Console.WriteLine("Enemy Health " + enemy.Health);
                        enemy.IsHit = true;
                        enemy.Damage = 10;

                        if (enemy.Direction == GV.Player.Direction)
                        {
                            enemy.BackStabber = true;
                        }
                        else
                        {
                            enemy.FaceOff = true;
                        }
                    }

                }

            }
            // check to see if there is a collision
            Vector2 sightDepth = RectangleExtensions.GetIntersectionDepth(playerBounds, enemySightBounds);
            if (sightDepth != Vector2.Zero)
            {
                //if (!enemy.IsHit && enemy.State.GetType().ToString() != "KismetDataTypes.KnockedDownState")
                enemy.CollisionDetected = true;
            }
            float radius = GV.Player.LightRadius;
            //Vector2 PlayerPosition = new Vector2(GV.Player.Position.X, GV.Player.Position.Y - ((float)GV.Player.Sprite.BoundingBox.Height));
            //Vector2 enemyPosition = new Vector2(enemy.Position.X, enemy.Position.Y - ((float)enemy.Sprite.BoundingBox.Top));
            Vector2 PlayerPosition = new Vector2(GV.Player.Position.X, GV.Player.Position.Y - (((float)GV.Player.Sprite.BoundingBox.Bottom - (float)GV.Player.Sprite.BoundingBox.Top)) / 2);
            Vector2 enemyPosition = new Vector2(enemy.Position.X, enemy.Position.Y - (((float)enemy.Sprite.BoundingBox.Bottom - (float)enemy.Sprite.BoundingBox.Top)) / 2);
            // check to see if there is a collision
            float diffx = enemyPosition.X - PlayerPosition.X;
            float diffy = enemyPosition.Y - PlayerPosition.Y;
            bool blocked = false;
            double diffRadius = Math.Sqrt(Math.Pow((double)diffx, 2) + Math.Pow((double)diffy, 2));

            if (diffRadius <= radius)
            {

                //calculate the starting and finishing x coordinates
                int start = (int)PlayerPosition.X, finish = (int)PlayerPosition.X;
                float slope = 0, b = 0;
                if (diffx > 0 && diffy >= 0)
                {
                    start = (int)PlayerPosition.X;
                    finish = (int)enemyPosition.X;
                }
                else if (diffx < 0 && diffy < 0)
                {
                    start = (int)PlayerPosition.X;
                    finish = (int)enemyPosition.X;
                }
                else if (diffx > 0 && diffy < 0)
                {
                    start = (int)PlayerPosition.X;
                    finish = (int)enemyPosition.X;
                }
                else if (diffx < 0 && diffy > 0)
                {
                    finish = (int)PlayerPosition.X;
                    start = (int)enemyPosition.X;
                }
                // calculate the slope
                if (diffy != 0.0f)
                {
                    slope = diffy / diffx;
                    b = PlayerPosition.Y - (slope * PlayerPosition.X);
                }

                for (int x = start; x <= finish; x++)
                {
                    float y = slope * (float)x + b;
                    int collision = GV.Level.GetCollision(x / Layer.TileWidth, (int)y / Layer.TileHeight);
                    if (collision == Layer.Impassable)
                    {
                        blocked = true;
                        break;
                    }
                }

                if (!blocked)
                {
                    if (diffx > 0 && enemy.Direction == GV.LEFT)
                    {
                        enemy.SightDetected = true;
                        LeftLedgeDetected = false;
                        RightLedgeDetected = false;
                        //enemy.JumpVelocity = new Vector2(-diffx, -diffy);
                    }
                    else if (diffx < 0 && enemy.Direction == GV.RIGHT)
                    {
                        enemy.SightDetected = true;
                        LeftLedgeDetected = false;
                        RightLedgeDetected = false;
                        //enemy.JumpVelocity = new Vector2(-diffx, -diffy);

                    }

                    if (diffy > 0 && GV.Player.IsOnGround)
                        enemy.JumpVelocity = new Vector2(-diffx, -diffy);

                }

            }

            if (LeftLedgeDetected && enemy.IsOnGround)
                enemy.Direction = GV.RIGHT;

            else if (RightLedgeDetected & enemy.IsOnGround)
                enemy.Direction = GV.LEFT;

            Vector2 boundDepth = RectangleExtensions.GetIntersectionDepth(playerBounds, enemyBounds);
            if (boundDepth != Vector2.Zero)
            {
                //if (enemy.IsHit && enemy.State.GetType().ToString() != "KismetDataTypes.KnockedDownState")
                //GV.Player.State = new HittingState(GV.Player);
                GV.Player.IsHit = true;
                GV.Player.Damage = 1;
            }

            if (magicItemList.Count != 0)
            {
                for (int i = 0; i < magicItemList.Count(); i++)
                {
                    MagicItem magicItem = magicItemList[i];
                    Vector2 ItemCollisionDepth = RectangleExtensions.GetIntersectionDepth(enemy.Bounds, magicItem.Bounds);
                    if (ItemCollisionDepth != Vector2.Zero && magicItem.Enemy == null && magicItem.State.GetType().ToString() == "KismetDataTypes.InUseState")
                    {
                        //Console.WriteLine("Enemy Health " + enemy.Health);

                        if (ItemCollisionDepth != Vector2.Zero && magicItem.ItemType == "earth" && magicItem.State.GetType().ToString() == "KismetDataTypes.InUseState" && vel.Y > 0)
                        {
                            float absDepthY = Math.Abs(ItemCollisionDepth.Y);
                            float absDepthX = Math.Abs(ItemCollisionDepth.X);
                            if (absDepthY > absDepthX)
                            {

                                if (enemy.Direction == GV.LEFT)
                                {
                                    xVel = 10;
                                    enemy.Direction = GV.RIGHT;

                                }
                                else
                                {
                                    xVel = -10;
                                    enemy.Direction = GV.LEFT;
                                }

                            }
                            //if (GV.Player.PreviousBottom <= magicItem.Bounds.Top)
                            else
                            {
                                xVel = 1;
                                yVel = -10;
                                //enemy.IsHit = true;
                            }

                            //enemy.Damage = 2;

                        }

                        if (ItemCollisionDepth != Vector2.Zero && magicItem.ItemType == "wind" && magicItem.State.GetType().ToString() == "KismetDataTypes.InUseState")
                        {

                            enemy.Position = new Vector2(magicItem.Position.X, magicItem.Bounds.Bottom);
                            enemy.State = new KnockedDownState(enemy);
                            enemy.ToggleDirections();
                            //enemy.Damage = 2;

                        }
                        else if (ItemCollisionDepth != Vector2.Zero && magicItem.ItemType != "light" && magicItem.ItemType != "earth")
                        {

                            enemy.Damage = 2;
                            enemy.State = new KnockedDownState(enemy);
                        }

                    }

                    if (magicItem.Enemy == null && magicItem.Owner == "player" && enemy.State.GetType().ToString() == "KismetDataTypes.PatrolState")
                    {
                        float xdistance = enemyPosition.X - magicItem.Position.X;
                        float ydistance = enemyPosition.Y - magicItem.Position.Y;

                        double c = Math.Sqrt(Math.Pow((double)xdistance, 2) + Math.Pow((double)ydistance, 2));

                        if (c <= magicItem.EffectRadius)
                        {

                            if (magicItem.ItemType == "light")
                            {
                                xVel /= 5.0f;
                                yVel /= 5.0f;
                            }

                            if (magicItem.ItemType == "dark")
                            {
                                enemy.State = new EnemyDyingState(enemy);

                            }

                        }

                    }

                }
            }

            return new Vector2(xVel, yVel);
        }
Example #15
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public TeleportState(Enemy enemy)
 {
     Enemy = enemy;
     Enemy.Sprite.PlayAnimation("teleport1");
     //CollisionManager.ResolveCollisions(Enemy);
 }
Example #16
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            level01 = Content.Load<Level>("Levels/Level_01");
            level01.Initialise(Content);
            player = new Player(this.Content, "XML Documents/DanAnimations", level01);
            goblin = new Enemy(this.Content, "XML Documents/GoblinAnimations", level01);
            goblin1 = new Enemy(this.Content, "XML Documents/GoblinAnimations", level01);
            goblin2 = new Enemy(this.Content, "XML Documents/GoblinAnimations", level01);
            player.Position = new Vector2(0.0f, 420.0f);

            goblin.Position = new Vector2(500.0f, 420.0f);
            goblin1.Position = new Vector2(1000.0f, 420.0f);
            goblin2.Position = new Vector2(200.0f, 420.0f);
            // TODO: use this.Content to load your game content here
        }
Example #17
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public EnemyDyingState(Enemy enemy)
 {
     Enemy = enemy;
     //Enemy.Sprite.PlayAnimation("knockedDown");
     //Enemy.Velocity /= 5.0f;
 }
Example #18
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player"></param>
 public FlockAttackState(Enemy enemy)
 {
     Enemy = enemy;
     time = 0.0f;
     returnPosition = Enemy.Position;
 }
Example #19
0
        public static Vector2 ResolveCollisions(Enemy enemy, Vector2 nextPosition, Vector2 vel, List<MagicItem> magicItemList)
        {
            float xVel = vel.X, yVel = vel.Y;

            if (vel.Y >= 32)
                yVel = 31;
            if (Math.Abs(vel.X) > 32)
            {
                if (vel.X < 0)
                    vel.X = -31;
                else
                    vel.X = 31;
            }

            // Get the player's bounding rectangle and find neighboring tiles.

            Rectangle nextBounds = enemy.getBounds(nextPosition);
            //Rectangle pBounds = GV.Player.Bounds;
            int leftTile = (int)Math.Floor((float)nextBounds.Left / Layer.TileWidth);
            int rightTile = (int)Math.Ceiling(((float)nextBounds.Right / Layer.TileWidth)) - 1;
            int topTile = (int)Math.Floor((float)nextBounds.Top / Layer.TileHeight);
            int bottomTile = (int)Math.Ceiling(((float)nextBounds.Bottom / Layer.TileHeight)) - 1;

            // Reset flag to search for ground collision.
            enemy.IsOnGround = false;
            enemy.WallDetected = false;
            bool BottomCollision = false;
            bool LeftLedgeDetected = false;
            bool RightLedgeDetected = false;
            bool LeftCollision = false;
            bool XCollision = false;
            Rectangle tileBounds;
            Vector2 depth;

            // For each potentially colliding tile,
            for (int y = topTile; y < bottomTile; ++y)
            {

                int lCollision = GV.Level.GetCollision(leftTile, y);
                if (lCollision == Layer.Impassable)
                {
                    LeftCollision = true;
                    XCollision = true;
                }
                int rCollision = GV.Level.GetCollision(rightTile, y);

                if (rCollision == Layer.Impassable)
                {

                    XCollision = true;
                }
            }
            //handles collison in x axis
            if (XCollision)
            {
                if (LeftCollision)
                {
                    tileBounds = GV.Level.GetBounds(leftTile, bottomTile);
                    enemy.Direction = GV.RIGHT;
                }
                else
                {
                    tileBounds = GV.Level.GetBounds(rightTile, bottomTile);
                    enemy.Direction = GV.LEFT;
                }
                depth = RectangleExtensions.GetIntersectionDepth(nextBounds, tileBounds);

                xVel = vel.X + depth.X;
            }

            if (vel.Y > 0)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    int collision = GV.Level.GetCollision(x, bottomTile);
                    if (collision != Layer.Passable)
                    {
                        BottomCollision = true;
                    }
                    else if (x == leftTile && collision == Layer.Passable)
                        LeftLedgeDetected = true;
                    else if (x == rightTile && collision == Layer.Passable)
                        RightLedgeDetected = true;

                }
            }
            if (BottomCollision)
            {
                tileBounds = GV.Level.GetBounds(leftTile, bottomTile);
                depth = RectangleExtensions.GetIntersectionDepth(nextBounds, tileBounds);
                float absDepthY = Math.Abs(depth.Y);
                if (enemy.PreviousBottom <= tileBounds.Top)
                    yVel = vel.Y + depth.Y;
                enemy.IsOnGround = true;

            }

            if (LeftLedgeDetected && enemy.IsOnGround)
                enemy.Direction = GV.RIGHT;
            else if (RightLedgeDetected & enemy.IsOnGround)
                enemy.Direction = GV.LEFT;

            //GV.Player.IsHit = false;
            //enemy.IsHit = false;
            enemy.BackStabber = false;
            enemy.FaceOff = false;
            enemy.CollisionDetected = false;
            Rectangle enemySightBounds = enemy.SightRange;
            Rectangle playerBounds = GV.Player.Bounds;
            Rectangle enemyBounds = enemy.Bounds;
            Rectangle playerAttackBounds = GV.Player.AttackBounds;

            //Console.WriteLine("Enemy Health " + enemy.Health);
            Vector2 PlayerAttackDepth = RectangleExtensions.GetIntersectionDepth(enemyBounds, playerAttackBounds);
            if (PlayerAttackDepth != Vector2.Zero)
            {

                if (enemy.State.GetType().ToString() != "KismetDataTypes.KnockedDownState")
                {
                    if (GV.Player.State.GetType().ToString() == "KismetDataTypes.Attack1State" && GV.Player.Sprite.CurrentFrame == 7)
                    {

                        Console.WriteLine("Enemy Health " + enemy.Health);
                        enemy.IsHit = true;
                        //enemy.Damage = 10;

                        if (enemy.Direction == GV.Player.Direction)
                        {
                            enemy.BackStabber = true;
                        }
                        else
                        {
                            enemy.FaceOff = true;
                        }
                    }

                }

            }
            // check to see if there is a collision
            Vector2 sightDepth = RectangleExtensions.GetIntersectionDepth(playerBounds, enemySightBounds);
            if (sightDepth != Vector2.Zero)
            {
                //if (!enemy.IsHit && enemy.State.GetType().ToString() != "KismetDataTypes.KnockedDownState")
                    enemy.CollisionDetected = true;
            }
            float radius = GV.Player.LightRadius;
            Vector2 PlayerPosition = new Vector2(GV.Player.Position.X, GV.Player.Position.Y - (((float)GV.Player.Sprite.BoundingBox.Bottom - (float)GV.Player.Sprite.BoundingBox.Top)) / 2);
            Vector2 enemyPosition = new Vector2(enemy.Position.X, enemy.Position.Y - (((float)enemy.Sprite.BoundingBox.Bottom - (float)enemy.Sprite.BoundingBox.Top)) / 2);
            // check to see if there is a collision
            float diffx = PlayerPosition.X - enemyPosition.X;
            float diffy = enemyPosition.Y - PlayerPosition.Y;

            double diffRadius = Math.Sqrt(Math.Pow((double)diffx,2) + Math.Pow((double)diffy,2));

            if (diffRadius <= radius)
            {
                if (diffx < 0)
                    enemy.Direction = GV.LEFT;
                else
                    enemy.Direction = GV.RIGHT;

                enemy.SightDetected = true;

                enemy.JumpVelocity = new Vector2(diffx, -diffy);
            }

            Vector2 boundDepth = RectangleExtensions.GetIntersectionDepth(playerBounds, enemyBounds);
            if (boundDepth != Vector2.Zero)
            {
                //if (enemy.IsHit && enemy.State.GetType().ToString() != "KismetDataTypes.KnockedDownState")
                GV.Player.State = new HittingState(GV.Player);
            }

            if (magicItemList.Count != 0)
            {
                for (int i = 0; i < magicItemList.Count(); i++)
                {
                    MagicItem magicItem = magicItemList[i];
                    Vector2 ItemCollisionDepth = RectangleExtensions.GetIntersectionDepth(enemy.Bounds, magicItem.Bounds);
                    if (ItemCollisionDepth != Vector2.Zero && magicItem.Enemy == null && magicItem.State.GetType().ToString() == "KismetDataTypes.InUseState")
                    {
                        Console.WriteLine("Enemy Health " + enemy.Health);

                        if (ItemCollisionDepth != Vector2.Zero && magicItem.ItemType == "earth" && magicItem.State.GetType().ToString() == "KismetDataTypes.InUseState" && vel.Y > 0)
                        {
                            float absDepthY = Math.Abs(ItemCollisionDepth.Y);
                            float absDepthX = Math.Abs(ItemCollisionDepth.X);
                            if (absDepthY > absDepthX)
                            {

                                if (enemy.Direction == GV.LEFT)
                                {
                                    xVel = 10;
                                    enemy.Direction = GV.RIGHT;

                                }
                                else
                                {
                                    xVel = -10;
                                    enemy.Direction = GV.LEFT;
                                }

                            }
                            //if (GV.Player.PreviousBottom <= magicItem.Bounds.Top)
                            else
                            {
                                xVel =1;
                                yVel = -10;
                                enemy.IsHit = true;
                            }

                            //enemy.Damage = 50;

                        }

                        if (ItemCollisionDepth != Vector2.Zero && magicItem.ItemType == "wind" && magicItem.State.GetType().ToString() == "KismetDataTypes.InUseState")
                        {

                            enemy.Position = new Vector2(magicItem.Position.X,magicItem.Bounds.Bottom);
                            enemy.State = new KnockedDownState(enemy);
                            enemy.ToggleDirections();

                        }
                    }

                }
            }

            return new Vector2(xVel, yVel);
        }
Example #20
0
 /// <summary>
 ///Constructor 
 /// </summary>
 /// <param name="p_Object"></param>
 public StateMachine(Enemy p_Enemy, EnemyState enemyState)
 {
     enemy = p_Enemy;
     enemy.State = enemyState;
 }
Example #21
0
        /// <summary>
        /// Constructors a new sprite.
        /// </summary>        
        public MagicItem(string filePath,string owner, string p_ItemType, int p_Duration, Enemy enemy)
        {
            Enemy = enemy;
            Owner = owner;
            sprite = new Sprite(GV.ContentManager, filePath);
            Sprite.Scale = 1.0f;
            if (Owner != "player")
            {
                if (p_ItemType == "arrow")
                {
                    this.state = new ArrowState(this);
                    Position = new Vector2(enemy.Position.X, enemy.Position.Y -9) ;
                    Velocity = new Vector2((GV.Player.Position.X - enemy.Position.X) / 20, (GV.Player.Position.Y - enemy.Position.Y)/20);
                    Direction = enemy.Direction;
                    Sprite.Rotation = 0.0f;
                    itemType = p_ItemType;

                }
                else if (p_ItemType == "fireRow")
                {
                        this.state = new FireState(this);
                        //Position = new Vector2(enemy.Position.X, enemy.Position.Y);
                        //Direction = enemy.Direction;
                        light = new LightSource((int)Position.X, (int)Position.Y, (int)Position.X, (int)Position.Y, 200, 2);
                        itemType = p_ItemType;
                }
                else if (p_ItemType == "egg")
                {
                        Position = Enemy.Position;
                        this.state = new EggState(this);

                        light = new LightSource((int)Position.X, (int)Position.Y, (int)Position.X, (int)Position.Y, 200, 2);

                        itemType = "light";

                    //Position = new Vector2(enemy.Position.X, enemy.Position.Y);
                    //Direction = enemy.Direction;
                }
            }
            else
            {
                this.state = new InAirState(this);
                Position = GV.Player.Position;

               if (p_ItemType == "light")
               {
                    light = new LightSource((int)Position.X, (int)Position.Y, (int)Position.X, (int)Position.Y, 200, 2);
                    EffectRadius = light.Radius;
                }
                else if (p_ItemType == "dark")
                {
                    EffectRadius = 100.0f;
                }
                else
                    EffectRadius = 0.0f;

                //this.state = new InAirState(this);
                //Position = GV.Player.Position;
                itemType = p_ItemType;
            }

            Duration = p_Duration;
            active = true;
        }