/// <summary> /// Create move behavior. /// </summary> /// <param name="type"> The type of move behavior.</param> /// <returns> A move behavior.</returns> public static IMoveBehavior CreateMoveBehavior(MoveBehaviorType type) { IMoveBehavior moveBehavior = null; // Depending on type, change behavior. switch (type) { case MoveBehaviorType.Fly: moveBehavior = new FlyBehavior(); break; case MoveBehaviorType.Pace: moveBehavior = new PaceBehavior(); break; case MoveBehaviorType.Swim: moveBehavior = new SwimBehavior(); break; case MoveBehaviorType.NoMove: moveBehavior = new NoMoveBehavior(); break; } return(moveBehavior); }
/// <summary> /// Creates the animal's move behavior. /// </summary> /// <param name="type">The type of animal.</param> /// <returns>Returns the intended movement behavior.</returns> public static IMoveBehavior CreateMoveBehavior(MoveBehaviorType type) { IMoveBehavior animalMove = null; switch (type) { case MoveBehaviorType.Fly: animalMove = new FlyBehavior(); break; case MoveBehaviorType.Pace: animalMove = new PaceBehavior(); break; case MoveBehaviorType.Swim: animalMove = new SwimBehavior(); break; case MoveBehaviorType.Climb: animalMove = new ClimbBehavior(); break; case MoveBehaviorType.Hover: animalMove = new HoverBehavior(); break; case MoveBehaviorType.NoMove: animalMove = new NoMoveBehavior(); break; } return(animalMove); }
/// <summary> /// Changes the behavior of an animal. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void changeBehaviorButton_Click(object sender, RoutedEventArgs e) { // Get the animal selected from the list box. Animal animal = (Animal)this.animalListBox.SelectedItem; // Gets the behavior selected from the combo box. MoveBehaviorType behavior = (MoveBehaviorType)this.changeBehaviorComboBox.SelectedItem; if (animal != null && behavior != null) { // Create an IMoveBehavior using the MoveBehaviorFactory. IMoveBehavior newBehavior = MoveBehaviorFactory.CreateMoveBehavior(behavior); // Set the aniaml's behavior to the new behavior. animal.MoveBehavior = newBehavior; } }
/// <summary> /// Changes the move behavior via the button click. /// </summary> /// <param name="sender">System data.</param> /// <param name="e">Associated event data.</param> private void changeMoveBehaviorButton_Click(object sender, RoutedEventArgs e) { try { // Get the selected animal in the list box. Animal animal = this.animalListBox.SelectedItem as Animal; MoveBehaviorType moveBehaviorType = (MoveBehaviorType)this.changeMoveBehaviorComboBox.SelectedItem; if (animal != null) { IMoveBehavior animalMove = MoveBehaviorFactory.CreateMoveBehavior(moveBehaviorType); animal.MoveBehavior = animalMove; } } catch (NullReferenceException) { MessageBox.Show("An animal and a behavior both need to be selected."); } }
public static IMoveBehavior CreateMoveBehavior(MoveBehaviorType type) { IMoveBehavior result = null; switch (type) { case MoveBehaviorType.Fly: result = new FlyBehavior(); break; case MoveBehaviorType.Pace: result = new PaceBehavior(); break; case MoveBehaviorType.Swim: result = new SwimBehavior(); break; case MoveBehaviorType.Climb: result = new ClimbBehavior(); break; case MoveBehaviorType.Hover: result = new HoverBehavior(); break; case MoveBehaviorType.Hop: result = new HopBehavior(); break; case MoveBehaviorType.NoMove: result = new NoMoveBehavior(); break; } return(result); }