• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 311, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            class.lua實現了在Lua中創建類的模擬,非常方便。class.lua參考自http://lua-users.org/wiki/SimpleLuaClasses

            1 -- class.lua
            2 -- Compatible with Lua 5.1 (not 5.0).
            3
            4 function class(base, init)
            5 local c = {} -- a new class instance
            6 if not init and type(base) == 'function' then
            7 init = base
            8 base = nil
            9 elseif type(base) == 'table' then
            10 -- our new class is a shallow copy of the base class!
            11 for i,v in pairs(base) do
            12 c[i] = v
            13 end
            14 c._base = base
            15 end
            16 -- the class will be the metatable for all its objects,
            17 -- and they will look up their methods in it.
            18 c.__index = c
            19
            20 -- expose a constructor which can be called by <classname>(<args>)
            21 local mt = {}
            22 mt.__call = function(class_tbl, ...)
            23 local obj = {}
            24 setmetatable(obj,c)
            25
            26 -- below 2 lines are updated based on the Comments from 'http://lua-users.org/wiki/SimpleLuaClasses'
            27 -- if init then
            28 -- init(obj,...)
            29 if class_tbl.init then
            30 class_tbl.init(obj,...)
            31 else
            32 -- make sure that any stuff from the base class is initialized!
            33 if base and base.init then
            34 base.init(obj, ...)
            35 end
            36 end
            37 return obj
            38 end
            39 c.init = init
            40 c.is_a = function(self, klass)
            41 local m = getmetatable(self)
            42 while m do
            43 if m == klass then return true end
            44 m = m._base
            45 end
            46 return false
            47 end
            48 setmetatable(c, mt)
            49 return c
            50 end

            State基類,包含三個stub函數,enter()和exit()分別在進入和退出state時被執行,onUpdate()函數將會在state被激活時的每幀被執行。

            1 require "class"
            2
            3 State = class()
            4
            5 function State:init( name )
            6 self.name = name
            7 end
            8
            9 function State:enter()
            10 end
            11
            12 function State:onUpdate()
            13 end
            14
            15 function State:exit()
            16 end

            StateMachine類,該類集成了Moai的MOAIThread類。MOAIThread類似于Lua中的coroutine,但是在Moai中被yield的MOAIThread,會在game loop的每幀中被自動resume,見StateMachine:updateState函數,利用此特點,來實現每幀執行State:onUpdate函數。

            1 require "State"
            2
            3 StateMachine = class()
            4
            5 function StateMachine:init()
            6 self.currentState = nil
            7 self.lastState = nil
            8 end
            9
            10 function StateMachine:run()
            11 if ( self.mainThread == nil )
            12 then
            13 self.mainThread = MOAIThread.new()
            14 self.mainThread:run( self.updateState, self )
            15 end
            16 end
            17
            18 function StateMachine:stop()
            19 if ( self.mainThread )
            20 then
            21 self.mainThread:stop()
            22 end
            23 end
            24
            25 function StateMachine:setCurrentState( state )
            26 if ( state and state:is_a( State ) )
            27 then
            28 if ( state == self.currentState )
            29 then
            30 print( "WARNING @ StateMachine::setCurrentState - " ..
            31 "var state [" .. state.name .. "] is the same as current state" )
            32 return
            33 end
            34 self.lastState = self.currentState
            35 self.currentState = state
            36 if ( self.lastState )
            37 then
            38 print( "exiting state [" .. self.lastState.name .. "]" )
            39 self.lastState:exit()
            40 end
            41 print( "entering state [" .. self.currentState.name .. "]" )
            42 self.currentState:enter()
            43 else
            44 print( "ERROR @ StateMachine::setCurrentState - " ..
            45 "var [state] is not a class type of State" )
            46 end
            47 end
            48
            49 function StateMachine:updateState()
            50 while ( true )
            51 do
            52 if ( self.currentState ~= nil )
            53 then
            54 self.currentState:onUpdate()
            55 end
            56 coroutine.yield()
            57 end
            58 end

            如何利用State和StateMachine類的示例,首先定義兩個state。
            SampleState.lua

            1 require "State"
            2
            3 State1 = class( State )
            4
            5 function State1:init()
            6 State.init( self, "State1" )
            7 end
            8
            9 function State1:enter()
            10 self.i = 0
            11 end
            12
            13 function State1:exit()
            14 self.i = 0
            15 end
            16
            17 function State1:onUpdate()
            18 print( self.name .. " is updated" )
            19 self.i = self.i + 1
            20 print( "self.i=" .. self.i )
            21 if ( self.i == 10 )
            22 then
            23 print( state2 )
            24 SM:setCurrentState( state2 )
            25 self.i = 0
            26 end
            27 end
            28
            29 -----------------------
            30
            31 State2 = class( State )
            32
            33 function State2:init()
            34 State.init( self, "State2" )
            35 end
            36
            37 function State2:onUpdate()
            38 print( "State2 is updated" )
            39 end

            test.lua

            1 require "StateMachine"
            2 require "SampleState"
            3
            4 SM = StateMachine()
            5 SM:run()
            6 state1 = State1()
            7 state2 = State2()
            8 SM:setCurrentState( state1 )
            久久国产福利免费| 久久成人国产精品免费软件| 热re99久久精品国99热| 久久99精品久久久久久hb无码| a高清免费毛片久久| 国产毛片久久久久久国产毛片| 亚洲精品无码久久毛片| 国产亚洲婷婷香蕉久久精品| 久久av免费天堂小草播放| 久久人妻少妇嫩草AV蜜桃| 国产精品久久成人影院| 欧美伊人久久大香线蕉综合| 国产成年无码久久久久毛片| 久久久久综合国产欧美一区二区| 久久精品无码专区免费东京热| 99久久亚洲综合精品网站| 午夜天堂精品久久久久| 久久人人爽人人爽人人片AV麻豆| 久久精品人人做人人妻人人玩| 亚洲国产成人久久笫一页| 久久精品国产99国产电影网| 国产69精品久久久久观看软件| 国产99久久久久久免费看| 国产美女久久精品香蕉69| 久久人与动人物a级毛片| 色8激情欧美成人久久综合电| 伊人久久免费视频| 日本精品久久久中文字幕| 国内精品九九久久久精品| 日本欧美久久久久免费播放网| 久久久一本精品99久久精品88| 久久亚洲中文字幕精品一区| 久久er国产精品免费观看8| 国产成人99久久亚洲综合精品| 精品精品国产自在久久高清| 国产精品一久久香蕉产线看| 国产精品无码久久久久久| 久久66热人妻偷产精品9| 91精品国产综合久久精品| 亚洲国产精品无码久久98| 久久66热人妻偷产精品9|