Created
August 28, 2014 18:35
-
-
Save lardratboy/52cf8cd1c87799d8258e to your computer and use it in GitHub Desktop.
Phaser bpt.Prefab breadthfirst/depthfirst children searching (work in progress)
This file contains hidden or 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
// Author Brad P. Taylor ([email protected]) license MIT | |
///<reference path="../../../../bower_components/phaser-official/build/phaser.d.ts"/> | |
///<reference path="../../DefinitelyTyped/lodash/lodash.d.ts"/> | |
///<reference path="./prefab.ts"/> | |
///<reference path="./basic.ts"/> | |
// this really should just be a collection of utils | |
module bpt.prefab { | |
export function applyMixins_(derivedCtor:any, baseCtors:any[]):any { | |
baseCtors.forEach(baseCtor => { | |
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => { | |
derivedCtor.prototype[name] = baseCtor.prototype[name]; | |
}) | |
}); | |
return derivedCtor; | |
} | |
export class PrefabAuditData { | |
sources:any[] = []; | |
addToSourceChain( source:any ) { | |
this.sources.push( source ); | |
} | |
} | |
export function prefabAuditTrail( target:any, source:any ) { | |
target.audit = target.audit || new PrefabAuditData(); | |
target.audit.addToSourceChain( source ); | |
} | |
export function createPrefab( prefab, ...args:any[] ) { | |
return Factory.create_( prefab, args ); | |
} | |
export function createPrefab_( prefab, params:any[] ) { | |
return Factory.create_( prefab, params ); | |
} | |
// return false to break loop | |
export interface IVisitFunction<T,O>{ (a:T,o:O): any; } | |
export function eachDepthFirst( parent:any, func:IVisitFunction<any,any>, result:any = {}, thisArg? ) { | |
var r:any = func.call( thisArg||parent, parent, result ); | |
if ( (undefined !== r) && !r ) return r; | |
if ( ('undefined' !== typeof parent.children) && (0 != parent.children.length) ) { | |
for ( var i = 0, l = parent.children.length; i < l; ++i ) { | |
r = eachDepthFirst( parent.children[i], func, result, thisArg ); | |
if ( (undefined !== r) && !r ) return r; | |
} | |
} | |
} | |
export function eachBreadthFirst( parent:any, func:IVisitFunction<any,any>, result:any = {}, thisArg?, clearCookies = true, cookie = "$c$o$o$k$i$e$" ) { | |
var queue:any[] = [ parent ]; | |
var cookied:any[] = []; | |
var top:number = 0; | |
parent[cookie] = cookied.length; | |
clearCookies ? cookied.push( parent ) : ++cookied.length; | |
var r:any = func.call(thisArg||parent, parent, result); | |
if ( (undefined !== r) && !r ) return r; | |
while ( top < queue.length ) { | |
var current = queue[top++]; | |
if ( ('undefined' !== typeof current.children) && (0 != current.children.length) ) { | |
for ( var i = 0, l = current.children.length; i < l; ++i ) { | |
var child:any = current.children[i]; | |
if ( undefined === child[cookie] ) { | |
child[cookie] = cookied.length; | |
clearCookies ? cookied.push( child ) : ++cookied.length; | |
r = func.call(thisArg||child, child, result); | |
if ( (undefined !== r) && !r ) return r; | |
if ( ('undefined' !== typeof child.children) && (0 != child.children.length) ) { | |
queue.push( child ); | |
} | |
} | |
} | |
} | |
} | |
if ( clearCookies ) for ( i = 0, l = cookied.length; i < l; ++i ) { | |
delete cookied[i][cookie]; | |
} | |
} | |
export function testDepths( parent ) { | |
//eachBreadthFirst( parent, function(i){ console.log( i.name, i ); }, {}, this, false ); | |
eachBreadthFirst( parent, ()=>{}, {}, this, false ); | |
var visit = 0; | |
eachDepthFirst( parent, (item) => { | |
console.log( visit++, item["$c$o$o$k$i$e$"], item ); | |
delete item["$c$o$o$k$i$e$"]; | |
} ); | |
} | |
export function traceDecendents( parent:any, thisArg? ) { | |
eachDepthFirst( parent, console.log.bind(console), {}, thisArg ); | |
} | |
export function getDecendents( parent:any, decendents:any[] = [], thisArg? ) { | |
eachDepthFirst( parent, (item:any, result:any[]) => { decendents.push( item ); }, decendents , thisArg ); | |
return decendents; | |
} | |
export function findDecendantsOfType( parent:any, T:any, depthFirst = false ):any[] { | |
var fn:any = depthFirst ? eachDepthFirst : eachBreadthFirst; | |
var found:any[] = []; | |
fn( parent, (child) => { | |
if ( child instanceof T.prototype.constructor ) { | |
// console.log( child, T ); | |
found.push( child ); | |
} | |
}); | |
return found; | |
} | |
// TODO - this probably should take a predicate function instead of a regex/field | |
export function findMatchingDecendantsOfType( parent:any, re:RegExp, field:string, T:any, depthFirst = false ):any[] { | |
var candidates:any[] = findDecendantsOfType( parent, T, depthFirst ); | |
if ( undefined === candidates || 0 == candidates.length ) return; | |
var found:any[] = []; | |
for ( var i in candidates ) { | |
if ( re.test( candidates[i][field].toString() ) ) { | |
found.push( candidates[i] ); | |
} | |
} | |
return found; | |
} | |
export function addOverlayAsChild( target, key, xCoeff, yCoeff ) { | |
var overlay = target.game.add.image( 0, 0, key ); | |
overlay.anchor.setTo( 0.5, 0.5 ); | |
var ow = overlay.width, oh = overlay.height; | |
overlay.x = (ow*0.5)+(target.width-ow)*xCoeff - target.width*target.anchor.x; | |
overlay.y = (oh*0.5)+(target.height-oh)*yCoeff - target.height*target.anchor.y; | |
overlay.alpha = 0.25; | |
target.addChild( overlay ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment