forked from GWRon/Dig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.util.frameanimation.bmx
More file actions
167 lines (118 loc) · 4 KB
/
base.util.frameanimation.bmx
File metadata and controls
167 lines (118 loc) · 4 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
SuperStrict
Import BRL.Map
Type TFrameAnimationCollection
Field animations:TMap = CreateMap()
Field currentAnimationName:string = ""
'insert a TFrameAnimation with a certain Name
Method Set(name:string, animation:TFrameAnimation)
animations.insert(lower(animationName), animation)
if not animations.contains("default") then setCurrentAnimation(name, 0)
End Method
'Set a new Animation
'If it is a different animation name, the Animation will get reset (start from begin)
Method SetCurrent(name:string, start:int = TRUE)
name = lower(name)
local reset:int = 1 - (currentAnimationName = name)
currentAnimationName = name
if reset then getCurrent().Reset()
if start then getCurrent().Playback()
End Method
Method GetCurrent:TFrameAnimation()
local obj:TFrameAnimation = TFrameAnimation(animations.ValueForKey(currentAnimationName))
'load default if nothing was found
if not obj then obj = TFrameAnimation(animations.ValueForKey("default"))
return obj
End Method
Method Get:TFrameAnimation(name:string="default")
local obj:TFrameAnimation = TFrameAnimation(animations.ValueForKey(name.toLower()))
if not obj then obj = TFrameAnimation(animations.ValueForKey("default"))
return obj
End Method
Method getCurrentAnimationName:string()
return currentAnimationName
End Method
Method Update(deltaTime:float)
getCurrent().Update(deltaTime)
End Method
End Type
Type TFrameAnimation
Field repeatTimes:int = 0 'how many times animation should repeat until finished
Field currentImageFrame:int = 0 'frame of sprite/image
Field currentFrame:int = 0 'position in frames-array
Field frames:int[]
Field framesTime:float[] 'duration for each frame
Field paused:Int = FALSE 'stay with currentFrame or cycle through frames?
Field frameTimer:float = null
Field randomness:int = 0
Function Create:TFrameAnimation(framesArray:int[][], repeatTimes:int=0, paused:int=0, randomness:int = 0)
local obj:TFrameAnimation = new TFrameAnimation
local framecount:int = len( framesArray )
obj.frames = obj.frames[..framecount] 'extend
obj.framesTime = obj.framesTime[..framecount] 'extend
For local i:int = 0 until framecount
obj.frames[i] = framesArray[i][0]
obj.framesTime[i] = float(framesArray[i][1]) * 0.001
Next
obj.repeatTimes = repeatTimes
obj.paused = paused
return obj
End Function
Method Update:int()
'skip update if only 1 frame is set
'skip if paused
If paused or frames.length <= 1 then return 0
if frameTimer = null then ResetFrameTimer()
frameTimer :- GetDeltaTimer().GetDelta()
'time for next frame
if frameTimer <= 0.0
local nextPos:int = currentFramePos + 1
'increase current frameposition but only if frame is set
'resets frametimer too
setCurrentFramePos(nextPos)
'reached end? (use nextPos as setCurrentFramePos already limits value)
If nextPos >= len(frames)
If repeatTimes = 0
Pause() 'stop animation
Else
setCurrentFramePos(0)
repeatTimes :-1
EndIf
EndIf
Endif
End Method
Method Reset()
setCurrentFramePos(0)
End Method
Method ResetFrameTimer()
frameTimer = framesTime[currentFramePos] + Rand(-randomness, randomness)
End Method
Method GetFrameCount:int()
return len(frames)
End Method
Method GetCurrentImageFrame:int()
return currentImageFrame
End Method
Method SetCurrentImageFrame(frame:int)
currentImageFrame = frame
ResetFrameTimer()
End Method
Method GetCurrentFrame:int()
return currentFramePos
End Method
Method SetCurrentFrame(framePos:int)
currentFramePos = Max( Min(framePos, len(frames) - 1), 0)
setCurrentFrame( frames[currentFramePos] )
End Method
Method isPaused:Int()
return paused
End Method
Method isFinished:Int()
return paused AND (currentFramePos >= len(frames)-1)
End Method
Method Playback()
paused = 0
End Method
Method Pause()
paused = 1
End Method
End Type