I have implemented a form of Steering behavior for a game, and per use of an editor, a designer should be able to choose for (currently) seven parameters whether or not they should be 0, random or a set amount, and in case of the latter two, in which range random should fall or which set amount the variable should get. The settings will be saved in a file, and have to be able to be loaded in at any time.
What I ended up with is a painful to look at constructor and a whole, whole lot of fields and properties.
I am looking for a more intelligent approach to solve this problem, especially when adding more parameters later.
public enum SteerType
{
None,
Random,
Variable
}
public IdleSteering(SteerType rotationOnIntervalType, int rotationOnInterval,
SteerType rotationOnCollisionType, int rotationOnCollision,
SteerType breakIntervalType, int breakInterval,
SteerType breakTimeType, int breakTime,
SteerType rotationOnBreakType, int rotationOnBreak,
SteerType rangeFromSpawnType, int rangeFromSpawn,
SteerType speedType, int speed)
{
_rotationOnIntervalType = rotationOnIntervalType;
_rotationOnInterval = rotationOnInterval;
_rotationOnCollisionType = rotationOnCollisionType;
_rotationOnCollision = rotationOnCollision;
<...> //And this five more times
}
And the following chunk for every single parameter, so seven times as well.
private SteerType _rotationOnIntervalType;
private float _randomRotationOnInterval;
private float _rotationOnInterval;
public float RotationOnInterval
{
get
{
switch (_rotationOnIntervalType)
{
case SteerType.None:
return 0;
case SteerType.Random:
if (_randomRotationOnInterval.Equals(0))
_randomRotationOnInterval =
MathHelper.ToRadians((float)(-Static.Random.NextDouble() + Static.Random.NextDouble()) * _rotationOnInterval);
return _randomRotationOnInterval;
case SteerType.Variable:
return _rotationOnInterval;
}
return 0;
}
}
Suggest creating a class to hold the 2/paired values, and then group into a collection:
Over in your IdleSteering class:
Pass and use that collection between all your methods.