Created
January 18, 2015 17:07
-
-
Save theacodes/f84b911496be7ee79cd3 to your computer and use it in GitHub Desktop.
Really basic collision manager for luxe
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
import luxe.collision.ShapeDrawerLuxe; | |
import luxe.collision.shapes.Shape; | |
import luxe.collision.Collision; | |
import luxe.collision.CollisionData; | |
import luxe.Entity; | |
import luxe.Color; | |
class CollisionManager { | |
private var drawer : ShapeDrawerLuxe; | |
private var shapes : Array<Shape> = []; | |
public function new() { | |
drawer = new ShapeDrawerLuxe(); | |
} | |
public function add(shape : Shape, ?callback : CollisionData -> Void, ?entity : Entity, ?type : Int = 0) { | |
var cfg = new CollisionConfig(); | |
cfg.callback = callback; | |
cfg.entity = entity; | |
cfg.type = type; | |
shape.data = cfg; | |
shapes.push(shape); | |
} | |
public function testAll() { | |
if (shapes.length < 2) return; | |
for(i in 0...shapes.length) { | |
for(j in i+1...shapes.length) { | |
// No collision between solids. This should probably be extended into a | |
// collision mask system. | |
if(shapes[i].data.type == 0 && shapes[j].data.type == 0) continue; | |
var data = Collision.test(shapes[i], shapes[j]); | |
if(data != null){ | |
var aconfig = cast(shapes[i].data, CollisionConfig); | |
var bconfig = cast(shapes[j].data, CollisionConfig); | |
if(aconfig.callback != null){ | |
aconfig.callback(data); | |
} | |
if(bconfig.callback != null){ | |
// invert data | |
data.separation = data.separation.multiplyScalar(-1); | |
var tmp = data.shape1; | |
data.shape1 = data.shape2; | |
data.shape2 = tmp; | |
bconfig.callback(data); | |
} | |
} | |
} | |
} | |
} | |
public function placeFree(shape : Shape, ?typemask : Int = 0xFFFFFFFF) : Bool { | |
for(other in shapes){ | |
if(shape == other) continue; | |
if(shape.data.type & typemask == 0) continue; | |
if(Collision.test(shape, other) != null) return false; | |
} | |
return true; | |
} | |
public function drawAll() { | |
for(i in shapes){ | |
drawer.drawShape(i, new Color().rgb(0x1111CC), true); | |
} | |
} | |
} | |
class CollisionConfig { | |
public var callback : CollisionData -> Void; | |
public var entity : Entity; | |
public var type : Int; | |
public function new() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment