/// <summary>
    /// 创建Entity
    /// </summary>
    /// <typeparam name="EntityType">Entity类型</typeparam>
    /// <typeparam name="RespondType">服务器返回协议类型</typeparam>
    /// <typeparam name="VOType">数据类型</typeparam>
    /// <param name="uId">服务器ID</param>
    /// <param name="tId">模板ID</param>
    /// <param name="respond">服务器返回协议</param>
    /// <returns></returns>
    public EntityType CreateEntityByRespond <EntityType, RespondType>(uint uId, uint tId, uint itemId, ulong playerId, RespondType respond) where EntityType : GameEntity <RespondType> where RespondType : KProtoBuf
    {
        ulong entityId = uId;

        if (respond == null)
        {
            throw new Exception("respond is null");
        }

        GameObject gameObject = new GameObject(typeof(EntityType).Name + "_" + entityId);

        gameObject.transform.SetParent(m_EntityRoot);

        ComponentEventDispatcher componentEventDispatcher = new ComponentEventDispatcher();

        EntityType entity = gameObject.AddComponent <EntityType>();

        entity.m_Index = m_IdCreator.Next();
        entity.SetEntityId(entityId);
        entity.SetTemplateID(tId);
        entity.SetItemID(itemId);
        entity.SetSendEventToEntity(this);
        entity.SetEntityEventDispatcher(componentEventDispatcher);
        entity.SetComponentsFactory(m_ComponentsFactory);
        entity.SetPlayerId(playerId);

        entity.InitializeByRespond(respond);
        entity.InitializeComponents();
        entity.AfterInitializeComponents();

        AddEntity <EntityType, RespondType>(entityId, entity);

        return(entity);
    }
    /// <summary>
    /// 创建子弹Entity
    /// </summary>
    /// <typeparam name="EntityType">Entity类型</typeparam>
    /// <param name="tId">模板ID</param>
    /// <returns></returns>
    public EntityType CreateBulletEntity <EntityType, RespondType>(RespondType respond) where EntityType : GameEntity <RespondType> where RespondType : KProtoBuf
    {
        ulong entityId = GetClientId();

        GameObject gameObject = new GameObject(typeof(EntityType).Name + "_" + entityId);

        gameObject.transform.SetParent(m_EntityRoot, false);
        ComponentEventDispatcher componentEventDispatcher = new ComponentEventDispatcher();

        EntityType entity = gameObject.AddComponent <EntityType>();

        entity.m_Index  = m_IdCreator.Next();
        gameObject.name = string.Format("{0}_{1}", typeof(EntityType).Name, entity.m_Index);
        entity.SetEntityId(entityId);
        entity.SetSendEventToEntity(this);
        entity.SetEntityEventDispatcher(componentEventDispatcher);
        entity.SetComponentsFactory(m_ComponentsFactory);

        entity.InitializeByRespond(respond);
        try
        {
            entity.InitializeComponents();
        }
        catch (Exception ex)
        {
            Debug.LogError("InitializeComponents Error " + ex);
            throw;
        }
        entity.AfterInitializeComponents();

        AddEntity <EntityType, RespondType>(entityId, entity);

        return(entity);
    }
    /// <summary>
    /// 创建Entity
    /// </summary>
    /// <typeparam name="EntityType">Entity类型</typeparam>
    /// <param name="tId">模板ID</param>
    /// <returns></returns>
    public EntityType CreateEntity <EntityType>(GameObject gameObject = null) where EntityType : GameEntity <KProtoBuf>
    {
        ulong entityId = GetClientId();

        if (gameObject == null)
        {
            gameObject = new GameObject(typeof(EntityType).Name + "_" + entityId);
            gameObject.transform.SetParent(m_EntityRoot);
        }

        ComponentEventDispatcher componentEventDispatcher = new ComponentEventDispatcher();

        EntityType entity = gameObject.AddComponent <EntityType>();

        entity.m_Index = m_IdCreator.Next();
        entity.SetEntityId(entityId);
        entity.SetSendEventToEntity(this);
        entity.SetEntityEventDispatcher(componentEventDispatcher);
        entity.SetComponentsFactory(m_ComponentsFactory);

        entity.Initialize();
        entity.InitializeComponents();
        entity.AfterInitializeComponents();

        AddEntity <EntityType, KProtoBuf>(entityId, entity);

        return(entity);
    }
Exemple #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="entityEventDispatcher"></param>
 public void SetEntityEventDispatcher(ComponentEventDispatcher entityEventDispatcher)
 {
     m_EntityEventDispatcher = entityEventDispatcher;
 }
Exemple #5
0
    public ComponentType CreateComponent <ComponentType, PropertyType>(PropertyType property, ComponentEventDispatcher entityEventDispatcher, ISendEventToEntity sendEventToEntity) where ComponentType : EntityComponent <PropertyType>, new()
    {
        ComponentType component = new ComponentType();

        component.SetEntityEventDispatcher(entityEventDispatcher);
        component.SetHashCode <ComponentType>();
        component.OnInitialize(property);
        component.OnAddListener();

        return(component);
    }