Debian Codegraph @

src/animation.rs

raw symbols SCIP
1 /*
2 * Copyright 2024 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use std::ops::Range;
21
22 pub trait Animation {
23 fn frame(&self, frame: usize) -> Option<usize>;
24 fn len(&self) -> usize;
25
26 fn limit(self, limit: usize) -> impl Animation
27 where
28 Self: Sized,
29 {
30 Limit { inner: self, limit }
31 }
32
33 fn then<A: Animation>(self, another: A) -> impl Animation
34 where
35 Self: Sized,
36 {
37 Then {
38 first: self,
39 second: another,
40 }
41 }
42
43 fn repeat(self, count: usize) -> impl Animation
44 where
45 Self: Sized,
46 {
47 Repeat { inner: self, count }
48 }
49
50 fn forever(self) -> impl InfiniteAnimation
51 where
52 Self: Sized,
53 {
54 Forever { inner: self }
55 }
56 }
57
58 pub trait InfiniteAnimation {
59 fn frame(&self, frame: usize) -> usize;
60 }
61
62 pub fn sequence(range: Range<usize>) -> impl Animation {
63 Sequence {
64 start: range.start,
65 limit: range.len(),
66 step: 1,
67 }
68 }
69
70 pub fn rev_sequence(range: Range<usize>) -> impl Animation {
71 let count = range.len();
72 Sequence {
73 start: range.start + count - 1,
74 limit: count,
75 step: -1,
76 }
77 }
78
79 struct Sequence {
80 start: usize,
81 limit: usize,
82 step: isize,
83 }
84
85 impl Animation for Sequence {
86 fn frame(&self, frame: usize) -> Option<usize> {
87 if frame >= self.limit {
88 return None;
89 }
90 Some(
91 self.start
92 .saturating_add_signed((frame as isize) * self.step),
93 )
94 }
95
96 fn len(&self) -> usize {
97 self.limit
98 }
99 }
100
101 struct Limit<A: Animation> {
102 inner: A,
103 limit: usize,
104 }
105
106 impl<A: Animation> Animation for Limit<A> {
107 fn frame(&self, frame: usize) -> Option<usize> {
108 if frame >= self.limit {
109 return None;
110 }
111 self.inner.frame(frame)
112 }
113
114 fn len(&self) -> usize {
115 self.inner.len().min(self.limit)
116 }
117 }
118
119 pub fn bounce(range: Range<usize>) -> impl Animation {
120 sequence(range.clone()).then(rev_sequence(range))
121 }
122
123 struct Then<F: Animation, S: Animation> {
124 first: F,
125 second: S,
126 }
127
128 impl<F: Animation, S: Animation> Animation for Then<F, S> {
129 fn frame(&self, frame: usize) -> Option<usize> {
130 let first_len = self.first.len();
131 if frame < first_len {
132 self.first.frame(frame)
133 } else {
134 self.second.frame(frame - first_len)
135 }
136 }
137
138 fn len(&self) -> usize {
139 self.first.len() + self.second.len()
140 }
141 }
142
143 struct Repeat<A: Animation> {
144 inner: A,
145 count: usize,
146 }
147
148 impl<A: Animation> Animation for Repeat<A> {
149 fn frame(&self, frame: usize) -> Option<usize> {
150 let len = self.inner.len();
151 if frame / len < self.count {
152 self.inner.frame(frame % len)
153 } else {
154 None
155 }
156 }
157
158 fn len(&self) -> usize {
159 self.inner.len() * self.count
160 }
161 }
162
163 struct Forever<A: Animation> {
164 inner: A,
165 }
166
167 impl<A: Animation> InfiniteAnimation for Forever<A> {
168 fn frame(&self, frame: usize) -> usize {
169 self.inner.frame(frame % self.inner.len()).unwrap()
170 }
171 }
172
173 #[cfg(test)]
174 mod test {
175 use super::*;
176 use crate::theme::PLAYER_ANIMATION_FRAMES;
177
178 #[test]
179 fn test_sequence() {
180 let animation = sequence(0..3);
181
182 assert_eq!(
183 (0..4).map(|f| animation.frame(f)).collect::<Vec<_>>(),
184 vec![Some(0), Some(1), Some(2), None,]
185 );
186 }
187
188 #[test]
189 fn test_repeat() {
190 let animation = rev_sequence(4..6).repeat(3);
191
192 assert_eq!(
193 (0..7).map(|f| animation.frame(f)).collect::<Vec<_>>(),
194 vec![Some(5), Some(4), Some(5), Some(4), Some(5), Some(4), None]
195 );
196 }
197
198 #[test]
199 fn test_player_animation() {
200 let animation = sequence(PLAYER_ANIMATION_FRAMES)
201 .limit(1)
202 .repeat(5)
203 .then(bounce(PLAYER_ANIMATION_FRAMES).repeat(2))
204 .forever();
205
206 assert_eq!(
207 (0..26).map(|f| animation.frame(f)).collect::<Vec<_>>(),
208 vec![
209 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 0, 0, 0, 0, 0, 0,
210 ]
211 );
212 }
213 }