Got my idea from openwarfare, but made my own version. In my opinion does it make your code more clear and consisting of lesser code. Here's my version:
Just thread init() once. Then you can use [[level.on]] anywhere you want to add new events.
Example usage:
Hope this helped some people.
Code:
init()
{
level.event = [];
level.event[ "connecting" ] = [];
level.event[ "connected" ] = [];
level.event[ "spawned" ] = [];
level.event[ "death" ] = [];
level.event[ "killed" ] = [];
level.event[ "disconnected" ] = [];
level.event[ "joined_team" ] = [];
level.event[ "joined_spectators" ] = [];
level.event[ "menu_response" ] = [];
level.on = ::addEvent;
[[level.on]]( "connected", ::onPlayerSpawned );
[[level.on]]( "connected", ::onPlayerDeath );
[[level.on]]( "connected", ::onPlayerKilled );
[[level.on]]( "connected", ::onPlayerSpawned );
[[level.on]]( "connected", ::onPlayerJoinedSpectators );
[[level.on]]( "connected", ::onPlayerJoinedTeam );
[[level.on]]( "connected", ::onPlayerMenuResponse );
thread onPlayerConnecting();
thread onPlayerConnected();
}
addEvent( event, process )
{
if( !isdefined( level.event[ event ] ) || !isdefined( process ))
return;
level.event[ event ][ level.event[ event ].size ] = process;
}
onPlayerConnecting()
{
while(1)
{
level waittill( "connecting", player );
for( i=0; i<level.event[ "connecting" ].size; i++ )
player thread [[level.event[ "connecting" ][i]]]();
}
}
onPlayerConnected()
{
while(1)
{
level waittill( "connected", player );
for( i=0; i<level.event[ "connected" ].size; i++ )
player thread [[level.event[ "connected" ][i]]]();
}
}
onPlayerSpawned()
{
self endon( "disconnect" );
while(1)
{
self waittill( "spawned_player" );
for( i=0; i<level.event[ "spawned" ].size; i++ )
self thread [[level.event[ "spawned" ][i]]]();
}
}
onPlayerDeath()
{
self endon( "disconnect" );
while(1)
{
self waittill( "death" );
for( i=0; i<level.event[ "death" ].size; i++ )
self thread [[level.event[ "death" ][i]]]();
}
}
onPlayerKilled()
{
self endon( "disconnect" );
while(1)
{
self waittill( "killed_player" );
for( i=0; i<level.event[ "killed" ].size; i++ )
self thread [[level.event[ "killed" ][i]]]();
}
}
onPlayerJoinedTeam()
{
self endon( "disconnect" );
while(1)
{
self waittill( "joined_team" );
for( i=0; i<level.event[ "joined_team" ].size; i++ )
self thread [[level.event[ "joined_team" ][i]]]();
}
}
onPlayerJoinedSpectators()
{
self endon( "disconnect" );
while(1)
{
self waittill( "joined_spectators" );
for( i=0; i<level.event[ "joined_spectators" ].size; i++ )
self thread [[level.event[ "joined_spectators" ][i]]]();
}
}
onPlayerDisconnected()
{
while(1)
{
self waittill( "disconnect" );
for( i=0; i<level.event[ "disconnect" ].size; i++ )
self thread [[level.event[ "disconnect" ][i]]]();
}
}
onPlayerMenuResponse()
{
self endon( "disconnect" );
while(1)
{
self waittill( "menuresponse", menu, response );
for( i=0; i<level.event[ "menu_response" ].size; i++ )
self thread [[level.event[ "menu_response" ][i]]]( menu, response );
}
}
Example usage:
Code:
init()
{
[[level.on]]( "connected", ::onPlayerConnected );
[[level.on]]( "spawned", ::onPlayerSpawned );
[[level.on]]( "menu_response", ::onPlayerMenuResponse );
}
onPlayerConnected()
{
iprintln( self, " is connected." );
}
onPlayerSpawned()
{
self iprintln( "You are spawned!" );
}
onPlayerMenuResponse( menu, response )
{
self iprintln( "Menu: ", menu, " response: ", response );
}