Created
October 4, 2021 06:27
-
-
Save kinoko87/91ff1a1498a041c8da5c6bc847b97d3d to your computer and use it in GitHub Desktop.
AABB collision I coded although it's useless since FlxG.collide() exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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