Esempio n. 1
0
        /// <summary>
        /// update override
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (Active)
            {
                var rotationDegrees = RigidBody.GetAngle() * 180 / System.Math.PI;

                if (Keyboard.GetState().IsKeyDown(Keys.OemOpenBrackets))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        //apply impulse to push the player to left
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees - 90);
                        RigidBody.ApplyImpulse(impulseVec * GameData.PlayerLateralImpulse
                                               , RigidBody.GetPosition());
                    }
                }

                if (Keyboard.GetState().IsKeyDown(Keys.OemCloseBrackets))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        //apply impulse to push the player to right
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees + 90);

                        RigidBody.ApplyImpulse(impulseVec * GameData.PlayerLateralImpulse
                                               , RigidBody.GetPosition());
                    }
                }

                if (FilteredInputListener.WasKeyPressed(Keys.Left))
                {
                    WeaponInventory.SelectPreviousWeapon();
                    FilteredInputListener.ResetKey(Keys.Left);
                }

                if (FilteredInputListener.WasKeyPressed(Keys.Right))
                {
                    WeaponInventory.SelectNextWeapon();
                    FilteredInputListener.ResetKey(Keys.Right);
                }

                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees);
                        if (Vec2.Dot(impulseVec, RigidBody.GetLinearVelocity()) == 0)
                        {
                            RigidBody.SetLinearVelocity(Vec2.Zero);
                        }
                        RigidBody.ApplyImpulse(impulseVec * GameData.PlayerImpulse
                                               , RigidBody.GetPosition());
                    }
                }
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    if (Vec2.Distance(Vec2.Zero, RigidBody.GetLinearVelocity()) < GameData.PlayerMaxSpeed)
                    {
                        var impulseVec = GameUtils.RotationToVec2((float)rotationDegrees);
                        RigidBody.ApplyImpulse(impulseVec * -GameData.PlayerImpulse
                                               , RigidBody.GetPosition());
                    }
                }
                if (Keyboard.GetState().IsKeyDown(Keys.D))
                {
                    //DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 1);
                    RigidBody.ApplyTorque(GameData.PlayerTurnTorque);
                }
                if (Keyboard.GetState().IsKeyDown(Keys.A))
                {
                    //DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 1);
                    RigidBody.ApplyTorque(-GameData.PlayerTurnTorque);
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    var weapon = WeaponInventory.GetSelectedWeapon();
                    if (weapon != null && weapon.RemainingAmmo > 0)
                    {
                        if (DateTime.Now - _lastProjectileTime > TimeSpan.FromMilliseconds(100))
                        {
                            ShootingEffect.Play();

                            _lastProjectileTime = DateTime.Now;
                            WeaponInventory.DecreaseAmmo(1);
                            SpawnProjectile(weapon.ProjectileName, ProjectileSource.Player);
                        }
                    }
                }

                if (Keyboard.GetState().IsKeyUp(Keys.W) &&
                    Keyboard.GetState().IsKeyUp(Keys.A) &&
                    Keyboard.GetState().IsKeyUp(Keys.S) &&
                    Keyboard.GetState().IsKeyUp(Keys.D))
                {
                    DecreaseLinearVelocity(GameData.PlayerTurnVelocityDecrement, 0);
                }

                if (Keyboard.GetState().IsKeyUp(Keys.A) && Keyboard.GetState().IsKeyUp(Keys.D))
                {
                    RigidBody.SetAngularVelocity(0);
                }
            }
        }
Esempio n. 2
0
        /// <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);
        }