-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
993 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.