예제 #1
0
        public void SignIn(Entity entity)
        {
            if (entity != null)
            {
                entity.OnActivStateChanged += Entity_OnActivStateChanged;

                if (entity.IsActiv)
                {
                    entities.Add(entity);
                }

                if (entity is CollidAble)
                {
                    if (entity.IsActiv)
                    {
                        CollidAble collidableEnt = entity as CollidAble;
                        collidAbleEntities.Add(collidableEnt);
                        CollisionDetection.ActivCollisionDetection.SignIn(collidableEnt);
                    }
                }

                if (entity is DrawAble)
                {
                    DrawAble drawAble = entity as DrawAble;
                    drawAble.OnVisabillityChanged += DrawAble_OnVisabillityChanged;
                    if (drawAble.IsVisable)
                    {
                        drawAbleEntities.Add(drawAble);
                    }
                }
            }
        }
예제 #2
0
        public CollisionGrid[] UpdateGrids(CollidAble collidable)
        {
            List <CollisionGrid> newGrids      = CheckGridIntersections(collidable);
            List <int>           alreadyExists = new List <int>();

            //Remove
            for (int i = 0; i < collidable.Grids.Length; i++)
            {
                if (!newGrids.Contains(collidable.Grids[i]))
                {
                    collidable.Grids[i].CollisionObjects.Remove(collidable);
                }
                else
                {
                    alreadyExists.Add(i);
                }
            }

            //Add
            for (int i = 0; i < newGrids.Count; i++)
            {
                if (!alreadyExists.Contains(i))
                {
                    newGrids[i].CollisionObjects.Add(collidable);
                }
            }

            return(newGrids.ToArray());
        }
예제 #3
0
 public void SignOut(CollidAble collidable)
 {
     if (collidable != null)
     {
         collisionObjects.Remove(collidable);
         if (collidable.Grids != null)
         {
             for (int i = 0; i < collidable.Grids.Length; i++)
             {
                 collidable.Grids[i].CollisionObjects.Remove(collidable);
             }
         }
     }
 }
예제 #4
0
        private List <CollisionGrid> CheckGridIntersections(CollidAble collidable)
        {
            List <CollisionGrid> collisions = new List <CollisionGrid>();

            for (int i = 0; i < grids.Length; i++)
            {
                if (collidable.CollisionBox.Intersects(grids[i].CollisionBox))
                {
                    collisions.Add(grids[i]);
                }
            }

            return(collisions);
        }
예제 #5
0
        public void SignOut(Entity entity)
        {
            if (entity != null)
            {
                entities.Remove(entity);

                if (entity is DrawAble)
                {
                    drawAbleEntities.Remove(entity as DrawAble);
                }

                if (entity is CollidAble)
                {
                    CollidAble col = entity as CollidAble;

                    collidAbleEntities.Remove(col);
                    CollisionDetection.ActivCollisionDetection.SignOut(col);
                }
            }
        }
예제 #6
0
        public List <CollidAble> CheckCollision(CollidAble collidable)
        {
            CollisionGrid[]   grids      = collidable.Grids;
            List <CollidAble> collisions = new List <CollidAble>();

            for (int i = 0; i < grids.Length; i++)
            {
                for (int j = 0; j < grids[i].CollisionObjects.Count; j++)
                {
                    if (collidable != grids[i].CollisionObjects[j])
                    {
                        if (collidable.CollisionBox.Intersects(grids[i].CollisionObjects[j].CollisionBox))
                        {
                            collisions.Add(grids[i].CollisionObjects[j]);
                        }
                    }
                }
            }

            return(collisions);
        }
예제 #7
0
        public CollisionGrid[] SignIn(CollidAble collidable)
        {
            if (collidable != null)
            {
                if (!collisionObjects.Contains(collidable))
                {
                    collisionObjects.Add(collidable);
                }

                List <CollisionGrid> grids = CheckGridIntersections(collidable);
                for (int i = 0; i < grids.Count; i++)
                {
                    grids[i].CollisionObjects.Add(collidable);
                }

                collidable.Grids = grids.ToArray();

                return(grids.ToArray());
            }
            return(null);
        }
예제 #8
0
        private void Entity_OnActivStateChanged(object sender, System.EventArgs e)
        {
            Entity ent = sender as Entity;

            if (ent != null)
            {
                if (ent.IsActiv)
                {
                    if (!entities.Contains(ent))
                    {
                        entities.Add(ent);
                    }

                    if (ent is CollidAble)
                    {
                        CollidAble collidableEnt = ent as CollidAble;
                        if (!collidAbleEntities.Contains(collidableEnt))
                        {
                            collidAbleEntities.Add(collidableEnt);
                            CollisionDetection.ActivCollisionDetection.SignIn(collidableEnt);
                        }
                    }
                }
                else
                {
                    entities.Remove(ent);

                    if (ent is CollidAble)
                    {
                        CollidAble collidableEnt = ent as CollidAble;
                        collidAbleEntities.Remove(collidableEnt);
                        CollisionDetection.ActivCollisionDetection.SignOut(collidableEnt);
                    }
                }
            }
        }
예제 #9
0
        public void DoCollisionCheck(bool triggered = false, CollidAble collidable = null)
        {
            if (IsActiv)
            {
                List <CollidAble> collisions;
                List <int>        alreadyExists = new List <int>();
                if (!triggered)
                {
                    grids      = CollisionDetection.ActivCollisionDetection.UpdateGrids(this);
                    collisions = CollisionDetection.ActivCollisionDetection.CheckCollision(this);
                }
                else
                {
                    collisions = new List <CollidAble>();
                    collisions.Add(collidable);
                }

                if (OnCollisionExit != null || OnTriggerExit != null)
                {
                    for (int i = 0; i < previouseCollisions.Length; i++)
                    {
                        if (!collisions.Contains(previouseCollisions[i]))
                        {
                            if (blocking && previouseCollisions[i].blocking)
                            {
                                OnCollisionExit?.Invoke(previouseCollisions[i], new CollisionArgs(previouseCollisions[i]));
                                if (!triggered)
                                {
                                    collisions[i].DoCollisionCheck(true, this);
                                }
                            }
                            else
                            {
                                OnTriggerExit?.Invoke(previouseCollisions[i], new CollisionArgs(previouseCollisions[i]));
                                if (!triggered)
                                {
                                    collisions[i].DoCollisionCheck(true, this);
                                }
                            }
                        }
                        else
                        {
                            alreadyExists.Add(i);
                        }
                    }
                }

                for (int i = 0; i < collisions.Count; i++)
                {
                    if (!alreadyExists.Contains(i))
                    {
                        if (blocking && collisions[i].blocking)
                        {
                            OnCollisionEnter?.Invoke(collisions[i], new CollisionArgs(collisions[i]));
                            if (!triggered)
                            {
                                collisions[i].DoCollisionCheck(true, this);
                            }
                        }
                        else
                        {
                            OnTriggerEnter?.Invoke(collisions[i], new CollisionArgs(collisions[i]));
                            if (!triggered)
                            {
                                collisions[i].DoCollisionCheck(true, this);
                            }
                        }
                    }
                    else
                    {
                        if (blocking && collisions[i].blocking)
                        {
                            OnCollision?.Invoke(collisions[i], new CollisionArgs(collisions[i]));
                            if (!triggered)
                            {
                                collisions[i].DoCollisionCheck(true, this);
                            }
                        }
                        else
                        {
                            OnTrigger?.Invoke(collisions[i], new CollisionArgs(collisions[i]));
                            if (!triggered)
                            {
                                collisions[i].DoCollisionCheck(true, this);
                            }
                        }
                    }
                }
            }
        }
예제 #10
0
 public CollisionArgs(CollidAble collisionObject)
 {
     CollisionObject = collisionObject;
 }