Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ProcessingMotion : ProcessingBase, ITick, ITickFixed
- {
- public Group<ComponentMotion, ComponentRigidbody, ComponentObject> groupMotion;
- public Group<ComponentInAir, ComponentMotion, ComponentObject> groupInAir;
- public ProcessingMotion()
- {
- groupMotion.Add += entity =>
- {
- var cRigid = entity.ComponentRigidBody();
- var cObject = entity.ComponentObject();
- cRigid.body = cObject.transform.GetComponent<Rigidbody2D>();
- };
- }
- public void TickFixed()
- {
- foreach (var entity in groupMotion)
- {
- var cMotion = entity.ComponentMotion();
- var cRigid = entity.ComponentRigidBody();
- cRigid.body.MovePosition(cMotion.positionTo);
- }
- }
- public void Tick()
- {
- var delta = Time.delta;
- foreach (var entity in groupInAir)
- {
- var cInAir = entity.ComponentInAir();
- var cRigid = entity.ComponentRigidBody();
- var cMotion = entity.ComponentMotion();
- var transform = cRigid.body.transform;
- var position = transform.position;
- cInAir.height = position.y - cInAir.landingSpot;
- if (cInAir.landingSpot >= Game.horizonMax)
- {
- cInAir.landingSpot = Game.horizonMax;
- cMotion.direction.y = 0;
- }
- else if (cInAir.landingSpot <= Game.horizonMin)
- {
- cInAir.landingSpot = Game.horizonMin;
- cMotion.direction.y = 0;
- }
- position.y += cInAir.gravity * delta;
- if (position.y <= cInAir.landingSpot && cInAir.allowLanding)
- {
- position.y = cInAir.landingSpot;
- transform.position = position;
- cInAir.height = cInAir.landingSpot;
- cInAir.actionOnFinish(entity);
- cInAir.allowLanding = false;
- entity.Remove<ComponentInAir>();
- }
- else
- {
- transform.position = position;
- }
- }
- var bounds = Game.roomBounds;
- foreach (var entity in groupMotion)
- {
- var cMotion = entity.ComponentMotion();
- var cRigid = entity.ComponentRigidBody();
- var position = cRigid.body.transform.position;
- var velocity = cMotion.velocity;
- velocity.x += (0 - velocity.x) * delta / cMotion.drag;
- velocity.y += (0 - velocity.y) * delta / cMotion.drag;
- cMotion.velocity = velocity;
- position.x += (cMotion.velocity.x + cMotion.direction.x * cMotion.speed) * delta;
- position.y += (cMotion.velocity.y + cMotion.direction.y * cMotion.speed) * delta;
- position.x = Mathf.Clamp(position.x, bounds.min.x, bounds.max.x);
- cMotion.positionTo = position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement