From b3f5b89b04b269516ff648b8f175d3f9e74656f2 Mon Sep 17 00:00:00 2001 From: Don Honerbrink Date: Fri, 11 Dec 2015 16:17:23 -0600 Subject: [PATCH] Adding lua scripts --- Package/Environment/Common/lua/Animate.lua | 69 ++++++ Package/Environment/Common/lua/Component.lua | 24 ++ Package/Environment/Common/lua/Event.lua | 32 +++ Package/Environment/Common/lua/Image.lua | 25 ++ Package/Environment/Common/lua/Tween.lua | 229 +++++++++++++++++++ 5 files changed, 379 insertions(+) create mode 100644 Package/Environment/Common/lua/Animate.lua create mode 100644 Package/Environment/Common/lua/Component.lua create mode 100644 Package/Environment/Common/lua/Event.lua create mode 100644 Package/Environment/Common/lua/Image.lua create mode 100644 Package/Environment/Common/lua/Tween.lua diff --git a/Package/Environment/Common/lua/Animate.lua b/Package/Environment/Common/lua/Animate.lua new file mode 100644 index 0000000..80c4373 --- /dev/null +++ b/Package/Environment/Common/lua/Animate.lua @@ -0,0 +1,69 @@ +event = require(Event) +tween = require(Tween) + +local Animate = {} +Animate.__index = Animate + +function Animate.new() + instance = { + component = nil, + loop = false, + animationIndex, + elapsedTime = 0 + startInfo = {} + animations = [] + } + setmetatable(instance, Animate) + + event.register("update", Animate, Animate.update) + + return instance +end + +function Animate.add(time, tweenInfo) + self.animations[#self.animations+1] = {duration=time, info=tweenInfo} +end + +function Animate:start(component, loop) + self.component = component + self.loop = loop + self.elapsedTime = 0 + self.startInfo = component:getProperties() + +end + +end + +function Animate:update(dt) + if self.animationIndex > #self.animations and not self.loop then + return + end + + self.elapsedTime = self.elapsedTime + dt + animation = self.animations[animationIndex] + + if animation.duration > self.elapsedTime then + self.animationIndex = self.animationIndex + 1 + self.elapsedTime = 0 + self.startInfo = component:getProperties() + + if self.animationIndex > #self.animations and self.loop then + self.animationIndex = 0 + end + end + curr = { + x = tween.calculate(animation.x, startInfo.x, duration, elapsedTime), + y = tween.calculate(animation.y, startInfo.y, duration, elapsedTime), + width = tween.calculate(animation.width, startInfo.width, duration, elapsedTime), + height = tween.calculate(animation.height, startInfo.height, duration, elapsedTime), + transparency = tween.calculate(animation.transparency, startInfo.transparency, duration, elapsedTime), + rotate = Tween.calculate(animation.rotate, startInfo.rotate, duration, elapsedTime) + } + + component.setPosition(componentId, curr.x, curr.y) + component.setDimensions(componentId, curr.width, curr.height) + component.setTransparency(componentId, curr.transparency) + component.setRotate(componentId, curr.rotate) +end + +return Animate diff --git a/Package/Environment/Common/lua/Component.lua b/Package/Environment/Common/lua/Component.lua new file mode 100644 index 0000000..f5ebb4b --- /dev/null +++ b/Package/Environment/Common/lua/Component.lua @@ -0,0 +1,24 @@ +local Component = {} +Component.__index = Component + +function Component.new() + instance = { + x = 0, + y = 0, + width = 0, + height = 0, + transparency = 1, + rotate = 0, + originalWidth = 0, + originalHeight = 0 + } + + setmetatable(instance, Image) + return instance +end + +function Component:getProperties() + return self +end + + diff --git a/Package/Environment/Common/lua/Event.lua b/Package/Environment/Common/lua/Event.lua new file mode 100644 index 0000000..557073a --- /dev/null +++ b/Package/Environment/Common/lua/Event.lua @@ -0,0 +1,32 @@ +local Event = {} +Event.__index = Event + +function Event.new() + local instance = { + events = {} + } + + setmetatable(instance, EventManager) + + return instance +end + +function Event:register(event, cbself, callback) + local listeners = self.events[event] + if listeners ~= nil then + listeners[#listeners + 1] = callback + else + self.events[event] = {{cb=callback, s=cbself}} + end +end + +function Event:trigger(event, ...) + local listeners = self.events[event] + if listeners ~= nil then + for i = 1, #listeners do + listeners[i].cb(listeners[i].s, ...) + end + end +end + +return Event diff --git a/Package/Environment/Common/lua/Image.lua b/Package/Environment/Common/lua/Image.lua new file mode 100644 index 0000000..97a1eef --- /dev/null +++ b/Package/Environment/Common/lua/Image.lua @@ -0,0 +1,25 @@ +Component = require("Component") +Event = require("Event") + +local Image = {} +Image.__index = Image + +function Image.new() + instance = {id = 0} + setmetatable(instance, Image) + + Event.register("draw", Image, Image.draw) + return instance +end + +function Image:load(name) + self.id = image.load(name) + self.originalWidth, self.originalHeight = image.getDimensions(self.id) +end + +function Image:draw() + image.draw(component.getProperties()) +end + +setmetatable(Image, {__index = Component}) +return Image diff --git a/Package/Environment/Common/lua/Tween.lua b/Package/Environment/Common/lua/Tween.lua new file mode 100644 index 0000000..66f2e32 --- /dev/null +++ b/Package/Environment/Common/lua/Tween.lua @@ -0,0 +1,229 @@ +local Tween = {} +Tween.__index = Tween + +function Tween.new() + instance = { + tweens = {} + } + + setmetatable(instance, Tween) + + instance.tweens = { + easeInQuadratic = Tween.easeInQuadratic, + easeOutQuadratic = Tween.easeOutQuadratic, + easeInOutQuadratic = Tween.easeInOutQuadratic, + easeInCubic = Tween.easeInCubic, + easeOutCubic = Tween.easeOutCubic, + easeInOutCubic = Tween.easeInOutCubic, + easeInQuartic = Tween.easeInQuartic, + easeOutQuartic = Tween.easeOutQuartic, + easeInOutQuartic = Tween.easeInOutQuartic, + easeInQuintic = Tween.easeInQuintic, + easeOutQuintic = Tween.easeOutQuintic, + easeInOutQuintic = Tween.easeInOutQuintic, + easeInSine = Tween.easeInSinev, + easeOutSine = Tween.easeOutSine, + easeInOutSine = Tween.easeInOutSine, + easeInExponential = Tween.easeInExponential, + easeOutExponential = Tween.easeOutExponential, + easeInOutExponential = Tween.easeInOutExponential, + easeInCircular = Tween.easeInCircular, + easeOutCircular = Tween.easeOutCircular, + easeInOutCircular = Tween.easeInOutCircular, + linear = Tween.linear + } + + return instance +end + +function Tween.calculate(type, startval, endval, duration, elapsedTime) + a = startval; + b = endval - startval; + result = tweens[type](elapsedTime, duration, a, b) +end + + +function Tween.linear(t, d, b, c) + if d == 0 then + return b + end + + return c*t/d + b +end + +function Tween.easeInQuadratic(t, d, b, c) + if d == 0 then + return b + end + + t = t/d + return c*t*t + b +end + +function Tween.easeOutQuadratic(t, d, b, c) + if d == 0 then + return b + end + + t = t/d + return -1 * c * t*(t-2) + b +end + +function Tween.easeInOutQuadratic(t, d, b, c) + if d == 0 then + return b + end + + t = t/(d/2) + if (t < 1) then + return c/2*t*t + b + end + t = t - 1 + return -1 * c/2 * (t*(t-2) - 1) + b +end + +function Tween.easeInCubic(t, d, b, c) + if d == 0 then + return b + end + + t = t/d + return c*t*t*t + b +end + +function Tween.easeOutCubic(t, d, b, c) + if d == 0 then + return b + end + + t = t/d + t = t - 1 + return c*(t*t*t + 1) + b +end + +function Tween.easeInOutCubic(t, d, b, c) + if d == 0 then + return b + end + + t = t/(d/2) + if t < 1 then + return c/2*t*t*t + b + end + + t = t - 2 + return c/2*(t*t*t + 2) + b +end + +function Tween.easeInQuartic(t, d, b, c) + if d == 0 then + return b + end + + t = t/d + return c*t*t*t*t + b +end + +function easeOutQuartic(t, d, b, c) + if d == 0 then + return b + end + + t = t/d + t = t - 1 + return -1 * c * (t*t*t*t - 1) + b +end + +function Tween.easeInOutQuartic(t, d, b, c) + if d == 0 then + return b + end + + t = t/(d/2) + if t < 1 then + return c/2*t*t*t*t + b + end + + t = t - 2 + return -1 * c/2 * (t*t*t*t - 2) + b +end + +function Tween.easeInQuintic(t, d, b, c) + if d == 0 then + return b + end + + t = t/d + return c*t*t*t*t*t + b +end + + +function Tween.easeOutQuintic(t, d, b, c) + if d == 0 then return b end + t = t/d + t = t - 1 + return c*(t*t*t*t*t + 1) + b +end + +function Tween.easeInOutQuintic(t, d, b, c) + if d == 0 then + return b + end + + t = t / (d/2) + if t < 1 then + return c/2*t*t*t*t*t + b + end + + t = t - 2 + return c/2*(t*t*t*t*t + 2) + b +end + +function Tween.easeInSine(t, d, b, c) + return -1 * c * cos(t/d * (math.pi / 2)) + c + b +end + +function Tween.easeOutSine(t, d, b, c) + return c * math.sin(t/d * (math.pi / 2)) + b +end + +function Tween.easeInOutSine(t, d, b, c) + return -1 * c/2 * (math.cos( math.pi * t/d) - 1) + b +end + +function Tween.easeInExponential(t, d, b, c) + return c * math.pow( 2, 10 * (t/d - 1) ) + b +end + +function Tween.easeOutExponential(t, d, b, c) + return c * ( - math.pow( 2, -10 * t/d ) + 1 ) + b +end + +function Tween.easeInOutExponential(t, d, b, c) + t = t/(d/2) + if (t < 1) return c/2 * math.pow( 2, 10 * (t - 1) ) + b + t = t - 1 + return c/2 * ( -1* math.pow( 2, -10 * t) + 2 ) + b +end + +function Tween.easeInCircular(t, d, b, c) + t = t/d + return -1 * c * (sqrt(1 - t*t) - 1) + b +end + + +function Tween.easeOutCircular(t, d, b, c) + t = t/d + t = t - 1 + return c * sqrt(1 - t*t) + b +end + +function Tween.easeInOutCircular(t, d, b, c) + t = t/(d/2) + if (t < 1) return -c/2 * (sqrt(1 - t*t) - 1) + b + t = t - 2 + return c/2 * (sqrt(1 - t*t) + 1) + b +end + + +