/// <summary> /// Iterate over all head/vertical rays /// </summary> public void ForeachHeadRay(float rayLength, ref Vector3 velocity, RayItr itr, bool checkGravitySwap = true) { // swap gravity? if (checkGravitySwap && gravitySwapped) { ForeachFeetRay(rayLength, ref velocity, itr, false); return; } if (velocity.y > 0) { rayLength += velocity.y; } Vector3 origin = raycastOrigins.topLeft; origin.x += velocity.x; for (int i = 0; i < verticalRayCount; i++) { verticalRays[i] = Raycast(origin, Vector2.up, rayLength, collisionMask, new Color(1, 1, 1, 0.5f)); origin.x += verticalRaySpacing; itr(ref verticalRays[i], ref velocity, 1, i); } }
public void ForeachFeetRay(float rayLength, ref Vector3 velocity, RayItr itr) { Vector3 origin = raycastOrigins.bottomLeft; origin.x += velocity.x; float length; for (int i = 0; i < verticalRayCount; i++) { length = velocity.y < 0 ? rayLength - velocity.y : rayLength; verticalRays[i] = Raycast(origin, Vector2.down, length, collisionMask, new Color(1, 0, 0, 0.5f)); origin.x += verticalRaySpacing; itr(ref verticalRays[i], ref velocity, -1, i); } }
/// <summary> /// Iterate over all left/horizontal rays /// </summary> public void ForeachLeftRay(float rayLength, ref Vector3 velocity, RayItr itr) { if (velocity.x < 0) { rayLength -= velocity.x; } Vector3 origin = raycastOrigins.bottomLeft; origin.y += velocity.y; for (int i = 0; i < horizontalRayCount; i++) { horizontalRays[i] = Raycast(origin, Vector2.left, rayLength, collisionMask, new Color(1, 0, 0, 0.5f)); origin.y += horizontalRaySpacing; itr(ref horizontalRays[i], ref velocity, -1, i); } }
public void ForeachHeadRay(float rayLength, ref Vector3 velocity, RayItr itr) { if (velocity.y > 0) { rayLength += velocity.y; } Vector3 origin = raycastOrigins.topLeft; origin.x += velocity.x; for (int i = 0; i < verticalRayCount; i++) { verticalRays[i] = Raycast(origin, Vector2.up, rayLength, collisionMask, new Color(1, 0, 0, 0.5f)); origin.x += verticalRaySpacing; itr(ref verticalRays[i], ref velocity, 1, i); } }
/// <summary> /// Iterate over all feet/vertical rays /// </summary> public void ForeachFeetRay(float rayLength, ref Vector3 velocity, RayItr itr, bool checkGravitySwap = true) { // swap gravity? if (checkGravitySwap && gravitySwapped) { ForeachHeadRay(rayLength, ref velocity, itr, false); return; } Vector3 origin = raycastOrigins.bottomLeft; origin.x += velocity.x; float length; for (int i = 0; i < verticalRayCount; i++) { length = velocity.y < 0 ? rayLength - velocity.y : rayLength; verticalRays[i] = Raycast(origin, Vector2.down, length, collisionMask, new Color(1, 0, 0, 0.5f)); origin.x += verticalRaySpacing; itr(ref verticalRays[i], ref velocity, -1, i); } }
/// <summary> /// Calculate how much each passenger should move /// </summary> void CalculatePassengerMovement(Vector3 velocity) { HashSet <Transform> movedPassengers = new HashSet <Transform> (); pPassengerMovement = passengerMovement; passengerMovement = new List <PassengerMovement> (); float directionY = Mathf.Sign(velocity.y); float rayLength = skinWidth * 2 + Configuration.instance.minDistanceToEnv; collisionMask = passengerMask; // Passenger on top of a horizontally or downward moving platform if (velocity.y != 0 || velocity.x != 0) { ForeachHeadRay(rayLength, ref velocity, (ref RaycastHit2D ray, ref Vector3 vel, int dir, int idx) => { if (ray && ray.distance != 0) { if (!movedPassengers.Contains(ray.transform)) { movedPassengers.Add(ray.transform); passengerMovement.Add( new PassengerMovement(ray.transform, new Vector3(vel.x, vel.y, 0), true, false)); } } }); } // Vertically moving platform // Disable for OWP moving down otherwise will push-down characters if ( // up (directionY == 1) || // down (directionY == -1 && (!disableDownRayCast && !Configuration.IsOneWayPlatformUp(gameObject))) ) { RayItr itr = (ref RaycastHit2D ray, ref Vector3 vel, int dir, int idx) => { if (ray && ray.distance != 0) { if (!movedPassengers.Contains(ray.transform)) { movedPassengers.Add(ray.transform); float pushX = (directionY == 1) ? vel.x : 0; float pushY = vel.y - (ray.distance - skinWidth) * directionY; passengerMovement.Add(new PassengerMovement(ray.transform, new Vector3(pushX, pushY), directionY == 1, true)); } } }; if (directionY == 1) { ForeachHeadRay(rayLength, ref velocity, itr); } else { ForeachFeetRay(rayLength, ref velocity, itr); } } /* * // Horizontally moving platform * // Cannot be a OWP otherwise will push-right|left characters * if (velocity.x != 0 && !Configuration.IsOneWayPlatformUp(gameObject)) { * RayItr itr = (ref RaycastHit2D ray, ref Vector3 vel, int dir, int idx) => { * if (ray && ray.distance != 0) { * if (!movedPassengers.Contains(ray.transform)) { * movedPassengers.Add(ray.transform); * passengerMovement.Add(new PassengerMovement(ray.transform, * new Vector3(directionX * (ray.distance - skinWidth), 0), false, true)); * } * } * }; * * rayLength = skinWidth; * * if (directionX == -1) { * ForeachLeftRay(rayLength, ref velocity, itr); * } else { * ForeachRightRay(rayLength, ref velocity, itr); * } * } */ if (prevPassengers != null) { // diff foreach (var i in movedPassengers) { prevPassengers.Remove(i); } // what stays is out of the platform now: null foreach (var i in prevPassengers) { Character c = i.gameObject.GetComponent <Character>(); if (c == null) { Debug.LogWarning("Found a passenger that is not a character!", i.gameObject); } else { c.platform = null; } } } // what is updated, is on the platform: set foreach (var i in movedPassengers) { Character c = i.gameObject.GetComponent <Character>(); if (c == null) { Debug.LogWarning("Found a passenger that is not a character!", i.gameObject); } else { c.platform = this; } } prevPassengers = movedPassengers; }
/// <summary> /// Iterate over all feet/vertical rays /// </summary> public void ForeachFeetRay(float rayLength, ref Vector3 velocity, RayItr itr) { Vector3 origin = raycastOrigins.bottomLeft; origin.x += velocity.x; float length; for (int i = 0; i < verticalRayCount; i ++) { length = velocity.y < 0 ? rayLength - velocity.y : rayLength; verticalRays[i] = Raycast(origin, Vector2.down, length, collisionMask, new Color(1, 0, 0, 0.5f)); origin.x += verticalRaySpacing; itr(ref verticalRays[i], ref velocity, -1, i); } }
/// <summary> /// Iterate over all head/vertical rays /// </summary> public void ForeachHeadRay(float rayLength, ref Vector3 velocity, RayItr itr) { if (velocity.y > 0) { rayLength += velocity.y; } Vector3 origin = raycastOrigins.topLeft; origin.x += velocity.x; for (int i = 0; i < verticalRayCount; i ++) { verticalRays[i] = Raycast(origin, Vector2.up, rayLength, collisionMask, new Color(1, 0, 0, 0.5f)); origin.x += verticalRaySpacing; itr(ref verticalRays[i], ref velocity, 1, i); } }
/// <summary> /// Iterate over all left/horizontal rays /// </summary> public void ForeachLeftRay(float rayLength, ref Vector3 velocity, RayItr itr) { if (velocity.x < 0) { rayLength -= velocity.x; } Vector3 origin = raycastOrigins.bottomLeft; origin.y += velocity.y; for (int i = 0; i < horizontalRayCount; i ++) { horizontalRays[i] = Raycast(origin, Vector2.left, rayLength, collisionMask, new Color(1, 0, 0, 0.5f)); origin.y += horizontalRaySpacing; itr(ref horizontalRays[i], ref velocity, -1, i); } }