Advertisement
dimmpixeye

Processing Motion

Dec 8th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. public class ProcessingMotion : ProcessingBase, ITick, ITickFixed
  2.     {
  3.         public Group<ComponentMotion, ComponentRigidbody, ComponentObject> groupMotion;
  4.  
  5.         public Group<ComponentInAir, ComponentMotion, ComponentObject> groupInAir;
  6.  
  7.         public ProcessingMotion()
  8.         {
  9.             groupMotion.Add += entity =>
  10.             {
  11.                 var cRigid = entity.ComponentRigidBody();
  12.                 var cObject = entity.ComponentObject();
  13.                 cRigid.body = cObject.transform.GetComponent<Rigidbody2D>();   
  14.             };
  15.         }
  16.  
  17.         public void TickFixed()
  18.         {
  19.             foreach (var entity in groupMotion)
  20.             {
  21.                 var cMotion = entity.ComponentMotion();
  22.                 var cRigid = entity.ComponentRigidBody();
  23.                 cRigid.body.MovePosition(cMotion.positionTo);
  24.             }
  25.         }
  26.  
  27.         public void Tick()
  28.         {
  29.             var delta = Time.delta;
  30.  
  31.  
  32.             foreach (var entity in groupInAir)
  33.             {
  34.                 var cInAir    = entity.ComponentInAir();
  35.                 var cRigid    = entity.ComponentRigidBody();
  36.                 var cMotion   = entity.ComponentMotion();
  37.                 var transform = cRigid.body.transform;
  38.                 var position  = transform.position;
  39.                 cInAir.height = position.y - cInAir.landingSpot;
  40.  
  41.                 if (cInAir.landingSpot >= Game.horizonMax)
  42.                 {
  43.                     cInAir.landingSpot = Game.horizonMax;
  44.                     cMotion.direction.y = 0;
  45.                 }
  46.                 else if (cInAir.landingSpot <= Game.horizonMin)
  47.                 {
  48.                     cInAir.landingSpot = Game.horizonMin;
  49.                     cMotion.direction.y = 0;
  50.                 }
  51.  
  52.                 position.y += cInAir.gravity * delta;
  53.  
  54.                 if (position.y <= cInAir.landingSpot && cInAir.allowLanding)
  55.                 {
  56.                     position.y = cInAir.landingSpot;
  57.                     transform.position = position;
  58.                     cInAir.height = cInAir.landingSpot;
  59.                     cInAir.actionOnFinish(entity);
  60.                     cInAir.allowLanding = false;
  61.                     entity.Remove<ComponentInAir>();
  62.                 }
  63.                 else
  64.                 {
  65.                     transform.position = position;
  66.                 }
  67.             }
  68.  
  69.  
  70.             var bounds = Game.roomBounds;
  71.  
  72.             foreach (var entity in groupMotion)
  73.             {
  74.                 var cMotion = entity.ComponentMotion();
  75.                 var cRigid = entity.ComponentRigidBody();
  76.  
  77.                 var position = cRigid.body.transform.position;  
  78.                 var velocity = cMotion.velocity;
  79.  
  80.  
  81.                 velocity.x += (0 - velocity.x) * delta / cMotion.drag;
  82.                 velocity.y += (0 - velocity.y) * delta / cMotion.drag;
  83.  
  84.                 cMotion.velocity = velocity;
  85.  
  86.                 position.x += (cMotion.velocity.x + cMotion.direction.x * cMotion.speed) * delta;
  87.                 position.y += (cMotion.velocity.y + cMotion.direction.y * cMotion.speed) * delta;
  88.  
  89.                 position.x = Mathf.Clamp(position.x, bounds.min.x, bounds.max.x);
  90.  
  91.                 cMotion.positionTo = position;
  92.             }
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement