Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace scare2D
- {
- // define some component
- public struct PingPongComponent : IComponentData
- {
- public float2 velocity;
- }
- // define a component system
- public class PingPongSystem : IComponentSystem
- {
- // define a Group of Components processed by the system
- public class PingPong : IComponentGroup
- {
- public Transform transform;
- public BoxCollider collider;
- public PingPongComponent ball;
- }
- // this collection will be automagically filled by the engine during runtime
- IComponentCollection<PingPong> items;
- // all arguments will be automagically be provided by the engine
- // the runtime model makes sur that before calling update, rect and deltaTime will be provided to the entity system
- public void Update(float4 rect, float deltaTime)
- {
- float4 window = rect;
- // iterate through all component group
- foreach (ref var item in items)
- {
- var origin = item.collider.origin;
- rect = window + new float4(origin.X, origin.Y, origin.X, origin.Y);
- var position = item.transform.position;
- var velocity = item.ball.velocity;
- var p = new float2(position.X + velocity.X * deltaTime, position.Y + velocity.Y * deltaTime);
- var half = item.collider.halfExtends;
- if (p.X - half.X < rect.X || rect.Z < p.X + half.X)
- {
- item.ball.velocity.X = -item.ball.velocity.X;
- p.X = p.X.Clamp(rect.X + half.X, rect.Z - half.X);
- }
- if (p.Y - half.Y < rect.Y || rect.W < p.Y + half.Y)
- {
- item.ball.velocity.Y = -item.ball.velocity.Y;
- p.Y = p.Y.Clamp(rect.Y + half.Y, rect.W - half.Y);
- }
- item.transform.position = p;
- // at the end of the loop iteration the enumerator will check what components have changed
- // and update these components
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement