private static bool RectContainsCircle(Rect rect, CollisionCircle obj) { if (!rect.Contains(obj.Trans.position)) { return(false); } float radius = 0; if (obj.IsStatic) { radius = obj.GetRadius(); } if ((obj.Trans.position.x - radius) <= rect.x) { return(false); } if ((obj.Trans.position.x + radius) >= (rect.x + rect.width)) { return(false); } if ((obj.Trans.position.y - radius) <= rect.y) { return(false); } if ((obj.Trans.position.y + radius) >= (rect.y + rect.height)) { return(false); } return(true); }
void CheckWeapon(CollisionCircle unit1, CollisionCircle unit2) { if (unit1.InstanceId == unit2.InstanceId) { return; } if (unit1.Owner.PlayerNumber == unit2.Owner.PlayerNumber) { return; } if (unit1.Owner.PlayerNumber == -1 || unit2.Owner.PlayerNumber == -1) { return; } if (unit1.IsDead || unit2.IsDead) { return; } if (unit1.IsWeapon && unit2.IsWeapon) { return; } float distance = (unit1.Trans.position - unit2.Trans.position).sqrMagnitude; if (distance > 0) { if (unit1.IsWeapon) { float radiuses = unit1.GetRadius() + unit2.GetRadius(); if (distance < radiuses * radiuses) { if (!unit1.Weapon.HasTarget) { unit1.Weapon.Target = unit2.Trans.GetComponent <ITargetable>(); unit1.Weapon.HasTarget = true; unit1.Weapon.AttackTarget(); } else { unit1.Weapon.AttackTarget(); } } else { if (unit1.Weapon.Target != null) { if (unit1.Weapon.Target.GameObj == unit2.Trans.gameObject) { unit1.Weapon.Target = null; } } } } else if (unit2.IsWeapon) { float radiuses = unit1.GetRadius() + unit2.GetRadius(); if (distance < radiuses * radiuses) { if (!unit2.Weapon.HasTarget) { unit2.Weapon.Target = unit1.Trans.GetComponent <ITargetable>(); unit2.Weapon.HasTarget = true; unit2.Weapon.AttackTarget(); } else { unit2.Weapon.AttackTarget(); } } else { if (unit2.Weapon.Target != null) { if (unit2.Weapon.Target.GameObj == unit1.Trans.gameObject) { unit2.Weapon.Target = null; } } } } } }
private static void CheckBodies(CollisionCircle unit1, CollisionCircle unit2) { if (unit1.InstanceId == unit2.InstanceId) { return; } if (unit1.IsStatic && unit2.IsStatic) { return; } if (unit1.IsDead || unit2.IsDead) { return; } if (unit1.IsWeapon || unit2.IsWeapon) { return; } float distance = (unit1.Trans.position - unit2.Trans.position).sqrMagnitude; // if no mover then it's a base if (unit1.IsStatic) { if (unit1.Owner.PlayerNumber == -1) { float radiuses = unit1.GetRadius(); if (distance < radiuses * radiuses) { if (unit2.IsCollidedWithBase == false) { unit2.CollidedBaseCircle = unit1; unit2.IsCollidedWithBase = true; unit1.CollidedCount++; } } else { if (unit2.CollidedBaseCircle == unit1) { unit2.CollidedBaseCircle = null; unit2.IsCollidedWithBase = false; unit1.CollidedCount--; } } } else { float radiuses = unit1.GetRadius(); if (distance < radiuses * radiuses) { if (unit1.Owner.PlayerNumber == unit2.Owner.PlayerNumber) { if (!unit2.IsDead) { if (unit2.Mover.FollowBase.TargetPlanet.gameObject != unit1.Trans.gameObject) { if (unit2.Mover.Separation.Enabled) { unit2.Mover.Separation.AddSeparation(unit1.Trans.position, distance); } } } } } } } // if no mover then it's a base else if (unit2.IsStatic) { if (unit2.Owner.PlayerNumber == -1) { float radiuses = unit2.GetRadius(); if (distance < radiuses * radiuses) { if (unit1.IsCollidedWithBase == false) { unit1.CollidedBaseCircle = unit2; unit1.IsCollidedWithBase = true; unit2.CollidedCount++; } } else { if (unit1.CollidedBaseCircle == unit2) { unit1.CollidedBaseCircle = null; unit1.IsCollidedWithBase = false; unit2.CollidedCount--; } } } else { float radiuses = unit2.GetRadius(); if (distance < radiuses * radiuses) { if (unit2.Owner.PlayerNumber == unit1.Owner.PlayerNumber) { if (!unit1.IsDead) { if (unit1.Mover.FollowBase.TargetPlanet.gameObject != unit2.Trans.gameObject) { if (unit1.Mover.Separation.Enabled) { unit1.Mover.Separation.AddSeparation(unit2.Trans.position, distance); } } } } } } } else if (!unit1.IsStatic && !unit2.IsStatic) { if (distance > 0) { // check all dynamic bodies to apply separation if (distance < unit1.Mover.Separation.Desired * unit1.Mover.Separation.Desired) { if (unit1.Mover.Separation.Enabled) { unit1.Mover.Separation.AddSeparation(unit2.Trans.position, distance); } if (unit2.Mover.Separation.Enabled) { unit2.Mover.Separation.AddSeparation(unit1.Trans.position, distance); } } // check ally bodies only if (unit1.Owner.PlayerNumber == unit2.Owner.PlayerNumber) { // check to apply cohesion if (distance < unit1.Mover.Cohesion.Desired * unit1.Mover.Cohesion.Desired) { if (unit1.Mover.Cohesion.Enabled) { unit1.Mover.Cohesion.AddCohesion(unit2.Trans.position); } if (unit2.Mover.Cohesion.Enabled) { unit2.Mover.Cohesion.AddCohesion(unit1.Trans.position); } } } } else { unit2.Trans.Translate(-0.2f, 0, 0); unit1.Trans.Translate(0.2f, 0, 0); } } }