public virtual void OnCollide(ICollidingEntity entity) { if (!nonPhysics && !entity.nonPhysics) { AdjustForCollision(entity); } }
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); } }
public static bool Collides(ref ICollidingEntity e1, ref ICollidingEntity e2) { if (e1 == null || e2 == null) { return(false); } return(e1.Overlaps(e2)); }
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); }
public override void OnCollide(ICollidingEntity entity) { if (entity.type == "Player") { Ship ship = (Ship)entity; if (ship) { } } //GameManager.GM.DestroyObject(this); }
public void DestroyObject(ICollidingEntity entity, bool destroy = true) { if (destroy) { _collisionSystem.DestroyEntity(ref entity); } else { _collisionSystem.RemoveEntity(ref entity); } }
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); }
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(); } } } }
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); }
public void DestroyEntity(ref ICollidingEntity entity) { _entities.Remove(entity); Destroy(entity.gameObject); }
public void RemoveEntity(ref ICollidingEntity entity) { _entities.Remove(entity); }
public void AddEntity(ref ICollidingEntity entity) { _entities.Add(entity); }
public override void OnCollide(ICollidingEntity entity) { base.OnCollide(entity); }
public void AddEntityToCollisionSystem(ICollidingEntity entity) { _collisionSystem.AddEntity(ref entity); }
public void AdjustForCollision(ICollidingEntity other) { Vector2 overlap = GetOverlap(other); _force += -overlap; }