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

remove implicit int casts to/from directions #3308

Open
wants to merge 9 commits into
base: release6
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1247,14 +1247,14 @@ class FlxObject extends FlxBasic
endDrawDebug(camera);
}

function drawDebugBoundingBox(gfx:Graphics, rect:FlxRect, allowCollisions:Int, partial:Bool)
function drawDebugBoundingBox(gfx:Graphics, rect:FlxRect, allowCollisions:FlxDirectionFlags, partial:Bool)
{
// Find the color to use
final color = getDebugBoundingBoxColor(allowCollisions);
drawDebugBoundingBoxColor(gfx, rect, color);
}

function getDebugBoundingBoxColor(allowCollisions:Int)
function getDebugBoundingBoxColor(allowCollisions:FlxDirectionFlags)
{
if (debugBoundingBoxColor != null)
return debugBoundingBoxColor;
Expand Down
5 changes: 3 additions & 2 deletions flixel/group/FlxSpriteGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import flixel.math.FlxRect;
import flixel.system.FlxAssets;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
import flixel.util.FlxDirectionFlags;
import flixel.util.FlxSort;

/**
Expand Down Expand Up @@ -753,7 +754,7 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
return alpha = Value;
}

override function set_facing(Value:Int):Int
override function set_facing(Value:FlxDirectionFlags):FlxDirectionFlags
{
if (exists && facing != Value)
transformChildren(facingTransform, Value);
Expand Down Expand Up @@ -1023,7 +1024,7 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
inline function directAlphaTransform(Sprite:FlxSprite, Alpha:Float)
Sprite.alpha = Alpha; // direct set

inline function facingTransform(Sprite:FlxSprite, Facing:Int)
inline function facingTransform(Sprite:FlxSprite, Facing:FlxDirectionFlags)
Sprite.facing = Facing;

inline function flipXTransform(Sprite:FlxSprite, FlipX:Bool)
Expand Down
4 changes: 2 additions & 2 deletions flixel/system/FlxQuadTree.hx
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ class FlxQuadTree extends FlxRect
{
_iterator = iterator.next;
}
if (_object != null && _object.exists && _object.allowCollisions > 0 && _iterator != null && _iterator.object != null && overlapNode())
if (_object != null && _object.exists && _object.allowCollisions != NONE && _iterator != null && _iterator.object != null && overlapNode())
{
overlapProcessed = true;
}
Expand Down Expand Up @@ -666,7 +666,7 @@ class FlxQuadTree extends FlxRect
while (_iterator != null)
{
checkObject = _iterator.object;
if (_object == checkObject || !checkObject.exists || checkObject.allowCollisions <= 0)
if (_object == checkObject || !checkObject.exists || checkObject.allowCollisions == NONE)
{
_iterator = _iterator.next;
continue;
Expand Down
8 changes: 4 additions & 4 deletions flixel/system/macros/FlxDefines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ class FlxDefines
checkOpenFLVersions();
#end

#if (flixel_addons < version("3.0.2"))
abortVersion("Flixel Addons", "3.0.2 or newer", "flixel-addons", (macro null).pos);
#if (flixel_addons < version("3.3.0"))
abortVersion("Flixel Addons", "3.3.0 or newer", "flixel-addons", (macro null).pos);
#end
#if (flixel_ui < version("2.4.0"))
abortVersion("Flixel UI", "2.4.0 or newer", "flixel-addons", (macro null).pos);
#if (flixel_ui < version("2.6.2"))
abortVersion("Flixel UI", "2.6.2 or newer", "flixel_ui", (macro null).pos);
#end
}

Expand Down
2 changes: 1 addition & 1 deletion flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
return tileSprite;
}

override function set_allowCollisions(value:Int):Int
override function set_allowCollisions(value:FlxDirectionFlags):FlxDirectionFlags
{
for (tile in _tileObjects)
if (tile.index >= _collideIndex)
Expand Down
46 changes: 43 additions & 3 deletions flixel/util/FlxDirection.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,61 @@ package flixel.util;
* Simple enum for orthogonal directions. Can be combined into `FlxDirectionFlags`.
* @since 4.10.0
*/
enum abstract FlxDirection(Int) to Int
enum abstract FlxDirection(Int)
{
var LEFT = 0x0001;
var RIGHT = 0x0010;
var UP = 0x0100;
var DOWN = 0x1000;


var self(get, never):FlxDirection;
inline function get_self():FlxDirection
{
#if (haxe >= version("4.3.0"))
return abstract;
#else
return cast this;
#end
}

inline function new(value:Int)
{
this = value;
}

public function toString()
{
return switch (cast this : FlxDirection)
return switch self
{
case LEFT: "L";
case RIGHT: "R";
case UP: "U";
case DOWN: "D";
}
}

@:deprecated("implicit cast from FlxDirection to Int is deprecated, use an explicit cast")
@:to
inline function toIntImplicit()
{
return toInt();
}


inline public function toInt()
{
return this;
}

@:deprecated("implicit cast from Int to FlxDirectionFlags is deprecated, use an explicit cast")
@:from
inline static function fromIntImplicit(value:Int):FlxDirection
{
return fromInt(value);
}

public inline static function fromInt(value:Int):FlxDirection
{
return new FlxDirection(value);
}
}
80 changes: 65 additions & 15 deletions flixel/util/FlxDirectionFlags.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import flixel.math.FlxAngle;
* many `FlxObject` features like `allowCollisions` and `touching`.
* @since 4.10.0
*/
enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
enum abstract FlxDirectionFlags(Int)
{
var LEFT = 0x0001; // FlxDirection.LEFT;
var RIGHT = 0x0010; // FlxDirection.RIGHT;
Expand All @@ -32,7 +32,18 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int

/** Special-case constant meaning any, or all directions. */
var ANY = 0x1111; // LEFT | RIGHT | UP | DOWN;


var self(get, never):FlxDirectionFlags;

inline function get_self():FlxDirectionFlags
{
#if (haxe >= version("4.3.0"))
return abstract;
#else
return cast this;
#end
}

/**
* Calculates the angle (in degrees) of the facing flags.
* Returns 0 if two opposing flags are true.
Expand All @@ -41,7 +52,7 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
public var degrees(get, never):Float;
function get_degrees():Float
{
return switch (this)
return switch self
{
case RIGHT: 0;
case DOWN: 90;
Expand Down Expand Up @@ -82,42 +93,63 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
public var right(get, never):Bool;
inline function get_right() return has(RIGHT);


inline function new(value:Int)
{
this = value;
}

/**
* Returns true if this contains **all** of the supplied flags.
*/
public inline function has(dir:FlxDirectionFlags):Bool
{
return this & dir == dir;
return this & dir.toInt() == dir.toInt();
}

/**
* Returns true if this contains **any** of the supplied flags.
*/
public inline function hasAny(dir:FlxDirectionFlags):Bool
{
return this & dir > 0;
return this & dir.toInt() > 0;
}

/**
* Creates a new `FlxDirections` that includes the supplied directions.
*/
public inline function with(dir:FlxDirectionFlags):FlxDirectionFlags
{
return this | dir;
return fromInt(this | dir.toInt());
}

/**
* Creates a new `FlxDirections` that excludes the supplied directions.
*/
public inline function without(dir:FlxDirectionFlags):FlxDirectionFlags
{
return this & ~dir;
return fromInt(this & ~dir.toInt());
}

public inline function not():FlxDirectionFlags
{
return fromInt(~this);
}

@:deprecated("implicit cast from FlxDirectionFlags to Int is deprecated, use an explicit cast")
@:to
inline function toIntImplicit()
{
return toInt();
}

public inline function toInt():Int
{
return this;
}

public function toString()
{
if (this == NONE)
if (self == NONE)
return "NONE";

var str = "";
Expand Down Expand Up @@ -146,16 +178,34 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
| (down ? DOWN : NONE);
}

// Expose int operators
@:op(A & B) static function and(a:FlxDirectionFlags, b:FlxDirectionFlags):FlxDirectionFlags;
@:deprecated("implicit cast from Int to FlxDirectionFlags is deprecated, use an explicit cast")
@:from
public inline static function fromIntImplicit(value:Int):FlxDirectionFlags
{
return fromInt(value);
}

public inline static function fromInt(value:Int):FlxDirectionFlags
{
return new FlxDirectionFlags(value);
}

@:from
inline static function fromDir(dir:FlxDirection):FlxDirectionFlags
{
return fromInt(dir.toInt());
}

@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")// Expose int operators
@:op(A & B) static function and(a:FlxDirectionFlags, b:FlxDirectionFlags):FlxDirectionFlags;
@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
@:op(A | B) static function or(a:FlxDirectionFlags, b:FlxDirectionFlags):FlxDirectionFlags;

@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
@:op(A > B) static function gt(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;

@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
@:op(A < B) static function lt(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;

@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
@:op(A >= B) static function gte(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;

@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
@:op(A <= B) static function lte(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;
}
44 changes: 1 addition & 43 deletions tests/unit/src/flixel/util/FlxDirectionFlagsTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FlxDirectionFlagsTest extends FlxTest
}

Assert.isTrue(f(null));
Assert.isFalse(f(0));
Assert.isFalse(f(cast 0));
Assert.isFalse(f(LEFT));
}

Expand Down Expand Up @@ -153,48 +153,6 @@ class FlxDirectionFlagsTest extends FlxTest
Assert.areEqual(ANY, dirs.with(ANY));
}

@Test
function operatorInts()
{
dirs = WALL;
Assert.isTrue((dirs & LEFT) > 0);
Assert.isTrue((dirs & LEFT) > NONE);
Assert.isTrue((dirs & 0x0001) > 0);
Assert.isTrue((dirs & 0x0001) > NONE);
Assert.isTrue((dirs & LEFT) >= 0x1);
Assert.isTrue((dirs & LEFT) >= LEFT);
Assert.isTrue((dirs & 0x0001) >= 0x1);
Assert.isTrue((dirs & 0x0001) >= LEFT);
Assert.isTrue((dirs & LEFT) == LEFT);
Assert.isTrue((dirs & LEFT) == 0x1);
Assert.isTrue((dirs & 0x0001) == LEFT);
Assert.isTrue((dirs & 0x0001) == 0x1);

Assert.isFalse((dirs & LEFT) <= 0);
Assert.isFalse((dirs & LEFT) <= NONE);
Assert.isFalse((dirs & 0x0001) <= 0);
Assert.isFalse((dirs & 0x0001) <= NONE);

dirs = NONE;
Assert.isTrue((dirs | LEFT) > 0);
Assert.isTrue((dirs | LEFT) > NONE);
Assert.isTrue((dirs | 0x0001) > 0);
Assert.isTrue((dirs | 0x0001) > NONE);
Assert.isTrue((dirs | LEFT) >= 0x1);
Assert.isTrue((dirs | LEFT) >= LEFT);
Assert.isTrue((dirs | 0x0001) >= 0x1);
Assert.isTrue((dirs | 0x0001) >= LEFT);
Assert.isTrue((dirs | LEFT) == LEFT);
Assert.isTrue((dirs | LEFT) == 0x1);
Assert.isTrue((dirs | 0x0001) == LEFT);
Assert.isTrue((dirs | 0x0001) == 0x1);

Assert.isFalse((dirs | LEFT) <= 0);
Assert.isFalse((dirs | LEFT) <= NONE);
Assert.isFalse((dirs | 0x0001) <= 0);
Assert.isFalse((dirs | 0x0001) <= NONE);
}

@Test
function testAngles()
{
Expand Down
Loading