Autotiling backdrop
What it does: creates a backdrop using a spritemap, and randomly places tiles. It creates a nice textured backdrop with the right sprite sheet.
Note: Due to the modifications required, this could be considered alpha at best.
In order to get this to work you must:- Edit net.flashpunk.graphics.Image:
Change
private var _buffer:BitmapData;
to
protected var _buffer:BitmapData;
- Then you must edit net.flashpunk.graphics.Spritemap:
add a getter function for the variable pixels:
/*
* Get the current buffer
*
*/
public function get pixels():BitmapData
{
return super._buffer;
}
These are necessary mods, maybe someone can figure out a way to do this without them.
Here's the code for the class:package net.flashpunk.graphics
{
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Point;
import net.flashpunk.FP;
import net.flashpunk.Graphic;
/**
* A background texture that can be repeated horizontally and vertically
* when drawn. Really useful for parallax backgrounds, textures, etc.
*/
public class BackdropAutotile extends Canvas
{
/**
* Constructor.
* @param texture Source texture.
* @param repeatX Repeat horizontally.
* @param repeatY Repeat vertically.
*/
public function BackdropAutotile(texture:*, size:uint = 16)
{
if (autoTile) _autoTileSpriteMap = new Spritemap(texture, size, size);
_textWidth = _texture.width;
_textHeight = _texture.height;
super(FP.width + _textWidth, FP.height + _textHeight);
FP.rect.x = FP.rect.y = 0;
FP.rect.width = _width;
FP.rect.height = _height;
var matrix:Matrix = new Matrix();
_graphics.clear();
for (var rows:uint = 0; rows < uint(_height / size); rows++)
for (var cols:uint = 0; cols < uint(_width / size); cols++)
{
_autoTileSpriteMap.setFrame(uint(FP.random * _autoTileSpriteMap.cols), uint(FP.random * _autoTileSpriteMap.rows));
matrix.translate(cols * size, rows * size);
draw(cols * size, rows * size, _autoTileSpriteMap.pixels);
}
}
/** @private Renders the Backdrop. */
override public function render(point:Point, camera:Point):void
{
point.x += x - camera.x * scrollX;
point.y += y - camera.y * scrollY;
if (_repeatX)
{
point.x %= _textWidth;
if (point.x > 0) point.x -= _textWidth;
}
if (_repeatY)
{
point.y %= _textHeight;
if (point.y > 0) point.y -= _textHeight;
}
_x = x; _y = y;
camera.x = camera.y = x = y = 0;
super.render(point, camera);
x = _x; y = _y;
}
// Backdrop information.
/** @private */ private var _autoTileSpriteMap:Spritemap;
/** @private */ private var _texture:BitmapData;
/** @private */ private var _textWidth:uint;
/** @private */ private var _textHeight:uint;
/** @private */ private var _repeatX:Boolean;
/** @private */ private var _repeatY:Boolean;
/** @private */ private var _x:Number;
/** @private */ private var _y:Number;
}
}