private AiIdentifiableObjectInfo FindClosestSpacecraft(List <AiIdentifiableObjectInfo> list, Spacecraft me)
        {
            AiIdentifiableObjectInfo closestObject = null;
            float minDistance = float.MaxValue;

            foreach (var info in list)
            {
                if (info.GameObjectId == me.GameObjectId)
                {
                    continue;
                }
                if (info.GameObjectType != GameObjectType.Spacecraft)
                {
                    continue;
                }

                var distanceFromMe = MyMath.Distance(info.Position, me.GamePosition);
                if (distanceFromMe < minDistance)
                {
                    minDistance   = distanceFromMe;
                    closestObject = info;
                }
            }
            return(closestObject);
        }
Пример #2
0
        private void CheckDeath()
        {
            if (energy > 0)
            {
                return;
            }

            dead = true;
            for (int i = 0; i < 10; i++)
            {
                gameWorld.Add(new BulletExplosion(gameWorld,
                                                  new Vector2(gamePosition.X + MyMath.Random.Next() % 20 - 10,
                                                              gamePosition.Y + MyMath.Random.Next() % 20 - 10),
                                                  100 + MyMath.Random.Next() % 100));
                MyContentManager.Sound_Explosion.Play(Math.Max(0, 0.2f - MyMath.Distance(gameWorld.CameraPosition, gamePosition) / 4000.0f / 10));
            }
        }
        public void Control(GameWorld gameWorld, Spacecraft me)
        {
            Infinity.debugString += "  " + targetId;
            var aiIdentifiableObjectInfos = gameWorld.GetIdentifiableObjects();

            var target = FindTarget(aiIdentifiableObjectInfos, me);

            if (target == null)
            {
                return;
            }
            targetId       = target.GameObjectId;
            facingOpponent = facingOpponent = MyMath.MovingTowards(target.Position, me.GamePosition, oldVelocity, 0.6f);;

            //if (me.Energy < 20) plan = 2;
            turnVelocityLimit = 10f;
            moveVelocityLimit = 60f;
            if (plan == 1)
            {
                MoveTowardsAndAttack(me, target, aiIdentifiableObjectInfos);
                if (facingOpponent &&
                    MyMath.Distance(target.Position, me.GamePosition) < 600 &&
                    MyMath.Random.NextDouble() < .4f)
                {
                    me.FireBullet();
                }
                if (MyMath.Distance(me.GamePosition, target.Position) < 50 + MyMath.Random.Next(50))
                {
                    plan = 2;
                }
            }
            else
            {
                MoveAwayFrom(me, target);
                if (MyMath.Distance(me.GamePosition, target.Position) > 200 &&
                    MyMath.Random.NextDouble() < .1f)
                {
                    plan = 1;
                }
            }

            //if (MyMath.Random.NextDouble() < .01f) plan = plan%2 +1;
        }
        private AiIdentifiableObjectInfo AnotherTargetAttackingMe(List <AiIdentifiableObjectInfo> list, AiIdentifiableObjectInfo identityInfo, Spacecraft spacecraft)
        {
            AiIdentifiableObjectInfo target = null;

            if (identityInfo.GameObjectId != spacecraft.GameObjectId &&
                identityInfo.GameObjectType == GameObjectType.Bullet &&
                MyMath.Distance(identityInfo.Position, spacecraft.GamePosition) < 100 &&
                MyMath.MovingTowards(spacecraft.GamePosition, identityInfo.Position, identityInfo.Velocity, 0.7f))
            {
                if (!enimity.ContainsKey(identityInfo.GameObjectId))
                {
                    enimity[identityInfo.GameObjectId] = 0;
                }
                enimity[identityInfo.GameObjectId] += 10;
                if (enimity[identityInfo.GameObjectId] > targetEnimity)
                {
                    AiIdentifiableObjectInfo identityInfo1 = identityInfo;
                    target        = list.Find(info => info.GameObjectId == identityInfo1.GameObjectId);
                    targetEnimity = enimity[identityInfo.GameObjectId];
                }
            }
            return(target);
        }
        private void MoveTowardsAndAttack(Spacecraft me, AiIdentifiableObjectInfo target, List <AiIdentifiableObjectInfo> list)
        {
            var adjustedTargetVelocity = target.Position - me.GamePosition;

            foreach (var info in list)
            {
                var diffDistanceFromMe = MyMath.Distance(me.GamePosition, info.Position) -
                                         MyMath.Distance(me.GamePosition, target.Position);
                if (info.GameObjectId == me.GameObjectId && info.GameObjectType == GameObjectType.Bullet &&
                    (diffDistanceFromMe) < 3 * MyMath.Distance(me.GamePosition, target.Position)
                    )
                {
                    var vector2 = (MyMath.ReflectAcrossLine(info.Position - me.GamePosition, target.Position - me.GamePosition));
                    adjustedTargetVelocity += vector2;
                    adjustedTargetVelocity += target.Position - me.GamePosition;
                }
            }
            adjustedTargetVelocity.Normalize();
            adjustedTargetVelocity = MyMath.Distance(me.GamePosition, target.Position) * adjustedTargetVelocity;

            me.TargetReticle = adjustedTargetVelocity;
            RotateAndMoveWithVelocitySpeedAndTurnLimits(me, adjustedTargetVelocity);
        }
Пример #6
0
 public void CollidedWith(IColloidableObject colloidableObject)
 {
     MyContentManager.Sound_Explosion.Play(Math.Max(0, 0.1f - MyMath.Distance(gameWorld.CameraPosition, gamePosition) / 4000.0f / 10));
     gameWorld.Add(new BulletExplosion(gameWorld, gamePosition, Energy));
     dead = true;
 }
Пример #7
0
 public bool Intersects(CollisionSphere sphere)
 {
     return(MyMath.Distance(this.Center, sphere.Center) < this.Radius + sphere.Radius);
 }