Skip to content

Instantly share code, notes, and snippets.

@adarapata
Created October 27, 2015 16:46
Show Gist options
  • Save adarapata/c96048cd99f6ef7d9537 to your computer and use it in GitHub Desktop.
Save adarapata/c96048cd99f6ef7d9537 to your computer and use it in GitHub Desktop.
require 'json'
class AtlasJsonBuilder
def build(file_name, dist)
patterns = %w(up upright right downright down downleft left upleft)
sprites = []
patterns.each_with_index do |name, i|
4.times do |frame|
sprites << SpriteRule.new(name: name, frame: frame, pattern: i, width: 24, height: 32)
end
end
hashes = sprites.each_with_object({}) do |s, list|
list.merge! s.template_json
end
jsons = {
frames: hashes,
meta: {
app: "http://www.codeandweb.com/texturepacker",
version: "1.0",
image: file_name,
format: "RGBA8888",
size: {w:256,h:256},
scale: 1,
smartupdate: "$TexturePacker:SmartUpdate:4d39c0a2b05b86b5a26de044242bd6ef:f98eef6920bac6b6005274cd69b5e8ce:eb1c03f86a55fce9b06582a0b985db04$"
}
}
File.write(dist, jsons.to_json)
end
end
class SpriteRule
def initialize(name:, frame:, pattern:, width:, height:)
@name = name
@frame = frame
@width = width
@height = height
@pattern = pattern
end
def template_json
{
"#{@name}_#{@frame}": {
frame: { x: @width * @frame, y: @height * @pattern, w: @width, h: @height },
rotated: false,
trimmed: false,
spriteSourceSize: { x:0,y:0,w: @width, h: @height },
sourceSize: { w: @width, h: @height }
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment