-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriter.lua
553 lines (407 loc) · 13.8 KB
/
Spriter.lua
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
require("json")
-- TODO : for test only, remove later
local function dump(value)
print(json.encode(value))
end
-- namespace
Spriter = {}
Spriter.TexturePack = Core.class()
function Spriter.TexturePack:init(texture, texture_pack_file)
self.texture = texture
local file = io.open(texture_pack_file)
self.data = json.decode(file:read("*all"))
file:close()
end
function Spriter.TexturePack:getTextureRegion(name)
local data = self.data
local frame_data = data.frames[name]
local frame = frame_data.frame
local spriter_source_size = frame_data.spriteSourceSize
local source_size = frame_data.sourceSize
local x = frame.x
local y = frame.y
local width = frame.w
local height = frame.h
-- left and top in source image
local dx1 = spriter_source_size.x
local dy1 = spriter_source_size.y
-- right and bottom space in source image
local dx2 = source_size.w - width - dx1
local dy2 = source_size.h - height - dy1
return TextureRegion.new(self.texture, x, y, width, height, dx1, dy1, dx2, dy2)
end
-- math functions
local function linear(a, b, t)
return (b - a) * t + a
end
local function vectorLinear(vector_a, vector_b, t)
return Spriter.Vector.new(linear(vector_a.x, vector_b.x, t), linear(vector_a.y, vector_b.y, t))
end
local function angleLinear(angle_a, angle_b, spin, t)
if spin == 0 then
return angle_a
elseif spin > 0 then
if angle_b - angle_a > 0 then angle_b = angle_b - 360 end
elseif spin < 0 then
if angle_b - angle_a < 0 then angle_b = angle_b + 360 end
end
return linear(angle_a, angle_b, t)
end
local function spatialLinear(spatial_a, spatial_b, spin, t)
local spatial = Spriter.Spatial.new()
spatial.position = vectorLinear(spatial_a.position, spatial_b.position, t)
spatial.angle = angleLinear(spatial_a.angle, spatial_b.angle, spin, t)
spatial.scale = vectorLinear(spatial_a.scale, spatial_b.scale, t)
spatial.a = linear(spatial_a.a, spatial_b.a, t)
return spatial
end
-- factory method for spriter object
function Spriter.create(filename)
local file = io.open(filename)
local json_data = json.decode(file:read("*all"))
file:close()
return Spriter.Object.new(json_data)
end
Spriter.Object = Core.class()
function Spriter.Object:init(spriter_data)
local folders = {}
for i, folder in ipairs(spriter_data.folder) do
-- lua array index start form 1
folders[folder.id + 1] = Spriter.Folder.new(folder)
end
self.folders = folders
local entities = {}
for i, entity in ipairs(spriter_data.entity) do
entities[entity.id + 1] = Spriter.Entity.new(entity)
-- use name as key, for faster name reference
entities[entity.name] = entities[entity.id + 1]
end
self.entities = entities
end
function Spriter.Object:createEntitySprite(name)
return Spriter.EntitySprite.new(self.entities[name], self.folders)
end
Spriter.EntitySprite = Core.class(Sprite)
function Spriter.EntitySprite:init(entity, folders)
self.time = 0
self.entity = entity
self.animation = entity.animations[1]
self.bitmaps = {}
self.folders = folders
self.playing = false
self:addEventListener(Event.ENTER_FRAME, Spriter.EntitySprite.onUpdate, self)
end
function Spriter.EntitySprite:play(name)
self.animation = self.entity.animations[name]
self.time = 0
self.playing = true
end
function Spriter.EntitySprite:pause()
self.playing = not self.playing
end
function Spriter.EntitySprite:stop()
self.playing = false
self.time = 0
end
function Spriter.EntitySprite:setTime(time)
local animation = self.animation
if animation.looping then
self.time = time % animation.length
else
self.time = math.min(time, animation.length)
end
end
function Spriter.EntitySprite:onUpdate(event)
if self.playing then
self:setTime(self.time + event.deltaTime * 1000.0)
end
if self.texture_pack == nil then return end
self:applyAnimation()
self:updateObjects()
end
function Spriter.EntitySprite:updateObjects()
local folders = self.folders
local texture_pack = self.texture_pack
local bitmaps = self.bitmaps
for i, bitmap in ipairs(bitmaps) do
bitmap:setVisible(false)
end
for i, key in ipairs(self.object_keys) do
if bitmaps[i] == nil then
bitmaps[i] = Bitmap.new(texture_pack.texture)
self:addChild(bitmaps[i])
bitmaps[i]:setVisible(false)
end
local bitmap = bitmaps[i]
if key.folder and key.folder > 0 then
bitmap:setVisible(true)
local file = folders[key.folder].files[key.file]
bitmap:setTextureRegion(texture_pack:getTextureRegion(file.name))
local pivot = key.pivot
if key.use_default_pivot then
pivot = file.pivot
end
bitmap:setAnchorPoint(pivot.x, pivot.y)
bitmap:setPosition(key.spatial.position.x, key.spatial.position.y)
bitmap:setRotation(key.spatial.angle)
bitmap:setScale(key.spatial.scale.x, key.spatial.scale.y)
end
end
end
function Spriter.EntitySprite:applyAnimation()
local animation = self.animation
local mainline_key = animation:getMainlineKeyFromTime(self.time)
local bone_keys = {}
for i, reference in ipairs(mainline_key.bone_references) do
local current_key = animation:getKeyFromReference(reference, self.time)
if reference.parent > 0 and bone_keys[reference.parent] then
current_key.spatial = current_key.spatial:combineParent(bone_keys[reference.parent].spatial)
end
current_key.parent = reference.parent
bone_keys[i] = current_key
end
local object_keys = {}
for i, reference in ipairs(mainline_key.object_references) do
local current_key = animation:getKeyFromReference(reference, self.time)
if reference.parent > 0 and bone_keys[reference.parent] then
current_key.spatial = current_key.spatial:combineParent(bone_keys[reference.parent].spatial)
end
current_key.parent = reference.parent
object_keys[i] = current_key
end
self.object_keys = object_keys
end
-- folder class
Spriter.Folder = Core.class()
function Spriter.Folder:init(folder_data)
self.name = folder_data.name
local files = {}
for i, file in ipairs(folder_data.file) do
local pivot = Spriter.Vector.new(file.pivot_x, file.pivot_y)
pivot.y = 1 - pivot.y
files[file.id + 1] =
{
name = file.name,
pivot = pivot
}
end
self.files = files
end
-- entity class
Spriter.Entity = Core.class()
function Spriter.Entity:init(entity_data)
self.name = entity_data.name
local animations = {}
for i, animation in ipairs(entity_data.animation) do
animations[animation.id + 1] = Spriter.Animation.new(animation)
animations[animation.name] = animations[animation.id + 1]
end
self.animations = animations
end
Spriter.Animation = Core.class()
function Spriter.Animation:init(animation_data)
self.name = animation_data.name
self.looping = animation_data.looping == nil and true or animation_data.looping
self.length = animation_data.length
local mainline_keys = {}
for i, mainline_key in ipairs(animation_data.mainline.key) do
mainline_keys[mainline_key.id + 1] = Spriter.MainlineKey.new(mainline_key)
end
self.mainline_keys = mainline_keys
local timelines = {}
for i, timeline in ipairs(animation_data.timeline) do
timelines[timeline.id + 1] = Spriter.Timeline.new(timeline)
end
self.timelines = timelines
end
function Spriter.Animation:getMainlineKeyFromTime(time)
local mainline_keys = self.mainline_keys
local index = 0
for i, key in ipairs(mainline_keys) do
if key.time <= time then
index = i
else
break
end
end
return mainline_keys[index]
end
function Spriter.Animation:getKeyFromReference(reference, time)
local timeline = self.timelines[reference.timeline]
local key_length = #timeline.keys
if key_length == 0 then return nil end
local key_a = timeline.keys[reference.key]
if key_length == 1 then return key_a:clone() end
local next_key_index = reference.key + 1
if next_key_index > key_length then
if self.looping then next_key_index = 1 else return key_a:clone() end
end
local key_b = timeline.keys[next_key_index]
local key_b_time = key_b.time
if key_b_time < key_a.time then key_b_time = key_b_time + self.length end
return key_a:interpolate(key_b, key_b_time, time)
end
Spriter.MainlineKey = Core.class()
function Spriter.MainlineKey:load_references(references_data)
local references = {}
for i, reference in ipairs(references_data) do
references[reference.id + 1] =
{
-- lua array start form 1, so add 1 to all index
parent = reference.parent ~= nil and reference.parent + 1 or 0,
timeline = reference.timeline + 1,
key = reference.key + 1,
z_index = reference.z_index
}
end
return references
end
function Spriter.MainlineKey:init(mainline_key_data)
self.time = mainline_key_data.time or 0
self.bone_references = self:load_references(mainline_key_data.bone_ref)
self.object_references = self:load_references(mainline_key_data.object_ref)
end
Spriter.Timeline = Core.class()
function Spriter.Timeline:init(timeline_data)
self.name = timeline_data.name
self.type = timeline_data.object_type or "sprite"
local keys = {}
for i, key in ipairs(timeline_data.key) do
-- support sprite timeline key only
keys[key.id + 1] = Spriter.TimelineKey.classes[self.type].new(key)
end
self.keys = keys
end
Spriter.TimelineKey = Core.class()
function Spriter.TimelineKey:init(timeline_key_data)
timeline_key_data = timeline_key_data or {}
self.time = timeline_key_data.time or 0
self.spin = timeline_key_data.spin or 1
-- support linear only
--self.cruve = timeline_key_data.cruve_type or 0
end
function Spriter.TimelineKey:copyTo(timeline_key)
timeline_key.time = self.time
timeline_key.spin = self.spin
end
function Spriter.TimelineKey:clone()
local timeline_key = self:create()
self:copyTo(timeline_key)
return timeline_key
end
function Spriter.TimelineKey:create()
return Spriter.TimelineKey.new()
end
function Spriter.TimelineKey:interpolate(next_key, next_key_time, time)
return self:linear(next_key, self:getTWithNextKey(next_key, next_key_time, time))
end
function Spriter.TimelineKey:getTWithNextKey(next_key, next_key_time, time)
-- support linear only
if time == next_key.time then return 0 end
local t = (time - self.time) / (next_key_time - self.time)
-- support linear only
return t
end
Spriter.Vector = Core.class()
function Spriter.Vector:init(x, y)
self.x = x or 0
self.y = y or 0
end
Spriter.Spatial = Core.class()
function Spriter.Spatial:init(spatial_data)
spatial_data = spatial_data or {}
self.position = Spriter.Vector.new(spatial_data.x, spatial_data.y)
self.position.y = -self.position.y
self.angle = spatial_data.angle or 0
self.angle = -self.angle
self.scale = Spriter.Vector.new(spatial_data.scale_x or 1, spatial_data.scale_y or 1)
self.a = spatial_data.a or 1
end
function Spriter.Spatial:copyTo(spatial)
spatial.position = Spriter.Vector.new(self.position.x, self.position.y)
spatial.angle = self.angle
spatial.scale = Spriter.Vector.new(self.scale.x, self.scale.y)
spatial.a = self.a
end
function Spriter.Spatial:clone()
local spatial = Spriter.Spatial.new()
self:copyTo(spatial)
return spatial
end
function Spriter.Spatial:combineParent(parent)
local spatial = Spriter.Spatial.new()
self:copyTo(spatial)
spatial.angle = spatial.angle + parent.angle
spatial.scale.x = spatial.scale.x * parent.scale.x
spatial.scale.y = spatial.scale.y * parent.scale.y
spatial.a = spatial.a * parent.a
if x ~= 0 or y ~= 0 then
local pre_x = spatial.position.x * parent.scale.x
local pre_y = spatial.position.y * parent.scale.y
local radians = parent.angle * math.pi / 180
local s = math.sin(radians)
local c = math.cos(radians)
spatial.position.x = (pre_x * c) - (pre_y * s) + parent.position.x
spatial.position.y = (pre_x * s) + (pre_y * c) + parent.position.y
else
spatial.position.x = parent.position.x
spatial.position.y = parent.position.y
end
return spatial
end
Spriter.SpatialTimelineKey = Core.class(Spriter.TimelineKey)
function Spriter.SpatialTimelineKey:init(timeline_key_data)
timeline_key_data = timeline_key_data or {}
self.spatial = Spriter.Spatial.new(timeline_key_data.bone or timeline_key_data.object)
end
function Spriter.SpatialTimelineKey:copyTo(timeline_key)
Spriter.TimelineKey.copyTo(self, timeline_key)
timeline_key.spatial = self.spatial:clone()
end
function Spriter.SpatialTimelineKey:create()
return Spriter.SpatialTimelineKey.new()
end
Spriter.SpriteTimelineKey = Core.class(Spriter.SpatialTimelineKey)
function Spriter.SpriteTimelineKey:init(timeline_key_data)
local sprite_object = timeline_key_data and timeline_key_data.object or {}
self.folder = sprite_object.folder ~= nil and sprite_object.folder + 1 or 0
self.file = sprite_object.file ~= nil and sprite_object.file + 1 or 0
self.use_default_pivot = sprite_object.pivot_x == nil
self.pivot = Spriter.Vector.new(sprite_object.pivot_x, sprite_object.pivot_y)
self.pivot.y = 1 - self.pivot.y
end
function Spriter.SpriteTimelineKey:copyTo(timeline_key)
Spriter.SpatialTimelineKey.copyTo(self, timeline_key)
timeline_key.folder = self.folder
timeline_key.file = self.file
timeline_key.use_default_pivot = self.use_default_pivot
timeline_key.pivot = Spriter.Vector.new(self.pivot.x, self.pivot.y)
end
function Spriter.SpriteTimelineKey:create()
return Spriter.SpriteTimelineKey.new()
end
function Spriter.SpriteTimelineKey:linear(key_b, t)
local key = self:clone()
key.spatial = spatialLinear(self.spatial, key_b.spatial, self.spin, t)
if not self.use_default_pivot and not key_b.use_default_pivot then
key.pivot = vectorLinear(self.pivot, key_b.pivot, t)
key.use_default_pivot = false
else
key.use_default_pivot = true
end
return key
end
Spriter.BoneTimelineKey = Core.class(Spriter.SpatialTimelineKey)
function Spriter.BoneTimelineKey:create()
return Spriter.BoneTimelineKey.new()
end
function Spriter.BoneTimelineKey:linear(key_b, t)
local key = self:clone()
key.spatial = spatialLinear(self.spatial, key_b.spatial, self.spin, t)
return key
end
Spriter.TimelineKey.classes =
{
sprite = Spriter.SpriteTimelineKey,
bone = Spriter.BoneTimelineKey
}