Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresourceHandler.lua
More file actions
Latest commit
315 lines (256 loc) · 8.37 KB
/
Copy pathresourceHandler.lua
File metadata and controls
315 lines (256 loc) · 8.37 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
localutil=require("include/util")
localapi= {}
localself= {
debugMode=false
}
--------------------------------------------------
-- Images
--------------------------------------------------
localfunctionLoadImage(resData)
localimage=love.graphics.newImage(resData.file)
localimageWidth, imageHeight=image:getWidth(), image:getHeight()
localdata= {
image=image,
xScale=resData.xScaleor1,
yScale=resData.yScaleor1,
imageWidth=imageWidth,
imageHeight=imageHeight,
}
data.xOffset= (resData.xOffsetor0.5)*imageWidth
data.yOffset= (resData.yOffsetor0.5)*imageHeight
returndata
end
localfunctionLoadIsoImage(resData)
localimage= {}
localimageWidth, imageHeight
fori=1, #resData.filesdo
image[i] =love.graphics.newImage(resData.files[i])
ifnotimageWidththen
imageWidth, imageHeight=image[i]:getWidth(), image[i]:getHeight()
end
end
localdata= {
image=image,
xScale=resData.xScaleor1,
yScale=resData.yScaleor1,
firstDir=resData.firstDiror0,
directionCount=#resData.files,
rotate=resData.rotate,
}
data.xOffset= (resData.xOffsetor0.5)*imageWidth
data.yOffset= (resData.yOffsetor0.5)*imageHeight
returndata
end
localfunctionLoadAnimation(resData)
localdata=LoadImage(resData)
data.quads= {}
data.duration=resData.duration
localwidth=resData.width
localimageWidth=data.image:getWidth()
localimageHeight=data.image:getHeight()
data.xOffset= (resData.xOffsetor0.5)*width
data.yOffset= (resData.yOffsetor0.5)*imageHeight
data.quadWidth=width
data.quadHeight=imageHeight
forx=0, imageWidth-width, widthdo
--print(x)
data.quads[#data.quads+1] =love.graphics.newQuad(x, 0, width, imageHeight, imageWidth, imageHeight)
end
data.frames=#data.quads
returndata
end
localfunctionLoadIsoAnimation(resData)
localdirAnim= {}
fori=1, #resData.filesdo
dirAnim[i] =LoadAnimation({
file=resData.files[i],
width=resData.width,
duration=resData.duration,
xScale=resData.xScale,
yScale=resData.yScale,
xOffset=resData.xOffset,
yOffset=resData.yOffset,
})
end
ifresData.anchorsthen
fori=1, #resData.filesdo
dirAnim[i].anchors= {}
forkey, valueinpairs(resData.anchors) do
dirAnim[i].anchors[key] =value[i]
end
end
end
localdata= {
dirAnim=dirAnim,
duration=resData.duration,
firstDir=resData.firstDiror0,
directionCount=#resData.files,
rotate=resData.rotate,
anchors=resData.anchors,
}
returndata
end
localfunctionGetAnimationFrame(progress, duration, frames)
returnmath.floor((progress%duration) /duration*frames) +1
end
--------------------------------------------------
-- Sound
--------------------------------------------------
localfunctionLoadSound(resData)
end
--------------------------------------------------
-- Loading
--------------------------------------------------
localfunctionLoadResouce(name, res)
ifres.form=="image" then
self.images[name] =LoadImage(res)
elseifres.form=="iso_image" then
self.images[name] =LoadIsoImage(res)
elseifres.form=="animation" then
self.animations[name] =LoadAnimation(res)
elseifres.form=="iso_animation" then
self.animations[name] =LoadIsoAnimation(res)
elseifres.form=="sound" then
self.sounds[name] =LoadSound(res)
else
print("Invalid form ", res.form, " for resource ", name)
end
end
localfunctionLoadResourceFile(name)
localres=require("resources/defs/" ..name)
ifres.formthen
LoadResouce(name, res)
return
end
fori=1, #resdo
LoadResouce(res[i].name, res[i])
end
end
functionapi.LoadResources()
localresList=util.GetDefDirList("resources/defs")
self.images= {}
self.animations= {}
self.sounds= {}
fori=1, #resListdo
LoadResourceFile(resList[i])
end
end
--------------------------------------------------
-- Drawing Functions
--------------------------------------------------
functionapi.SetTexture(mesh, name)
ifnotself.images[name] then
print("Invalid SetTexture ", name)
return
end
mesh:setTexture(self.images[name].image)
end
functionapi.DrawImage(name, x, y, rotation, alpha, scale, color)
ifnotself.images[name] then
print("Invalid DrawImage ", name)
return
end
rotation=rotationor0
localscaleX, scaleY=scale, scale
iftype(scale) =="table" then
scaleX, scaleY=scale[1], scale[2]
end
scaleX=scaleXor1
scaleY=scaleYor1
love.graphics.setColor(
(colorandcolor[1]) or1,
(colorandcolor[2]) or1,
(colorandcolor[3]) or1,
((colorandcolor[4]) or1)*(alphaor1)
)
localdata=self.images[name]
love.graphics.draw(data.image, x, y, rotation, data.xScale*scaleX, data.yScale*scaleY, data.xOffset, data.yOffset, 0, 0)
end
functionapi.DrawIsoImage(name, x, y, direction, alpha, scale, color)
ifnotself.images[name] then
print("Invalid DrawIsoImage ", name)
return
end
scale=scaleor1
love.graphics.setColor(
(colorandcolor[1]) or1,
(colorandcolor[2]) or1,
(colorandcolor[3]) or1,
((colorandcolor[4]) or1)*(alphaor1)
)
localdata=self.images[name]
localdrawDir=util.DirectionToCardinal(direction, data.firstDir, data.directionCount)
localrotation=0
ifdata.rotatethen
rotation=-util.AngleToCardinal(direction, drawDir, data.firstDir, data.directionCount)
end
love.graphics.draw(data.image[drawDir], x, y, rotation, data.xScale*scale, data.yScale*scale, data.xOffset, data.yOffset, 0, 0)
end
functionapi.UpdateAnimation(name, progress, dt)
ifnotself.animations[name] then
print("Invalid UpdateAnimation ", name)
return
end
return (progress+dt)%self.animations[name].duration
end
functionapi.GetAnimationDuration(name)
ifnotself.animations[name] then
print("Invalid GetAnimationDuration ", name)
return
end
returnself.animations[name].duration
end
functionapi.DrawAnimInternal(data, x, y, progress, rotation, alpha, scale, color)
love.graphics.setColor(
(colorandcolor[1]) or1,
(colorandcolor[2]) or1,
(colorandcolor[3]) or1,
((colorandcolor[4]) or1)*(alphaor1)
)
scale=scaleor1
rotation=rotationor0
progress=progressor0
localquadToDraw=GetAnimationFrame(progress, data.duration, data.frames)
love.graphics.draw(data.image, data.quads[quadToDraw], x, y, rotation, data.xScale*scale, data.yScale*scale, data.xOffset, data.yOffset, 0, 0)
ifself.debugModethen
love.graphics.rectangle("line", x-data.xOffset*data.xScale, y-data.yOffset*data.yScale, data.quadWidth*data.xScale, data.quadHeight*data.yScale, 0, 0)
end
end
functionapi.DrawAnimation(name, x, y, progress, rotation, alpha, scale, color)
ifnotself.animations[name] then
print("Invalid DrawAnimation ", name)
return
end
api.DrawAnimInternal(self.animations[name], x, y, progress, rotation, alpha, scale, color)
end
functionapi.DrawIsoAnimation(name, x, y, progress, direction, alpha, scale, color)
ifnotself.animations[name] then
print("Invalid DrawIsoAnimation ", name)
return
end
localdata=self.animations[name]
localdrawDir=util.DirectionToCardinal(direction, data.firstDir, data.directionCount)
localrotation=0
ifdata.rotatethen
rotation=-util.AngleToCardinal(direction, drawDir, data.firstDir, data.directionCount)
end
api.DrawAnimInternal(data.dirAnim[drawDir], x, y, progress, rotation, alpha, scale, color)
end
functionapi.GetIsoAnimationAnchorOffset(name, anchorName, progress, direction, scale)
ifnotself.animations[name] then
print("Invalid GetIsoAnimationAnchorOffset ", name)
return {0, 0}
end
scale=scaleor1
progress=progressor0
localdata=self.animations[name]
localdrawDir=util.DirectionToCardinal(direction, data.firstDir, data.directionCount)
dirData=data.dirAnim[drawDir]
localanimFrame=GetAnimationFrame(progress, dirData.duration, dirData.frames)
localoffset=dirData.anchors[anchorName][animFrame]
return {offset[1]*dirData.xScale*scale, offset[2]*dirData.yScale*scale}
end
--------------------------------------------------
-- Drawing Functions
--------------------------------------------------
returnapi