Example #1
0
        public void Process(GameTime gameTime)
        {
            //Always updates the closest enemy, used for setting target
            closestObject = MathFunctions.ReturnClosestObject(ship, ship.SightRange,
                                                              Game.stateManager.shooterState.gameObjects, "enemy");

            if (target == null)
            {
                behaviour.SetTarget(closestObject);
            }

            else
            {
                targetYDistance = ship.BoundingY - (target.BoundingY + target.BoundingHeight);
                targetXDistance = ship.PositionX - (target.BoundingX + target.BoundingWidth);
                if (targetXDistance < 0)
                {
                    targetXDistance *= -1;
                }

                //Removes target when dead or too far from AI-controlled ship
                if (target.IsOutside || target.IsKilled || targetYDistance < -100 || targetXDistance > 400)
                {
                    if (Behaviour.IgnoreList.Contains(target))
                    {
                        Behaviour.GarbageIgnoreList.Add(target);
                        Behaviour.UpdateIgnoreList();
                    }

                    target = null;
                }
            }

            //Calls the behaviour and asks which action to take
            behaviour.Action();
        }
Example #2
0
        public void Avoid()
        {
            foreach (GameObjectVertical obj in Game.stateManager.shooterState.gameObjects)
            {
                if (obj.ObjectClass == "enemy" || obj.ObjectClass == "enemyBullet")
                {
                    if (CollisionDetection.IsPointInsideCircle(obj.Position, ship.Position, AvoidRadius))
                    {
                        AvoidList.Add(obj);
                    }
                }
            }

            if (AvoidList.Count > 0)
            {
                objToAvoid = MathFunctions.ReturnClosestObject(ship, AvoidRadius, AvoidList);
            }

            if (objToAvoid == target)
            {
                Behaviour.GarbageIgnoreList.Add(target);
                Behaviour.UpdateIgnoreList();
                target = null;
            }

            if (AvoidList.Count > 0 && objToAvoid != null)
            {
                //enemy is to the right on AI-ship's center
                if (objToAvoid.BoundingX + objToAvoid.BoundingWidth >= ship.PositionX &&
                    objToAvoid.BoundingX - 20 < ship.PositionX + ship.CenterPointX)
                {
                    if (ship.BoundingX > (Game1.ScreenSize.X - Game.stateManager.shooterState.CurrentLevel.LevelWidth) / 2 + 20)
                    {
                        ship.Move(new Vector2(-1, ship.DirectionY));
                    }
                }

                else if (objToAvoid.BoundingX < ship.PositionX &&
                         objToAvoid.BoundingX + objToAvoid.BoundingWidth + 20 > ship.BoundingX)
                {
                    if (ship.BoundingX + ship.BoundingWidth < Game.stateManager.shooterState.CurrentLevel.LevelWidth - 20)
                    {
                        ship.Move(new Vector2(1, ship.DirectionY));
                    }
                }

                else
                {
                    ship.Stop("x");
                }
            }

            for (int i = 0; i < AvoidList.Count; i++)
            {
                if (AvoidList[i].IsKilled || AvoidList[i].IsOutside ||
                    !CollisionDetection.IsPointInsideCircle(AvoidList[i].Position, ship.Position, AvoidRadius))
                {
                    GarbageAvoidList.Add(AvoidList[i]);

                    if (objToAvoid == AvoidList[i])
                    {
                        objToAvoid = null;
                    }
                }
            }

            foreach (GameObjectVertical obj in GarbageAvoidList)
            {
                AvoidList.Remove(obj);
            }

            GarbageAvoidList.Clear();
        }