Match 2 differents object with the same instance name

I would like to know if an object can be combined with an instance name.

I got:

class AnimatedEntity : DrawableEntity { Animation BL { get; set; } Animation BR { get; set; } Animation TL { get; set; } Animation TR { get; set; } Animation T { get; set; } Animation R { get; set; } Animation L { get; set; } Animation B { get; set; } Orientation orientation ; public virtual int Draw(SpriteBatch spriteBatch, GameTime gameTime) { //draw depends on orientation } } 

and

 enum Orientation { SE, SO, NE, NO, N , E, O, S, BL, BR, TL, TR, T, R, L, B } 

If Orientation is an Enum and Animation class.

Is it possible to call the correct animation from Orientation with the same name?

+4
source share
2 answers

Instead of storing Animations properties in properties, how about using a dictionary?

 Dictionary<Orientation, Animation> anim = new Dictionary<Orientation, Animation> { { Orientation.BL, blAnimation }, { Orientation.BR, brAnimation }, { Orientation.TL, tlAnimation }, { Orientation.TR, trAnimation }, { Orientation.T, tAnimation }, { Orientation.R, rAnimation }, { Orientation.L, lAnimation }, { Orientation.B, bAnimation } }; 

Then you can use anim[orientation] to access the corresponding animation.

+3
source

Indeed, Dictionary would be a good option. It may even have an Animation index if the animation is installed outside:

 class AnimatedEntity : DrawableEntity { Dictionary<Orientation, Animation> Animations { get; set; } public AnimatedEntity() { Animations = new Dictionary<Orientation, Animation>(); } public Animation this[Orientation orientation] { get{ return Animations[orientation]; } set{ Animations[orientation] = value;} } Orientation Orientation { get; set; } public void Draw(SpriteBatch spriteBatch, GameTime gameTime) { Animation anim = Animations[Orientation]; } } 

It will be used as:

 AnimatedEntity entity = new AnimatedEntity(); entity[Orientation.B] = bAnimation; entity[Orientation.E] = eAnimation; entity[Orientation.SE] = seAnimation; 
+1
source

All Articles