• <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 )
            久久精品人人槡人妻人人玩AV | 久久久久久国产精品免费免费| 亚洲精品高清久久| 久久精品亚洲男人的天堂| 精品国产日韩久久亚洲| 久久国产精品无码一区二区三区| 久久精品无码一区二区三区| 午夜福利91久久福利| 久久99国产综合精品| 久久伊人中文无码| 国产精品免费福利久久| 久久青青草原亚洲av无码| 国产产无码乱码精品久久鸭| 久久久这里有精品中文字幕| 久久人人爽人人爽人人AV东京热 | 国内精品九九久久精品| 日本精品久久久久中文字幕| 久久亚洲熟女cc98cm| 中文字幕久久欲求不满| 久久棈精品久久久久久噜噜| 久久久99精品成人片中文字幕| 久久国产色AV免费观看| 久久精品国产99国产精品导航| 精品久久久久一区二区三区| 国产亚洲综合久久系列| 午夜精品久久久久久毛片| 久久一区二区免费播放| 久久成人18免费网站| 99久久国产免费福利| 欧美日韩中文字幕久久伊人| 996久久国产精品线观看| 久久无码人妻一区二区三区| 国产亚洲美女精品久久久2020| 欧美久久久久久| 久久影院亚洲一区| 久久亚洲精品无码播放| 性高朝久久久久久久久久| 久久免费99精品国产自在现线| 久久av免费天堂小草播放| 国产亚州精品女人久久久久久 | 伊人色综合久久|