PDA

View Full Version : communicator-tree class


Novo
04-15-2007, 03:44 AM
/*********************************
(+) addControl( obj ) returns BOOL
- adds sub-controller
- MUST be in class "communicator-tree"

(+) remControl( obj ) returns BOOL
- removes sub-controller

(+) getControls( ctype ) returns controls[];
- Returns array of all objects with this.ctype == ctype
- Recursive ( applies to sub-controllers, and thenforth )

(-) removeEmpty() returns BOOL
- Cleans up controls list of empty objects (object.type() != 2 )

( ) onEvent( event, param1, param2, param3, ... )
- called through obj.trigger( "Event", param1, param2, param3 );
- Is recursive
- to override, do this:
function onEvent()
{
// Stuff
controller::onEvent( params[0], ..., params[n] );
}

( ) onEvent[EventName]( params )
- Event Called when onEvent( EventName, params ) is.
- no default.

( ) onDestroy()
- SHOULD BE USED INSTEAD OF obj.destroy();
- called to destroy object

( ) onParentDestroy()
- Event Called when Parent Destroyed
- default:
if other parent, stay alive, else
call Destroy
- to override, do this:
function onParentDestroyed( obj )
{
this.parentControls.remove( obj );
return;
}
*********************************/

public function addControl( obj )
{
if ( ! obj.isinclass( "communicator-tree" ) )
{
echo( obj.id SPC "is not a communicator-tree.");
return false;
}

if ( this.controls.index( obj ) == -1 )
this.controls.add( obj );
if ( obj.parentControls.index( this ) == -1 )
obj.parentControls.add(this);

return true;
}

public function remControl( obj )
{
obj.parentControls.remove( this );
this.controls.remove( obj );
return true;
}

public function getControls( ctype )
{ // Searched entire directory
removeEmpty(); // Clean Up
for ( c: this.controls )
{
temp.arr = c.getControls( ctype );
if ( temp.arr.type() == 3 )
temp.array.addarray( temp.arr );
}

if ( this.ctype == ctype )
temp.array.add( this );

return temp.array;
}

function onEvent()
{ // Recursive
param = params;
removeEmpty(); // Clean up!
this.trigger( "Event"@ param[0], param[1] );
for ( c: this.controls )
c.trigger( "Event", param );
}

function onDestroy()
{
removeEmpty(); // Clean up!
for ( c: this.parentControls )
c.remControl( this );
for ( c: this.controls )
c.trigger( "ParentDestroyed", this );

this.destroy();
}

function onParentDestroyed( this )
{
removeEmpty();
this.parentControls.remove( this );

// No parent? Destroy itself!
if ( this.parentControls.size() == 0 )
this.trigger( "Destroy", "" );
}

function removeEmpty()
{
for ( i = 0; i < this.controls.size(); i ++ )
{
if ( this.controls[ i ].type() != 2 )
{
this.controls.delete( i );
-- i;
}
}

return true;
}


A tree-based communicator.
Trigger an event on the top of the tree, and it goes all the way down.

cbk1994
04-15-2007, 05:42 AM
/*********************************
(+) addControl( obj ) returns BOOL
- adds sub-controller
- MUST be in class "communicator-tree"

(+) remControl( obj ) returns BOOL
- removes sub-controller

(+) getControls( ctype ) returns controls[];
- Returns array of all objects with this.ctype == ctype
- Recursive ( applies to sub-controllers, and thenforth )

(-) removeEmpty() returns BOOL
- Cleans up controls list of empty objects (object.type() != 2 )

( ) onEvent( event, param1, param2, param3, ... )
- called through obj.trigger( "Event", param1, param2, param3 );
- Is recursive
- to override, do this:
function onEvent()
{
// Stuff
controller::onEvent( params[0], ..., params[n] );
}

( ) onEvent[EventName]( params )
- Event Called when onEvent( EventName, params ) is.
- no default.

( ) onDestroy()
- SHOULD BE USED INSTEAD OF obj.destroy();
- called to destroy object

( ) onParentDestroy()
- Event Called when Parent Destroyed
- default:
if other parent, stay alive, else
call Destroy
- to override, do this:
function onParentDestroyed( obj )
{
this.parentControls.remove( obj );
return;
}
*********************************/

public function addControl( obj )
{
if ( ! obj.isinclass( "communicator-tree" ) )
{
echo( obj.id SPC "is not a communicator-tree.");
return false;
}

if ( this.controls.index( obj ) == -1 )
this.controls.add( obj );
if ( obj.parentControls.index( this ) == -1 )
obj.parentControls.add(this);

return true;
}

public function remControl( obj )
{
obj.parentControls.remove( this );
this.controls.remove( obj );
return true;
}

public function getControls( ctype )
{ // Searched entire directory
removeEmpty(); // Clean Up
for ( c: this.controls )
{
temp.arr = c.getControls( ctype );
if ( temp.arr.type() == 3 )
temp.array.addarray( temp.arr );
}

if ( this.ctype == ctype )
temp.array.add( this );

return temp.array;
}

function onEvent()
{ // Recursive
param = params;
removeEmpty(); // Clean up!
this.trigger( "Event"@ param[0], param[1] );
for ( c: this.controls )
c.trigger( "Event", param );
}

function onDestroy()
{
removeEmpty(); // Clean up!
for ( c: this.parentControls )
c.remControl( this );
for ( c: this.controls )
c.trigger( "ParentDestroyed", this );

this.destroy();
}

function onParentDestroyed( this )
{
removeEmpty();
this.parentControls.remove( this );

// No parent? Destroy itself!
if ( this.parentControls.size() == 0 )
this.trigger( "Destroy", "" );
}

function removeEmpty()
{
for ( i = 0; i < this.controls.size(); i ++ )
{
if ( this.controls[ i ].type() != 2 )
{
this.controls.delete( i );
-- i;
}
}

return true;
}


A tree-based communicator.
Trigger an event on the top of the tree, and it goes all the way down.

Out of curiosity ... what does that do?

Novo
04-15-2007, 07:30 AM
Out of curiosity ... what does that do?

It's a tree communicator. Basically...

There is a parent object, and child objects. You can attach an object to the parent object... When you trigger an event to the parent object, it sends it out to all children objects.

For instance...

If you have a house ( parent object ) and you have furniture in ( children objects ). Sadly, the house burns down, and everything in it. ( Event ). One way of doing this is house.trigger( "Event", "burnDown", 4 );
So every object in the house gets a object.onEventburnDown( 4 ); called.

theHAWKER
04-15-2007, 07:58 AM
wow, what server would u use this on?

Novo
04-15-2007, 08:01 AM
wow, what server would u use this on?

I personally used it for earthquakes and weather... When there was rain, the plants would get a rain trigger, and would get the required water.

Chandler
04-15-2007, 08:22 AM
I'll be using this =D
Thank you! :p

cbk1994
04-15-2007, 03:08 PM
It's a tree communicator. Basically...

There is a parent object, and child objects. You can attach an object to the parent object... When you trigger an event to the parent object, it sends it out to all children objects.

For instance...

If you have a house ( parent object ) and you have furniture in ( children objects ). Sadly, the house burns down, and everything in it. ( Event ). One way of doing this is house.trigger( "Event", "burnDown", 4 );
So every object in the house gets a object.onEventburnDown( 4 ); called.

Okay, I didn't quite understand it.

Nice, fancy code! Good work, keep it up :D

Thanks for contributing, I'll sure use this.