• <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 )
            国产精品亚洲综合久久| 午夜人妻久久久久久久久| 91久久精品91久久性色| 国产91久久精品一区二区| 日本久久久久久中文字幕| 久久综合色区| 亚洲国产另类久久久精品小说 | 色综合久久综精品| 久久影院亚洲一区| 久久er99热精品一区二区| 国产精品日韩深夜福利久久| 狠狠色丁香久久婷婷综合_中| 久久亚洲精品无码AV红樱桃| 久久99热这里只有精品国产| 久久精品国产亚洲av麻豆色欲| 久久激情亚洲精品无码?V| 欧美熟妇另类久久久久久不卡| 97超级碰碰碰碰久久久久| 日产精品久久久一区二区| 久久精品成人欧美大片| 久久精品国产亚洲av水果派 | 久久久国产视频| 国产免费久久精品丫丫| 精品久久久久久国产| 无码国内精品久久人妻蜜桃| 亚洲一级Av无码毛片久久精品| 嫩草影院久久99| 久久不射电影网| 72种姿势欧美久久久久大黄蕉| 中文字幕日本人妻久久久免费 | 久久婷婷激情综合色综合俺也去 | 久久99久国产麻精品66| 热综合一本伊人久久精品| 国产精自产拍久久久久久蜜| 久久伊人精品青青草原高清| 人妻精品久久无码专区精东影业| 久久亚洲中文字幕精品一区| 亚洲国产综合久久天堂| 亚洲欧洲久久久精品| 一级a性色生活片久久无少妇一级婬片免费放| 久久天堂电影网|