Created
September 8, 2014 22:18
-
-
Save bobcgausa/0f3ebd524e2a2653840c to your computer and use it in GitHub Desktop.
Corona JSON Loader/Manager
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
local json = require "json" | |
local assets={} | |
function assets.new(path) | |
local file = io.open( path, "r" ) | |
local s = file:read( "*a" ) | |
io.close( file ) | |
return json.decode( s ) | |
end --new | |
function assets.isInstance(self,name) | |
local where=string.find(self.class, ',' .. name .. ',', 1, true) | |
return where ~= nil | |
end --isInstance | |
function assets.isInstanceByName(struct,asset,name) | |
local where=string.find(struct[asset].class, ',' .. name .. ',', 1, true) | |
return where ~= nil | |
end --isInstanceByName | |
--imagesheet, paint, sound are persistent | |
function assets.get(struct, asset) | |
local o=struct[asset] | |
if o == nil then return nil end | |
if assets.isInstance(o, 'image') then | |
local t = display.newImage(o.path, 0, 0) | |
t.isVisible = false | |
return t | |
elseif assets.isInstance(o, 'text') then | |
if o.options.fontName == 'system' then | |
o.font = native.systemFont | |
elseif o.options.fontName == 'systemBold' then | |
o.font = native.systemFontBold | |
end --if | |
local t | |
if o.options['embossed'] ~= nil then | |
t = display.newEmbossedText(o.options) | |
else t = display.newText(o.options) | |
end --if | |
if o.options['fill'] ~= nil then | |
if type(o.options.fill)=='table' then | |
t:setFillColor(unpack(o.options.fill)) | |
else t.fill = struct[o.options.fill].options | |
end --if | |
else | |
t:setFillColor(1,1,1) | |
end --if | |
if o.options['embossed'] ~= nil then | |
t:setEmbossColor(o.options.embossed) | |
end --if | |
t.isVisible = false | |
return t | |
elseif assets.isInstance(o, 'imagesheet') then | |
if o.object ~= nil then return o.object end | |
local t = graphics.newImageSheet(o.path, o.options) | |
o.object = t | |
return t | |
elseif assets.isInstance(o, 'paint') then | |
if o.object ~= nil then return o.object end | |
o.object = o.options | |
elseif assets.isInstance(o, 'emitter') then | |
t = display.newEmitter(o.options) | |
t.isVisible = false | |
return t | |
elseif assets.isInstance(o, 'sound') then | |
if o.object ~= nil then return o.object end | |
local t = audio.loadSound(o.path) | |
o.object = t | |
return t | |
elseif assets.isInstance(o, 'sprite') then | |
if assets.isInstance(o, 'array') then return nil | |
else | |
local t = assets.get(struct, o.sheet) | |
if t == nil then return nil end | |
local s = display.newSprite(t, o.options) | |
s.isVisible = false | |
return s | |
end --if | |
elseif assets.isInstance(o, 'shape') then | |
if assets.isInstance(o, 'rect') then | |
t = display.newRect(o.options.x, o.options.y, o.options.width, o.options.height) | |
elseif assets.isInstance(o, 'circle') then | |
t = display.newCircle(o.options.x, o.options.y, o.options.radius) | |
elseif assets.isInstance(o, 'roundedRect') then | |
t = display.newRoundedRect(o.options.x, o.options.y, o.options.width, o.options.height, o.options.corner) | |
elseif assets.isInstance(o, 'polygon') then | |
t = display.newPolygon(o.options.x, o.options.y, o.options.points) | |
elseif assets.isInstance(o, 'line') then | |
t = display.newLine(o.options.x, o.options.y, o.options.x2, o.options.y2) | |
if o.options['points']~=nil then t:append(unpack(o.options.points)) end | |
end --if | |
if o.options['fill']~=nil then | |
if type(o.options.fill)=='table' then | |
t:setFillColor(unpack(o.options.fill)) | |
else t.fill = struct[o.options.fill].options | |
end --if | |
end --if | |
if o.options['stroke']~=nil then | |
if type(o.options.stroke)=='table' then | |
t:setStrokeColor(unpack(o.options.stroke)) | |
else t.stroke = struct[o.options.stroke].options | |
end --if | |
end --if | |
if o.options['strokeWidth']~=nil then | |
t.strokeWidth = o.options.strokeWidth | |
end --if | |
t.isVisible = false | |
return t | |
end --if | |
return nil | |
end --get | |
function assets.get2(struct, assetSet, element) | |
local o=struct[assetSet] | |
if o == nil then return nil end | |
if assets.isInstance(o, 'sprite') then | |
if assets.isInstance(o, 'array') then | |
local s = o[element] | |
local t = assets.get(struct, s.sheet) | |
if t == nil then return nil end | |
s = display.newSprite(t, s.options) | |
s.isVisible = false | |
return s | |
end --if | |
end --if | |
return nil | |
end --get2 | |
local function show(t,k) --asset structure name | |
if assets.isInstance(t[k], 'imagesheet') then | |
if t[k].object ~= nil then return t[k] end | |
t[k].object = graphics.newImageSheet(t[k].path, t[k].options) | |
elseif assets.isInstance(t[k], 'sprite') then | |
if assets.isInstance(t[k], 'array') then | |
local y=100 | |
local g=display.newGroup() | |
for i=1,#t[k].names do | |
local s = t[k][t[k].names[i]] | |
s.object = display.newSprite(g,show(t, s.sheet).object, s.options) | |
s.object.x=display.contentCenterX s.object.y=y | |
y=y+100 | |
s.object:play() --plays first option | |
end --for i | |
t[k].object = g | |
else | |
local s | |
s = display.newSprite(show(t,t[k].sheet).object, t[k].options) | |
s.x=display.contentCenterX s.y=display.contentCenterY | |
s:play() --plays first option | |
t[k].object = s | |
end --if | |
elseif assets.isInstance(t[k], 'image') then | |
t[k].object = display.newImage(t[k].path, 0, 0) | |
t[k].object.x=display.contentCenterX | |
t[k].object.y=display.contentCenterY | |
elseif assets.isInstance(t[k], 'emitter') then | |
t[k].object = display.newEmitter(t[k].options) | |
t[k].object.x=display.contentCenterX | |
t[k].object.y=display.contentCenterY+200 | |
elseif assets.isInstance(t[k], 'paint') then | |
if t[k].object ~= nil then return t[k] end | |
t[k].object = t[k].options | |
elseif assets.isInstance(t[k], 'sound') then | |
local s = audio.loadSound(t[k].path) | |
audio.play(s) | |
t[k].object = s | |
elseif assets.isInstance(t[k], 'text') then | |
if t[k].options.fontName == 'system' then | |
t[k].font = native.systemFont | |
elseif t[k].options.fontName == 'systemBold' then | |
t[k].font = native.systemFontBold | |
end --if | |
if t[k].options['embossed'] ~= nil then | |
t[k].object = display.newEmbossedText(t[k].options) | |
else t[k].object = display.newText(t[k].options) | |
end --if | |
if t[k].options['fill'] ~= nil then | |
if type(t[k].options.fill)=='table' then | |
t[k].object:setFillColor(unpack(t[k].options.fill)) | |
else t[k].object.fill = t[t[k].options.fill].options | |
end --if | |
else | |
t[k].object:setFillColor(1,1,1) | |
end | |
if t[k].options['embossed'] ~= nil then | |
t[k].object:setEmbossColor(t[k].options.embossed) | |
end --if | |
t[k].object.x=display.contentCenterX | |
t[k].object.y=display.contentCenterX | |
elseif assets.isInstance(t[k], 'shape') then | |
if assets.isInstance(t[k], 'rect') then | |
t[k].object = display.newRect(t[k].options.x, t[k].options.y, t[k].options.width, t[k].options.height) | |
elseif assets.isInstance(t[k], 'circle') then | |
t[k].object = display.newCircle(t[k].options.x, t[k].options.y, t[k].options.radius) | |
elseif assets.isInstance(t[k], 'roundedRect') then | |
t[k].object = display.newRoundedRect(t[k].options.x, t[k].options.y, t[k].options.width, t[k].options.height, t[k].options.corner) | |
elseif assets.isInstance(t[k], 'polygon') then | |
t[k].object = display.newPolygon(t[k].options.x, t[k].options.y, t[k].options.points) | |
elseif assets.isInstance(t[k], 'line') then | |
t[k].object = display.newLine(t[k].options.x, t[k].options.y, t[k].options.x2, t[k].options.y2) | |
if t[k].options['points']~=nil then t[k].object:append(unpack(t[k].options.points)) end | |
end --if | |
if t[k].options['fill']~=nil then | |
if type(t[k].options.fill)=='table' then | |
t[k].object:setFillColor(unpack(t[k].options.fill)) | |
else t[k].object.fill = t[t[k].options.fill].options | |
end --if | |
end --if | |
if t[k].options['stroke']~=nil then | |
if type(t[k].options.stroke)=='table' then | |
t[k].object:setStrokeColor(unpack(t[k].options.stroke)) | |
else t[k].object.stroke = t[t[k].options.stroke].options | |
end --if | |
end --if | |
if t[k].options['strokeWidth']~=nil then | |
t[k].object.strokeWidth = t[k].options.strokeWidth | |
end --if | |
t[k].object.x=display.contentCenterX | |
t[k].object.y=display.contentCenterX | |
end --if | |
return t[k] | |
end --show | |
function assets.show(struct) | |
local step=1 | |
local object | |
local names={} | |
local myText = display.newText( "tap screen for next item", display.contentCenterX, 30, native.systemFont, 32 ) | |
myText:setFillColor( 1, 0, 0 ) | |
local myText2 = display.newText( "", 100, 50, native.systemFont, 32 ) | |
myText2.x = display.contentCenterX | |
myText2:setFillColor( 1, 0, 0 ) | |
local function onComplete( event ) | |
local function listener( event ) | |
display.remove(myText) | |
Runtime:removeEventListener('tap', onComplete ) | |
end | |
if object ~= nil then | |
if not assets.isInstance(object, 'imagesheet') and not assets.isInstance(object, 'paint') then | |
if assets.isInstance(object, 'sound') then | |
audio.dispose(object.object) | |
else | |
if object.object~=nil then object.object:removeSelf() end | |
end | |
end | |
object.object=nil | |
object = nil | |
end --if | |
if step > #names then | |
myText.text="FINISHED." display.remove(myText2) | |
timer.performWithDelay( 3000, listener ) | |
return | |
end | |
myText2.text=names[step] | |
object=show(struct,names[step]) | |
step=step+1 | |
end | |
Runtime:addEventListener('tap', onComplete ) | |
local i=1 | |
for k,v in pairs(struct) do | |
names[i]=k | |
i=i+1 | |
end --for k,v | |
print(names) | |
end --show | |
return assets |
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
{ | |
"sheet_bat": { | |
"class": ",imagesheet,", | |
"path": "images/bat.png", | |
"options": { | |
"width": 32, "height": 48, "numFrames": 12, | |
"sheetContentWidth": 192, "sheetContentHeight": 96 | |
} | |
}, | |
"background_mountain": { | |
"class": ",image,", | |
"path": "images/exterior-parallaxBG1.png" | |
}, | |
"text_paint": { | |
"class": ",paint,", | |
"options": [0, 1, 0] | |
}, | |
"paint_gradient": { | |
"class": ",paint,", | |
"options": { | |
"type": "gradient", | |
"color1": [ 1, 0, 0.4 ], | |
"color2": [ 1, 0, 0, 0.2 ], | |
"direction": "down" | |
} | |
}, | |
"paint_image": { | |
"class": ",paint,", | |
"options": { | |
"type": "image", | |
"filename": "images/exterior-parallaxBG1.png" | |
} | |
}, | |
"sprite_bat": { | |
"class": ",sprite,array,", | |
"names": ["brown","gray"], | |
"brown": { | |
"class": ",sprite,", | |
"sheet": "sheet_bat", | |
"attributes": { | |
"color": "brown" | |
}, | |
"options": [ | |
{ | |
"name": "fly", | |
"start": 8, | |
"count": 5, | |
"time": 750 | |
}, | |
{ | |
"name": "sleep", | |
"start": 7, | |
"count": 1 | |
} | |
] | |
}, | |
"gray": { | |
"class": ",sprite,", | |
"sheet": "sheet_bat", | |
"attributes": { | |
"color": "gray" | |
}, | |
"options": [ | |
{ | |
"name": "fly", | |
"start": 2, | |
"count": 5, | |
"time": 750 | |
}, | |
{ | |
"name": "sleep", | |
"start": 1, | |
"count": 1 | |
} | |
] | |
} | |
}, | |
"text_credits": { | |
"class": ",text,", | |
"options": { | |
"text": "Bat Sprite: MoikMellah\nEnvironment art: Jetrel\nfrom opengameart.org\nunder CC-By 2.0", | |
"width": 190, | |
"height": 144, | |
"fontName": "system", | |
"fontSize": 12, | |
"fill": [1,0,0] | |
} | |
}, | |
"text_high": { | |
"class": ",text,", | |
"options": { | |
"text": "High Scores", | |
"fontName": "systemBold", | |
"fontSize": 32, | |
"fill": "text_paint" | |
} | |
}, | |
"text_high_gradient": { | |
"class": ",text,", | |
"options": { | |
"text": "High Scores", | |
"fontName": "systemBold", | |
"fontSize": 32, | |
"fill": "paint_gradient" | |
} | |
}, | |
"text_high_image": { | |
"class": ",text,", | |
"options": { | |
"text": "High Scores", | |
"fontName": "systemBold", | |
"fontSize": 56, | |
"fill": "paint_image" | |
} | |
}, | |
"text_high_embossed": { | |
"class": ",text,", | |
"options": { | |
"text": "High Scores", | |
"fontName": "systemBold", | |
"fontSize": 48, | |
"fill": [0, 0, 1], | |
"embossed": { | |
"highlight": { "r": 1, "g": 1, "b": 1 }, | |
"shadow": { "r": 0.3, "g": 0.3, "b": 0.3 } | |
} | |
} | |
}, | |
"shape_rect": { | |
"class": ",shape,rect,", | |
"options": { | |
"x": 100, | |
"y": 100, | |
"width": 200, | |
"height": 50, | |
"fill": [0, 0, 1], | |
"stroke": [1, 0, 1], | |
"strokeWidth": 4, | |
} | |
}, | |
"shape_circle": { | |
"class": ",shape,circle,", | |
"options": { | |
"x": 100, | |
"y": 100, | |
"radius": 40, | |
"fill": [0, 1, 1], | |
"stroke": [1, 0.2, 1], | |
"strokeWidth": 4, | |
} | |
}, | |
"shape_polygon": { | |
"class": ",shape,polygon,", | |
"options": { | |
"x": 0, | |
"y": 0, | |
"points": [0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35,], | |
"fill": "paint_gradient", | |
"stroke": [1, 0.2, 1], | |
"strokeWidth": 4, | |
} | |
}, | |
"shape_line": { | |
"class": ",shape,line,", | |
"options": { | |
"x": 200, | |
"y": 90, | |
"x2": 227, | |
"y2": 165, | |
"points": [305,165, 243,216, 265,290, 200,245, 135,290, 157,215, 95,165, 173,165, 200,90], | |
"stroke": [1, 0.2, 1], | |
"strokeWidth": 4, | |
} | |
}, | |
"sound_pop": { | |
"class": ",sound,", | |
"path": "sounds/pop.mp3" | |
}, | |
"heart04_emitter": { | |
"class": ",emitter,", | |
"options": { | |
"startColorAlpha":1,"startParticleSizeVariance":20,"startColorGreen":1,"rotatePerSecond":360,"radialAcceleration":0,"yCoordFlipped":-1,"emitterType":0,"blendFuncSource":770,"finishColorVarianceAlpha":1,"rotationEnd":0,"startColorVarianceBlue":0,"rotatePerSecondVariance":0,"particleLifespan":1,"minRadius":300,"configName":"heart04","tangentialAcceleration":-0,"rotationStart":0,"startColorVarianceGreen":0,"speed":400,"minRadiusVariance":0,"finishColorVarianceBlue":0,"finishColorBlue":0,"finishColorGreen":0.05410007,"blendFuncDestination":1,"finishColorAlpha":1,"sourcePositionVariancex":84.97,"startParticleSize":30,"sourcePositionVariancey":231.62,"startColorRed":0.999996,"finishColorVarianceRed":0,"textureFileName":"images/assetT.png","startColorVarianceAlpha":1,"maxParticles":80,"finishColorVarianceGreen":0,"finishParticleSize":20,"duration":-1,"startColorVarianceRed":0,"finishColorRed":0,"gravityx":0,"maxRadiusVariance":0,"finishParticleSizeVariance":20,"gravityy":-0,"rotationEndVariance":0,"startColorBlue":1,"rotationStartVariance":0,"speedVariance":0,"radialAccelVariance":0,"textureImageData":"","tangentialAccelVariance":-0,"particleLifespanVariance":0,"angleVariance":-0,"angle":-90,"maxRadius":0 | |
} | |
} | |
} |
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
--go to professorcook.org/lua/json.zip for the whole project | |
display.setStatusBar(display.HiddenStatusBar) | |
require('mobdebug').start() | |
game = {score=0, count=30, assets={}, scenes={}} | |
as=require 'assets' | |
game.assets=as.new(system.pathForFile( "assetTest.json" )) | |
as.show(game.assets) | |
--[[local t =as.get(game.assets, 'heart04_emitter') | |
t.isVisible=true | |
t:translate(200,200) | |
t:scale(0.1,0.5)--]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment