コード例 #1
0
ファイル: Alien.cs プロジェクト: timgarwood/invaders
        /// <summary>
        /// GameObject update method
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            // track the player, just for now.
            var toTarget     = Player.GetWorldPosition() - RigidBody.GetPosition();
            var distToTarget = Vec2.Distance(Player.GetWorldPosition(), RigidBody.GetPosition());

            if (distToTarget > 10 && !Active)
            {
                return;
            }

            if (!Active)
            {
                ActiveEffect.Play();
            }

            Active = true;

            var rot = GameUtils.Vec2ToRotation(toTarget);

            RigidBody.SetXForm(RigidBody.GetPosition(), (float)(rot * System.Math.PI / 180.0f));
            Rotation = (float)(rot * System.Math.PI / 180.0f);

            toTarget.Normalize();

            _lastDecision = DateTime.UtcNow;

            var targetVelocity = Player.RigidBody.GetLinearVelocity();
            var myVelocity     = RigidBody.GetLinearVelocity();

            //velocityAngle will tell us if the alien and player are moving in different directions
            //180 degrees means they are moving opposite
            //0 means they are moving in the same direction
            //90, etc..

            //if(velocityAngle > 0)
            //{
            //}

            //getting further away?
            if (_lastDistanceToTarget < distToTarget)
            {
                if (myVelocity.Length() > 1)
                {
                    var velocityAngle = System.Math.Abs(System.Math.Acos(Vec2.Dot(targetVelocity, myVelocity) / (targetVelocity.Length() * myVelocity.Length())));
                    //if we're not going in the same direction, start slowing down
                    if (velocityAngle >= System.Math.PI / (2 * 60))
                    {
                        //start slowing down so we can pursue the player
                        GoToStopping();
                    }
                    else
                    {
                        GoToMoving(distToTarget, toTarget);
                    }
                }
                else
                {
                    GoToMoving(distToTarget, toTarget);
                }
            }
            else
            {
                //distance hasn't changed
                if (distToTarget > 1)
                {
                    GoToMoving(distToTarget, toTarget);
                }
                else
                {
                    GoToStopping();
                }
            }

            if (distToTarget <= 5)
            {
                var attackDiff = DateTime.UtcNow - LastAttackTime;
                if (attackDiff.TotalMilliseconds >= 500)
                {
                    LastAttackTime = DateTime.UtcNow;
                    ShootingEffect.Play();
                    SpawnProjectile("GreenLaser-small", ProjectileSource.Alien);
                }
            }

            /*if (distToTarget > 1 && distToTarget <= 3)
             * {
             *  // if the target is stopped
             *  if (Player.RigidBody.GetLinearVelocity().Length() < 0.05)
             *  {
             *      if (_moveState == MoveState.Stopping)
             *      {
             *          GoToStopping();
             *      }
             *      else
             *      {
             *          GoToMoving(toTarget);
             *      }
             *  }
             *  else if (Player.Instance.RigidBody.GetLinearVelocity().Length() > RigidBody.GetLinearVelocity().Length())
             *  {
             *      GoToMoving(toTarget);
             *  }
             * }
             * else if (distToTarget > 3 && targetVelocity <= 0.05)
             * {
             *  if (myVelocity >= 0.05)
             *  {
             *      GoToStopping();
             *  }
             *  else if(_moveState == MoveState.Stopped)
             *  {
             *      RigidBody.SetLinearVelocity(Vec2.Zero);
             *      GoToMoving(toTarget);
             *  }
             * }
             */



            //if we're getting close enough to the player, decrease our impulse
            //if (_lastDistanceToPlayer.Length() > distToPlayer && distToPlayer)
            //{
            //}

            //if (Vec2.Distance(Player.Instance.GetWorldPosition(), RigidBody.GetPosition()) > 1 &&
            //    Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < _definition.MaxSpeed)
            //{
            //    RigidBody.ApplyImpulse(toPlayer * _definition.MoveImpulse, RigidBody.GetPosition());
            // }

            /*else if (GameUtils.DistanceFrom(Player.Instance.GetWorldPosition(), RigidBody.GetPosition()) > 1)
             * {
             *  RigidBody.ApplyImpulse(toPlayer * _definition.MoveImpulse, RigidBody.GetPosition());
             * }
             */
            //else
            //{
            //    DecreaseLinearVelocity(_definition.AlienTurnVelocityDecrement, 0);
            //    RigidBody.SetAngularVelocity(0);
            //}

            _lastDistanceToTarget = distToTarget;
            base.Update(gameTime);
        }