@@ -5,7 +5,10 @@

public class UserInteraction : MonoBehaviour {


private CarController m_car;
private CarUserControl m_carUser;
private CarState m_carstate;
private Transform m_transform;
public bool isCarBottom;
public bool isBoosting;
public float boostTimer;
@@ -27,9 +30,19 @@ public class UserInteraction : MonoBehaviour {
public GameObject oilPrefab;
public Camera backCamera;
public Camera frontCamera;

void Awake()
{
m_carUser = gameObject.GetComponentInParent<CarUserControl>();
m_car = gameObject.GetComponentInParent<CarController>();
m_carstate = gameObject.GetComponentInParent<CarState>();
m_transform = gameObject.GetComponentInParent<Transform>();


}
// Use this for initialization
void Start () {
isCarBottom = gameObject.GetComponentInParent<CarUserControl>().isBottomCar;
void Start () {
isCarBottom = m_carUser.isBottomCar;
boostTimer = Time.time;
}

@@ -53,18 +66,17 @@ public void placeOilSpill()
if (Main.S.carBottomDone)
return;
}
CarUserControl userControl = gameObject.GetComponentInParent<CarUserControl>();
// Prevent players from using reset right away
if (!Main.S.getRaceStarted() && !Main.S.practicing)
return;
// If car has no controllers attached to it
if (userControl.first >= InputManager.Devices.Count)
if (m_carUser.first >= InputManager.Devices.Count)
return;


// Get player controller object
var playerAInput = InputManager.Devices[userControl.first];
var playerBInput = InputManager.Devices[userControl.second];
var playerAInput = InputManager.Devices[m_carUser.first];
var playerBInput = InputManager.Devices[m_carUser.second];

if (playerAInput.RightStickY < 0 || playerBInput.RightStickY < 0)
{
@@ -172,21 +184,21 @@ public void placeOilSpill()
isBoosting = false;

bool resetting = false;
if ((playerAInput.RightBumper.WasPressed || playerBInput.RightBumper.WasPressed) && gameObject.GetComponentInParent<CarState>().currLap != 0)
if ((playerAInput.RightBumper.WasPressed || playerBInput.RightBumper.WasPressed) && m_carstate.currLap != 0)
{
resetting = true;
}
if (resetting)
{
if (!isCarBottom)
{
Main.S.carTop.GetComponent<CarState>().resets++;
m_carstate.resets++;
}
else
{
Main.S.carBottom.GetComponent<CarState>().resets++;
m_carstate.resets++;
}
Logger.S.writeFile(!isCarBottom, "Reset To Before " + gameObject.GetComponentInParent<CarState>().currCheckpoint + " at: " + Main.S.getGameTime());
Logger.S.writeFile(!isCarBottom, "Reset To Before " + m_carstate.currCheckpoint + " at: " + Main.S.getGameTime());

startReset();
}
@@ -200,144 +212,136 @@ public void startReset()
Quaternion newRotation;
// Find the location and rotation of the prev checkpoint
int index = 0;
if (gameObject.GetComponentInParent<CarState>().currCheckpoint != 0)
if (m_carstate.currCheckpoint != 0)
{
index = gameObject.GetComponentInParent<CarState>().currCheckpoint - 1;
checkPoint = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().currCheckpoint - 1];
newRotation = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().currCheckpoint - 1].rotation;
index = m_carstate.currCheckpoint - 1;
checkPoint = m_carstate.checkpoints[m_carstate.currCheckpoint - 1];
newRotation = m_carstate.checkpoints[m_carstate.currCheckpoint - 1].rotation;
}
else
{
index = gameObject.GetComponentInParent<CarState>().checkpoints.Count - 1;
checkPoint = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().checkpoints.Count - 1];
newRotation = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().checkpoints.Count - 1].rotation;
index = m_carstate.checkpoints.Count - 1;
checkPoint = m_carstate.checkpoints[m_carstate.checkpoints.Count - 1];
newRotation = m_carstate.checkpoints[m_carstate.checkpoints.Count - 1].rotation;
}

newPos.x = checkPoint.position.x;
newPos.y = checkPoint.position.y;
newPos.z = checkPoint.position.z;

if (gameObject.GetComponentInParent<UserInteraction>().isCarBottom)
if (isCarBottom)
{
newPos.x = gameObject.GetComponentInParent<CarState>().bottomResetLocation[index].position.x;
newPos.z = gameObject.GetComponentInParent<CarState>().bottomResetLocation[index].position.z;
newPos.x = m_carstate.bottomResetLocation[index].position.x;
newPos.z = m_carstate.bottomResetLocation[index].position.z;
}
else
{
newPos.x = gameObject.GetComponentInParent<CarState>().topResetLocation[index].position.x;
newPos.z = gameObject.GetComponentInParent<CarState>().topResetLocation[index].position.z;
newPos.x = m_carstate.topResetLocation[index].position.x;
newPos.z = m_carstate.topResetLocation[index].position.z;
}

targetLocation = newPos;
targetRotation = newRotation;
initalRotation = gameObject.GetComponentInParent<Transform>().rotation;
initalRotation = m_transform.rotation;

// Move car to correct spot
goingUp = true;

//this is only lowering him to ~1-5 right now. could polish this.
gameObject.GetComponentInParent<CarController>().zeroSpeed();
m_car.zeroSpeed();
}

public void moveToNextCheckpoint()
{
moveToCheckpoint(gameObject.GetComponentInParent<CarState>().currCheckpoint);
moveToCheckpoint(m_carstate.currCheckpoint);
}

public void moveToStart()
{
print("moving to start " + isCarBottom);
carryHeight = 30;
Vector3 newPos = new Vector3();
Transform checkPoint;
Quaternion newRotation;
// Find the location and rotation of the prev checkpoint
if (isCarBottom)
{
checkPoint = Main.S.Map.GetComponent<Map>().playerBottomStart.transform;
}
else
{
checkPoint = Main.S.Map.GetComponent<Map>().playerTopStart.transform;
}

newRotation = gameObject.GetComponentInParent<CarState>().checkpoints[0].rotation;
targetRotation = m_carstate.checkpoints[0].rotation;

newPos.x = checkPoint.position.x;
newPos.y = checkPoint.position.y;
newPos.z = checkPoint.position.z;
targetLocation = newPos;
targetRotation = newRotation;
initalRotation = gameObject.GetComponentInParent<Transform>().rotation;
setTargetLocation(checkPoint);

initalRotation = m_transform.rotation;

// Move car to correct spot
goingUp = true;

//this is only lowering him to ~1-5 right now. could polish this.
gameObject.GetComponentInParent<CarController>().zeroSpeed();
m_car.zeroSpeed();
}

public void moveToCheckpoint(int checkpointToGo)
{
carryHeight = 10;
Vector3 newPos = new Vector3();
Transform checkPoint;
Quaternion newRotation;
// Find the location and rotation of the prev checkpoint

checkPoint = gameObject.GetComponentInParent<CarState>().checkpoints[checkpointToGo];
newRotation = gameObject.GetComponentInParent<CarState>().checkpoints[checkpointToGo].rotation;
checkPoint = m_carstate.checkpoints[checkpointToGo];
newRotation = m_carstate.checkpoints[checkpointToGo].rotation;

newPos.x = checkPoint.position.x;
newPos.y = checkPoint.position.y;
newPos.z = checkPoint.position.z;
targetLocation = newPos;
setTargetLocation(checkPoint);
targetRotation = newRotation;
initalRotation = gameObject.GetComponentInParent<Transform>().rotation;
initalRotation = m_transform.rotation;

// Move car to correct spot
goingUp = true;

//this is only lowering him to ~1-5 right now. could polish this.
gameObject.GetComponentInParent<CarController>().zeroSpeed();
m_car.zeroSpeed();
}
void setTargetLocation(Transform target)
{
Vector3 newPos = new Vector3();
newPos.x = target.position.x;
newPos.y = target.position.y;
newPos.z = target.position.z;
targetLocation = newPos;
}


//immediatly move the car to above the last checkpoint and immediatly rotate to correct direction
public void hardReset()
{
Vector3 newPos = new Vector3();
Transform checkPoint;
Quaternion newRotation;
// Find the location and rotation of the prev checkpoint
if (gameObject.GetComponentInParent<CarState>().currCheckpoint != 0)
if (m_carstate.currCheckpoint != 0)
{
checkPoint = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().currCheckpoint - 1];
newRotation = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().currCheckpoint - 1].rotation;
checkPoint = m_carstate.checkpoints[m_carstate.currCheckpoint - 1];
newRotation = m_carstate.checkpoints[m_carstate.currCheckpoint - 1].rotation;
}
else
{
checkPoint = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().checkpoints.Count - 1];
newRotation = gameObject.GetComponentInParent<CarState>().checkpoints[gameObject.GetComponentInParent<CarState>().checkpoints.Count - 1].rotation;
checkPoint = m_carstate.checkpoints[m_carstate.checkpoints.Count - 1];
newRotation = m_carstate.checkpoints[m_carstate.checkpoints.Count - 1].rotation;
}


newPos.x = checkPoint.position.x;
newPos.y = checkPoint.position.y;
newPos.z = checkPoint.position.z;
targetLocation = newPos;
setTargetLocation(checkPoint);

targetRotation = newRotation;
initalRotation = gameObject.GetComponentInParent<Transform>().rotation;
initalRotation = m_transform.rotation;

// Adjust angle to face correct direction
Vector3 Angles = newRotation.eulerAngles;
Angles.y += 90;
newRotation = Quaternion.Euler(Angles.x, Angles.y, Angles.z);

// Move car to correct spot
gameObject.GetComponentInParent<Transform>().position = newPos;
gameObject.GetComponentInParent<Transform>().rotation = newRotation;
m_transform.position = targetLocation;
m_transform.rotation = newRotation;
//this is only lowering him to ~1-5 right now. could polish this.
gameObject.GetComponentInParent<CarController>().zeroSpeed();
m_car.zeroSpeed();
}

//called in update of carController
@@ -347,43 +351,41 @@ public void moveToLocation()
if (goingUp)
{
//Move up to y = 10;
gameObject.GetComponentInParent<CarController>().zeroSpeed();
m_car.zeroSpeed();

Vector3 newPos2 = gameObject.GetComponentInParent<Transform>().position;
Vector3 newPos2 = m_transform.position;
newPos2.y += .2f;

gameObject.GetComponentInParent<Transform>().rotation = initalRotation;
m_transform.rotation = initalRotation;

if (newPos2.y >= carryHeight)
{
goingUp = false;
goingToPoint = true;
newPos2.y = carryHeight;
if (carrySpeed == 0)
carrySpeed = (float)Vector3.Distance(gameObject.GetComponentInParent<Transform>().position,targetLocation)/75;
carrySpeed = (float)Vector3.Distance(m_transform.position,targetLocation)/75;
}
gameObject.GetComponentInParent<Transform>().position = newPos2;

Vector3 newRotation = gameObject.GetComponentInParent<Transform>().rotation.eulerAngles;
gameObject.GetComponentInParent<Transform>().rotation = Quaternion.Euler(0, newRotation.y, 0); ;
gameObject.GetComponentInParent<Transform>().position = newPos2;


m_transform.position = newPos2;

Vector3 newRotation = m_transform.rotation.eulerAngles;
m_transform.rotation = Quaternion.Euler(0, newRotation.y, 0); ;
m_transform.position = newPos2;
}
else if (goingToPoint)
{

Vector3 newPos = gameObject.GetComponentInParent<Transform>().position;
Vector3 newPos = m_transform.position;
//stop momentum and falling
gameObject.GetComponentInParent<CarController>().zeroSpeed();
newPos.y = carryHeight;
m_car.zeroSpeed();
newPos.y = carryHeight;

//Chose X direction to go;
if ((newPos.x - targetLocation.x) > 2*carrySpeed)
if ((newPos.x - targetLocation.x) > 2 * carrySpeed)
{
newPos.x -= carrySpeed;
}else if ((newPos.x - targetLocation.x ) < -2*carrySpeed)
}
else if ((newPos.x - targetLocation.x) < -2 * carrySpeed)
{
newPos.x += carrySpeed;
}
@@ -398,7 +400,7 @@ public void moveToLocation()
newPos.z += carrySpeed;
}

Vector3 Angles = gameObject.GetComponentInParent<Transform>().rotation.eulerAngles;
Vector3 Angles = m_transform.rotation.eulerAngles;
bool rotationDone = false;
//Chose whether to rotate or not
float rotateQuantity = 2;
@@ -412,19 +414,19 @@ public void moveToLocation()
{
rotateQuantity = -rotateQuantity;
}
if (Mathf.Abs(gameObject.GetComponentInParent<Transform>().rotation.eulerAngles.y % 360 - (targetRotation.eulerAngles.y + 90) % 360) > 3)
if (Mathf.Abs(m_transform.rotation.eulerAngles.y % 360 - (targetRotation.eulerAngles.y + 90) % 360) > 3)
{
Angles.y += rotateQuantity;
gameObject.GetComponentInParent<Transform>().rotation = Quaternion.Euler(0, Angles.y, 0);
m_transform.rotation = Quaternion.Euler(0, Angles.y, 0);
}
else
{
//if rotation is done, stabalize
rotationDone = true;
Vector3 Angles2 = targetRotation.eulerAngles;
Angles2.y += 90;
gameObject.GetComponentInParent<Transform>().rotation = Quaternion.Euler(0, Angles2.y, 0);

m_transform.rotation = Quaternion.Euler(0, Angles2.y, 0);

}

@@ -435,29 +437,27 @@ public void moveToLocation()
goingToPoint = false;
goingDown = true;
}
gameObject.GetComponentInParent<Transform>().position = newPos;
m_transform.position = newPos;

}
else if (goingDown)
{
//Do not allow rotation until close to hitting the ground.
Vector3 Angles2 = targetRotation.eulerAngles;
Angles2.y += 90;
gameObject.GetComponentInParent<Transform>().rotation = Quaternion.Euler(0, Angles2.y, 0);
if (gameObject.GetComponentInParent<Transform>().position.y < .1)
m_transform.rotation = Quaternion.Euler(0, Angles2.y, 0);
if (m_transform.position.y < .1)
{
goingDown = false;
carrySpeed = 0;
gameObject.GetComponentInParent<CarController>().zeroSpeed();
m_car.zeroSpeed();
Main.S.setCarReady();
}
}
}

public void startBoost()
{
//isBoosting = true;
//boostTimer = Time.time;
StartCoroutine("boostPowerup");
}

@@ -14,26 +14,38 @@ public class CarUserControl : MonoBehaviour
private CarController m_Car; // the car controller we want to use
public bool isBottomCar;

private UserInteraction m_userInteract;
private WheelCollider[] m_wheels;// the wheel colliders of the car
public int first = 3; // first - player who turns right and accelerates
public int second = 3; // second - player who turns left and brakes



void Start()
{
}

// Show SwapText and start Co-Routine
public void playerSwap()
public void startVibration()
{
print("in player swap");
// Start pulse vibration
var playerAInput = InputManager.Devices[first];
var playerBInput = InputManager.Devices[second];
playerAInput.Vibrate(1f, 1f);
playerBInput.Vibrate(1f, 1f);
}
public void endVibration()
{
var playerAInput = InputManager.Devices[first];
var playerBInput = InputManager.Devices[second];
playerAInput.Vibrate(0f, 0f);
playerBInput.Vibrate(0f, 0f);
}

// Show SwapText and start Co-Routine
public void playerSwap()
{
// Start pulse vibration
startVibration();

//GamePad.SetVibration((PlayerIndex)first, 1f, 1f);
//GamePad.SetVibration((PlayerIndex)second, 1f, 1f);
if (!isBottomCar)
{
CarmonyGUI.S.topSwapText.SetActive(true);
@@ -62,13 +74,7 @@ void swapControls()
{
print("in swap controls");
// Turn off vibrate
var playerAInput = InputManager.Devices[first];
var playerBInput = InputManager.Devices[second];
playerAInput.Vibrate(0f, 0f);
playerBInput.Vibrate(0f, 0f);
//GamePad.SetVibration((PlayerIndex)first, 0f, 0f);
//GamePad.SetVibration((PlayerIndex)second, 0f, 0f);

endVibration();

// Remove SwapText and swap control images
if (!isBottomCar)
@@ -98,19 +104,22 @@ private void Awake()
{
// get the car controller
m_Car = GetComponent<CarController>();
m_wheels = gameObject.GetComponentsInChildren<WheelCollider>();
m_userInteract = gameObject.GetComponentInParent<UserInteraction>();

}

//Modify these values in order to tweak steering
private void FixedUpdate()
{
gameObject.GetComponentInParent<UserInteraction>().moveToLocation();
m_userInteract.moveToLocation();

if (first >= InputManager.Devices.Count)
return;

if (!Main.S.getRaceStarted() && !Main.S.practicing)
{
gameObject.GetComponentInParent<CarController>().zeroXYSpeed();
m_Car.zeroXYSpeed();
return;
}

@@ -160,44 +169,41 @@ private void FixedUpdate()
}
float steering = playerA_turnRight + playerB_turnLeft;
if (gameObject.transform.localScale.x < 1) {
steering *= Mathf.Pow(2.718f, gameObject.GetComponent<CarController> ().getSpeed()/-60);
gameObject.GetComponent<CarController> ().SlipLimit = .7f;
gameObject.GetComponent<CarController> ().SteerHelperProperty = 1f;
WheelCollider[] wheels = gameObject.GetComponentsInChildren<WheelCollider>();
steering *= Mathf.Pow(2.718f, m_Car.getSpeed()/-60);
m_Car.SlipLimit = .7f;
m_Car.SteerHelperProperty = 1f;
for(int i = 0; i < 4; i++)
{
WheelFrictionCurve wheelCurve = wheels[i].sidewaysFriction;
WheelFrictionCurve wheelCurve = m_wheels[i].sidewaysFriction;
wheelCurve.asymptoteSlip = .0001f;
wheelCurve.extremumSlip = .0001f;
wheels[i].sidewaysFriction = wheelCurve;
m_wheels[i].sidewaysFriction = wheelCurve;

JointSpring wheelSpring = wheels[i].suspensionSpring;
JointSpring wheelSpring = m_wheels[i].suspensionSpring;
wheelSpring.damper = 3500;
wheels[i].suspensionSpring = wheelSpring;
m_wheels[i].suspensionSpring = wheelSpring;
}
}else if (gameObject.transform.localScale.x > 1)
{
WheelCollider[] wheels = gameObject.GetComponentsInChildren<WheelCollider>();
for (int i = 0; i < 4; i++)
{
JointSpring wheelSpring = wheels[i].suspensionSpring;
JointSpring wheelSpring = m_wheels[i].suspensionSpring;
wheelSpring.damper = 10;
wheels[i].suspensionSpring = wheelSpring;
m_wheels[i].suspensionSpring = wheelSpring;
}
} else {
gameObject.GetComponent<CarController> ().SlipLimit = .3f;
gameObject.GetComponent<CarController> ().SteerHelperProperty = .644f;
WheelCollider[] wheels = gameObject.GetComponentsInChildren<WheelCollider>();
m_Car.SlipLimit = .3f;
m_Car.SteerHelperProperty = .644f;
for (int i = 0; i < 4; i++)
{
WheelFrictionCurve wheelCurve = wheels[i].sidewaysFriction;
WheelFrictionCurve wheelCurve = m_wheels[i].sidewaysFriction;
wheelCurve.asymptoteSlip = .5f;
wheelCurve.extremumSlip = .2f;
wheels[i].sidewaysFriction = wheelCurve;
m_wheels[i].sidewaysFriction = wheelCurve;

JointSpring wheelSpring = wheels[i].suspensionSpring;
JointSpring wheelSpring = m_wheels[i].suspensionSpring;
wheelSpring.damper = 3500;
wheels[i].suspensionSpring = wheelSpring;
m_wheels[i].suspensionSpring = wheelSpring;
}
}