From 75dd9eadbdfbbfdd2be23d428fc012df1fd5aa17 Mon Sep 17 00:00:00 2001 From: Christian Dunkel Date: Tue, 9 Jul 2019 22:59:20 +0200 Subject: [PATCH] added static utility class with distance-calculating methods --- .../Controllers/ScriptedEventsManager.cs | 4 +- Assets/Scripts/Level/Elements/ClosingDoor.cs | 1 + .../Level/Elements/DisappearingBlockDouble.cs | 10 +- .../Level/Elements/DisappearingBlockSingle.cs | 11 +- Assets/Scripts/Level/Elements/ToxicWater.cs | 4 +- Assets/Scripts/Level/Enemies/Bomberling.cs | 30 +-- Assets/Scripts/Level/Enemies/LaserTurret.cs | 11 +- Assets/Scripts/Other/Util.cs | 246 ++++++++++++++++++ Assets/Scripts/Other/Util.cs.meta | 11 + README.md | 2 +- 10 files changed, 281 insertions(+), 49 deletions(-) create mode 100644 Assets/Scripts/Other/Util.cs create mode 100644 Assets/Scripts/Other/Util.cs.meta diff --git a/Assets/Scripts/Level/Controllers/ScriptedEventsManager.cs b/Assets/Scripts/Level/Controllers/ScriptedEventsManager.cs index bb1098fb..ec7625a0 100644 --- a/Assets/Scripts/Level/Controllers/ScriptedEventsManager.cs +++ b/Assets/Scripts/Level/Controllers/ScriptedEventsManager.cs @@ -326,10 +326,10 @@ private IEnumerator StartFrequenceLvl4() { rightEyeLaserLR = GameObject.Find("RightEyeLaserLineRenderer").GetComponent(); 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()); diff --git a/Assets/Scripts/Level/Elements/ClosingDoor.cs b/Assets/Scripts/Level/Elements/ClosingDoor.cs index f955bb97..9e796d02 100644 --- a/Assets/Scripts/Level/Elements/ClosingDoor.cs +++ b/Assets/Scripts/Level/Elements/ClosingDoor.cs @@ -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 diff --git a/Assets/Scripts/Level/Elements/DisappearingBlockDouble.cs b/Assets/Scripts/Level/Elements/DisappearingBlockDouble.cs index 32aeadac..4e3b09b3 100644 --- a/Assets/Scripts/Level/Elements/DisappearingBlockDouble.cs +++ b/Assets/Scripts/Level/Elements/DisappearingBlockDouble.cs @@ -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); } diff --git a/Assets/Scripts/Level/Elements/DisappearingBlockSingle.cs b/Assets/Scripts/Level/Elements/DisappearingBlockSingle.cs index 21e54c5b..a65c3cfe 100644 --- a/Assets/Scripts/Level/Elements/DisappearingBlockSingle.cs +++ b/Assets/Scripts/Level/Elements/DisappearingBlockSingle.cs @@ -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); } diff --git a/Assets/Scripts/Level/Elements/ToxicWater.cs b/Assets/Scripts/Level/Elements/ToxicWater.cs index 38b47f7d..fc274e8e 100644 --- a/Assets/Scripts/Level/Elements/ToxicWater.cs +++ b/Assets/Scripts/Level/Elements/ToxicWater.cs @@ -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; } diff --git a/Assets/Scripts/Level/Enemies/Bomberling.cs b/Assets/Scripts/Level/Enemies/Bomberling.cs index a5425919..8d0db5b2 100644 --- a/Assets/Scripts/Level/Enemies/Bomberling.cs +++ b/Assets/Scripts/Level/Enemies/Bomberling.cs @@ -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() { /* @@ -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() { /* diff --git a/Assets/Scripts/Level/Enemies/LaserTurret.cs b/Assets/Scripts/Level/Enemies/LaserTurret.cs index d25ee2b7..2da29999 100644 --- a/Assets/Scripts/Level/Enemies/LaserTurret.cs +++ b/Assets/Scripts/Level/Enemies/LaserTurret.cs @@ -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(); @@ -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; } diff --git a/Assets/Scripts/Other/Util.cs b/Assets/Scripts/Other/Util.cs new file mode 100644 index 00000000..d1c20b5c --- /dev/null +++ b/Assets/Scripts/Other/Util.cs @@ -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); + + } + +} diff --git a/Assets/Scripts/Other/Util.cs.meta b/Assets/Scripts/Other/Util.cs.meta new file mode 100644 index 00000000..5f34b8dd --- /dev/null +++ b/Assets/Scripts/Other/Util.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6fb8da1f6ba6a4d469a3816d6750c2f7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md b/README.md index 7840f2ab..e74c3ca6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # tensquared -The 2D puzzle platformer **tensquared** was made in Unity for the *8-bit Bauhaus* study project in the summer semester of 2019 at the *Bauhaus University Weimar*. The project was started to create a number of Bauhaus-themed games in order to celebrate 100 years of Bauhaus. +The 2D puzzle platformer **tensquared** is a three-man project and was made in Unity for the *8-bit Bauhaus* study project in the summer semester of 2019 at the *Bauhaus University Weimar*. The project was started to create a number of Bauhaus-themed games in order to celebrate 100 years of Bauhaus. See the [documentation](/Documentation/Documentation.md) for more information.