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 createClassclassNameclasses )
{
  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.addtemp.newClass );
}

public function 
getClasses()
{
  return 
this.classes;

custom class script: (test_custom_class)
PHP Code:
public function newInstanceobjectName )
{
  if ( (@ 
objectName ) != null )
    (@ 
objectName ).destroy();

  
temp.newInstance = new TStaticVar( @objectName );
  for ( 
temp.cjointhis.classes )
  {
    
temp.newInstance.jointemp.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.nodeparams )
  {
    if ( 
temp.node == null )
      return;
    if ( 
temp.node in this.nodes )
      return;
  
    
this.nodes.addtemp.node );
  }
}

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

/**
  * 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 findPathendNode )
{
  
temp.nodeList.add( {this, new[0]} );
  
temp.nodeIndex 0;
  while ( 
temp.nodeList.size() > temp.nodeIndex )
  {
    
temp.node temp.nodeListtemp.nodeIndex ][0];
    
temp.nodePath temp.nodeListtemp.nodeIndex ][1];
    
temp.nodePath.addtemp.node );
    if ( 
temp.node == endNode )
      return 
temp.nodePath;

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

      
temp.nodeList.add( { temp.subnodetemp.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.connectnode2 );
  
node2.connectnode3node4 );
  
node3.connectnode4 );
  
  
nodePath node1.findPathnode4 );

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

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

Reply With Quote