Created
March 29, 2014 20:39
-
-
Save azrafe7/9862505 to your computer and use it in GitHub Desktop.
Collision tests for FlashPunk (testing new Circle and Polygon classes too)
This file contains 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
// see https://github.com/azrafe7/FlashPunk/commit/475c4c4fb11a3554261ba871f64f16c5ddd53693 | |
package | |
{ | |
import flash.display.BlendMode; | |
import flash.geom.Point; | |
import flash.system.System; | |
import flash.text.AntiAliasType; | |
import flash.text.TextField; | |
import flash.text.TextFormat; | |
import net.flashpunk.Entity; | |
import net.flashpunk.FP; | |
import net.flashpunk.graphics.Image; | |
import net.flashpunk.graphics.Text; | |
import net.flashpunk.masks.Circle; | |
import net.flashpunk.masks.Grid; | |
import net.flashpunk.masks.Hitbox; | |
import net.flashpunk.masks.Masklist; | |
import net.flashpunk.masks.Pixelmask; | |
import net.flashpunk.masks.Polygon; | |
import net.flashpunk.utils.Draw; | |
import net.flashpunk.utils.Input; | |
import net.flashpunk.utils.Key; | |
import net.flashpunk.World; | |
/** | |
* ... | |
* @author azrafe7 | |
*/ | |
public class TestWorld extends World | |
{ | |
[Embed(source="assets/skeleton.png")] | |
private var SKELETON:Class; | |
private var eActive:Entity; | |
private var ePoly:Entity; | |
private var eCircle:Entity; | |
private var circle:Circle; | |
private var polygon:Polygon; | |
private var text:Text; | |
private var messages:Vector.<String>; | |
private static var MAX_MESSAGES:int = 7; | |
private var hitEntities:Vector.<Entity>; | |
private var e6:Entity; | |
private var imgPoly:Image; | |
private var pixelmask:Pixelmask; | |
private var hitboxMasklist:Masklist; | |
public function TestWorld() | |
{ | |
} | |
override public function begin():void { | |
// interactive CIRCLE | |
eCircle = addMask(circle = new Circle(20, 50,20), "circle"); | |
eCircle.x = FP.halfWidth; | |
eCircle.y = FP.halfHeight + 20; | |
eCircle.graphic = Image.createCircle(20, 0xFF00FF, .5, false, 1); | |
eCircle.graphic.x = 50; | |
eCircle.graphic.y = 20; | |
eCircle.active = eCircle.visible = true; | |
// interactive POLYGON | |
var points:Vector.<Point> = new Vector.<Point>(); | |
points.push(new Point(0, 0)); | |
points.push(new Point(30, 0)); | |
points.push(new Point(30, 60)); | |
ePoly = addMask(polygon = new Polygon(points), "polygon"); // triangle | |
ePoly = addMask(polygon = Polygon.createRegular(5, 20), "polygon"); // regular poly | |
ePoly.x = FP.halfWidth; | |
ePoly.y = FP.halfHeight; | |
polygon.angle = 45; | |
polygon.x = 30; | |
polygon.y = 10; | |
polygon.originX = 50; | |
polygon.originY = 0; | |
// create an Image from the previously set Polygon | |
imgPoly = Image.createPolygon(polygon, 0xFFFF00, .7, true); | |
imgPoly.smooth = true; | |
ePoly.addGraphic(imgPoly); | |
ePoly.active = ePoly.visible = true; | |
// other MASKS | |
// Mask/Entity | |
var e1:Entity = new Entity(200, 30); | |
e1.type = "mask"; | |
e1.width = 40; | |
e1.height = 50; | |
add(e1); | |
// Hitbox | |
var hitbox:Hitbox = new Hitbox(30, 30, 20); | |
var e2:Entity = addMask(hitbox, "hitbox"); | |
e2.x = 20; | |
e2.y = 20; | |
// Circle | |
var e3:Entity = addMask(new Circle(30, 0, 0), "circle"); | |
e3.x = 250; | |
e3.y = 110; | |
// Grid | |
var gridMask:Grid = new Grid(140, 80, 20, 20); | |
var gridStr:String = | |
"1,0,0,1,1,1,0\n" + | |
"0,0,0,1,0,1,1\n" + | |
"1,0,0,0,0,0,1\n" + | |
"0,0,0,0,0,0,1\n"; | |
gridMask.loadFromString(gridStr); | |
gridMask.x = -6; | |
gridMask.y = -10; | |
var e4:Entity = addMask(gridMask, "grid", 5, 120); | |
// Polygon | |
var polyMask:Polygon = Polygon.createRegular(5, 20); | |
var e5:Entity = addMask(polyMask, "polygon"); | |
e5.x = 130; | |
e5.y = 40; | |
e5.addGraphic(imgPoly); | |
// Pixelmask | |
pixelmask = new Pixelmask(SKELETON); | |
e6 = addMask(pixelmask, "pixelmask"); | |
e6.x = 260; | |
e6.y = 20; | |
//pixelmask.x = 10; | |
//pixelmask.y = 10; | |
var pixelmask2:Pixelmask = new Pixelmask(SKELETON); | |
var p2:Entity = addMask(pixelmask2, "pixelmask"); | |
p2.x = 160; | |
p2.y = 190; | |
pixelmask2.x = 10; | |
pixelmask2.y = 10; | |
var masklist:Masklist = new Masklist(new Hitbox(30, 30, 20), new Hitbox(30, 40, 30, 40)); | |
addMask(masklist, "masklist", 330, 200); | |
var flat:Vector.<Number> = new <Number>[0, 0, 30, 0, 30, 30]; | |
var poly1:Polygon = Polygon.createFromFlatVector(flat); | |
var poly2:Polygon = Polygon.createFromFlatVector(flat); | |
poly1.y = 30; | |
poly2.x = 40; | |
poly2.y = 20; | |
var polylist:Masklist = new Masklist(poly1, poly2); | |
var e7:Entity = addMask(polylist, "polylist", 50, 200); | |
text = new Text("puppa!"); | |
var textEntity:Entity = new Entity(5, 55, text); | |
text.blend = BlendMode.OVERLAY; | |
text.scrollX = 0; | |
text.scrollY = 0; | |
text.scale = .5; | |
text.setTextProperty("multiline", true); | |
add(textEntity); | |
messages = new Vector.<String>(); | |
messages.push("Enable the console..."); | |
messages.push("hit the play button on top..."); | |
messages.push("then use keys described below"); | |
text.text = messages.join("\n"); | |
FP.log("~: Console | SHIFT+ARROWS: Circle | ARROWS: Polygon | SPACE: rotate polygon"); | |
hitEntities = new Vector.<Entity>(); | |
eActive = e6; | |
FP.console.paused = true; | |
FP.engine.paused = false; | |
} | |
override public function update():void | |
{ | |
super.update(); | |
// ESC to exit | |
if (Input.pressed(Key.ESCAPE)) { | |
System.exit(1); | |
} | |
if (Input.pressed(Key.SPACE)) { | |
rotate(polygon, imgPoly, polygon.angle + (Input.check(Key.SHIFT) ? -1 : 1) * 15); | |
} | |
if (Input.check(Key.CONTROL)) { | |
e6.x += Input.check(Key.LEFT) ? -1 : Input.check(Key.RIGHT) ? 1 : 0; | |
e6.y += Input.check(Key.UP) ? -1 : Input.check(Key.DOWN) ? 1 : 0; | |
eActive = e6; | |
} else if (!Input.check(Key.SHIFT)) { | |
ePoly.x += Input.check(Key.LEFT) ? -1 : Input.check(Key.RIGHT) ? 1 : 0; | |
ePoly.y += Input.check(Key.UP) ? -1 : Input.check(Key.DOWN) ? 1 : 0; | |
eActive = ePoly; | |
} else if (Input.check(Key.SHIFT)) { | |
eCircle.x += Input.check(Key.LEFT) ? -1 : Input.check(Key.RIGHT) ? 1 : 0; | |
eCircle.y += Input.check(Key.UP) ? -1 : Input.check(Key.DOWN) ? 1 : 0; | |
eActive = eCircle; | |
} | |
hitEntities.length = 0; | |
eActive.collideTypesInto(["hitbox", "mask", "circle", "grid", "polygon", "pixelmask", "masklist", "polylist"], eActive.x, eActive.y, hitEntities); | |
for (var i:int = 0; i < hitEntities.length; i++) { | |
var hitEntity:Entity = Entity(hitEntities[i]); | |
trace("hit " + hitEntity.type); | |
messages.push("hit " + hitEntity.type); | |
if (messages.length > MAX_MESSAGES) messages.splice(0, 1); | |
text.text = messages.join("\n"); | |
FP.alarm(.2, function ():void | |
{ | |
messages.splice(0, 1); | |
text.text = messages.join("\n"); | |
}); | |
} | |
} | |
override public function render():void | |
{ | |
super.render(); | |
// x,y | |
Draw.rect(ePoly.x + imgPoly.x, ePoly.y + imgPoly.y, 5, 5, 0x00FF00, 1); | |
Draw.rect(ePoly.x + polygon.x, ePoly.y + polygon.y, 4, 4, 0xFF0000, 1); | |
// origin | |
Draw.rect(ePoly.x + imgPoly.originX, ePoly.y + imgPoly.originY, 3, 3, 0x0000FF, 1); | |
Draw.rect(ePoly.x + polygon.x + polygon.originX, ePoly.y + polygon.y + polygon.originY, 2, 2, 0xFFFFFF, 1); | |
} | |
public function rotate(poly:Polygon, image:Image, angle:Number):void | |
{ | |
poly.angle = angle; | |
image.angle = angle; | |
trace(poly.x, poly.y, poly.originX, poly.originY, poly.minX, poly.minY); | |
trace(imgPoly.x, imgPoly.y, imgPoly.originX, imgPoly.originY); | |
trace(poly.width, poly.height, imgPoly.width, imgPoly.height); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment