-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanimation.ms
107 lines (87 loc) · 2.15 KB
/
animation.ms
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
// Animation class
// It can animate a Sprite from its current position to
// a target position in the desired amount of time, in a
// series of steps.
//
// This last point is important, since it means other things
// can happen at the same time while animating (e.g. processing
// user input, animating other sprites, etc.)
//
// Basic usage:
//
// a = new Animation
// a.init someSprite, targetX, targetY, durationInSeconds
//
// while a.isRunning
// a.update
// yield
// end while
Animation = {}
Animation.sprite = null
Animation.durationSecs = 1
Animation.startX = 0
Animation.startY = 0
Animation.endX = 0
Animation.endY = 0
Animation.startTime = null
Animation.endTime = null
Animation.init = function(sprite, endX, endY, durationSecs=1)
self.sprite = sprite
self.startX = sprite.x
self.startY = sprite.y
self.endX = endX
self.endY = endY
self.deltaX = endX - self.startX
self.deltaY = endY - self.startY
self.startTime = time
self.durationSecs = durationSecs
self.endTime = self.startTime + durationSecs
end function
Animation.update = function
if self.hasTimeLeft then
elapsedTime = time - self.startTime
newX = self.startX + self.deltaX * elapsedTime / self.durationSecs
newY = self.startY + self.deltaY * elapsedTime / self.durationSecs
self.sprite.x = newX
self.sprite.y = newY
else
// Force end position if time is up
self.sprite.x = self.endX
self.sprite.y = self.endY
end if
end function
Animation.hasTimeLeft = function
return time < self.endTime
end function
Animation.endPositionReached = function
endXreached = self.sprite.x == self.endX
endYreached = self.sprite.y == self.endY
return endXreached and endYreached
end function
Animation.isRunning = function
return not self.endPositionReached
end function
// Example
if globals == locals then
s = new Sprite
s.image = file.loadImage("/sys/pics/Wumpus.png")
s.x = 20
s.y = 300
clear
sprDisp = display(4)
sprDisp.sprites.push s
while true
a1 = new Animation
a1.init s,800,s.y,2
while a1.isRunning
a1.update
yield
end while
a2 = new Animation
a2.init s,20,s.y,2
while a2.isRunning
a2.update
yield
end while
end while
end if