Пример #1
0
 public virtual void OnCollide(ICollidingEntity entity)
 {
     if (!nonPhysics && !entity.nonPhysics)
     {
         AdjustForCollision(entity);
     }
 }
Пример #2
0
 public void CheckFrame(float dt)
 {
     for (int i = 0; i < _entities.Count; i++)
     {
         ICollidingEntity entity1 = _entities[i];
         //triggers dont cause collisions
         if (entity1.isTrigger)
         {
             for (int j = 0; j < _entities.Count; j++)
             {
                 if (i != j)
                 {
                     ICollidingEntity entity2 = _entities[j];
                     if (ICollidingEntity.Collides(ref entity1, ref entity2))
                     {
                         Debug.Log("Collision between " + entity1.name + " and " + entity2.name);
                         entity1.OnCollide(entity2);
                         //entity2.OnCollide(entity1);
                     }
                 }
             }
         }
         entity1.FinalizeFrame(dt);
     }
 }
Пример #3
0
 public static bool Collides(ref ICollidingEntity e1, ref ICollidingEntity e2)
 {
     if (e1 == null || e2 == null)
     {
         return(false);
     }
     return(e1.Overlaps(e2));
 }
Пример #4
0
    Vector2 GetOverlap(ICollidingEntity other)
    {
        Rect r1 = GetNextRect();
        Rect r2 = other.GetNextRect();

        Vector2 dir = (GetNextFramePosition() - new Vector2(this.transform.position.x, this.transform.position.y)).normalized;

        Vector2 overlap = new Vector2(0, 0);

        if (!other.invertedCollider)
        {
            //collides on the right
            if (dir.x > 0 && r1.xMax > r2.xMin)
            {
                overlap.x += (r1.xMax - r2.xMin);
            }
            //collides on the left
            if (dir.x < 0 && r1.xMin < r2.xMax)
            {
                overlap.x -= (r2.xMax - r1.xMin);
            }
            //collides on the top
            if (dir.y > 0 && r1.yMax > r2.yMin)
            {
                overlap.y += (r1.yMax - r2.yMin);
            }
            //collides on the bottom
            if (dir.y < 0 && r1.yMin < r2.yMax)
            {
                overlap.y -= (r2.yMax - r1.yMin);
            }
        }
        else
        {
            //collides on the right
            if (dir.x > 0 && r1.xMax > r2.xMax)
            {
                overlap.x += (r1.xMax - r2.xMax);
            }
            //collides on the left
            if (dir.x < 0 && r1.xMin < r2.xMin)
            {
                overlap.x -= (r2.xMin - r1.xMin);
            }
            //collides on the top
            if (dir.y > 0 && r1.yMax > r2.yMax)
            {
                overlap.y += (r1.yMax - r2.yMax);
            }
            //collides on the bottom
            if (dir.y < 0 && r1.yMin < r2.yMin)
            {
                overlap.y -= (r2.yMin - r1.yMin);
            }
        }
        return(overlap);
    }
Пример #5
0
 public override void OnCollide(ICollidingEntity entity)
 {
     if (entity.type == "Player")
     {
         Ship ship = (Ship)entity;
         if (ship)
         {
         }
     }
     //GameManager.GM.DestroyObject(this);
 }
Пример #6
0
 public void DestroyObject(ICollidingEntity entity, bool destroy = true)
 {
     if (destroy)
     {
         _collisionSystem.DestroyEntity(ref entity);
     }
     else
     {
         _collisionSystem.RemoveEntity(ref entity);
     }
 }
Пример #7
0
    public bool Overlaps(ICollidingEntity other)
    {
        Rect r1 = GetNextRect();
        Rect r2 = other.GetNextRect();

        if (!invertedCollider && !other.invertedCollider)
        {
            return(r1.Overlaps(r2));
        }
        else if (!invertedCollider && other.invertedCollider)
        {
            if (!r1.Overlaps(r2))
            {
                return(true);
            }

            var a          = r2.Contains(new Vector2(r1.xMax, r1.yMax));
            var b          = r2.Contains(new Vector2(r1.xMax, r1.yMin));
            var c          = r2.Contains(new Vector2(r1.xMin, r1.yMax));
            var d          = r2.Contains(new Vector2(r1.xMin, r1.yMin));
            int pointCount = 0;
            if (a)
            {
                pointCount++;
            }
            if (b)
            {
                pointCount++;
            }
            if (c)
            {
                pointCount++;
            }
            if (d)
            {
                pointCount++;
            }

            return(pointCount < 4);
        }
        return(false);
    }
Пример #8
0
 public override void OnCollide(ICollidingEntity entity)
 {
     if (active)
     {
         if (entity.type == "Destroy")
         {
             OnDestruction();
         }
         if (entity.type != "Player" && !entity.nonPhysics)
         {
             OnDestruction();
         }
         else if (entity.type == "Player")
         {
             if (_owner != entity)
             {
                 Ship reciever = (Ship)entity;
                 reciever.TakeDamage(1);
                 OnDestruction();
             }
         }
     }
 }
Пример #9
0
    public override void OnCollide(ICollidingEntity entity)
    {
        if (entity.type == "Bullet")
        {
            IBullet bullet = (IBullet)entity;
            if (bullet)
            {
                if (bullet.OwnedBy != _owner)
                {
                    if (!bulletsReflected.Contains(bullet))
                    {
                        _owner.bulletPool.Create(transform.position, _owner.shootDirection);
                        bulletsReflected.Add(bullet);
                        bullet.OnDestruction();
                    }
                    bulletCollided = bullet;
                }
            }
        }



        base.OnCollide(entity);
    }
Пример #10
0
 public void DestroyEntity(ref ICollidingEntity entity)
 {
     _entities.Remove(entity);
     Destroy(entity.gameObject);
 }
Пример #11
0
 public void RemoveEntity(ref ICollidingEntity entity)
 {
     _entities.Remove(entity);
 }
Пример #12
0
 public void AddEntity(ref ICollidingEntity entity)
 {
     _entities.Add(entity);
 }
Пример #13
0
 public override void OnCollide(ICollidingEntity entity)
 {
     base.OnCollide(entity);
 }
Пример #14
0
 public void AddEntityToCollisionSystem(ICollidingEntity entity)
 {
     _collisionSystem.AddEntity(ref entity);
 }
Пример #15
0
    public void AdjustForCollision(ICollidingEntity other)
    {
        Vector2 overlap = GetOverlap(other);

        _force += -overlap;
    }