/** * Compute overlap (between 0 & 1) of player and all incompatible traps * the player stands on */ public double ComputeOverlap() { var playerBounds = GetComponent <BoxCollider2D>().bounds; var playerArea = playerBounds.size.x * playerBounds.size.y; Shapes.BoundsToPath(playerBounds, _playerShapeBuffer); // Compute the union of the shapes of all traps the player is standing on var trapsShape = Shapes.Union( _traps .Where(trap => !trap.IsCompatibleWith(_playerController.Color)) .Select(trap => trap.Path) .ToList(), clipper ); // compute intersection with bounds of players var playerTrapIntersection = Shapes.Intersection( trapsShape, _playerShapeBuffer, clipper ); // return share of player on traps return(Shapes.Area(playerTrapIntersection) / playerArea); }
private void OnTriggerStay2D(Collider2D other) { // We now want to compute, whether the player is standing on the chasm, which means they should fall if (other.CompareTag("Player") || other.CompareTag("Item")) { // Compute some geometric properties of the player, e.g. area of its collider and the shape of the collider var playerBounds = other.GetComponent <BoxCollider2D>().bounds; var playerArea = playerBounds.size.x * playerBounds.size.y; Shapes.BoundsToPath(playerBounds, _playerShapeBuffer); // Get all platforms the player is currently standing on var platforms = other.GetComponent <PlatformTransportable>().PlatformsInContact; // Compute the union of the shapes of all platforms var platformsShape = Shapes.Union( platforms .Select(platform => Shapes.PathFromCollider(platform.gameObject)) .ToList(), clipper ); // Now compute the shape of the area, which the player collider and the chasm share, // i.e. intersect their shapes var playerChasmIntersection = Shapes.Intersection( _ownShape, _playerShapeBuffer, clipper ); // Now subtract from that shape, the shape of the platforms the player is standing on. // Therefore, all that is then left is the shape of the area where nothing is between the player and the // chasm. playerChasmIntersection = Shapes.Difference( playerChasmIntersection, platformsShape ); // Compute the area of this shape. var intersectionArea = Shapes.Area(playerChasmIntersection); // If the player is standing on a chasm with more than 50% of their own area, they fall if (intersectionArea / playerArea > 0.5) { // Get a handle on the player if (other.TryGetComponent <PlayerController>(out var playerController)) { playerController.InitiateFall(); } else if (other.TryGetComponent <Item>(out var item)) { item.gameObject.Shrink(new Vector3(0.01f, 0.01f, 0.01f)); } } } }
/** * Compute overlap (between 0 & 1) of player and this trap */ public double ComputeOverlap(Bounds playerBounds) { var playerArea = playerBounds.size.x * playerBounds.size.y; Shapes.BoundsToPath(playerBounds, _playerShapeBuffer); // compute intersection with bounds of player var playerTrapIntersection = Shapes.Intersection( _ownPath, _playerShapeBuffer, clipper); // return share of player on traps return(Shapes.Area(playerTrapIntersection) / playerArea); }