コード例 #1
0
ファイル: Game.cs プロジェクト: weimingtom/LuaSTGSharp
    // Use this for initialization
    public void Start()
    {
        Debug.Assert(CurrentStatus == Status.NotInitialized);
        Debug.Assert(GameInstance == null);
        DontDestroyOnLoad(gameObject);
        GameInstance  = this;
        CurrentStatus = Status.Initializing;

        GlobalUI.OnAcquireJson("{}");

        var audioSources = GetComponents <AudioSource>();

        MusicAudioSource = audioSources[0];
        SoundAudioSource = audioSources[1];

        GlobalImageScaleFactor  = 1.5f;
        GlobalMusicVolume       = 1f;
        GlobalSoundEffectVolume = 1f;

        GameLogger      = new Logger(new GameLogHandler(LogFilePath));
        ResourceManager = new ResourceManager();
        ResourceManager.AddResourceDataProvider(new LocalFileProvider(DataPath));

        Bound           = gameObject.AddComponent <BoxCollider>();
        Bound.isTrigger = true;

        LuaVM = new LuaSvr();
        LuaVM.init(null, () =>
        {
            var l = LuaVM.luaState.L;

            LuaDLL.lua_pushglobaltable(l);
            LuaTable globalTable;
            LuaObject.checkType(l, -1, out globalTable);
            if (globalTable == null)
            {
                throw new Exception("Cannot get global table");
            }
            GlobalTable = globalTable;
            LuaDLL.lua_pop(l, 1);

            LuaDLL.lua_gc(l, LuaGCOptions.LUA_GCSTOP, 0);

            LuaDLL.luaL_openlibs(l);

            BuiltinFunctions.Register(l);
            BuiltinFunctions.InitMetaTable(l);

            LuaDLL.lua_gc(l, LuaGCOptions.LUA_GCRESTART, -1);

            ResourceManager.FindResourceAs <ResLuaScript>("launch").Execute(LuaVM.luaState);
            ResourceManager.FindResourceAs <ResLuaScript>("core.lua").Execute(LuaVM.luaState);

            var gameInit = globalTable["GameInit"] as LuaFunction;
            if (gameInit == null)
            {
                throw new Exception("GameInit does not exist or is not a function");
            }

            _gameExitFunc = globalTable["GameExit"] as LuaFunction;
            if (_gameExitFunc == null)
            {
                throw new Exception("GameExit does not exist or is not a function");
            }

            _focusLoseFunc = globalTable["FocusLoseFunc"] as LuaFunction;
            if (_focusLoseFunc == null)
            {
                throw new Exception("FocusLoseFunc does not exist or is not a function");
            }

            _focusGainFunc = globalTable["FocusGainFunc"] as LuaFunction;
            if (_focusGainFunc == null)
            {
                throw new Exception("FocusGainFunc does not exist or is not a function");
            }

            _frameFunc = globalTable["FrameFunc"] as LuaFunction;
            if (_frameFunc == null)
            {
                throw new Exception("FrameFunc does not exist or is not a function");
            }

            _renderFunc = globalTable["RenderFunc"] as LuaFunction;
            if (_renderFunc == null)
            {
                throw new Exception("RenderFunc does not exist or is not a function");
            }

            gameInit.call();

            CurrentStatus = Status.Initialized;
        });
    }