• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            class.lua實現(xiàn)了在Lua中創(chuàng)建類的模擬,非常方便。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函數(shù),enter()和exit()分別在進(jìn)入和退出state時被執(zhí)行,onUpdate()函數(shù)將會在state被激活時的每幀被執(zhí)行。

            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函數(shù),利用此特點,來實現(xiàn)每幀執(zhí)行State:onUpdate函數(shù)。

            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 )
            国产精品美女久久久| 狠狠久久亚洲欧美专区 | 久久久久噜噜噜亚洲熟女综合| 三级片免费观看久久| 精品久久久久久国产潘金莲| 亚洲精品综合久久| 精品人妻伦九区久久AAA片69 | 伊人久久五月天| 99久久人人爽亚洲精品美女| 久久久久久久久久久久中文字幕| 2019久久久高清456| 亚洲&#228;v永久无码精品天堂久久| 99精品国产综合久久久久五月天| 久久久久国产精品嫩草影院| 久久久久人妻精品一区| 久久综合日本熟妇| 日韩久久无码免费毛片软件| 久久精品国产99久久无毒不卡 | 亚洲狠狠婷婷综合久久久久| 久久精品国产色蜜蜜麻豆| 亚洲伊人久久综合中文成人网| 久久99精品国产一区二区三区| 亚洲日韩中文无码久久| 久久久久亚洲AV成人网人人软件| 久久久久久久久久久久中文字幕| 色狠狠久久综合网| 思思久久99热免费精品6| 日本免费一区二区久久人人澡| 99久久www免费人成精品| 97超级碰碰碰久久久久| 日韩精品久久久肉伦网站| 国内精品人妻无码久久久影院| 麻豆av久久av盛宴av| 香蕉99久久国产综合精品宅男自| 久久国产精品免费| 久久综合中文字幕| 亚洲国产精品久久久久久| 国产亚洲婷婷香蕉久久精品| 国产精品久久久久影院嫩草| 久久夜色tv网站| 99久久精品免费看国产免费|