-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframes.lua
More file actions
126 lines (97 loc) · 3.25 KB
/
frames.lua
File metadata and controls
126 lines (97 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
local JSON = require "JSON"
local frames = {}
function frames.timeBasedNextFrameCondition(frameDuration)
return function(time_elapsed, drawAnchor, object)
drawAnchor.x = object.x
drawAnchor.y = object.y
return time_elapsed > frameDuration
end
end
function frames.gapBasedNextFrameCondition(gap)
return function(time_elapsed, drawAnchor, object)
if drawAnchor.x == nil or object.x == nil then
return true
end
drawAnchor.y = object.y
local shouldSwitch = math.abs(drawAnchor.x - object.x) >= gap
if shouldSwitch then
drawAnchor.x = object.x
end
return shouldSwitch
end
end
function frames.generator(imageFile, jsonFile, complementaryJsonFile)
local jsonSpriteSheet
local complementaryData
if jsonFile ~= nil then
local spriteFile = io.open(jsonFile)
local spriteFileContents = spriteFile:read("*a")
spriteFile:close()
jsonSpriteSheet = JSON:decode(spriteFileContents)
end
if complementaryJsonFile ~= nil then
local file = io.open(complementaryJsonFile)
local fileContents = file:read("*a")
file:close()
complementaryData = JSON:decode(fileContents)
end
local sourceImage = love.graphics.newImage(imageFile)
local image_w = sourceImage:getWidth() --or SourceImage.getWidth(SourceImage)
local image_h = sourceImage:getHeight()
local function frameAt(x, y, dx, dy, complement)
local anchor = {}
local attackbox
if complement ~= nil then
if complement.anchor ~= nil then
anchor = complement.anchor
end
if complement.attackbox ~= nil then
attackbox = complement.attackbox
end
end
if anchor.x == nil then
anchor.x = dx / 2
end
if anchor.y == nil then
anchor.y = 0
end
return {
q = love.graphics.newQuad(x, y, dx, dy, image_w, image_h),
dx = dx,
dy = dy,
x = x,
y = y,
anchor = anchor,
attackbox = attackbox
}
end
local function startsWith(str, start)
return str:sub(1, #start) == start
end
local function resolveAnimation(name, duration, alternatives, next, nextFrameCondition)
local animation = {}
for i, f in ipairs(jsonSpriteSheet.frames) do
if (startsWith(f.filename, name .. "/")) then
local complement
if complementaryData ~= nil then
complement = complementaryData[f.filename]
end
local frame = frameAt(f.frame.x, f.frame.y, f.frame.w, f.frame.h, complement)
table.insert(animation, frame)
end
end
animation.alternates = alternatives
animation.next = next
if nextFrameCondition == nil then
nextFrameCondition = frames.timeBasedNextFrameCondition(duration)
end
animation.nextFrameCondition = nextFrameCondition
return animation
end
return {
image = sourceImage,
frameAt = frameAt,
resolveAnimation = resolveAnimation
}
end
return frames