private void OnLivesChanged(Group @group, Entity entity, int index, IComponent component)
        {
            livesTxt.text = "" + entity.lives.lives;

            if (entity.lives.lives == 0)
                gameOverTxt.gameObject.SetActive(true);
        }
        //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);
            }
        }
        public void Initialize()
        {
            _goalGroup = _pool.GetGroup(Matcher.AllOf(Matcher.Goal, Matcher.Position, Matcher.Velocity));

            //By design: Systems created before Entities, so wait :)
            _pool.GetGroup(Matcher.AllOf(Matcher.Game, Matcher.Bounds, Matcher.Score)).OnEntityAdded += OnGameEntityAdded;
        }
示例#4
0
        ReactiveSystem(Pool pool, IReactiveExecuteSystem subSystem, TriggerOnEvent[] triggers)
        {
            _subsystem = subSystem;
            var ensureComponents = subSystem as IEnsureComponents;
            if (ensureComponents != null) {
                _ensureComponents = ensureComponents.ensureComponents;
            }
            var excludeComponents = subSystem as IExcludeComponents;
            if (excludeComponents != null) {
                _excludeComponents = excludeComponents.excludeComponents;
            }

            _clearAfterExecute = (subSystem as IClearReactiveSystem) != null;

            var triggersLength = triggers.Length;
            var groups = new Group[triggersLength];
            var eventTypes = new GroupEventType[triggersLength];
            for (int i = 0; i < triggersLength; i++) {
                var trigger = triggers[i];
                groups[i] = pool.GetGroup(trigger.trigger);
                eventTypes[i] = trigger.eventType;
            }
            _observer = new GroupObserver(groups, eventTypes);
            _buffer = new List<Entity>();
        }
 public GroupObserver(Group group, GroupEventType eventType)
 {
     _collectedEntities = new HashSet<Entity>(EntityEqualityComparer.comparer);
     _group = group;
     _eventType = eventType;
     Activate();
 }
 public void SetPool(Pool pool)
 {
     _pool = pool;
     _asteroids = pool.GetGroup(Matcher.Asteroid);
     _levels = pool.GetGroup(Matcher.Level);
     _players = pool.GetGroup(Matcher.Player);
 }
        public void Initialize()
        {
            _group = _pool.GetGroup(Matcher.AllOf(Matcher.Paddle, Matcher.Position, Matcher.View));
            _group.OnEntityUpdated += PaddleGroup_OnEntityAdded;

            //By design: Systems created before Entities, so wait :)
            _pool.GetGroup(Matcher.AllOf(Matcher.Game, Matcher.Bounds, Matcher.Score)).OnEntityAdded += GameGroup_OnEntityAdded;
        }
 //Whenever a new ball is created, follow it
 protected virtual void BallCreatedGroup_OnEntityAdded(Group collection, Entity ballEntity, int index, IComponent component)
 {
     //Debug.Log ("created" + ballEntity);
     foreach (var entity in _aiGroup.GetEntities())
     {
         entity.ReplaceAI (ballEntity, entity.aI.deadZoneY, entity.aI.velocityY);
     }
 }
        public void Initialize()
        {
            // Get the group of entities that have a Move and Position component
            _inputGroup = _pool.GetGroup(Matcher.AllOf(Matcher.Input));
            _acceptInputGroup = _pool.GetGroup(Matcher.AllOf(Matcher.AcceptInput));

            //By design: Systems created before Entities, so wait :)
            _pool.GetGroup(Matcher.AllOf(Matcher.Score)).OnEntityAdded += OnGameEntityAdded;
        }
 public void SetPool(Pool pool)
 {
     _pool = pool;
     _pool.GetGroup(Matcher.SpaceshipDeathroes).OnEntityRemoved += OnDeaththroesRemoved;
     _waiting = _pool.GetGroup(Matcher.AllOf(Matcher.WaitingForSpace, Matcher.Spaceship));
     _asteroids = pool.GetGroup(Matcher.AllOf(Matcher.Asteroid, Matcher.CollisionRadius));
     _games = _pool.GetGroup(Matcher.AllOf(Matcher.Game, Matcher.Bounds));
     _lives = _pool.GetGroup(Matcher.Lives);
 }
示例#11
0
 ReactiveSystem(Pool pool, IReactiveExecuteSystem subSystem, IMatcher[] triggers, GroupEventType[] eventTypes)
 {
     _subsystem = subSystem;
     var groups = new Group[triggers.Length];
     for (int i = 0, triggersLength = triggers.Length; i < triggersLength; i++) {
         groups[i] = pool.GetGroup(triggers[i]);
     }
     _observer = new GroupObserver(groups, eventTypes);
     _buffer = new List<Entity>();
 }
        private void OnDeaththroesRemoved(Group group, Entity entity, int index, IComponent component)
        {
            if (_lives.GetSingleEntity().lives.lives == 0)
                return;

            _pool.CreateEntity()
                .AddSpaceship(0.5f, 0.02f)
                .AddCollisionRadius(5)
                .IsWaitingForSpace(true);
        }
        public void Initialize()
        {
            // Get the group of entities that have these component(s)
            _aiGroup = _pool.GetGroup(Matcher.AllOf(Matcher.AI, Matcher.Position, Matcher.Velocity));

            Group ballCreatedGroup = _pool.GetGroup(Matcher.AllOf(Matcher.Goal, Matcher.Position).NoneOf (Matcher.Destroy));
            ballCreatedGroup.OnEntityAdded += BallCreatedGroup_OnEntityAdded;

            Group ballDestroyGroup = _pool.GetGroup(Matcher.AllOf(Matcher.Goal, Matcher.Position, Matcher.Destroy));
            ballDestroyGroup.OnEntityAdded += BallDestroyGroup_OnEntityAdded;
        }
示例#14
0
        public GroupObserver(Group[] groups, GroupEventType[] eventTypes)
        {
            if (groups.Length != eventTypes.Length) {
                throw new GroupObserverException("Unbalanced count with groups (" + groups.Length +
                    ") and event types (" + eventTypes.Length + ")");
            }

            _collectedEntities = new HashSet<Entity>(EntityEqualityComparer.comparer);
            _groups = groups;
            _eventTypes = eventTypes;
            Activate();
        }
 ReactiveSystem(Pool pool, IReactiveExecuteSystem subSystem, IMatcher[] triggers, GroupEventType[] eventTypes)
 {
     _subsystem = subSystem;
     var ensureComponents = subSystem as IEnsureComponents;
     if (ensureComponents != null) {
         _ensureComponents = ensureComponents.ensureComponents;
     }
     var excludeComponents = subSystem as IExcludeComponents;
     if (excludeComponents != null) {
         _excludeComponents = excludeComponents.excludeComponents;
     }
     var groups = new Group[triggers.Length];
     for (int i = 0, triggersLength = triggers.Length; i < triggersLength; i++) {
         groups[i] = pool.GetGroup(triggers[i]);
     }
     _observer = new GroupObserver(groups, eventTypes);
     _buffer = new List<Entity>();
 }
        private void SetGameGroup(Group group)
        {
            //The group should have 1 thing, always, but...
            //FIXME: after multiple restarts (via 'r' button in HUD) this fails - srivello
            if (group.count == 1)
            {
                _gameEntity = group.GetSingleEntity();
                _gameEntity.OnComponentReplaced += Entity_OnComponentReplaced;

                //set first value
                var scoreComponent = _gameEntity.score;
                SetText (scoreComponent.score);

            }
            else
            {
                Debug.LogWarning ("CC _scoreGroup failed, should have one entity, has count: " + group.count);
            }
        }
示例#17
0
        /// Creates an Collector and will collect changed entities
        /// based on the specified groupEvents.
        public Collector(Group[] groups, GroupEvent[] groupEvents)
        {
            _groups = groups;
            _collectedEntities = new HashSet<Entity>(
                EntityEqualityComparer.comparer
            );
            _groupEvents = groupEvents;

            if(groups.Length != groupEvents.Length) {
                throw new CollectorException(
                    "Unbalanced count with groups (" + groups.Length +
                    ") and group events (" + groupEvents.Length + ").",
                    "Group and group events count must be equal."
                );
            }

            _addEntityCache = addEntity;
            Activate();
        }
        //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);
            }
        }
 public void Initialize()
 {
     // Get the group of entities that have these component(s)
     _group = _pool.GetGroup(Matcher.AllOf(Matcher.Velocity, Matcher.Position, Matcher.Friction, Matcher.Tick));
 }
 public void SetPool(Pool pool)
 {
     _pool = pool;
     _lives = pool.GetGroup(Matcher.Lives);
     _spaceships = pool.GetGroup(Matcher.AllOf(Matcher.Spaceship, Matcher.View));
 }
 private void GameGroup_OnEntityAdded(Group group, Entity entity, int index, IComponent component)
 {
     //TODO: I expect this to be called on game start and game restart, but not every StartNextRound, why - srivello
     //Debug.Log("added _gameEntity: " + _gameEntity);
     _gameEntity = group.GetSingleEntity();
 }
示例#22
0
 /// Creates an Collector and will collect changed entities
 /// based on the specified groupEvent.
 public Collector(Group group, GroupEvent groupEvent)
     : this(new [] { group }, new [] { groupEvent })
 {
 }
示例#23
0
 void addEntity(Group group, Entity entity, int index, IComponent component)
 {
     _collectedEntities.Add(entity);
 }
示例#24
0
        public virtual Group GetGroup(IMatcher matcher)
        {
            Group group;
            if (!_groups.TryGetValue(matcher, out group)) {
                group = new Group(matcher);
                var entities = GetEntities();
                for (int i = 0, entitiesLength = entities.Length; i < entitiesLength; i++) {
                    group.HandleEntitySilently(entities[i]);
                }
                _groups.Add(matcher, group);

                for (int i = 0, indicesLength = matcher.indices.Length; i < indicesLength; i++) {
                    var index = matcher.indices[i];
                    if (_groupsForIndex[index] == null) {
                        _groupsForIndex[index] = new List<Group>();
                    }
                    _groupsForIndex[index].Add(group);
                }

                if (OnGroupCreated != null) {
                    OnGroupCreated(this, group);
                }
            }

            return group;
        }
示例#25
0
 void addEntity(Group group,
                Entity entity,
                int index,
                IComponent component)
 {
     var added = _collectedEntities.Add(entity);
     if(added) {
         entity.Retain(this);
     }
 }
示例#26
0
        static GroupObserver createGroupObserver(Pool pool, TriggerOnEvent[] triggers)
        {
            var triggersLength = triggers.Length;
            var groups = new Group[triggersLength];
            var eventTypes = new GroupEventType[triggersLength];
            for (int i = 0; i < triggersLength; i++) {
                var trigger = triggers[i];
                groups[i] = pool.GetGroup(trigger.trigger);
                eventTypes[i] = trigger.eventType;
            }

            return new GroupObserver(groups, eventTypes);
        }
 //whenever a ball is destroyed, stop following it.
 protected virtual void BallDestroyGroup_OnEntityAdded(Group collection, Entity ballEntity, int index, IComponent component)
 {
     //Debug.Log ("destroy" + ballEntity);
     foreach (var entity in _aiGroup.GetEntities())
     {
         entity.ReplaceAI (null, entity.aI.deadZoneY, entity.aI.velocityY);
         entity.ReplaceVelocity ( new RMC.Common.UnityEngineReplacement.Vector3 (0,0,0));
     }
 }
示例#28
0
 public GroupObserver(Group group, GroupEventType eventType)
     : this(new [] { group }, new [] { eventType })
 {
 }
 public void Initialize()
 {
     // Get the group of entities that have these component(s)
     _inputGroup = _pool.GetGroup(Matcher.AllOf(Matcher.Input));
     _acceptInputGroup = _pool.GetGroup(Matcher.AllOf(Matcher.AcceptInput));
 }
示例#30
0
 public void SetPool(Pool pool)
 {
     _ages = pool.GetGroup(Matcher.Age);
 }