diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index 1387dc9..ea3631c 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -1183,6 +1183,9 @@ PrefabInstance: - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} insertIndex: -1 addedObject: {fileID: 1579580572} + - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} + insertIndex: -1 + addedObject: {fileID: 1579580579} m_SourcePrefab: {fileID: 100100000, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} --- !u!114 &869964774 stripped MonoBehaviour: @@ -1492,6 +1495,9 @@ PrefabInstance: - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} insertIndex: -1 addedObject: {fileID: 1254139637} + - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + insertIndex: -1 + addedObject: {fileID: 1254139644} m_SourcePrefab: {fileID: 100100000, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} --- !u!114 &1232208433 stripped MonoBehaviour: @@ -1551,7 +1557,21 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: StepInterval: 0.8 - Volume: 1 + Volume: 0.25 +--- !u!114 &1254139644 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1254139629} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 20d294d88e5bc8a42afb1f72751fb61f, type: 3} + m_Name: + m_EditorClassIdentifier: + RopeRubleTolerance: 0.5 + rope: {fileID: 1920006248} --- !u!1 &1377274208 GameObject: m_ObjectHideFlags: 0 @@ -3225,7 +3245,21 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: StepInterval: 0.8 - Volume: 1 + Volume: 0.25 +--- !u!114 &1579580579 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1579580564} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 20d294d88e5bc8a42afb1f72751fb61f, type: 3} + m_Name: + m_EditorClassIdentifier: + RopeRubleTolerance: 0.5 + rope: {fileID: 1920006248} --- !u!1 &1600052931 GameObject: m_ObjectHideFlags: 0 @@ -3893,7 +3927,7 @@ MonoBehaviour: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4910321061857220296} - m_Enabled: 1 + m_Enabled: 0 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 8766ad5393174fa488ef4a58b62181b4, type: 3} m_Name: diff --git a/Assets/Scripts/Extensions/FloatExtensions.cs b/Assets/Scripts/Extensions/FloatExtensions.cs new file mode 100644 index 0000000..111d506 --- /dev/null +++ b/Assets/Scripts/Extensions/FloatExtensions.cs @@ -0,0 +1,29 @@ +using System.Collections; +using System.Collections.Generic; +using Unity.Mathematics; + +public static class FloatExtensions +{ + /// + /// Refrence link: https://forum.unity.com/attachments/upload_2021-1-14_13-8-33-png.773578/ + /// + public static float Remap(this float from, float fromMin, float fromMax, float toMin, float toMax) + { + var fromAbs = from - fromMin; + var fromMaxAbs = fromMax - fromMin; + + var normal = fromAbs / fromMaxAbs; + + var toMaxAbs = toMax - toMin; + var toAbs = toMaxAbs * normal; + + var to = toAbs + toMin; + + return to; + } + + /// + /// Just clamps the value between two numbers (Easier to write) + /// + public static float Clamp(this float input, float min, float max) => math.clamp(input, min, max); +} diff --git a/Assets/Scripts/Extensions/FloatExtensions.cs.meta b/Assets/Scripts/Extensions/FloatExtensions.cs.meta new file mode 100644 index 0000000..0f5a4a8 --- /dev/null +++ b/Assets/Scripts/Extensions/FloatExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f285fcef3bcd16648a7d1bc02576bad3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/PlayerMovement.cs b/Assets/Scripts/Player/PlayerMovement.cs index 27e8a5f..aa294fa 100644 --- a/Assets/Scripts/Player/PlayerMovement.cs +++ b/Assets/Scripts/Player/PlayerMovement.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using System.Linq; +using UnityEngine.Windows; +using UnityEngine.InputSystem.DualShock; [RequireComponent(typeof(PlayerInput))] public class PlayerMovement : MonoBehaviour @@ -13,11 +15,6 @@ public class PlayerMovement : MonoBehaviour private bool right = false; - private bool vibrate = false; - [SerializeField] private float stepCooldown = 0.05f; - [SerializeField] private float stepVibrationTime = 0.05f; - - [SerializeField] private GameObject rumble; [Header("Whipping")] [SerializeField] @@ -44,19 +41,13 @@ public class PlayerMovement : MonoBehaviour playerInput.useArrowKeys = true; } - StartCoroutine(ToggleWithDelay()); - } + if (playerInput.controller != null) + { + var pad = (DualShockGamepad)Gamepad.all.ElementAtOrDefault(playerInput.PlayerNum); + if (pad == null) return; - void Update() - { - if (playerInput.movement != Vector2.zero) - { - RumbleWalk(); - animationHandler.Run(true); - } - else - { - animationHandler.Run(false); + if (playerInput.PlayerNum == 1) + pad.SetLightBarColor(Color.red); } } private void FixedUpdate() @@ -81,37 +72,4 @@ public class PlayerMovement : MonoBehaviour } } - private void RumbleWalk() - { - if (vibrate && playerInput.controller != null) - { - if (right) - { - rumble.GetComponent().RumblePulse(0.0f, 0.004f, stepVibrationTime, playerInput.PlayerNum); - right = false; - } - else if (!right) - { - rumble.GetComponent().RumblePulse(0.004f, 0.0f, stepVibrationTime, playerInput.PlayerNum); - right = true; - } - vibrate = false; - } - } - private IEnumerator ToggleWithDelay() - { - while (true) - { - vibrate = !vibrate; - yield return new WaitForSeconds(stepCooldown); - } - } - - void OnCollisionStay2D(Collision2D collision) - { - if (collision.otherCollider.gameObject.CompareTag("Enemy")) - { // Other object is an enemy - hp.TakeDamage(1f); - } - } } diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index 8cc02ea..4e3a2eb 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -127,7 +127,7 @@ public class RopeSimulator : MonoBehaviour if (prevSubDivision - (int) subDivision >= 0) return; - // Extend from start + // Max from start rope.points.Insert(1, new Point(rope.points[1].position)); rope.sticks.Clear(); diff --git a/Assets/Scripts/Rumbling.meta b/Assets/Scripts/Rumbling.meta new file mode 100644 index 0000000..7cc883d --- /dev/null +++ b/Assets/Scripts/Rumbling.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4469770a8125fda4ea033ad0c3ba459b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Rumbling/PlayerRumbling.cs b/Assets/Scripts/Rumbling/PlayerRumbling.cs new file mode 100644 index 0000000..d876942 --- /dev/null +++ b/Assets/Scripts/Rumbling/PlayerRumbling.cs @@ -0,0 +1,84 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using UnityEngine.InputSystem; + +public class PlayerRumbling : MonoBehaviour +{ + [SerializeField] private float RopeRubleTolerance = 0.5f; + + private PlayerInput pInput; + private Gamepad pad; + + // Rope + [SerializeField] private RopeSimulator rope; + + private void Start() + { + Invoke("LateStart", 0.1f); + } + + void LateStart() + { + pInput = GetComponent(); + pad = Gamepad.all.ElementAtOrDefault(pInput.PlayerNum); + if (pad == null) + { + this.enabled = false; + } + + hasInit = true; + } + + bool hasInit = false; + private void Update() + { + if (!hasInit) return; + + var rumble = new RumbleData(); + + float ropeClamed = Mathf.Max(0, rope.Overshoot); + if (ropeClamed > RopeRubleTolerance) + { + float mapped = ropeClamed.Remap(0.5f, 1f, 0f, 1f); + rumble.Max(mapped, mapped); + } + + rumble.Commit(pad); + } +} + +public class RumbleData +{ + private bool modified = false; + private float lowFreq = 0; + private float highFreq = 0; + + public void Max(float low, float high) + { + modified = true; + lowFreq = Mathf.Max(lowFreq, low); + highFreq = Mathf.Max(highFreq, high); + } + + public void Min(float low, float high) + { + modified = true; + lowFreq = Mathf.Min(lowFreq, low); + highFreq = Mathf.Min(highFreq, high); + } + + public void Add(float low, float high) + { + modified = true; + lowFreq += low; + highFreq += high; + } + + public void Commit(Gamepad target) + { + modified = false; + target.SetMotorSpeeds(lowFreq, highFreq); + } +} diff --git a/Assets/Scripts/Rumbling/PlayerRumbling.cs.meta b/Assets/Scripts/Rumbling/PlayerRumbling.cs.meta new file mode 100644 index 0000000..19374db --- /dev/null +++ b/Assets/Scripts/Rumbling/PlayerRumbling.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 20d294d88e5bc8a42afb1f72751fb61f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: