Added RandomFloat Universal field type. To use it, change Filed type to "Decimal (random)" and enter a value as 'min max'.

This commit is contained in:
MaxED 2013-07-10 13:40:18 +00:00
parent 354be81d27
commit 8de9ad6b24
4 changed files with 108 additions and 1 deletions

View file

@ -788,6 +788,7 @@
<Compile Include="Rendering\SurfaceEntryCollection.cs" />
<Compile Include="Rendering\SurfaceManager.cs" />
<Compile Include="Rendering\SurfaceUpdate.cs" />
<Compile Include="Types\RandomFloatHandler.cs" />
<Compile Include="Types\RandomIntegerHandler.cs" />
<Compile Include="Types\ThingClassHandler.cs" />
<Compile Include="Types\ThingTypeHandler.cs" />

View file

@ -1573,6 +1573,11 @@ namespace CodeImp.DoomBuilder
return random.Next(min, max);
}
//mxd
public static float Random(float min, float max) {
return (float)Math.Round(min + (max - min) * random.NextDouble(), 2);
}
// This returns an element from a collection by index
public static T GetByIndex<T>(ICollection<T> collection, int index)
{

View file

@ -0,0 +1,100 @@
#region ================== Namespaces
using System;
using System.Globalization;
using CodeImp.DoomBuilder.Config;
#endregion
namespace CodeImp.DoomBuilder.Types
{
[TypeHandler(UniversalType.RandomFloat, "Decimal (Random)", true)]
internal class RandomFloatHandler : TypeHandler
{
#region ================== Constants
#endregion
#region ================== Variables
private float value;
private bool randomValue;
private float min;
private float max;
#endregion
#region ================== Properties
#endregion
#region ================== Methods
public override void SetupArgument(TypeHandlerAttribute attr, ArgumentInfo arginfo) {
base.SetupArgument(attr, arginfo);
//mxd. We don't want to store this type
index = (int)UniversalType.Float;
}
public override void SetupField(TypeHandlerAttribute attr, UniversalFieldInfo fieldinfo) {
base.SetupField(attr, fieldinfo);
//mxd. We don't want to store this type
index = (int)UniversalType.Float;
}
public override void SetValue(object value) {
float result;
// Null?
if(value == null) {
this.value = 0.0f;
}
// Compatible type?
else if((value is int) || (value is float) || (value is bool)) {
// Set directly
this.value = Convert.ToSingle(value);
} else {
// Try parsing as string
if(float.TryParse(value.ToString(), NumberStyles.Float, CultureInfo.CurrentCulture, out result)) {
this.value = result;
} else {
//mxd. Try to parse value as random range
string[] parts = value.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if(parts.Length == 2) {
if(float.TryParse(parts[0], NumberStyles.Float, CultureInfo.CurrentCulture, out min) &&
float.TryParse(parts[1], NumberStyles.Float, CultureInfo.CurrentCulture, out max)) {
randomValue = (min != max);
if(min == max)
this.value = min;
else if(min > max)
General.Swap(ref min, ref max);
}
}
this.value = 0.0f;
}
}
}
public override object GetValue() {
if(randomValue) return General.Random(min, max); //mxd
return this.value;
}
public override int GetIntValue() {
if(randomValue) return (int)General.Random(min, max); //mxd
return (int)this.value;
}
public override string GetStringValue() {
if(randomValue) return General.Random(min, max).ToString(); //mxd
return this.value.ToString();
}
#endregion
}
}

View file

@ -42,6 +42,7 @@ namespace CodeImp.DoomBuilder.Types
AngleDegreesFloat = 17,
ThingType = 18,
ThingClass = 19,
RandomInteger = 20 //mxd
RandomInteger = 20, //mxd
RandomFloat = 21 //mxd
}
}