Skip to content

Commit

Permalink
added static utility class with distance-calculating methods
Browse files Browse the repository at this point in the history
  • Loading branch information
christiandunkel committed Jul 9, 2019
1 parent 9a7bbb7 commit 75dd9ea
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Assets/Scripts/Level/Controllers/ScriptedEventsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ private IEnumerator StartFrequenceLvl4() {
rightEyeLaserLR = GameObject.Find("RightEyeLaserLineRenderer").GetComponent<LineRenderer>();
leftEyeLaserLR.gameObject.SetActive(false);
rightEyeLaserLR.gameObject.SetActive(false);
/*

yield return new WaitForSeconds(3f);
DialogSystem.loadDialog("lvl4_where_have_you_gone");
yield return new WaitForSeconds(9f);*/
yield return new WaitForSeconds(9f);
virtualCameraAnimator.SetTrigger("StartFrequenceOver");
yield return new WaitForSeconds(0.1f);
StopCoroutine(StartFrequenceLvl4());
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Level/Elements/ClosingDoor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private void Update() {
return;
}

// relation of doors position to player on x axis
xDistanceToPlayer = playerObject.transform.position.x - transform.position.x;

// door is open, when player is on the left of it
Expand Down
10 changes: 1 addition & 9 deletions Assets/Scripts/Level/Elements/DisappearingBlockDouble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,7 @@ private float distanceToPlayer() {
return 20000f;
}

Vector2 v1 = transform.position;
Vector2 v2 = PlayerManager.playerObject.transform.position;

Vector2 richtungsVektor = v1 - v2;

// distance player to disappearing blocks
float vektorBetrag = Mathf.Sqrt(Mathf.Pow(richtungsVektor.x, 2) + Mathf.Pow(richtungsVektor.y, 2));

return vektorBetrag;
return Util.distanceOnAxisXY(PlayerManager.playerObject, gameObject);

}

Expand Down
11 changes: 1 addition & 10 deletions Assets/Scripts/Level/Elements/DisappearingBlockSingle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,7 @@ private float distanceToPlayer() {
return 20000f;
}


Vector2 v1 = transform.position;
Vector2 v2 = PlayerManager.playerObject.transform.position;

Vector2 richtungsVektor = v1 - v2;

// distance player to disappearing blocks
float vektorBetrag = Mathf.Sqrt(Mathf.Pow(richtungsVektor.x, 2) + Mathf.Pow(richtungsVektor.y, 2));

return vektorBetrag;
return Util.distanceOnAxisXY(PlayerManager.playerObject, gameObject);

}

Expand Down
4 changes: 1 addition & 3 deletions Assets/Scripts/Level/Elements/ToxicWater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ private bool playerIsInRange() {
* the toxic bubble spawning process to activate
*/

float validRadius = 230f;

return (Mathf.Abs(pos.x - PlayerManager.playerObject.transform.position.x) <= validRadius);
return Util.distanceOnAxisX(pos, PlayerManager.playerObject) <= 230f;

}

Expand Down
30 changes: 14 additions & 16 deletions Assets/Scripts/Level/Enemies/Bomberling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,15 @@ private void Update() {

// activate the bomberling if the player is close enough
// and player is roughly on the same height (y value)
if (distanceToPlayer() <= radiusOfActivation &&
Mathf.Abs(playerObject.gameObject.transform.position.y - gameObject.transform.position.y) < 15f) {

if (
distanceToPlayer() <= radiusOfActivation &&
Util.distanceOnAxisY(playerObject, gameObject) < 15f
) {
startRunning();

}

}

private float distanceToPlayer() {

/*
* calculates the distance between this instance of a bomberling and the player
*/

float distance = ((Vector2)gameObject.transform.position - (Vector2)playerObject.transform.position).magnitude;

return distance;

}

private void startRunning() {

/*
Expand Down Expand Up @@ -198,6 +186,16 @@ IEnumerator playSound() {

}

private float distanceToPlayer() {

/*
* returns the distance of the turret and the player on the x and y axis
*/

return Util.distanceOnAxisXY(playerObject, gameObject);

}

private void selfDestruct() {

/*
Expand Down
11 changes: 3 additions & 8 deletions Assets/Scripts/Level/Enemies/LaserTurret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void Update() {

if (!isEnabled) {
// play 'disabled turret' particles
if (!disabledParticles1.activeSelf) {
if (!disabledParticles1.activeSelf || !disabledParticles2.activeSelf) {
disabledParticles1.SetActive(true);
disabledParticles2.SetActive(true);
disabledParticles1_PS.Play();
Expand All @@ -96,18 +96,13 @@ private void Update() {
return;
}
// don't play 'disabled turret' particles anymore
else if (disabledParticles1.activeSelf) {
else if (disabledParticles1.activeSelf || disabledParticles2.activeSelf) {
disabledParticles1.SetActive(false);
disabledParticles2.SetActive(false);
}

// test if player is close enough to turret for it to be active, otherwise return
Vector2 playerPos = PlayerManager.playerObject.transform.position;
if (
Mathf.Pow((playerPos.x - transform.position.x), 2) +
Mathf.Pow((playerPos.y - transform.position.y), 2)
>= Mathf.Pow(distanceToPlayerNeededForActivation, 2)
) {
if (Util.distanceOnAxisXY(PlayerManager.playerObject, gameObject) >= distanceToPlayerNeededForActivation) {
timeUntilNextShot = secondsBetweenShots;
return;
}
Expand Down
246 changes: 246 additions & 0 deletions Assets/Scripts/Other/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
using System.IO;
using UnityEngine;

/*
* provides a variety of utility methods
*/

public static class Util {

/*
* ========================
* === DISTANCE ON AXIS ===
* ========================
*/




/* X Axis */

public static float distanceOnAxisX(GameObject obj1, GameObject obj2, bool useLocalPosition = false) {

/*
* calculate the distance between two objects on the x axis
*/

if (useLocalPosition) {
return distance(obj1.transform.localPosition.x, obj2.transform.localPosition.x);
}
else {
return distance(obj1.transform.position.x, obj2.transform.position.x);
}

}

public static float distanceOnAxisX(Vector3 vec, GameObject obj, bool useLocalPosition = false) {

/*
* calculate the distance between a vector and an object on the x axis
*/

if (useLocalPosition) {
return distance(vec.x, obj.transform.localPosition.x);
}
else {
return distance(vec.x, obj.transform.position.x);
}

}

public static float distanceOnAxisX(GameObject obj, Vector3 vec, bool useLocalPosition = false) {

/*
* calculate the distance between a vector and an object on the x axis
*/

return distanceOnAxisX(vec, obj, useLocalPosition);

}




/* Y Axis */

public static float distanceOnAxisY(GameObject obj1, GameObject obj2, bool useLocalPosition = false) {

/*
* calculate the distance between two objects on the y axis
*/

if (useLocalPosition) {
return distance(obj1.transform.localPosition.y, obj2.transform.localPosition.y);
}
else {
return distance(obj1.transform.position.y, obj2.transform.position.y);
}

}

public static float distanceOnAxisY(Vector3 vec, GameObject obj, bool useLocalPosition = false) {

/*
* calculate the distance between a vector and an object on the y axis
*/

if (useLocalPosition) {
return distance(vec.y, obj.transform.localPosition.y);
}
else {
return distance(vec.y, obj.transform.position.y);
}

}

public static float distanceOnAxisY(GameObject obj, Vector3 vec, bool useLocalPosition = false) {

/*
* calculate the distance between a vector and an object on the y axis
*/

return distanceOnAxisY(vec, obj, useLocalPosition);

}




/* Z Axis */

public static float distanceOnAxisZ(GameObject obj1, GameObject obj2, bool useLocalPosition = false) {

/*
* calculate the distance between two objects on the z axis
*/

if (useLocalPosition) {
return distance(obj1.transform.localPosition.z, obj2.transform.localPosition.z);
}
else {
return distance(obj1.transform.position.z, obj2.transform.position.z);
}

}

public static float distanceOnAxisZ(Vector3 vec, GameObject obj, bool useLocalPosition = false) {

/*
* calculate the distance between a vector and an object on the z axis
*/

if (useLocalPosition) {
return distance(vec.z, obj.transform.localPosition.z);
}
else {
return distance(vec.z, obj.transform.position.z);
}

}

public static float distanceOnAxisZ(GameObject obj, Vector3 vec, bool useLocalPosition = false) {

/*
* calculate the distance between a vector and an object on the z axis
*/

return distanceOnAxisZ(vec, obj, useLocalPosition);

}

public static float distanceOnAxisXY(GameObject obj1, GameObject obj2, bool useLocalPosition = false) {

/*
* calculate the distance between two objects on the x and y axis
*/

if (useLocalPosition) {
return distance((Vector2)obj1.transform.localPosition, (Vector2)obj2.transform.localPosition);
}
else {
return distance((Vector2)obj1.transform.position, (Vector2)obj2.transform.position);
}

}





/*
* ======================
* === DISTANCE TOTAL ===
* ======================
*/

public static float distance(GameObject obj1, GameObject obj2, bool useLocalPosition = false) {

/*
* calculate the distance between two objects
*/

if (useLocalPosition) {
return (obj1.transform.localPosition - obj2.transform.localPosition).magnitude;
}
else {
return (obj1.transform.position - obj2.transform.position).magnitude;
}

}

public static float distance(Transform transform1, Transform transform2, bool useLocalPosition = false) {

/*
* calculate the distance between two transforms
*/

if (useLocalPosition) {
return (transform1.localPosition - transform2.localPosition).magnitude;
}
else {
return (transform1.position - transform2.position).magnitude;
}

}

public static float distance(Vector3 vec1, Vector3 vec2) {

/*
* calculate the distance between two vector3's
*/

return (vec1 - vec2).magnitude;

}

public static float distance(Vector2 vec1, Vector2 vec2) {

/*
* calculate the distance between two vector2's
*/

return (vec1 - vec2).magnitude;

}

public static float distance(float num1, float num2) {

/*
* calculates the difference between two floats
*/

return Mathf.Abs(num1 - num2);

}

public static float distance(int num1, int num2) {

/*
* calculates the difference between two integers
*/

return Mathf.Abs(num1 - num2);

}

}
Loading

0 comments on commit 75dd9ea

Please sign in to comment.