Skip to content

Commit

Permalink
Quicksave
Browse files Browse the repository at this point in the history
going on a walk
  • Loading branch information
aps16104 committed Apr 23, 2020
1 parent 0f8a666 commit ab7d47c
Show file tree
Hide file tree
Showing 13 changed files with 20,781 additions and 8,284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
public class PlatformController : RaycastController
{

public LayerMask passengerMask; //layer used for passengers
public LayerMask passengerMask;

public Vector3[] localWaypoints;//relative to platform
public Vector3[] localWaypoints;
Vector3[] globalWaypoints;

public float speed; //speed of platform
public bool cyclic; //cycles
public float waitTime; //how long to wait
public float speed;
public bool cyclic;
public float waitTime;
[Range(0, 2)]
public float easeAmount; // how much to ease
public float easeAmount;

int fromWaypointIndex; //global waypoint were moving away from
float percentBetweenWaypoints; //percent moved between waypoints (0-1)
int fromWaypointIndex;
float percentBetweenWaypoints;
float nextMoveTime;

float nextMoveTime; //next time to move
List<PassengerMovement> passengerMovement; //store all the bools
Dictionary<Transform, Controller2D> passengerDictionary = new Dictionary<Transform, Controller2D>(); //to be able to jump
List<PassengerMovement> passengerMovement;
Dictionary<Transform, Controller2D> passengerDictionary = new Dictionary<Transform, Controller2D>();

public override void Start()
{
base.Start();

globalWaypoints = new Vector3[localWaypoints.Length]; //set global waypoints so you actually go between
for (int i = 0; i < localWaypoints.Length; i++) //every local way point must be changed
globalWaypoints = new Vector3[localWaypoints.Length];
for (int i = 0; i < localWaypoints.Length; i++)
{
globalWaypoints[i] = localWaypoints[i] + transform.position;
}
Expand All @@ -37,14 +37,15 @@ public override void Start()
void Update()
{

UpdateRaycastOrigins(); //never changes
Vector3 velocity = move * Time.deltaTime;
UpdateRaycastOrigins();

CalculatePassengerMovement(velocity); //Move the passangers
Vector3 velocity = CalculatePlatformMovement();

MovePassengers(true);//Move
transform.Translate(velocity);//Trasnform
MovePassengers(false);//Dont move
CalculatePassengerMovement(velocity);

MovePassengers(true);
transform.Translate(velocity);
MovePassengers(false);
}

float Ease(float x)
Expand All @@ -53,88 +54,86 @@ float Ease(float x)
return Mathf.Pow(x, a) / (Mathf.Pow(x, a) + Mathf.Pow(1 - x, a));
}


Vector3 CalculatePlatformMovement()
{

if (Time.time < nextMoveTime) //dont move
if (Time.time < nextMoveTime)
{
return Vector3.zero;
}

fromWaypointIndex %= globalWaypoints.Length; //reset to 0 each time it reaches
int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length; //to the next waypoint
float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]); //distance between
percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints; //increase % each frame
percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints); //clamp between 0 and 1
float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints); //percent eased
fromWaypointIndex %= globalWaypoints.Length;
int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length;
float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]);
percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints;
percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints);
float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints);

Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints); //new position
Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints);

if (percentBetweenWaypoints >= 1) //reached or passed point
if (percentBetweenWaypoints >= 1)
{
percentBetweenWaypoints = 0; //reset %
fromWaypointIndex++; //increment index
percentBetweenWaypoints = 0;
fromWaypointIndex++;

if (!cyclic) //if not cyclic
if (!cyclic)
{
if (fromWaypointIndex >= globalWaypoints.Length - 1) //reached end
if (fromWaypointIndex >= globalWaypoints.Length - 1)
{
fromWaypointIndex = 0; //start again
System.Array.Reverse(globalWaypoints); //reverse through waypoints
fromWaypointIndex = 0;
System.Array.Reverse(globalWaypoints);
}
}
nextMoveTime = Time.time + waitTime; //set next time to move
nextMoveTime = Time.time + waitTime;
}

return newPos - transform.position; //amount we want to move this frame
return newPos - transform.position;
}


void MovePassengers(bool beforeMovePlatform) //before we move the platform
void MovePassengers(bool beforeMovePlatform)
{
foreach (PassengerMovement passenger in passengerMovement) //for every movement
foreach (PassengerMovement passenger in passengerMovement)
{
if (!passengerDictionary.ContainsKey(passenger.transform)) //if passanger not on
if (!passengerDictionary.ContainsKey(passenger.transform))
{
passengerDictionary.Add(passenger.transform, passenger.transform.GetComponent<Controller2D>());
}

if (passenger.moveBeforePlatform == beforeMovePlatform) //we on
if (passenger.moveBeforePlatform == beforeMovePlatform)
{
passengerDictionary[passenger.transform].Move(passenger.velocity, passenger.standingOnPlatform);
}
}
}

void CalculatePassengerMovement(Vector3 velocity) //Refers to any controller 2d affected by the platform
void CalculatePassengerMovement(Vector3 velocity)
{
HashSet<Transform> movedPassengers = new HashSet<Transform>(); //use because fast at adding things and checking whats contained
HashSet<Transform> movedPassengers = new HashSet<Transform>();
passengerMovement = new List<PassengerMovement>();

float directionX = Mathf.Sign(velocity.x); //left/right
float directionY = Mathf.Sign(velocity.y); //up
float directionX = Mathf.Sign(velocity.x);
float directionY = Mathf.Sign(velocity.y);

// Vertically moving platform
if (velocity.y != 0)
{
float rayLength = Mathf.Abs(velocity.y) + skinWidth; //ray length
float rayLength = Mathf.Abs(velocity.y) + skinWidth;

for (int i = 0; i < verticalRayCount; i++) //similar to controller2D
for (int i = 0; i < verticalRayCount; i++)
{
Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
rayOrigin += Vector2.right * (verticalRaySpacing * i);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, passengerMask);

if (hit) //there is a passenger on
if (hit)
{
if (!movedPassengers.Contains(hit.transform)) //if it doesnt contain hit. transform
if (!movedPassengers.Contains(hit.transform))
{
movedPassengers.Add(hit.transform); //add to list so moved once per frame
float pushX = (directionY == 1) ? velocity.x : 0; //if its moving up, otherwise set to 0
float pushY = velocity.y - (hit.distance - skinWidth) * directionY; //close distance
movedPassengers.Add(hit.transform);
float pushX = (directionY == 1) ? velocity.x : 0;
float pushY = velocity.y - (hit.distance - skinWidth) * directionY;

passengerMovement.Add(new PassengerMovement(hit.transform, new Vector3(pushX, pushY), directionY == 1, true)); //update list
passengerMovement.Add(new PassengerMovement(hit.transform, new Vector3(pushX, pushY), directionY == 1, true));
}
}
}
Expand All @@ -159,7 +158,7 @@ void CalculatePassengerMovement(Vector3 velocity) //Refers to any controller 2d
float pushX = velocity.x - (hit.distance - skinWidth) * directionX;
float pushY = -skinWidth;

passengerMovement.Add(new PassengerMovement(hit.transform, new Vector3(pushX, pushY), false, true)); //update list
passengerMovement.Add(new PassengerMovement(hit.transform, new Vector3(pushX, pushY), false, true));
}
}
}
Expand All @@ -172,30 +171,30 @@ void CalculatePassengerMovement(Vector3 velocity) //Refers to any controller 2d

for (int i = 0; i < verticalRayCount; i++)
{
Vector2 rayOrigin = raycastOrigins.topLeft + Vector2.right * (verticalRaySpacing * i); //origin of rays
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up, rayLength, passengerMask); //raycast hit
Vector2 rayOrigin = raycastOrigins.topLeft + Vector2.right * (verticalRaySpacing * i);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up, rayLength, passengerMask);

if (hit)
{
if (!movedPassengers.Contains(hit.transform)) //passanger is on
if (!movedPassengers.Contains(hit.transform))
{
movedPassengers.Add(hit.transform);
float pushX = velocity.x;
float pushY = velocity.y;

passengerMovement.Add(new PassengerMovement(hit.transform, new Vector3(pushX, pushY),true , false)); //update list
passengerMovement.Add(new PassengerMovement(hit.transform, new Vector3(pushX, pushY), true, false));
}
}
}
}
}

struct PassengerMovement //relevent info
struct PassengerMovement
{
public Transform transform; //transform of passenger
public Vector3 velocity; //desired velocity
public bool standingOnPlatform; //is he on plantform
public bool moveBeforePlatform; //move before platform?
public Transform transform;
public Vector3 velocity;
public bool standingOnPlatform;
public bool moveBeforePlatform;

public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standingOnPlatform, bool _moveBeforePlatform)
{
Expand All @@ -208,17 +207,18 @@ public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standing

void OnDrawGizmos()
{
if (localWaypoints != null) //only if its not equal to null
if (localWaypoints != null)
{
Gizmos.color = Color.red;
float size = .3f; //size we want to draw "gizmos" at
float size = .3f;

for (int i = 0; i < localWaypoints.Length; i++) //iterate through each waypoints
for (int i = 0; i < localWaypoints.Length; i++)
{
Vector3 globalWaypointPos = (Application.isPlaying) ? globalWaypoints[i] : localWaypoints[i] + transform.position; //check if the games on first
Vector3 globalWaypointPos = (Application.isPlaying) ? globalWaypoints[i] : localWaypoints[i] + transform.position;
Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
}
}
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ab7d47c

Please sign in to comment.