Skip to content

Instantly share code, notes, and snippets.

@zachwlewis
Created April 11, 2012 23:34
Show Gist options
  • Save zachwlewis/2363480 to your computer and use it in GitHub Desktop.
Save zachwlewis/2363480 to your computer and use it in GitHub Desktop.
A simple (untested) class used to manage and reuse a fixed number of Tilemaps in FlashPunk to prevent slowdown caused by multiple Tilemaps and garbage collection.
package net.thegamestudio.SimpleTilemapManager
{
import net.flashpunk.graphics.Tilemap;
/**
* Manages a fixed number of Tilemaps so you can reuse them.
* @author Zachary Weston Lewis (http://zacharylew.is)
*/
public class SimpleTilemapManager
{
/** Hold our Tilemaps. */
protected var _tilemaps:Vector.<Tilemap>;
/** Tilemap availability. */
protected var _available:Vector.<Boolean>;
/** The number of Tilemaps we have available. */
protected var _count:uint;
public function TilemapManager(tileset:*, height:uint, width:uint, tileWidth:uint, tileHeight:uint, count:uint)
{
_count = count;
_tilemaps = new Vector.<Tilemap>();
_available = new Vector.<Boolean>();
for (var i:uint = 0; i < count; i++)
{
// Generate our initial maps.
var t:Tilemap = new Tilemap(tileset, width, height, tileWidth, tileHeight);
// Turn the Tilemap off so it doesn't update while not in use.
t.visible = false;
t.active = false;
// Add it to our list.
_tilemaps.push(t);
// Make it available for use.
_available.push(true);
}
// Lock the length.
_tilemaps.fixed = true;
_available.fixed = true;
}
/**
* Grabs a fresh, preconstructed Tilemap for the user, preventing creation
* of a whole new one.
* @param index The tile index to default the returned Tilemap to.
* @return the first available Tilemap, or null if none are free.
*/
public function GetAvailableTilemap(index:uint = 0):Tilemap
{
// Find our first available Tilemap.
for (var i:uint = 0; i < count; i++)
{
if (_available[i])
{
// Our Tilemap isn't being used! Let's get it ready for prime time!
var t:Tilemap = _tilemaps[i];
// First, let's make it unavailable (sorry, ladies).
_available[i] = false;
// It's probably full of old data, so let's dispose that.
t.setRect(0, 0, t.rows - 1, t.columns - 1, index);
// Let's make it fully operational again!
t.visible = true;
t.active = true;
// Alright! Let's give it to the player.
return t;
}
}
// There are no available tilemaps! That's too bad for you.
// (If you wanted a more dynamic solution, you could just make
// the vectors unfixed and add a new Tilemap here...)
return null;
}
/**
* Keep your engine free from harmful waste! Recycle your Tilemaps
* when you're done with them for peak performance!
* @param t the Tilemap you're done with and wish to recycle
* @return true if successfully disposed, false if you gave me a Tilemap I don't manage.
*/
public function DisposeTilemap(t:Tilemap):Boolean
{
// Find this Tilemap in our list.
for (var i:uint = 0; i < _count; i++)
{
if (_tilemaps[i] == t)
{
// We found our Tilemap! Let's shove him in a dark corner
// somehwere, far away from any garbage collectors.
// Initiate shutdown sequence...
t.visible = false;
t.active = false;
// He's ripe for the pickin'! Come and get 'em!
_available[i] = true;
// Yay! We did it!
return true;
}
}
// Haha, oh. Erm... this is embarassing. That Tilemap you gave me
// wasn't one I loaned you... Huh. Where'd you find that thing?
return false;
}
/** The number of available Tilemaps. */
public function get unused():uint
{
var value:uint = 0;
for each(var b:Boolean in _available) { value += (b) ? 1 : 0; }
}
/** The total number of Tilemaps. */
public function get count():uint { return _count; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment