HaxePunk/HaxePunk — pull request #160 by azrafe7 (2013-11-07 17:45:49+00:00)
Title: Grid vs Grid collision ported from FlashPunk

Code ported from FlashPunk.

And here's a minimal TestScene.hx to interactively check collisions are working properly (remember to enable the console):

```as3
package ;
import com.haxepunk.Entity;
import com.haxepunk.graphics.Text;
import com.haxepunk.HXP;
import com.haxepunk.masks.Grid;
import com.haxepunk.Scene;
import com.haxepunk.utils.Input;
import com.haxepunk.utils.Key;
import flash.system.System;

/**
* ...
* @author azrafe7
*/
class TestScene extends Scene
{
var gridEntity:Entity;
var gridEntity2:Entity;

public function new()
{
super();
}

override public function begin():Void
{
var gridMask:Grid = new Grid(140, 80, 20, 20);
var gridStr =
"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);
gridEntity = addMask(gridMask, "solid", 0, 120);

var gridMask2:Grid = new Grid(140, 80, 20, 20);
var gridStr2 =
"1,0,0,0,0,0,0\n" +
"0,0,0,0,0,0,0\n" +
"0,0,0,0,0,1,0\n" +
"0,0,0,0,0,1,1\n";
gridMask2.loadFromString(gridStr2);
gridEntity2 = addMask(gridMask2, "solid", 150, 150);

HXP.log("ARROWS - grid one | SHIFT + ARROWS - grid two | WASD - camera");
var text:Text;
addGraphic(text = new Text("Enable the console to show the grids!"), HXP.BASELAYER, 145, 10);
text.size = 8;
text.color = 0xF03010;
text.scrollX = text.scrollY = 0;
}

override public function update():Void {
// ESC to exit
if (Input.pressed(Key.ESCAPE)) {
System.exit(1);
}

// move entities with arrows
var dx:Float = 0;
var dy:Float = 0;
if (Input.check("_ARROWS")) {
dx += Input.check(Key.LEFT) ? -1 : Input.check(Key.RIGHT) ? 1 : 0;
dy += Input.check(Key.UP) ? -1 : Input.check(Key.DOWN) ? 1 : 0;
}

if (Input.check(Key.SHIFT)) {
gridEntity.moveBy(dx * 2, dy * 2, ["solid"], true);
} else {
gridEntity2.moveBy(dx * 2, dy * 2, ["solid"], true);
}

// move camera around
if (Input.check(Key.A)) HXP.camera.x -= 2;
if (Input.check(Key.D)) HXP.camera.x += 2;
if (Input.check(Key.W)) HXP.camera.y -= 2;
if (Input.check(Key.S)) HXP.camera.y += 2;
}
}
```