@@ -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,42 @@ 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 dv = c ( dv) > 0.0 ;
268+ let ( new, old) = if dv {
269+ ( c ( new. pos + new. size ) , c ( old. pos + old. size ) )
270+ } else {
271+ ( c ( old. pos ) , c ( new. pos ) )
272+ } ;
273+ if dv == ( new - old > 0.0 ) {
274+ new - old
275+ } else {
276+ 0.0
277+ }
278+ } ;
279+
280+ let x = f ( aabb, new_aabb, vel. 0 , Vec2 :: x) ;
281+ let y = f ( aabb, new_aabb, vel. 0 , Vec2 :: y) ;
282+ if x. is_finite ( ) && y. is_finite ( ) {
283+ let diff = Vec2 :: new ( x, y) ;
284+ pos. 0 += diff;
285+ vel. 0 -= diff / dt. 0 . as_secs_f32 ( ) ;
286+ log:: debug!( "x: {}, y: {}, vel: {:?}" , x, y, vel. 0 ) ;
287+ for e in hierarchy. children ( entity) {
288+ if let Some ( trans) = mat3. get_mut ( * e) {
289+ trans. mat3 *= Mat3 :: translation ( diff. x ( ) , diff. y ( ) ) ;
290+ }
291+ if let Some ( sub) = sub. get_mut ( * e) {
292+ match sub. collider {
293+ Collidable :: AABox ( mut a) => a. pos += diff,
294+ Collidable :: RBox ( mut r) => r. pos += diff,
295+ Collidable :: Point ( mut p) => p += diff,
296+ } ;
297+ }
298+ }
299+ }
300+
260301 // modify position to avoid collisions
261302 }
262303 }
0 commit comments