-
Notifications
You must be signed in to change notification settings - Fork 0
/
character_sprite.gd
53 lines (42 loc) · 1.09 KB
/
character_sprite.gd
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
extends AnimatedSprite
var frame_count = 1
var tex = null
var ms_per_frame = 0
func _ready():
set_process(true)
func idle_ani(dir):
ani(tex, dir, 0, 6)
ms_per_frame = 250
func move_ani(dir):
ani(tex, dir, 4, 4)
ms_per_frame = 100
func ani(tex, dir, move, framecount):
# files have different direction than the mathematical, ccw direction...
# take the direction and turn it into the order inside sprite files.
# ugh :(
if dir == 0: # face right
dir = 1 # face our right
elif dir == 1: # face up
dir = 0 # face our up
elif dir == 2: # face left
dir = 3 # face our right
elif dir == 3: # face down
dir = 2 # face our down
var frs = SpriteFrames.new()
set_sprite_frames(frs)
frs.clear()
for i in range(0, framecount):
var atlas = AtlasTexture.new()
atlas.set_atlas(tex)
atlas.set_region(Rect2(i * 64, (dir + move) * 64, 64, 64))
frs.add_frame(atlas)
frame_count = framecount
func get_frame_count():
return frame_count
func _process(delta):
var f
if ms_per_frame == 0:
f = 0
else:
f = (OS.get_ticks_msec() / ms_per_frame) % get_frame_count()
set_frame(f)