Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace scare2D
  8. {
  9.     // define some component
  10.     public struct PingPongComponent : IComponentData
  11.     {
  12.         public float2 velocity;
  13.     }
  14.    
  15.     // define a component system
  16.     public class PingPongSystem : IComponentSystem
  17.     {
  18.         // define a Group of Components processed by the system
  19.         public class PingPong : IComponentGroup
  20.         {
  21.             public Transform transform;
  22.             public BoxCollider collider;
  23.             public PingPongComponent ball;
  24.         }
  25.  
  26.         // this collection will be automagically filled by the engine during runtime
  27.         IComponentCollection<PingPong> items;
  28.  
  29.         // all arguments will be automagically be provided by the engine
  30.         // the runtime model makes sur that before calling update, rect and deltaTime will be provided to the entity system
  31.         public void Update(float4 rect, float deltaTime)
  32.         {
  33.             float4 window = rect;
  34.             // iterate through all component group
  35.             foreach (ref var item in items)
  36.             {
  37.                 var origin = item.collider.origin;
  38.                 rect = window + new float4(origin.X, origin.Y, origin.X, origin.Y);
  39.  
  40.                 var position = item.transform.position;
  41.                 var velocity = item.ball.velocity;
  42.                 var p = new float2(position.X + velocity.X * deltaTime, position.Y + velocity.Y * deltaTime);
  43.                 var half = item.collider.halfExtends;
  44.  
  45.                 if (p.X - half.X < rect.X || rect.Z < p.X + half.X)
  46.                 {
  47.                     item.ball.velocity.X = -item.ball.velocity.X;
  48.                     p.X = p.X.Clamp(rect.X + half.X, rect.Z - half.X);
  49.                 }
  50.                 if (p.Y - half.Y < rect.Y || rect.W < p.Y + half.Y)
  51.                 {
  52.                     item.ball.velocity.Y = -item.ball.velocity.Y;
  53.                     p.Y = p.Y.Clamp(rect.Y + half.Y, rect.W - half.Y);
  54.                 }
  55.  
  56.                 item.transform.position = p;
  57.                 // at the end of the loop iteration the enumerator will check what components have changed
  58.                 // and update these components
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement