Adding lua scripts

This commit is contained in:
Don Honerbrink
2015-12-11 16:17:23 -06:00
parent a1c5b78dd2
commit b3f5b89b04
5 changed files with 379 additions and 0 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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