Skip to content

Commit

Permalink
清理meta文件等问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
jarjin committed Mar 25, 2016
1 parent 80e6c5e commit e8e9eb4
Show file tree
Hide file tree
Showing 16 changed files with 993 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Assets/LuaFramework/ToLua/Lua/system/Time.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local scaledTime = 0
local unscaledTime = tolua.gettime()

local Time =
{
fixedDeltaTime = 0,
deltaTime = 0,
frameCount = 1,
timeScale = 1,
timeSinceLevelLoad = 0,
unscaledDeltaTime = 0,
}

local mt = {}
mt.__index = function(obj, name)
if name == "time" then
return scaledTime
elseif name == "unscaledTime" then
return tolua.gettime() - unscaledTime
else
return rawget(obj, name)
end
end

setmetatable(Time, mt)

function Time:SetDeltaTime(deltaTime, unscaledDeltaTime)
scaledTime = scaledTime + deltaTime
self.deltaTime = deltaTime
self.timeSinceLevelLoad = self.timeSinceLevelLoad + deltaTime
self.unscaledDeltaTime = unscaledDeltaTime
end

function Time:SetFixedDelta(time)
self.fixedDeltaTime = time
self.deltaTime = time
end

function Time:SetFrameCount()
self.frameCount = self.frameCount + 1
end

function Time:SetTimeScale(scale)
local lastScale = self.timeScale
self.timeScale = scale
UnityEngine.Time.timeScale = scale
return lastScale
end

return Time
4 changes: 4 additions & 0 deletions Assets/LuaFramework/ToLua/Lua/system/Time.lua.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

186 changes: 186 additions & 0 deletions Assets/LuaFramework/ToLua/Lua/system/Timer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
--------------------------------------------------------------------------------
-- Copyright (c) 2015 , 蒙占志(topameng) topameng@gmail.com
-- All rights reserved.
-- Use, modification and distribution are subject to the "MIT License"
--------------------------------------------------------------------------------

Timer =
{
time = 0,
duration = 1,
loop = 1,
running = false,
scale = false,
func = nil,
}

local mt = {}
mt.__index = Timer

--scale false 采用deltaTime计时,true 采用 unscaledDeltaTime计时
function Timer.New(func, duration, loop, scale)
local timer = {}
scale = scale or false and true
setmetatable(timer, mt)
timer:Reset(func, duration, loop, scale)
return timer
end

function Timer:Start()
self.running = true
UpdateBeat:Add(self.Update, self)
end

function Timer:Reset(func, duration, loop, scale)
self.duration = duration
self.loop = loop or 1
self.scale = scale
self.func = func
self.time = duration
self.running = false
self.count = Time.frameCount + 1
end

function Timer:Stop()
self.running = false
UpdateBeat:Remove(self.Update, self)
end

function Timer:Update()
if not self.running then
return
end

local delta = self.scale and Time.deltaTime or Time.unscaledDeltaTime
self.time = self.time - delta

if self.time <= 0 and Time.frameCount > self.count then
self.func()

if self.loop > 0 then
self.loop = self.loop - 1
self.time = self.time + self.duration
end

if self.loop == 0 then
self:Stop()
elseif self.loop < 0 then
self.time = self.time + self.duration
end
end
end

--给协同使用的帧计数timer
FrameTimer =
{
count = 1,
duration = 1,
loop = 1,
func = nil,
running = false,
}

local mt2 = {}
mt2.__index = FrameTimer

function FrameTimer.New(func, count, loop)
local timer = {}
setmetatable(timer, mt2)
timer.count = Time.frameCount + count
timer.duration = count
timer.loop = loop
timer.func = func
return timer
end

function FrameTimer:Start()
self.running = true
CoUpdateBeat:Add(self.Update, self)
end

function FrameTimer:Stop()
self.running = false
CoUpdateBeat:Remove(self.Update, self)
end

function FrameTimer:Update()
if not self.running then
return
end

if Time.frameCount >= self.count then
self.func()

if self.loop > 0 then
self.loop = self.loop - 1
end

if self.loop == 0 then
self:Stop()
else
self.count = Time.frameCount + self.duration
end
end
end

CoTimer =
{
time = 0,
duration = 1,
loop = 1,
running = false,
func = nil,
}

local mt3 = {}
mt3.__index = CoTimer

function CoTimer.New(func, duration, loop)
local timer = {}
setmetatable(timer, mt3)
timer:Reset(func, duration, loop)
return timer
end

function CoTimer:Start()
self.running = true
self.count = Time.frameCount + 1
CoUpdateBeat:Add(self.Update, self)
end

function CoTimer:Reset(func, duration, loop)
self.duration = duration
self.loop = loop or 1
self.func = func
self.time = duration
self.running = false
self.count = Time.frameCount + 1
end

function CoTimer:Stop()
self.running = false
CoUpdateBeat:Remove(self.Update, self)
end

function CoTimer:Update()
if not self.running then
return
end

if self.time <= 0 and Time.frameCount > self.count then
self.func()

if self.loop > 0 then
self.loop = self.loop - 1
self.time = self.time + self.duration
end

if self.loop == 0 then
self:Stop()
elseif self.loop < 0 then
self.time = self.time + self.duration
end
end

self.time = self.time - Time.deltaTime
end
4 changes: 4 additions & 0 deletions Assets/LuaFramework/ToLua/Lua/system/Timer.lua.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions Assets/LuaFramework/ToLua/Lua/system/coroutine.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
--------------------------------------------------------------------------------
-- Copyright (c) 2015 , 蒙占志(topameng) topameng@gmail.com
-- All rights reserved.
-- Use, modification and distribution are subject to the "MIT License"
--------------------------------------------------------------------------------

local create = coroutine.create
local running = coroutine.running
local resume = coroutine.resume
local yield = coroutine.yield
local error = error

local comap = {}
setmetatable(comap, {_mode = "kv"})

function coroutine.start(f, ...)
local co = create(f)

if running() == nil then
local flag, msg = resume(co, ...)

if not flag then
msg = debug.traceback(co, msg)
error(msg)
end
else
local args = {...}
local timer = nil

local action = function()
local flag, msg = resume(co, unpack(args))

if not flag then
timer:Stop()
msg = debug.traceback(co, msg)
error(msg)
end
end

timer = FrameTimer.New(action, 0, 1)
comap[co] = timer
timer:Start()
end

return co
end

function coroutine.wait(t, co, ...)
local args = {...}
co = co or running()
local timer = nil

local action = function()
local flag, msg = resume(co, unpack(args))

if not flag then
timer:Stop()
msg = debug.traceback(co, msg)
error(msg)
return
end
end

timer = CoTimer.New(action, t, 1)
comap[co] = timer
timer:Start()
return yield()
end

function coroutine.step(t, co, ...)
local args = {...}
co = co or running()
local timer = nil

local action = function()
local flag, msg = resume(co, unpack(args))

if not flag then
timer:Stop()
msg = debug.traceback(co, msg)
error(msg)
return
end
end

timer = FrameTimer.New(action, t or 1, 1)
comap[co] = timer
timer:Start()
return yield()
end

function coroutine.www(www, co)
co = co or running()
local timer = nil

local action = function()
if not www.isDone then
return
end

timer:Stop()
local flag, msg = resume(co)

if not flag then
msg = debug.traceback(co, msg)
error(msg)
return
end
end

timer = FrameTimer.New(action, 1, -1)
comap[co] = timer
timer:Start()
return yield()
end

function coroutine.stop(co)
local timer = comap[co]

if timer ~= nil then
comap[co] = nil
timer:Stop()
end
end
4 changes: 4 additions & 0 deletions Assets/LuaFramework/ToLua/Lua/system/coroutine.lua.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e8e9eb4

Please sign in to comment.