Thread: Custom Classes!
View Single Post
  #1  
Old 12-06-2007, 02:37 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Custom Classes!

Yea... I was talking with Stan, and I was like 'you can't make custom classes using the new operator'... And it dawned: A function call is what you need. Taking inspiration from Java, I created a custom-class.

class manager: (DB: Class )
PHP Code:
public function createClass( className, classes )
{
  if ( (@ 
className ) != null )
    (@ 
className ).destroy();

  
// Create what it means to be an object...
  
echo("Creating class" SPC className );
  
temp.newClass = new TStaticVar( (@className) );
  
temp.newClass.join( "test_custom_class" );
  
temp.newClass.classes = classes;
  
  
// Don't let the garabe collecter get this!
  
this.classes.add( temp.newClass );
}

public function 
getClasses()
{
  return 
this.classes;
} 
custom class script: (test_custom_class)
PHP Code:
public function newInstance( objectName )
{
  if ( (@ 
objectName ) != null )
    (@ 
objectName ).destroy();

  
temp.newInstance = new TStaticVar( @objectName );
  for ( 
temp.cjoin: this.classes )
  {
    
temp.newInstance.join( temp.cjoin );
  }
  
  
temp.newInstance.class = this;
  return 
temp.newInstance;
} 
Testing Class: (test_tnode)
PHP Code:
/**
 * Nodes Interface for PathFinder
 *
 * (+) void connect( Node node, ... );
 * (+) void disconnect( Node nodes );
 * (+) Node[] getNodes();
 * (+) Node[] findPath( Node goal );
 **/
 
/**
  * This connects a node to the node-list.
  *
  * @param node Node, any object that could be treated as a Node.
  *                If used to connect afterwards, add Node interface!
  **/
public function connect()
{
  for ( 
temp.node: params )
  {
    if ( 
temp.node == null )
      return;
    if ( 
temp.node in this.nodes )
      return;
  
    
this.nodes.add( temp.node );
  }
}

/**
  * This removes a node to the node-list.
  *
  * @param node, ... Node, any object that could be treated as a Node.
  **/
public function disconnect( node )
{
  
this.nodes.remove( node );
}

/**
  * This obtains the nodes that are currently connected to the object.
  *
  * @return Node[] List of nodes attached to object
  **/

public function getNodes()
{
  return 
this.nodes;
}

/**
  * This calculates the shortest distance to a particular node.
  *
  * @return Node[] An ordered list of the nodes that need to be traveled
  *                    to reach given destination.
  **/
public function findPath( endNode )
{
  
temp.nodeList.add( {this, new[0]} );
  
temp.nodeIndex = 0;
  while ( 
temp.nodeList.size() > temp.nodeIndex )
  {
    
temp.node = temp.nodeList[ temp.nodeIndex ][0];
    
temp.nodePath = temp.nodeList[ temp.nodeIndex ][1];
    
temp.nodePath.add( temp.node );
    if ( 
temp.node == endNode )
      return 
temp.nodePath;

    for ( 
temp.subnode: node.getNodes() )
    {
      if ( 
temp.subnode in temp.nodeList )
          continue;

      
temp.nodeList.add( { temp.subnode, temp.nodePath } );
    }
    
temp.nodeIndex ++;
  }

  return 
null;
} 
Example of Usage:
PHP Code:
function onCreated()
{
  Class.
createClass( "TNode", {"test_tnode"} );

  
node1 = TNode.newInstance("N1");
  
node2 = TNode.newInstance("N2");
  
node3 = TNode.newInstance("N3");
  
node4 = TNode.newInstance("N4");

  
node1.connect( node2 );
  
node2.connect( node3, node4 );
  
node3.connect( node4 );
  
  
nodePath = node1.findPath( node4 );

  echo(
"Path:");
  for ( 
node: nodePath )
    echo( 
node.name );
}

/** Output **
The script of NPC Class has been updated by Novo
Creating class TNode
Path:
N1
N2
N4
**/ 

Reply With Quote