⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class_entity_list.php

📁 一个基于web的rpg游戏源代码
💻 PHP
📖 第 1 页 / 共 2 页
字号:
    }    /**    * Return an array containing all my parents list    *    * @param object $entity A reference to the entity to serach for    * @param string $key    Optional - get the key instead of the reference    */    function get_all_parents(&$entity, $key = NULL) {        // Check input        if($this->is_entity($entity) !== TRUE) {            if($key === NULL) {                return(FALSE);            }            $entity = &$this->get_entity($key);            if($entity === FALSE) {                return(FALSE);            }        }        // Check if $entity == root        $all_parents = array();        if($entity->get_key() == $this->root_entity->get_key()) return($all_parents);        // Note that we will return the empty from the root_entity        // Bucle saving all references        $parent = &$entity->parent;        // We can't compare object, we compare keys        while($parent->get_key() != $this->root_entity->get_key()) {            $k = $parent->get_key();            $all_parents[$k] = &$parent;            $parent = &$parent->parent;        }        // Add the root key        $k = $this->root_entity->get_key();        $all_parents[$k] = &$this->root_entity;        $this->flush_error();        return($all_parents);    }    /**    * Add a new entity to the list    *    * If the entity is the first to be created, then $root_entity    * will be created    *    * @param string $parent_key The parent key to attach the object    * @param string $type       Type of object to create    */    function &add_entity($key='', $parent_key='', $type=0) {        // Check input        if(empty($key)) {            $key = $this->create_key();        } else {            if(is_valid_key($key) == FALSE) {                return(FALSE);            }        }        if(($parent = &$this->get_entity($parent_key, FALSE)) == FALSE) {            // If we don't have a root_entity, create it!            if($this->is_entity($this->root_entity) !== TRUE) {                $this->root_entity = &new entity($this);            }            $parent = &$this->root_entity;        }        // Get class name to create from $type        if(!isset($this->entity_types[$type])) {            $this->last_error = "entity type not recognized >$type";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        $class_name = $this->entity_types[$type];        if(!class_exists($class_name)) {            $this->last_error = "class not declared >".$class_name;            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Check if there is already an entity with this key        if($this->key_exists($key, FALSE) == TRUE) {            $this->last_error = "cannot create an entity with duplicate key";            throw new exception($this->last_error, debug_backtrace(), $key);            return(FALSE);        }        // Go        $new_entity = &new $class_name($this, $key);        // Attach it to somewhere        if($this->attach_entity_to($new_entity, $parent) !== TRUE) {            return(FALSE);        }        // Add to the big entity list        $this->list["$key"] = &$new_entity;        // Save the world state        $this->save();        // Finish        $this->flush_error();        return($new_entity);    }    /**    * Delete entity    *    * This will only work if the entity has not any child    *    * @param object $entity A reference to the entity to delete    */    function del_entity(&$entity) {        //Check input        if($this->is_entity($entity) !== TRUE) {            return(FALSE);        }        // Check if the entity have childs        if(count($entity->childs) > 0) {            $this->last_error = "cannot delete entity with childs";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Is a character managed by an user?        if(isset($entity->user)) {            $entity->user->del_character($entity);        }        // Ok, break link with the parent        $parent = $entity->parent;        if($parent->del_child($entity) !== TRUE) {            return(FALSE);        }        // Since $entity reference will be erased at the end of the function        // all references to the object will be lost        $key = $entity->key;        unset($this->list["$key"]);        // Save the world state        $this->save();        // Finish        $this->flush_error();        return(TRUE);    }    /**    * Returns a reference to certain entity    *    * Only use it with carefully. Teory says that without this method the list    * could go on.    *    * @param string $key    The key of the entity to get reference    */    function &get_entity($key, $exception=TRUE) {        if($this->key_exists($key, FALSE) == FALSE) {            $this->last_error = "not valid entity";            if($exception == TRUE) {                throw new exception($this->last_error, debug_backtrace(), $key);            }            return(FALSE);        }        if($key == $this->root_entity->get_key()) return($this->root_entity);        $this->flush_error();        return($this->list["$key"]);    }    /**    * Attach one entity as a child to another in the tree    *    * @param object $entity The entity to move (must me orphan)    * @param object $parent The parent to get this new child    */    function attach_entity_to(&$entity, &$parent) {        //Check input        if($this->is_entity($entity) !== TRUE) {            return(FALSE);        }        if($this->is_entity($parent) !== TRUE) {            return(FALSE);        }        // Deny attaching $entity if it already have a parent        if(isset($entity->parent)) {            if($this->is_entity($entity->parent, FALSE) === TRUE) {                $this->last_error = "cannot detach an entity with a defined parent";                throw new exception($this->last_error, debug_backtrace());                return(FALSE);            }        }        // Add me to my new parent's child list        if($parent->add_child($entity) !== TRUE) {            return(FALSE);        }        // Write my parent        $entity->set_parent($parent);        $this->flush_error();        return(TRUE);    }    /**    * Delete my current position and attach me to a new one    *    * Note that does not exists a "detach_entity" to avoid creating orpahned    * entities. We only accept moving or attaching newly created intityes    *    * $param object $entity Entity to move    * $param object $parent Parent to move entity to    * $param object $door   If this movement if from a door    * $param bool   $notify If we have to notify thos movement    */    function move_entity_to(&$entity, &$parent, &$old_parent=NULL, $notify=TRUE) {        //Check input        if($this->is_entity($entity) == FALSE) {            return(FALSE);        }        if($this->is_entity($parent) == FALSE) {            return(FALSE);        }                // Deny move to the same place        if($entity->parent->get_key() == $parent->get_key()) {            return(TRUE);        }        // Deny a parent moving into a child (Later it would interesting        // to let do it. Too complex right now        $parents = $this->get_all_parents($parent);        if(isset($parents[$entity->get_key()])) {            $this->last_error = "cannot move from parent to a child of his own";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Delete $entity from his parent        $old_parent = &$entity->parent;        if($old_parent->del_child($entity) == FALSE) {            return(FALSE);        }        // De-Reference his old parent        unset($entity->parent);        // Notify relation        if($notify == TRUE) {            $this->notify_relation($entity, $old_parent, FALSE, $old_parent);        }        // Attach me to my new parent        if($this->attach_entity_to($entity, $parent) === FALSE) {            // Rollback, reattach me to my old parent            $old_parent->add_child($entity);            $entity->set_parent($old_parent);            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Notify relation        if($notify == TRUE) {            $this->notify_relation($entity, $parent, TRUE, $old_parent);        }        $this->flush_error();        return(TRUE);    }    /**    * Will return a string with graphical representation of the entities    */    function print_tree(&$parent = NULL, $level = 0) {        if($this->is_entity($this->root_entity, FALSE) == FALSE) return('');        if($this->is_entity($parent, FALSE) !== TRUE) {            $parent = &$this->root_entity;        }        $spaces = '';        for($i=0;$i<$level;$i++) $spaces .= " ";        if(empty($parent->game_vars["name"])) {            $name = "_unknown";        } else {            $name = $parent->game_vars["name"];        }        $type = $this->get_type_name($parent->type);        $tree = $spaces."[".$type."] ".$name."\n";        $level++;        $c = $parent->get_childs();        foreach (array_keys($c) as $key) {            $child = & $c["$key"];            $tree .= $this->print_tree($child, $level);        }        $level--;        return($tree);    }           /**    * Will return a string with the list of relations between entities    */    function print_list() {        if($this->is_entity($this->root_entity, FALSE) == FALSE) return('');        $entity = &$this->root_entity;        $list = "--- LIST OF GAME ENTITIES ---\n";        $list.= "Entity: ".$entity->get_key()." ($this->root_name)\n";        $c = $entity->get_childs();        foreach (array_keys($c) as $key) {            $child = &$c["$key"];            $list.= "Child: ".$child->get_key()."\n";        }        foreach (array_keys($this->list) as $key) {            $entity = &$this->list["$key"];            $list.= "---\n";            $list.= "Entity: ".$entity->get_key()."\n";            if(isset($entity->game_vars)) {                foreach($entity->game_vars as $name => $value) {                    $list.= "$name = $value\n";                }            }            $parent = &$entity->parent;            if(empty($parent->game_vars["name"])) {                $name = "_unknown";            } else {                $name = $parent->game_vars["name"];            }            $list.= "Parent ".$parent->get_key()." (".$name.")\n";            $c = $entity->get_childs();            foreach(array_keys($c) as $key) {                $list.= "Child: ".$c[$key]->get_key();                if(empty($c[$key]->game_vars["name"])) {                    $name = "_unknown";                } else {                    $name = $parent->game_vars["name"];                }                $list.= " (".$name.")\n";            }        }        return($list);    }    function notify_relation(&$entity, &$parent, $type, &$old_parent = NULL) {        //Check input        if($this->is_entity($entity) == FALSE) {            return(FALSE);        }        if($this->is_entity($parent) == FALSE) {            return(FALSE);        }        // Fill array with data        $msg['xml_name'] = "relation";        $msg['id'] = $entity->key;        if($type == TRUE) {            $msg['action'] = "1";        } else {            $msg['action'] = "0";        }        if($this->is_entity($old_parent, FALSE)) {            $msg["old_parent"] = $old_parent->key;        }        // send it        $parent->send_message_to_childs($msg);        return(TRUE);    }    function send_message_to_all($msg) {        foreach(array_keys($this->list) as $key) {            if($this->list[$key]->type == entity_list::T_CHAR) {                $this->list[$key]->send_message($msg);            }        }    }}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -