//I explored several ways to have this system only respond when a paddle's position changes //1. _group.OnEntityUpdated += OnPaddlePositionUpdated; I'm using this now. //2. I couldn't find a way to do it with "public TriggerOnEvent trigger". its more about entity add than components, yes? //3. _onPaddlePositionUpdated = _group.CreateObserver(GroupEventType.OnEntityAdded). its more about entity add than components, yes? private void Group_OnEntityUpdated(Group group, Entity entity, int index, IComponent previousComponent, IComponent newComponent) { float sizeY = entity.view.bounds.size.y / 2; Vector3 nextVelocity = entity.velocity.velocity; Vector3 nextPosition = entity.position.position; float bounceAmount = entity.boundsBounce.bounceAmount; //Bottom if (entity.position.position.y - sizeY < _bounds.min.y) { nextVelocity = new Vector3 (nextVelocity.x, nextVelocity.y * bounceAmount, nextVelocity.z); _pool.CreateEntity().AddPlayAudio(GameConstants.Audio_Collision, GameConstants.AudioVolume); //order matters //1 entity.ReplacePosition(new Vector3 (nextPosition.x, _bounds.min.y + sizeY, nextPosition.z)); //2 entity.ReplaceVelocity(nextVelocity); } //Top else if (entity.position.position.y + sizeY > _bounds.max.y) { nextVelocity = new Vector3 (nextVelocity.x, nextVelocity.y * bounceAmount, nextVelocity.z); _pool.CreateEntity().AddPlayAudio(GameConstants.Audio_Collision, GameConstants.AudioVolume); //order matters //1 entity.ReplacePosition(new Vector3 (nextPosition.x, _bounds.max.y - sizeY, nextPosition.z)); //2 entity.ReplaceVelocity(nextVelocity); } }
//I explored several ways to have this system only respond when a paddle's position changes //1. _group.OnEntityUpdated += OnPaddlePositionUpdated; I'm using this now. //2. I couldn't find a way to do it with "public TriggerOnEvent trigger". its more about entity add than components, yes? //3. _onPaddlePositionUpdated = _group.CreateObserver(GroupEventType.OnEntityAdded). its more about entity add than components, yes? private void PaddleGroup_OnEntityAdded(Group group, Entity paddleEntity, int index, IComponent previousComponent, IComponent newComponent) { Bounds bounds = _gameEntity.bounds.bounds; float sizeY = paddleEntity.view.bounds.size.y / 2; Vector3 nextPosition = paddleEntity.position.position; //Be careful only to call paddleEntity.ReplacePosition() within the 'if' to prevent an infinite loop - srivello //Bottom if (paddleEntity.position.position.y - sizeY < bounds.min.y) { nextPosition = new Vector3(nextPosition.x, bounds.min.y + sizeY, nextPosition.z); paddleEntity.ReplacePosition(nextPosition); paddleEntity.ReplaceVelocity(Vector3.zero); } //Top else if (paddleEntity.position.position.y + sizeY > bounds.max.y) { nextPosition = new Vector3(nextPosition.x, bounds.max.y - sizeY, nextPosition.z); paddleEntity.ReplacePosition(nextPosition); paddleEntity.ReplaceVelocity(Vector3.zero); } }