@@ -141,10 +141,12 @@ impl<'a> System<'a> for UpdateAnimationSystem {
141141 WriteStorage < ' a , Parent > ,
142142 ReadStorage < ' a , Present > ,
143143 ReadStorage < ' a , Scale > ,
144- ReadStorage < ' a , Pos > ,
144+ WriteStorage < ' a , Pos > ,
145+ WriteStorage < ' a , Vel > ,
145146 Entities < ' a > ,
146147 ReadExpect < ' a , Hierarchy < Parent > > ,
147148 Read < ' a , ElapsedTime > ,
149+ Read < ' a , DeltaTime > ,
148150 ) ;
149151
150152 fn run (
@@ -160,18 +162,21 @@ impl<'a> System<'a> for UpdateAnimationSystem {
160162 mut parent,
161163 present,
162164 scale,
163- pos,
165+ mut pos,
166+ mut vel,
164167 entities,
165168 hierarchy,
166169 elapsed,
170+ dt,
167171 ) : Self :: SystemData ,
168172 ) {
169173 let res = & mut * resources:: RESOURCE_TABLE . write ( ) ;
170- for ( mut animation, collider, scale, pos, entity, _) in (
174+ for ( mut animation, collider, scale, pos, vel , entity, _) in (
171175 & mut animations,
172176 & collider,
173177 & scale,
174- & pos,
178+ & mut pos,
179+ & mut vel,
175180 & entities,
176181 & present,
177182 )
@@ -257,6 +262,39 @@ impl<'a> System<'a> for UpdateAnimationSystem {
257262 . flatten ( ) ,
258263 ) ;
259264
265+ use crate :: boxes:: AABox ;
266+ let f = |old : AABox , new : AABox , dv, c : fn ( Vec2 ) -> f32 | {
267+ let u = |d, b| if b { d } else { 0.0 } ;
268+ if c ( dv) > 0.0 {
269+ let d = c ( old. pos + old. size ) - c ( new. pos + new. size ) ;
270+ u ( d, d < 0.0 )
271+ } else {
272+ let d = c ( old. pos - new. pos ) ;
273+ u ( d, d > 0.0 )
274+ }
275+ } ;
276+
277+ let x = f ( aabb, new_aabb, vel. 0 , Vec2 :: x) ;
278+ let y = f ( aabb, new_aabb, vel. 0 , Vec2 :: y) ;
279+ if x. is_finite ( ) && y. is_finite ( ) {
280+ let diff = Vec2 :: new ( x, y) ;
281+ pos. 0 += diff;
282+ vel. 0 -= diff / dt. 0 . as_secs_f32 ( ) ;
283+ log:: debug!( "x: {}, y: {}, vel: {:?}" , x, y, vel. 0 ) ;
284+ for e in hierarchy. children ( entity) {
285+ if let Some ( trans) = mat3. get_mut ( * e) {
286+ trans. mat3 *= Mat3 :: translation ( diff. x ( ) , diff. y ( ) ) ;
287+ }
288+ if let Some ( sub) = sub. get_mut ( * e) {
289+ match sub. collider {
290+ Collidable :: AABox ( mut a) => a. pos += diff,
291+ Collidable :: RBox ( mut r) => r. pos += diff,
292+ Collidable :: Point ( mut p) => p += diff,
293+ } ;
294+ }
295+ }
296+ }
297+
260298 // modify position to avoid collisions
261299 }
262300 }
0 commit comments