/// <summary>
    /// 创建自定义的simpleBullet
    /// <para>customizedName 自定义的子弹名称</para>
    /// <para>float posX</para>
    /// <para>float posY</para>
    /// <para>args...</para>
    /// </summary>
    /// <param name="luaState"></param>
    /// <returns></returns>
    public static int CreateCustomizedBullet(ILuaState luaState)
    {
        int               numArgs        = luaState.GetTop() - 3;
        string            customizedName = luaState.ToString(-3 - numArgs);
        float             posX           = (float)luaState.ToNumber(-2 - numArgs);
        float             posY           = (float)luaState.ToNumber(-1 - numArgs);
        EnemySimpleBullet bullet         = ObjectsPool.GetInstance().CreateBullet(BulletType.Enemy_Simple) as EnemySimpleBullet;

        bullet.SetPosition(posX, posY);
        // 设置自定义的数据
        BCCustomizedTask bc = bullet.AddOrGetComponent <BCCustomizedTask>();
        int funcRef         = InterpreterManager.GetInstance().GetBulletInitFuncRef(customizedName);

        luaState.RawGetI(LuaDef.LUA_REGISTRYINDEX, funcRef);
        if (!luaState.IsFunction(-1))
        {
            Logger.LogError("InitFuncRef of " + customizedName + " is not point to a function");
        }
        luaState.PushLightUserData(bullet);
        for (int i = 0; i < numArgs; i++)
        {
            luaState.PushValue(-2 - numArgs);
        }
        luaState.Call(numArgs + 1, 0);
        luaState.PushLightUserData(bullet);
        return(1);
    }
Exemplo n.º 2
0
    public void AddTask(Task task)
    {
        BCCustomizedTask component = GetComponent <BCCustomizedTask>();

        if (component == null)
        {
            component = AddOrGetComponent <BCCustomizedTask>();
        }
        component.AddTask(task);
    }
    public static int AddBulletTask(ILuaState luaState)
    {
        EnemyBulletBase bullet  = luaState.ToUserData(-2) as EnemyBulletBase;
        int             funcRef = InterpreterManager.GetInstance().RefLuaFunction(luaState);

        luaState.Pop(1);
        Task task = ObjectsPool.GetInstance().GetPoolClassAtPool <Task>();

        task.funcRef  = funcRef;
        task.isFinish = false;
        task.luaState = null;
        BCCustomizedTask bc = bullet.GetComponent <BCCustomizedTask>();

        bc.AddTask(task);
        return(0);
    }
    /// <summary>
    /// 创建自定义的simpleBullet
    /// <para>customizedName 自定义的子弹名称</para>
    /// <para>id 默认的id</para>
    /// <para>float posX</para>
    /// <para>float posY</para>
    /// </summary>
    /// <param name="luaState"></param>
    /// <returns></returns>
    public static int CreateCustomizedBullet2(ILuaState luaState)
    {
        int    numArgs        = luaState.ToInteger(-1);
        string customizedName = luaState.ToString(-5 - numArgs);
        string sysId;

        if (luaState.Type(-4 - numArgs) == LuaType.LUA_TNUMBER)
        {
            sysId = luaState.ToNumber(-4 - numArgs).ToString();
        }
        else
        {
            sysId = luaState.ToString(-4 - numArgs);
        }
        float posX = (float)luaState.ToNumber(-3 - numArgs);
        float posY = (float)luaState.ToNumber(-2 - numArgs);

        luaState.Pop(1);
        EnemySimpleBullet bullet = ObjectsPool.GetInstance().CreateBullet(BulletType.Enemy_Simple) as EnemySimpleBullet;

        bullet.SetStyleById(sysId);
        bullet.SetPosition(posX, posY);
        // 设置自定义的数据
        BCCustomizedTask bc = bullet.AddOrGetComponent <BCCustomizedTask>();
        int funcRef         = InterpreterManager.GetInstance().GetBulletInitFuncRef(customizedName);

        luaState.RawGetI(LuaDef.LUA_REGISTRYINDEX, funcRef);
        if (!luaState.IsFunction(-1))
        {
            Logger.LogError("InitFuncRef of " + customizedName + " is not point to a function");
        }
        luaState.PushLightUserData(bullet);
        luaState.Replace(-3 - numArgs);
        luaState.Replace(-3 - numArgs);
        luaState.Call(numArgs + 1, 0);
        // 弹出剩余两个参数
        luaState.Pop(2);
        luaState.PushLightUserData(bullet);
        return(1);
    }