• <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 )
            国产精品久久久久久久午夜片| 亚洲国产一成久久精品国产成人综合 | 久久精品亚洲精品国产欧美| 欧美麻豆久久久久久中文| 久久亚洲国产精品123区| 蜜桃麻豆WWW久久囤产精品| 久久国产色AV免费看| 久久99精品久久久久久9蜜桃| 怡红院日本一道日本久久 | 久久偷看各类wc女厕嘘嘘| 国产精品青草久久久久婷婷| 久久久久亚洲av毛片大| 久久99亚洲网美利坚合众国| 精品熟女少妇aⅴ免费久久| 中文字幕无码精品亚洲资源网久久| 国产精品伊人久久伊人电影| 久久综合九色综合网站| 国产成人无码精品久久久免费| 一本久久a久久精品vr综合| 久久国产成人| 精品国产91久久久久久久| 三级三级久久三级久久| 精品久久久久久无码国产| 久久婷婷五月综合色高清| 国产一区二区久久久| 久久嫩草影院免费看夜色| 欧美久久综合性欧美| 日韩精品无码久久久久久| 亚洲伊人久久综合中文成人网| 国产一区二区精品久久岳| 亚洲精品高清久久| 性高湖久久久久久久久| 久久福利资源国产精品999| 久久综合给合综合久久| 91精品国产91久久久久久| 国产精品久久一区二区三区| 婷婷久久久亚洲欧洲日产国码AV| 久久精品中文字幕一区| 久久狠狠爱亚洲综合影院| 久久亚洲精品无码aⅴ大香| 久久精品国产亚洲AV影院|