Skip to content

Commit

Permalink
Fix offsets on drifting, sign sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonDePlatino committed Jun 18, 2024
1 parent bf45009 commit cf7f9ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "kart-builder",
"displayName": "Kart Builder",
"description": "An aseprite extension for drawing, previewing and exporting racers for Dr. Robotnik's Ring Racers.",
"version": "1.3",
"version": "1.3.1",
"author": {
"name": "DragonDePlatino"
},
Expand Down
9 changes: 6 additions & 3 deletions src/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function image_dither(image, style)
end

-- Get image pixels as a lump.
function image_lump(image)
function image_lump(image, offset)
local out = {}

-- Write image dimensions.
Expand All @@ -96,6 +96,9 @@ function image_lump(image)

pivot = slice.pivot
if pivot == nil then return error_new{ 'Sprite slice did not have pivot.', 'Edit your slice and set a pivot on your racer\'s origin pixel.' } end

-- Add per-frame offset to pivot.
pivot = Point(pivot.x + offset.x, pivot.y + offset.y)
end

-- Write offset of sprite.
Expand Down Expand Up @@ -170,14 +173,14 @@ function image_lump(image)
end

-- Get file entry for an image.
function image_entry(image, layername, frame, angle, symmetrical)
function image_entry(image, layername, frame, angle, symmetrical, offset)
-- Get output filename.
local suffix = string.char(64 + frame) .. angle
if angle > 1 and angle < 5 and symmetrical then suffix = suffix .. string.char(64 + frame) .. (10 - angle) end
local name = layername .. suffix

-- Generate texture lump.
local lump = image_lump(image)
local lump = image_lump(image, offset)
if errored(lump) then return error_new({ 'Error writing texture lump: ' .. name }, lump) end

-- Output texture lump.
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dofile 'table.lua'

plugin_version = '1.3'
plugin_version = '1.3.1'
plugin_key = 'dragondeplatino/kart-builder'

-- Unpack plugin properties from object into a table.
Expand Down
45 changes: 36 additions & 9 deletions src/skin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ local skin_sprites = {
{ name = 'TALK', frames = {} }
}

-- Offset for each frame.
local skin_offsets = {
DRLO = { { 0, 3 }, { 0, 0 } },
DRLI = { {-6, 0 }, { 0, 0 }, { 6, 0 }, { 0, 0 } },

DRRO = { { 0, 3 }, { 0, 0 } },
DRRI = { { 6, 0 }, { 0, 0 }, {-6, 0 }, { 0, 0 } },

SIGN = { { 0, -5 } },
SSIG = { { 0, -5 } }
}

-- Layers that have their final output cropped.
local skin_crop = {
XTRA = true
}

-- Skin autofill rules.
local skin_autofills = {
STIL = { exact = true, rules = { '', '', '', '', 'STINA5' } },
Expand Down Expand Up @@ -103,10 +120,16 @@ local skin_buttons = {
}
}

-- Layers that have their final output cropped.
local layer_crop = {
XTRA = true
}
-- Get offset of a particular skin frame.
function skin_offset(layername, frame)
local frames = skin_offsets[layername]
if not frames then return Point(0, 0) end

local offset = frames[frame]
if not offset then return Point(0, 0) end

return Point(offset[1], offset[2])
end

-- Parse contents of a skin file into a table.
function skin_table(text)
Expand Down Expand Up @@ -253,6 +276,7 @@ function skin_save(path, properties)
-- Output single frame for each angle.
for frame, ditherstyle in ipairs(frames) do
local angles = { 1, 2, 3, 4, 5, 6, 7, 8 }
local offset = skin_offset(layername, frame)

-- Prescan for symmetrical output.
local symmetrical = true
Expand Down Expand Up @@ -286,7 +310,7 @@ function skin_save(path, properties)

-- Apply dithering to final image.
image_dither(image, ditherstyle)
local entry = image_entry(image, layername, frame, angle, symmetrical)
local entry = image_entry(image, layername, frame, angle, symmetrical, offset)
if errored(entry) then return error_new('Error writing angle image entry.', entry) end

-- Add to list of entries.
Expand All @@ -299,6 +323,7 @@ function skin_save(path, properties)
-- Directionless sprite, prescan for number of frames to output.
local symmetrical = true
local framecount = 0

for g, group in pairs(groups) do
local layer = layer_find(group, layername)
if layer == nil then goto continue end
Expand All @@ -313,6 +338,8 @@ function skin_save(path, properties)

-- Output each layer in stack.
for frame = 1, framecount do
local offset = skin_offset(layername, frame)

for g, group in pairs(groups) do
-- Treat different angles as frames for more compact sprite layout.
local cel, flip = cel_find(group, layername, 1, frame)
Expand All @@ -321,16 +348,16 @@ function skin_save(path, properties)
end
end

if layer_crop[layername] then
if skin_crop[layername] then
local bounds = image:shrinkBounds()
local cropped = Image(ImageSpec{ width=bounds.width, height=bounds.height, colorMode=ColorMode.INDEXED, transparentColor=1 })
local cropped = Image(ImageSpec{ width = bounds.width, height = bounds.height, colorMode = ColorMode.INDEXED, transparentColor = 1 })
image_blit(cropped, image, Point(), false)

local entry = image_entry(cropped, layername, frame, 0, false)
local entry = image_entry(cropped, layername, frame, 0, false, offset)
if errored(entry) then return error_new('Error writing cropped image entry.', entry) end
entries[#entries + 1] = entry
else
local entry = image_entry(image, layername, frame, 0, false)
local entry = image_entry(image, layername, frame, 0, false, offset)
if errored(entry) then return error_new('Error writing directionless image entry.', entry) end
entries[#entries + 1] = entry
end
Expand Down

0 comments on commit cf7f9ad

Please sign in to comment.