unbanished_game/behaviour/animation/
shared.rs1use super::Playback;
18use std::cell::RefCell;
19use unbanished_formats::skeleton::Transform;
20use unbanished_formats::types::{AnimationClip, AnimationGroup};
21
22struct Entry {
23 group: String,
24 clip: usize,
25 users: i32,
26 data: AnimationClip,
27 instances: Vec<(Playback, Vec<Transform>)>,
28}
29
30thread_local! {
31 static ENTRIES: RefCell<Vec<Option<Entry>>> = const { RefCell::new(Vec::new()) };
32}
33
34pub fn reset() {
36 ENTRIES.with(|e| e.borrow_mut().clear());
37}
38
39#[derive(Default)]
42pub struct Table(Vec<Option<Entry>>);
43
44pub fn swap(table: &mut Table) {
46 ENTRIES.with(|e| std::mem::swap(&mut *e.borrow_mut(), &mut table.0));
47}
48
49pub struct Saved<'g> {
54 pub group: &'g AnimationGroup,
56 pub clip: usize,
58 pub users: i32,
60 pub instances: Vec<(i64, f32)>,
62}
63
64pub fn restore(slots: Vec<Option<Saved>>) {
69 let entries = slots
70 .into_iter()
71 .map(|slot| {
72 let s = slot?;
73 let data = s.group.clips.get(s.clip)?.clone();
74 let instances = s
75 .instances
76 .into_iter()
77 .map(|(elapsed, speed)| {
78 let mut p = Playback::new(s.clip, elapsed);
79 p.speed = speed;
80 let pose = p.pose(&data);
81 (p, pose)
82 })
83 .collect();
84 Some(Entry {
85 group: key(s.group),
86 clip: s.clip,
87 users: s.users,
88 data,
89 instances,
90 })
91 })
92 .collect();
93 ENTRIES.with(|e| *e.borrow_mut() = entries);
94}
95
96pub fn snapshot() -> Vec<Option<(String, usize, i32, Vec<Playback>)>> {
99 ENTRIES.with(|e| {
100 e.borrow()
101 .iter()
102 .map(|x| {
103 x.as_ref().map(|x| {
104 (
105 x.group.clone(),
106 x.clip,
107 x.users,
108 x.instances.iter().map(|(p, _)| p.clone()).collect(),
109 )
110 })
111 })
112 .collect()
113 })
114}
115
116fn key(group: &AnimationGroup) -> String {
119 if group.name.is_empty() {
120 format!("@{:p}", group)
121 } else {
122 group.name.clone()
123 }
124}
125
126pub fn instance_id(group: &AnimationGroup, clip: usize) -> i32 {
128 let key = key(group);
129 ENTRIES.with(|e| {
130 let mut entries = e.borrow_mut();
131 let found = entries
132 .iter()
133 .position(|x| x.as_ref().is_some_and(|x| x.group == key && x.clip == clip));
134 let index = match found {
135 Some(i) => i,
136 None => {
137 let data = group.clips[clip].clone();
138 let count = data.instance_count.max(1) as usize;
139 let step = super::seconds(data.duration_ticks) * (1.0 / count as f32);
140 let mut at = 0.0f32;
141 let mut instances = Vec::with_capacity(count);
142 for _ in 0..count {
143 let mut p = Playback::new(clip, super::ticks(at));
144 for _ in 0..2 {
145 p.update(&data, 0);
146 }
147 let pose = p.pose(&data);
148 instances.push((p, pose));
149 at += step;
150 }
151 let entry = Entry {
152 group: key.clone(),
153 clip,
154 users: 0,
155 data,
156 instances,
157 };
158 match entries.iter().position(Option::is_none) {
159 Some(free) => {
160 entries[free] = Some(entry);
161 free
162 }
163 None => {
164 entries.push(Some(entry));
165 entries.len() - 1
166 }
167 }
168 }
169 };
170 let entry = entries[index].as_mut().unwrap();
171 entry.users += 1;
172 let mut best = (f32::MAX, 0usize);
173 for (k, (p, _)) in entry.instances.iter().enumerate() {
174 let t = super::seconds(p.elapsed);
175 if t < best.0 {
176 best = (t, k);
177 }
178 }
179 ((index as i32) << 8) | best.1 as i32
180 })
181}
182
183pub fn free(id: i32) {
185 ENTRIES.with(|e| {
186 let mut entries = e.borrow_mut();
187 let i = (id >> 8 & 0xff) as usize;
188 if let Some(Some(entry)) = entries.get_mut(i) {
189 entry.users -= 1;
190 if entry.users == 0 {
191 entries[i] = None;
192 }
193 }
194 });
195}
196
197pub fn update(dt: i64) {
199 ENTRIES.with(|e| {
200 for entry in e.borrow_mut().iter_mut().flatten() {
201 for (p, pose) in &mut entry.instances {
202 p.update(&entry.data, dt);
203 *pose = p.pose(&entry.data);
204 }
205 }
206 });
207}
208
209pub fn poses(slot: usize) -> Vec<Vec<Transform>> {
211 ENTRIES.with(|e| match e.borrow().get(slot) {
212 Some(Some(entry)) => entry.instances.iter().map(|(_, p)| p.clone()).collect(),
213 _ => Vec::new(),
214 })
215}
216
217pub fn pose(id: i32) -> Option<Vec<Transform>> {
219 ENTRIES.with(|e| {
220 let entries = e.borrow();
221 let entry = entries.get((id >> 8 & 0xff) as usize)?.as_ref()?;
222 entry
223 .instances
224 .get((id & 0xff) as usize)
225 .map(|(_, pose)| pose.clone())
226 })
227}