Skip to content

Instantly share code, notes, and snippets.

@kinoko87
Created October 4, 2021 06:27
Show Gist options
  • Save kinoko87/91ff1a1498a041c8da5c6bc847b97d3d to your computer and use it in GitHub Desktop.
Save kinoko87/91ff1a1498a041c8da5c6bc847b97d3d to your computer and use it in GitHub Desktop.
AABB collision I coded although it's useless since FlxG.collide() exists
package;
import flixel.FlxG;
import flixel.FlxObject;
class AABB extends FlxObject
{
var collider:FlxObject;
var isColliding:Bool;
public static var aabbCount:Int;
var collisionAction:Void->Dynamic;
public function new(x:Float, y:Float, width:Float, height:Float, ?collider:FlxObject = null, collisionAction:Void->Dynamic = null)
{
super(x, y, width, height);
this.collider = collider;
this.collisionAction = collisionAction;
if (collisionAction == null)
collisionAction = function()
{
if (isColliding)
FlxObject.separate(this, collider);
return null;
}
FlxG.collide(this, collider);
aabbCount++;
if (aabbCount > 10)
FlxG.log.warn('Do not use too many AABBs!\nThey may use too much computational power!\nCurrentAABB Amount: $aabbCount');
}
public override function update(elapsed:Float)
{
isColliding = collisionHandler();
if (isColliding)
collisionAction();
super.update(elapsed);
}
function collisionHandler()
{
if (this.x < collider.x + collider.width
&& this.x + this.width > collider.x
&& this.y < collider.y + collider.height
&& this.y + this.height > collider.y)
{
return true;
}
else
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment