Skip to content

Commit

Permalink
some tweaks and new functions for the plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamAtomic committed Apr 25, 2011
1 parent 077977c commit 5e2bd11
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 18 deletions.
68 changes: 63 additions & 5 deletions org/flixel/FlxG.as
Original file line number Diff line number Diff line change
Expand Up @@ -930,30 +930,88 @@ package org.flixel
*/
static public function addPlugin(Plugin:FlxBasic):FlxBasic
{
plugins.push(Plugin);
//Don't add repeats
var pluginList:Array = FlxG.plugins;
var i:uint = 0;
var l:uint = pluginList.length;
while(i < l)
{
if(pluginList[i++].toString() == Plugin.toString())
return Plugin;
}

//no repeats! safe to add a new instance of this plugin
pluginList.push(Plugin);
return Plugin;
}

/**
* Retrieves a plugin from the global plugin array.
* Retrieves a plugin based on its class name from the global plugin array.
*
* @param ClassType Useful for retrieving plugins based on their type. See the <code>FlxPath</code> or <code>FlxTimer</code> constructors for example usage.
* @param ClassType The class name of the plugin you want to retrieve. See the <code>FlxPath</code> or <code>FlxTimer</code> constructors for example usage.
*
* @return The plugin object, or null if no matching plugin was found.
*/
static public function getPlugin(ClassType:Class):FlxBasic
{
var pluginList:Array = FlxG.plugins;
var i:uint = 0;
var l:uint = plugins.length;
var l:uint = pluginList.length;
while(i < l)
{
if(plugins[i] is ClassType)
if(pluginList[i++] is ClassType)
return plugins[i];
i++;
}
return null;
}

/**
* Removes an instance of a plugin from the global plugin array.
*
* @param Plugin The plugin instance you want to remove.
*
* @return The same <code>FlxBasic</code>-based plugin you passed in.
*/
static public function removePlugin(Plugin:FlxBasic):FlxBasic
{
//Don't add repeats
var pluginList:Array = FlxG.plugins;
var i:int = pluginList.length-1;
while(i >= 0)
{
if(pluginList[i] == Plugin)
pluginList.splice(i,1);
i--;
}
return Plugin;
}

/**
* Removes an instance of a plugin from the global plugin array.
*
* @param ClassType The class name of the plugin type you want removed from the array.
*
* @return Whether or not at least one instance of this plugin type was removed.
*/
static public function removePluginType(ClassType:Class):Boolean
{
//Don't add repeats
var results:Boolean = false;
var pluginList:Array = FlxG.plugins;
var i:int = pluginList.length-1;
while(i >= 0)
{
if(pluginList[i] is ClassType)
{
pluginList.splice(i,1);
results = true;
}
i--;
}
return results;
}

/**
* Called by <code>FlxGame</code> to set up <code>FlxG</code> during <code>FlxGame</code>'s constructor.
*/
Expand Down
2 changes: 1 addition & 1 deletion org/flixel/FlxGame.as
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ package org.flixel
FlxG.destroySounds();
if(_debugger != null)
_debugger.watch.removeAll();
var timerManager:TimerManager = FlxG.getPlugin(TimerManager) as TimerManager;
var timerManager:TimerManager = FlxTimer.manager;
if(timerManager != null)
timerManager.clear();

Expand Down
17 changes: 11 additions & 6 deletions org/flixel/FlxPath.as
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ package org.flixel
debugScrollFactor = new FlxPoint(1.0,1.0);
debugColor = 0xffffff;

var plugin:DebugPathDisplay = FlxG.getPlugin(DebugPathDisplay) as DebugPathDisplay;
if(plugin != null)
plugin.add(this);
var debugPathDisplay:DebugPathDisplay = manager;
if(debugPathDisplay != null)
debugPathDisplay.add(this);
}

/**
* Clean up memory.
*/
public function destroy():void
{
var plugin:DebugPathDisplay = FlxG.getPlugin(DebugPathDisplay) as DebugPathDisplay;
if(plugin != null)
plugin.remove(this);
var debugPathDisplay:DebugPathDisplay = manager;
if(debugPathDisplay != null)
debugPathDisplay.remove(this);

debugScrollFactor = null;
_point = null;
Expand Down Expand Up @@ -264,5 +264,10 @@ package org.flixel
//then stamp the path down onto the game buffer
Camera.buffer.draw(FlxG.flashGfxSprite);
}

static public function get manager():DebugPathDisplay
{
return FlxG.getPlugin(DebugPathDisplay) as DebugPathDisplay;
}
}
}
17 changes: 11 additions & 6 deletions org/flixel/FlxTimer.as
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ package org.flixel
paused = false;
finished = false;

var plugin:TimerManager = FlxG.getPlugin(TimerManager) as TimerManager;
if(plugin != null)
plugin.add(this);
var timerManager:TimerManager = manager;
if(timerManager != null)
timerManager.add(this);
}

/**
Expand Down Expand Up @@ -129,9 +129,9 @@ package org.flixel
public function stop():void
{
finished = true;
var plugin:TimerManager = FlxG.getPlugin(TimerManager) as TimerManager;
if(plugin != null)
plugin.remove(this);
var timerManager:TimerManager = manager;
if(timerManager != null)
timerManager.remove(this);
}

/**
Expand All @@ -149,5 +149,10 @@ package org.flixel
{
return loops-_loopsCounter;
}

static public function get manager():TimerManager
{
return FlxG.getPlugin(TimerManager) as TimerManager;
}
}
}

0 comments on commit 5e2bd11

Please sign in to comment.