SurvivorNinja.Behaviors.PlayerBehavior.Update C# (CSharp) Method

Update() protected method

Allows this instance to execute custom logic during its Update.
This method will not be executed if the T:WaveEngine.Framework.Component, or the T:WaveEngine.Framework.Entity owning it are not Active.
protected Update ( System.TimeSpan gameTime ) : void
gameTime System.TimeSpan The game time.
return void
        protected override void Update(TimeSpan gameTime)
        {
            if (this.leftJoystick == null || this.rightJoystick == null)
            {
                return;
            }

            Input input = WaveServices.Input;

            // Player Move
            Vector2 moveDirection = this.leftJoystick.Direction;

            #region Keyboard move
            if (input.KeyboardState.IsConnected)
            {
                if (input.KeyboardState.IsKeyPressed(Keys.Up))
                    moveDirection += -Vector2.UnitY;
                if (input.KeyboardState.IsKeyPressed(Keys.Down))
                    moveDirection += Vector2.UnitY;
                if (input.KeyboardState.IsKeyPressed(Keys.Left))
                    moveDirection += -Vector2.UnitX;
                if (input.KeyboardState.IsKeyPressed(Keys.Right))
                    moveDirection += Vector2.UnitX;
            }
            #endregion

            if (!float.IsNaN(moveDirection.X) && !float.IsNaN(moveDirection.Y))
            {
                float x = this.transform.X + (moveDirection.X * velocity * 60 * (float)gameTime.TotalSeconds);
                this.transform.X = MathHelper.Clamp(x, this.virtualScreenManager.LeftEdge + this.borderMargin, this.virtualScreenManager.RightEdge - borderMargin);

                float y = this.transform.Y + (moveDirection.Y * velocity * 60 * (float)gameTime.TotalSeconds);
                this.transform.Y = MathHelper.Clamp(y, this.virtualScreenManager.TopEdge + this.borderMargin, this.virtualScreenManager.BottomEdge - borderMargin);

                float rotation = Vector2.Angle(moveDirection, this.textureDirection);
                if (float.IsNaN(rotation))
                {
                    this.transform.Rotation = 0;
                }
                else
                {
                    this.transform.Rotation = rotation;
                }
            }

            // Player shoot
            this.time -= gameTime;
            if (this.time <= TimeSpan.Zero)
            {
                Vector2 rightJoyDirection = this.rightJoystick.Direction;

                #region Keyboard move
                if (input.KeyboardState.IsConnected)
                {
                    if (input.KeyboardState.IsKeyPressed(Keys.W))
                        rightJoyDirection += -Vector2.UnitY;
                    if (input.KeyboardState.IsKeyPressed(Keys.S))
                        rightJoyDirection += Vector2.UnitY;
                    if (input.KeyboardState.IsKeyPressed(Keys.A))
                        rightJoyDirection += -Vector2.UnitX;
                    if (input.KeyboardState.IsKeyPressed(Keys.D))
                        rightJoyDirection += Vector2.UnitX;
                }
                #endregion

                Vector2 shootDirection = new Vector2((int)rightJoyDirection.X, (int)rightJoyDirection.Y);

                // Create bullet
                if (shootDirection != Vector2.Zero)
                {
                    Vector2 ninjaPosition = new Vector2(this.transform.X, this.transform.Y);
                    this.bulletEmitter.Shoot(ninjaPosition, rightJoyDirection);
                    SoundsManager.Instance.PlaySound(SoundsManager.SOUNDS.Shoot);
                }

                this.time = this.shootCadence;
            }
        }