c# - What is the simplest way to change the behavior of a method during runtime? -


I have a class named enemy , and I want many types of enemies, each enemy will have different movement behaviors, for example, can be directly transferred directly to the player, maintain a certain distance, etc. What would I want to know, what is different behavior, preferably in a class?

My current idea is to use a switch , and to include all the behaviors in a method

  public class enemy {full public} Health; Public damage; // etc etc go public zero (string type) {switch (type) {case "charge": chargeMove (); break; "Maintain" the case: keep uphold (); break; }} Private Zero Charges () {// Luggage} Maintaining Private Zero () {// Accessories} // All Behavior}   

What should I know, is it better to do this To put everything in a function, or to create a new class for each code that comes from Enemy ? I would like to keep it in a square, so that all enemies can be well stored in ilist without using any castings to use any particular task.

Any advice would be appreciated.

EDIT: Thanks for all the responses, I think I will use the proposed interface method by Alastair.

However, this may be better suited on this site, Consider using it.

A movement statement. E.g. Public Interface ImpexBeihara {Move zero (Enemy enemy); }

So you will have many different behaviors that describe different movements. for example.

  Public class chargingActivator: IMovementBehavior {...} Keeping public squares should be used: ImovementBehavior {...}   

then your enemy constructor may have Like Defniniton:

  Public enemies (IMVEIFTBehavior MovementBehavior) {...}   

This means that you can easily do different movement behaviors Can

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -