示例#1
0
        protected void FindTarget()
        {
            float smallestDistance = float.MaxValue;

            //This targeting scheme is just temporary, may need to send an update to the server for a set target event, similar to NPC logic
            foreach (var kvp in PotentialTargets)
            {
                if (kvp.Value == this)
                {
                    Console.WriteLine("Turret was targetting itself, removing from potential targets.");//Haven't been able to track down where a turret is added to its own list of potential targets
                    PotentialTargets.Remove(Id);
                    return;
                }

                if (kvp.Value.IsBodyValid && Vector2.Distance(kvp.Value.Position, Position) < smallestDistance && Vector2.Distance(kvp.Value.Position, Position) < turretRange)
                {
                    smallestDistance = Vector2.Distance(kvp.Value.Position, Position);
                    CurrentTarget    = kvp.Value;
                }
            }
        }
示例#2
0
        public override void Simulate(IGameTimeService gameTime)
        {
            if (!IsBodyValid)
            {
                return;
            }
            Weapon.Update(gameTime);
            DelayTime += gameTime.ElapsedMilliseconds; // Used for Loading.

            #region Logic


            if (logicState != LogicStates.Resting && (CurrentTarget == null || !CurrentTarget.IsBodyValid))
            {
                CurrentTarget = null;
                logicState    = LogicStates.SearchingForTarget;
            }
            switch (logicState)
            {
            case LogicStates.Resting:
                if (PotentialTargets.Count > 0)
                {
                    logicState = LogicStates.SearchingForTarget;
                }

                break;

            case LogicStates.SearchingForTarget:
                FindTarget();
                if (CurrentTarget != null)
                {
                    logicState = LogicStates.TurningTowardTarget;
                }
                break;

            case LogicStates.TurningTowardTarget:
                if (!CurrentTarget.IsBodyValid)
                {
                    PotentialTargets.Remove(CurrentTarget.Id);
                    CurrentTarget = null;
                    logicState    = LogicStates.Resting;
                    break;
                }

                if (gameTime.TotalMilliseconds - _lastTargetCheckTime > 200)    //Retargets closest item 5 times per second
                //Retargeting is expensive, if turrets lag this needs to be rethought
                {
                    _lastTargetCheckTime = (float)gameTime.TotalMilliseconds;
                    CurrentTarget        = null;
                    logicState           = LogicStates.Resting;
                    break;
                }


                if (Vector2.Distance(CurrentTarget.Position, Position) > turretRange)
                {
                    CurrentTarget = null;
                    logicState    = LogicStates.Resting;
                    break;
                }


                var result = AIHelper.TurnTowardPosition(Rotation, rotationSpeed, Position, CurrentTarget.Position, gameTime.ElapsedMilliseconds, AimTolerance);

                Rotation = result.Rotation;

                if (!result.Rotated)
                {
                    if (Weapon.CanFire())
                    {
                        Weapon.TimeOfWaitStart = gameTime.TotalMilliseconds;
                        Weapon.Fire_LocalOrigin(Rotation, 0, true);
                        Weapon.WaitingForFireResponse = true;
                    }
                }
                break;
            }

            #endregion

            OldTime += gameTime.ElapsedMilliseconds; // Updates Time since Last Action
        }
示例#3
0
        public override void Update(IGameTimeService gameTime)
        {
            if (IsLocalSim)
            {
                Simulate(gameTime);
                return;
            }



            if (IsDead)
            {
                return;
            }
            Weapon.Update(gameTime);
            DelayTime += gameTime.ElapsedMilliseconds; // Used for Loading.


            #region Logic

            //0 = No target(s) in range, laser at rest
            //1 = Target(s) on planet, find one
            //2 = Turn head to aim or Fire if within aim tolerance
            //3 = Fire

            switch (logicState)
            //Just FYI in case you're confused, a locally simulated turret doesn't run this code, it runs .Simulate() instead.
            {
            case LogicStates.Resting:
                if (PotentialTargets.Count > 0)
                {
                    logicState = LogicStates.SearchingForTarget;
                }

                break;

            case LogicStates.SearchingForTarget:
                FindTarget();
                if (CurrentTarget != null)
                {
                    logicState = LogicStates.TurningTowardTarget;
                }
                break;

            case LogicStates.TurningTowardTarget:
                if (CurrentTarget == null)
                {
                    logicState = LogicStates.Resting;
                    break;
                }
                else if (CurrentTarget != null && !CurrentTarget.IsBodyValid)
                {
                    PotentialTargets.Remove(CurrentTarget.Id);
                    CurrentTarget = null;
                    logicState    = LogicStates.Resting;
                    break;
                }
                else if (gameTime.TotalMilliseconds - _lastTargetCheckTime > 200)
                //Retargets closest item 5 times per second
                //Retargeting is expensive, if turrets lag this needs to be rethought
                {
                    _lastTargetCheckTime = (float)gameTime.TotalMilliseconds;
                    CurrentTarget        = null;
                    logicState           = LogicStates.Resting;
                    break;
                }
                else if (Vector2.Distance(CurrentTarget.Position, Position) > turretRange)
                {
                    CurrentTarget = null;
                    logicState    = LogicStates.Resting;
                    break;
                }


                Rotation = AIHelper.TurnTowardPosition(
                    Rotation, rotationSpeed,
                    Position, CurrentTarget.Position,
                    gameTime.ElapsedMilliseconds, AimTolerance
                    ).Rotation;



                break;
            }

            #endregion

            OldTime += gameTime.ElapsedMilliseconds; // Updates Time since Last Action
        }