Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Polymorphism question for my custom class

$
0
0
Hi all,
I have used polymorphism in the past, but not with anything too complicated. This time, I have a class which stores a bunch of info about the object I pass into it, and I wrote a very similar class for a different type of object in another script, which seems like a waste of time and typing since I could probably make a master class, and use copies of it instead of writing it over and over again, but I'm not 100% it's a good idea for this specific class. Here is the first class I wrote:
public class HUDElement
	{
		// Store object information for the passed GameObject
		private HUDSettings.HUDType _hudType;
		private HUDSettings.NGUIType _nguiType;
		private GameObject _hudObject;
		private GUITexture _gTexture;
		private bool _hudState;
		private bool _isControl;
		public HUDElement( HUDSettings.HUDType type )
		{
			this._hudType = type;
		}
		public void Init( GameObject go, bool state, bool isControl, HUDSettings.NGUIType nguiType )
		{
			_hudObject		= go;
			_gTexture		= go.GetComponent();
			_hudState		= state;
			_nguiType		= nguiType;
			_isControl		= isControl;
			// Toggle HUD Element to its default starting state,
			// this prevents HUD elements that are turned off by default from being activated on Initialization
			Toggle( state, true );
		}
		public void Toggle( bool isActive, bool isInit = false )
		{
			if( _hudType == HUDSettings.HUDType.BOOST && isInit == false )
			{
				if( _hudObject ) _hudObject.SetActiveRecursively( isActive );
			}
			else
			{
				if( _nguiType != HUDSettings.NGUIType.NONE && _hudObject )
				{
					switch( _nguiType )
					{	
						case HUDSettings.NGUIType.UISlicedSprite:
							var uiSliceSprites = _hudObject.GetComponentsInChildren();
							foreach( UISlicedSprite s in uiSliceSprites ) s.enabled = isActive;
							break;
						case HUDSettings.NGUIType.UISprite:
							var uiSprites = _hudObject.GetComponentsInChildren();
							foreach( UISprite s in uiSprites ) s.enabled = isActive;
							break;
					}
					// No need to toggle the gameObject itself nor check for a GUITexture, so let's stop here.
					return;
				}
				if( _gTexture != null ) _gTexture.enabled = isActive;
				else if( _hudObject ) _hudObject.active = isActive;
			}
		}
		public HUDSettings.HUDType HudType{ get{ return _hudType; }}
		public HUDSettings.NGUIType NGuiType{ get{ return _nguiType; }}
		public GameObject HudObject{ get{ return _hudObject; }}
		public GUITexture GTexture{ get{ return _gTexture; }}
		public bool HudState{ get{ return _hudState; } set{ _hudState = value; Toggle( _hudState ); }}
		public bool IsControl{ get{ return _isControl; } set{ _isControl = value; Toggle( _isControl ); }}
	}
	private HUDElement[] HUDElements = new HUDElement[(int)HUDSettings.NumOfHUDTypes];


	private HUDElement _mainHUD = null;
	public HUDElement MainHUD
	{
		get
		{
			if( _mainHUD == null ) _mainHUD = HUDElements[(int)HUDSettings.HUDType.MAIN];
			return _mainHUD;
		}
	}
	
	private HUDElement _menuHUD = null;
	public HUDElement MenuHUD
	{
		get
		{
			if( _menuHUD == null ) _menuHUD = HUDElements[(int)HUDSettings.HUDType.MENU];
			return _menuHUD;
		}
	}
	
	// more properties.......
}
Next, here's the second - similar - class I wrote for another type of object:
public class ControlElement
	{
		private ControlSettings.ControlType _controlType;
		private GameObject _controlObject;
		private bool _controlState;
		public ControlElement( ControlSettings.ControlType type )
		{
			_controlType = type;
		}
		public void Init( GameObject go, bool state )
		{
			_controlObject	= go;
			_controlState	= state;
			// Toggle this Control Element to its default starting state,
			// this prevents Control elements that are turned off by default from being activated on Initialization
			Toggle( state );
		}
		public void Toggle( bool isActive )
		{
			_controlObject.active = isActive;
		}
		public ControlSettings.ControlType ControlType{ get{ return _controlType; }}
		public GameObject ControlObject{ get{ return _controlObject; }}
		public bool ControlState{ get{ return _controlState; } set{ _controlState = value; }}
	}
	private ControlElement[] ControlElements = new ControlElement[(int)ControlSettings.NumOfControlTypes];


        private ControlElement _accelControl = null;
	public ControlElement AccelControl
	{
		get
		{
			if( _accelControl == null ) _accelControl = ControlElements[(int)ControlSettings.ControlType.ACCEL_BTN];
			return _accelControl;
		}
	}

	private ControlElement _brakeControl = null;
	public ControlElement BrakeControl
	{
		get
		{
			if( _brakeControl == null ) _brakeControl = ControlElements[(int)ControlSettings.ControlType.BRAKE_BTN];
			return _brakeControl;
		}
	}
	
	// more properties...
}
So my question is, given the differences between those 2 classes, is it still a good idea to use polymorphism with copies of 1 master class?
Thanks for your time guys!
Stephane

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>