/// <summary> /// Update the Attack item /// </summary> /// <param name="dt"> A delta time since the last update was called</param> /// <param name="input">A GenericInput to process.</param> public override void Update(TimeSpan dt, GenericInput input) { //on keyboard input determine the Hero's direction if (input is GenericKeyboardInput) { GenericKeyboardInput gki = (GenericKeyboardInput)input; DirectionIn = new Vector2(DirectionX, DirectionY); DetermineDirection(gki); DirectionOut = new Vector2(DirectionX, DirectionY); AnimateHero(); } //on mouse click launch an attack if (input is MouseGenericInput) { MouseGenericInput mgi = (MouseGenericInput)input; if (mgi.MouseInputType == MouseGenericInput.MouseGenericInputType.MouseClick && (DirectionX * DirectionX + DirectionY * DirectionY) != 0) { // create the scene switch message to switch the current scene to the top score scene Message_Attack heroAttack = new Message_Attack("Arrow", this.DirectionX, this.DirectionY, Location, Message_Attack.AttackType.Hero_Arrow, ArrowRange, ArrowDamage); MessageManager.AddMessageItem(heroAttack); } } //update hero location SetLocation(dt); HealthBar = new Windows.Foundation.Rect(Location.X - 10, Location.Y - 10, 50, 5); }
/// <summary> /// Simple method to determine the direction of the hero based on the keyboard inputs /// </summary> /// <param name="gki"></param> public void DetermineDirection(GenericKeyboardInput gki) { DirectionX = 0; DirectionY = 0; if (gki.IsWKeyPress) { this.DirectionY -= 1; } if (gki.IsSKeyPress) { this.DirectionY += 1; } if (gki.IsAKeyPress) { this.DirectionX -= 1; } if (gki.IsDKeyPress) { this.DirectionX += 1; } //normally id take sqrt(x^2/x^2+y^2) but knowing that x is 1 or -1 and y is -1 or 1 we can cheat :) //this way the comp does less math, and we dont need to extract the negative to get the direction.. woohoo if (DirectionX != 0 && DirectionY != 0) { DirectionX = DirectionX * 0.707107f; DirectionY = DirectionY * 0.707107f; } }
// private static object message_queue_lock = new object(); /// <summary> /// Returns the current GenericInput for processing. /// </summary> /// <returns>A GenericInput that needs to be process, null if the InputQueque is empty.</returns> public static GenericInput Update() { lock (input_queue_lock) { if (InputQueue.Count == 0) { return(null); } else { GenericInput gi = InputQueue.Dequeue(); if (gi is MouseGenericInput) { MouseGenericInput mgi = (MouseGenericInput)gi; if (MouseGenericInputType.MousePressed == mgi.MouseInputType) { IsMouseDown = true; } else if (MouseGenericInputType.MouseReleased == mgi.MouseInputType) { if (IsMouseDown) { MouseGenericInput mouse_click_event = new MouseGenericInput(mgi.X, mgi.Y); mouse_click_event.MouseInputType = MouseGenericInputType.MouseClick; InputManager.AddInputItem(mouse_click_event); } IsMouseDown = false; } } if (gi is GenericKeyboardInput) { GenericKeyboardInput ki = (GenericKeyboardInput)gi; IsWKeyPress = ki.IsWKeyPress; IsAKeyPress = ki.IsAKeyPress; IsSKeyPress = ki.IsSKeyPress; IsDKeyPress = ki.IsDKeyPress; } return(gi); } } }