/// <summary> /// When a row is marked for deletion /// </summary> private void RemoveRow(TileRow row) { // UnityEngine.Object.Destroy removes whatever is given out of the runtime context, effectively deleting it Destroy(row.gameObject); // we implicitly assume that the row to be deleted is in front the queue // this could very well cause wrong behaviour and the interface is ambiguous rows.Dequeue(); // and then we add for each tile a new one, so we don't run out of tiles AddRow(); }
/// <summary> /// Composited function to check if a TileRow intersects with a character /// </summary> private bool IntersectionHit(TileRow row) { // sometimes the selected row already has been "deleted" in the sense of removal from the scene. // In Unity the MonoBehaviour Null Equality operator is overwritten to make this check like this: if (row == null) { return(false); } // does a characters bounding box intersects enough of a tiles bounding box? return(Intersection.NormalizedIntersectionAmount(runCharacterController, row) > hitThreshold); }
/// <summary> /// Returns the percentage of an intersection of the character sprite with a tile. /// </summary> public static float NormalizedIntersectionAmount(RunCharacterController character, TileRow tile) { // get the bounds var tileBounds = RetrieveBounds(tile); var characterBounds = RetrieveBounds(character); // calculate the x/y overlap var xOverlap = max(0, min(tileBounds.max.x, characterBounds.max.x) - max(tileBounds.min.x, characterBounds.min.x)); var yOverlap = max(0, min(tileBounds.max.y, characterBounds.max.y) - max(tileBounds.min.y, characterBounds.min.y)); // an overlap is also just a rectangle - so calculate the area occupied var overlapArea = xOverlap * yOverlap; // similarly calculate the area of the character occupied var characterArea = characterBounds.size.x * characterBounds.size.y; // return the fraction of how much overlap occurs on the character sprite return(overlapArea / characterArea); }
/// <summary> /// Extracts the correct bounds of the top-most tile on a TileRow /// </summary> private static Bounds RetrieveBounds(TileRow tile) { return(tile.TopSprite.bounds); }