Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlxRandom: Fixed getObject(), removed colorExt() #1158

Merged
merged 8 commits into from
Jun 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
* exposed currentSeed as an external representation of internalSeed
* removed intRanged() and floatRanged(), int() and float() now provide optional ranges
* removed weightedGetObject(), getObject() now has an optional weights parameter
* removed colorExt(), try using FlxColor to get finer control over randomly-generated colors
* updated random number generation equation to avoid inconsistent results across platforms; may break recordings made in 3.x!
* FlxArrayUtil: removed randomness-related functions, please use FlxRandom instead
* FlxText:
Expand Down
46 changes: 14 additions & 32 deletions flixel/math/FlxRandom.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ class FlxRandom
*/
public static var globalSeed(default, set):Int = 1;

/**
* Current seed used to generate new random numbers. You can retrieve this value if,
* for example, you want to store the seed that was used to randomly generate a level.
*/
public static var currentSeed(get, set):Int;

/**
* Function to easily set the global seed to a new random number.
* Used primarily by FlxG whenever the game is reset.
Expand All @@ -33,9 +27,15 @@ class FlxRandom
*/
public static inline function resetGlobalSeed():Int
{
return globalSeed = Std.int(Math.random() * MODULUS);
return globalSeed = rangeBound(Std.int(Math.random() * FlxMath.MAX_VALUE_INT));
}

/**
* Current seed used to generate new random numbers. You can retrieve this value if,
* for example, you want to store the seed that was used to randomly generate a level.
*/
public static var currentSeed(get, set):Int;

/**
* Returns a pseudorandom integer between Min and Max, inclusive.
* Will not return a number in the Excludes array, if provided.
Expand Down Expand Up @@ -206,7 +206,7 @@ class FlxRandom
* @return A pseudorandomly chosen object from Objects.
*/
@:generic
public static function getObject<T>(Objects:Array<T>, ?WeightsArray:Array<Float>, StartIndex:Int = 0, EndIndex:Int = 0):T
public static function getObject<T>(Objects:Array<T>, ?WeightsArray:Array<Float>, StartIndex:Int = 0, ?EndIndex:Null<Int>):T
{
var selected:Null<T> = null;

Expand All @@ -217,18 +217,23 @@ class FlxRandom
WeightsArray = [for (i in 0...Objects.length) 1];
}

if (EndIndex == null)
{
EndIndex = Objects.length - 1;
}

StartIndex = Std.int(FlxMath.bound(StartIndex, 0, Objects.length - 1));
EndIndex = Std.int(FlxMath.bound(EndIndex, 0, Objects.length - 1));

// Swap values if reversed

if (EndIndex < StartIndex)
{
StartIndex = StartIndex + EndIndex;
EndIndex = StartIndex - EndIndex;
StartIndex = StartIndex - EndIndex;
}


if (EndIndex > WeightsArray.length - 1)
{
EndIndex = WeightsArray.length - 1;
Expand Down Expand Up @@ -291,29 +296,6 @@ class FlxRandom
return FlxColor.fromRGB(red, green, blue, Alpha);
}

/**
* Much like color(), but with finer control over the output color.
*
* @param RedMinimum The minimum amount of red in the output color, from 0 to 255.
* @param RedMaximum The maximum amount of red in the output color, from 0 to 255.
* @param GreedMinimum The minimum amount of green in the output color, from 0 to 255.
* @param GreenMaximum The maximum amount of green in the output color, from 0 to 255.
* @param BlueMinimum The minimum amount of blue in the output color, from 0 to 255.
* @param BlueMaximum The maximum amount of blue in the output color, from 0 to 255.
* @param AlphaMinimum The minimum alpha value for the output color, from 0 (fully transparent) to 255 (fully opaque).
* @param AlphaMaximum The maximum alpha value for the output color, from 0 (fully transparent) to 255 (fully opaque).
* @return A pseudorandomly generated color within the ranges specified.
*/
public static function colorExt(RedMinimum:Int = 0, RedMaximum:Int = 255, GreenMinimum:Int = 0, GreenMaximum:Int = 255, BlueMinimum:Int = 0, BlueMaximum:Int = 255, AlphaMinimum:Int = 255, AlphaMaximum:Int = 255):FlxColor
{
var red = int(Std.int(FlxMath.bound(RedMinimum, 0, 255)), Std.int(FlxMath.bound(RedMaximum, 0, 255)));
var green = int(Std.int(FlxMath.bound(GreenMinimum, 0, 255)), Std.int(FlxMath.bound(GreenMaximum, 0, 255)));
var blue = int(Std.int(FlxMath.bound(BlueMinimum, 0, 255)), Std.int(FlxMath.bound(BlueMaximum, 0, 255)));
var alpha = int(Std.int(FlxMath.bound(AlphaMinimum, 0, 255)), Std.int(FlxMath.bound(AlphaMaximum, 0, 255)));

return FlxColor.fromRGB(red, green, blue, alpha);
}

/**
* The actual internal seed. Stored as a Float value to prevent inaccuracies due to
* integer overflow in the generate() equation.
Expand Down