diff options
-rwxr-xr-x | demo.lua | 2 | ||||
-rw-r--r-- | draw.lua | 139 |
2 files changed, 17 insertions, 124 deletions
@@ -3,7 +3,7 @@ local d = require 'draw' local stats = { - title = 'Azer', + name = 'Azer', alignment = "lawful neutral", size = "medium", type = "elemental", @@ -6,117 +6,10 @@ setfenv(1, module) local m = require 'marigold-svg.marigold' -local function merge2(a, b) - local tbl = {} - for k, v in pairs(a) do tbl[k] = v end - for k, v in pairs(b) do tbl[k] = v end - return tbl -end -local function merge_recursive(array, acc) - local tbl = array[1] - table.remove(array, 1) - local acc = merge2(acc, tbl) - if #array == 0 then return acc end - return merge_recursive(array, acc) -end -local function merge(...) - return merge_recursive({...}, {}) -end - - function capitalize(str) return string.gsub(str, "^.", string.upper) end ---===== boxes =====-- - -Box = {} -function Box.new(_, tbl) - local parent = tbl.parent or { x0=0, y0=0 } - local width = tbl.width or parent.width - local height = tbl.height or parent.width - local offset = tbl.offset or { x=0, y=0 } - local padding = tbl.padding or { left=0, right=0, top=0, bottom=0 } - padding.left = padding.left or 0 - padding.right = padding.right or 0 - padding.top = padding.top or 0 - padding.bottom = padding.bottom or 0 - local self = { - x0 = parent.x0 + offset.x + padding.left, - y0 = parent.y0 + offset.y + padding.top, - width = width - padding.left - padding.right, - height = height - padding.top - padding.bottom, - } - setmetatable(self, {__index=Box}) - return self -end -setmetatable(Box, {__call=Box.new}) - - -function Box.point(self, x, y) - return x + self.x0, y + self.y0 -end - -function Box.center(self) - return self.x0 + (self.width/2), self.y0 + (self.height/2) -end - -local function hsplit(box, acc, count, settings) - local index = #acc - if index == count then return acc end - - local z = box.width / count - table.insert( - acc, - Box(merge({ - parent=box, - width=z, - offset= { x=index*z, y=0 } - }, settings)) - ) - return hsplit(box, acc, count, settings) -end -function Box.hsplit(self, count, settings) - local count = count or 2 - local settings = settings or {} - return hsplit(self, {}, count, settings) -end - -local function vsplit(box, acc, count, settings) - local index = #acc - if index == count then return acc end - - local z = box.height / count - table.insert( - acc, - Box(merge({ - parent=box, - height=z, - offset= { y=index*z, x=0 } - }, settings)) - ) - return vsplit(box, acc, count, settings) -end -function Box.vsplit(self, count, settings) - local count = count or 2 - local settings = settings or {} - return vsplit(self, {}, count, settings) -end - - -function Box.split_down(self, height) - local a = Box{ - parent=self, - height=height, - } - local b = Box{ - parent=self, - height=self.height - height, - offset={x=0, y=height}, - } - return a, b -end - --===== misc =====-- @@ -156,10 +49,10 @@ local divider = function() return pad(0, 10, _divider()) end --===== basic stats =====-- -local function title(stats) +local function name(stats) local size = 40 return function(y) - return m.text(stats.title, { + return m.text(stats.name, { x=10, y=y+size, style="font-size:"..size.."px;font-variant-caps:small-caps;stroke:black;fill:darkred;font-family:serif", }) @@ -179,7 +72,7 @@ end local function header(stats) local f, h = stack( - {pad(10, 10, title(stats))}, + {pad(10, 10, name(stats))}, {pad(0, 10, subheading(stats))} ) return f, h @@ -203,25 +96,25 @@ local function armor_hp(stats) return f, h end -local function bonus(raw) - return math.floor( (raw-10)/2 ) +local function bonus(ability_score) + return math.floor( (ability_score-10)/2 ) end -local function rawbox(name, raw, x, y) - local raw = tonumber(raw) - local value = string.format("%d (%s%d)", raw, (raw<0 and '-') or '+', bonus(raw)) +local function scorebox(name, ability_score, x, y) + local score = tonumber(ability_score) + local value = string.format("%d (%s%d)", score, (score<0 and '-') or '+', bonus(score)) return m.g{ m.text(name, { x=x+10, y=y+15, style="font-weight:bold;font-size:15px;fill:darkred" }), m.text(value, {x=x, y=y+30, style="font-size:15px;fill:darkred;" }), } end -local function raw(stats) +local function ability_scores(stats) return function(y) return m.g{ - rawbox('STR', stats.str, 10, y), - rawbox('DEX', stats.dex, 90, y), - rawbox('CON', stats.con, 170, y), - rawbox('INT', stats.int, 250, y), - rawbox('WIS', stats.wis, 330, y), - rawbox('CHA', stats.cha, 410, y), + scorebox('STR', stats.str, 10, y), + scorebox('DEX', stats.dex, 90, y), + scorebox('CON', stats.con, 170, y), + scorebox('INT', stats.int, 250, y), + scorebox('WIS', stats.wis, 330, y), + scorebox('CHA', stats.cha, 410, y), } end, 40 end @@ -231,7 +124,7 @@ local function base(stats) {divider()}, {armor_hp(stats)}, {divider()}, - {raw(stats)}, + {ability_scores(stats)}, {divider()} ) return f, h |