Skip to content

Commit c2648a1

Browse files
author
Klayton Kowalski
committed
Working beta complete. Supports two playbacks: loop_forward and loop_pingpong. Includes example project.
1 parent 1ece5be commit c2648a1

12 files changed

+17037
-1
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/.internal
2+
/build
3+
.externalToolBuilders
4+
.DS_Store
5+
Thumbs.db
6+
.lock-wscript
7+
*.pyc
8+
.project
9+
.cproject
10+
builtins

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# defold-tilemap-animator
1+
# Defold Tilemap Animator
22
Defold Tilemap Animator (DTA) allows for easy runtime tile animations in a Defold game engine project.

assets/tileset.png

1.08 KB
Loading

dta/dta.lua

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
local dta = {}
2+
3+
----------------------------------------------------------------------
4+
-- PROPERTIES
5+
----------------------------------------------------------------------
6+
7+
dta.animation_groups = {}
8+
dta.tilemap_relative_path = nil
9+
dta.tilemap_layer_ids = {}
10+
dta.tilemap_width = 0
11+
dta.tilemap_height = 0
12+
13+
----------------------------------------------------------------------
14+
-- PLAYBACK FUNCTIONS
15+
----------------------------------------------------------------------
16+
17+
function dta.pb_func_loop_forward(key, value)
18+
value["frame"] = value["frame"] + 1
19+
if key + value["frame"] == value["end_tile"] + 1 then
20+
value["frame"] = 0
21+
end
22+
end
23+
24+
function dta.pb_func_loop_pingpong(key, value)
25+
if value["extra"] == 0 then
26+
value["frame"] = value["frame"] + 1
27+
if key + value["frame"] == value["end_tile"] then
28+
value["extra"] = 1
29+
end
30+
elseif value["extra"] == 1 then
31+
value["frame"] = value["frame"] - 1
32+
if key + value["frame"] == key then
33+
value["extra"] = 0
34+
end
35+
end
36+
end
37+
38+
dta.playback_functions = {
39+
loop_forward = dta.pb_func_loop_forward,
40+
loop_pingpong = dta.pb_func_loop_pingpong
41+
}
42+
43+
----------------------------------------------------------------------
44+
-- HELPER FUNCTIONS
45+
----------------------------------------------------------------------
46+
47+
function dta.sweep_tile(prev_tile, new_tile)
48+
for i = 1, #dta.tilemap_layer_ids do
49+
for j = 1, dta.tilemap_height do
50+
for k = 1, dta.tilemap_width do
51+
if tilemap.get_tile(dta.tilemap_relative_path, dta.tilemap_layer_ids[i], k, j) == prev_tile then
52+
tilemap.set_tile(dta.tilemap_relative_path, dta.tilemap_layer_ids[i], k, j, new_tile)
53+
end
54+
end
55+
end
56+
end
57+
end
58+
59+
function dta.timer_callback(self, handle, time_elapsed)
60+
for key, value in pairs(dta.animation_groups) do
61+
if value["handle"] == handle then
62+
value["elapsed"] = value["elapsed"] + time_elapsed
63+
if value["elapsed"] >= value["step"] then
64+
local prev_tile = key + value["frame"]
65+
dta.playback_functions[value["playback"]](key, value)
66+
dta.sweep_tile(prev_tile, key + value["frame"])
67+
end
68+
end
69+
end
70+
end
71+
72+
function dta.extend_animation_groups()
73+
for key, value in pairs(dta.animation_groups) do
74+
value["handle"] = timer.delay(value["step"], true, dta.timer_callback)
75+
value["frame"] = 0
76+
value["elapsed"] = 0
77+
value["extra"] = 0
78+
end
79+
end
80+
81+
----------------------------------------------------------------------
82+
-- USER FUNCTIONS
83+
----------------------------------------------------------------------
84+
85+
function dta.init(animation_groups, tilemap_relative_path, tilemap_layer_ids)
86+
local x, y, w, h = tilemap.get_bounds(tilemap_relative_path)
87+
dta.tilemap_relative_path = tilemap_relative_path
88+
dta.animation_groups = animation_groups
89+
dta.tilemap_layer_ids = tilemap_layer_ids
90+
dta.tilemap_width = w
91+
dta.tilemap_height = h
92+
dta.extend_animation_groups(animation_groups)
93+
end
94+
95+
return dta

example/animation_groups.lua

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- RETURN USER-CREATED ANIMATION GROUPS.
2+
-- Format: table
3+
-- key = start_tile (from tilesource)
4+
-- value = table
5+
-- [1] = end_tile (from_tilesource)
6+
-- [2] = playback ("loop_forward", "loop_pingpong")
7+
-- [3] = step (frame duration in seconds)
8+
return {
9+
[1] = { end_tile = 4, playback = "loop_forward", step = 1 / 2 },
10+
[5] = { end_tile = 6, playback = "loop_forward", step = 1 / 2 },
11+
[7] = { end_tile = 13, playback = "loop_forward", step = 1 / 4 },
12+
[14] = { end_tile = 18, playback = "loop_forward", step = 1 / 4 },
13+
[20] = { end_tile = 22, playback = "loop_pingpong", step = 1 / 6 },
14+
[23] = { end_tile = 31, playback = "loop_pingpong", step = 1 / 6 }
15+
}

example/collection.collection

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: "collection"
2+
instances {
3+
id: "go"
4+
prototype: "/example/go.go"
5+
position {
6+
x: 0.0
7+
y: 0.0
8+
z: 0.0
9+
}
10+
rotation {
11+
x: 0.0
12+
y: 0.0
13+
z: 0.0
14+
w: 1.0
15+
}
16+
scale3 {
17+
x: 1.0
18+
y: 1.0
19+
z: 1.0
20+
}
21+
}
22+
scale_along_z: 0

example/go.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
components {
2+
id: "tilemap"
3+
component: "/example/tilemap.tilemap"
4+
position {
5+
x: 0.0
6+
y: 0.0
7+
z: 0.0
8+
}
9+
rotation {
10+
x: 0.0
11+
y: 0.0
12+
z: 0.0
13+
w: 1.0
14+
}
15+
}
16+
components {
17+
id: "script"
18+
component: "/example/script.script"
19+
position {
20+
x: 0.0
21+
y: 0.0
22+
z: 0.0
23+
}
24+
rotation {
25+
x: 0.0
26+
y: 0.0
27+
z: 0.0
28+
w: 1.0
29+
}
30+
}

example/script.script

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Import Defold Tilemap Automator (DTA) module.
2+
local dta = require "dta.dta"
3+
4+
-- Initialize script.
5+
function init(self)
6+
-- Import animation groups from user-created Lua module.
7+
local animation_groups = require "example.animation_groups"
8+
-- List tilemap layer ids.
9+
local tilemap_layer_ids = { "layer1", "layer2" }
10+
-- Initialize and run DTA.
11+
dta.init(animation_groups, "#tilemap", tilemap_layer_ids)
12+
end

0 commit comments

Comments
 (0)