diff --git a/Assets/Resources/Player1.prefab b/Assets/Resources/Player1.prefab index fc4a398..31e9dce 100644 --- a/Assets/Resources/Player1.prefab +++ b/Assets/Resources/Player1.prefab @@ -188,6 +188,7 @@ GameObject: - component: {fileID: 5467488512035376674} - component: {fileID: 3878447480781341932} - component: {fileID: 1749848915408613053} + - component: {fileID: 1949941092232239315} m_Layer: 7 m_Name: Player1 m_TagString: Player @@ -224,7 +225,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 3dbdb849a2f5df14d9e109a7776c5ac0, type: 3} m_Name: m_EditorClassIdentifier: - player: 0 moveSpeed: 70 stepCooldown: 0.2 stepVibrationTime: 0.05 @@ -295,6 +295,7 @@ MonoBehaviour: anchor: {fileID: 1170758327458850867} body: {fileID: 1508323898269695585} locked: 0 + playerInput: {fileID: 1949941092232239315} --- !u!114 &3878447480781341932 MonoBehaviour: m_ObjectHideFlags: 0 @@ -319,7 +320,25 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: b69b92d72c7244443854899c4b700d9c, type: 3} m_Name: m_EditorClassIdentifier: + playerInput: {fileID: 1949941092232239315} otherPlayerAttack: {fileID: 0} playerMovement: {fileID: 651446758998956252} joint: {fileID: 5467488512035376674} initialDrag: 0 +--- !u!114 &1949941092232239315 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3120938410244321186} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: de340bb967770a7499e140a52a287f84, type: 3} + m_Name: + m_EditorClassIdentifier: + playerNumber: 0 + movement: {x: 0, y: 0} + whipAttack: 0 + ropeLengthShrinken: 0 + ropeLengthExtend: 0 diff --git a/Assets/Scenes/EnemyRope.unity b/Assets/Scenes/EnemyRope.unity index ba5d226..a62fe33 100644 --- a/Assets/Scenes/EnemyRope.unity +++ b/Assets/Scenes/EnemyRope.unity @@ -1103,6 +1103,17 @@ MonoBehaviour: m_Area: 0 m_IgnoreFromBuild: 0 m_AffectedAgents: ffffffff +--- !u!114 &1081435768 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 1949941092232239315, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + m_PrefabInstance: {fileID: 1273044612} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: de340bb967770a7499e140a52a287f84, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1156361827 GameObject: m_ObjectHideFlags: 0 @@ -2064,6 +2075,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 27ac133d9e10e544ba603e07122e3359, type: 3} m_Name: m_EditorClassIdentifier: + playerInput: {fileID: 1081435768} gravity: 15 solveIterations: 10 constrainStickMinLength: 0 @@ -2334,6 +2346,10 @@ PrefabInstance: propertyPath: otherPlayerAttack value: objectReference: {fileID: 465041015} + - target: {fileID: 1949941092232239315, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: playerNumber + value: 1 + objectReference: {fileID: 0} - target: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} propertyPath: m_Name value: Player 2 diff --git a/Assets/Scripts/Player/PlayerInput.cs b/Assets/Scripts/Player/PlayerInput.cs new file mode 100644 index 0000000..37831f8 --- /dev/null +++ b/Assets/Scripts/Player/PlayerInput.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System; +using UnityEngine; +using UnityEngine.InputSystem; + +public class PlayerInput : MonoBehaviour +{ + [SerializeField] private int playerNumber; + + public Vector2 movement; + public Gamepad controller { get; private set; } + + public bool whipAttack; + public event Action ropeLengthShrinken; + public event Action ropeLengthExtend; + + public int PlayerNum => playerNumber; + + private void Awake() + { + controller = Gamepad.all.ElementAtOrDefault(playerNumber); + if (controller == null) + { + Debug.LogWarning($"No Gamepad found for player {playerNumber + 1}"); + } + } + + private void Update() + { + if (controller != null) + { + movement.x = controller.leftStick.x.ReadValue(); + movement.y = controller.leftStick.y.ReadValue(); + + whipAttack = controller.buttonWest.IsPressed(); + + if (controller.rightShoulder.IsPressed()) ropeLengthShrinken?.Invoke(); + if (controller.leftShoulder.IsPressed()) ropeLengthExtend?.Invoke(); + } + else + { + movement.x = Input.GetAxisRaw("Horizontal"); + movement.y = Input.GetAxisRaw("Vertical"); + + whipAttack = Input.GetKey(KeyCode.B); + } + //Debug.Log($"player {playerNumber}: move {movement}"); + } +} diff --git a/Assets/Scripts/Player/PlayerInput.cs.meta b/Assets/Scripts/Player/PlayerInput.cs.meta new file mode 100644 index 0000000..0e901a3 --- /dev/null +++ b/Assets/Scripts/Player/PlayerInput.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: de340bb967770a7499e140a52a287f84 +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 93d0a5c..ff91a14 100644 --- a/Assets/Scripts/Player/PlayerMovement.cs +++ b/Assets/Scripts/Player/PlayerMovement.cs @@ -4,13 +4,11 @@ using UnityEngine; using UnityEngine.InputSystem; using System.Linq; +[RequireComponent(typeof(PlayerInput))] public class PlayerMovement : MonoBehaviour { - public int player = 0; public float moveSpeed = 5f; private Rigidbody2D rb; - private Vector2 movement; - private Gamepad playerController; private bool right = false; @@ -30,28 +28,19 @@ public class PlayerMovement : MonoBehaviour [SerializeField] private float maxWhipMoveSpeed = 30f; + private PlayerInput playerInput; + private void Start() { rb = GetComponent(); - - playerController = Gamepad.all.ElementAtOrDefault(player); + playerInput = GetComponent(); StartCoroutine(ToggleWithDelay()); } void Update() { - if (playerController != null) - { - movement.x = playerController.leftStick.x.ReadValue(); - movement.y = playerController.leftStick.y.ReadValue(); - } - else - { - movement.x = Input.GetAxisRaw("Horizontal"); - movement.y = Input.GetAxisRaw("Vertical"); ; - } - if (movement.x != 0 || movement.y != 0) + if (playerInput.movement != Vector2.zero) { RumbleWalk(); GetComponent().Run(); @@ -66,31 +55,31 @@ public class PlayerMovement : MonoBehaviour Vector2 ropeDir = whipAttack.otherPlayerAttack.joint.position - whipAttack.joint.position; Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x).normalized; - rb.AddForce(Vector2.Dot(movement, tangent) * tangent * whipMoveSpeed); + rb.AddForce(Vector2.Dot(playerInput.movement, tangent) * tangent * whipMoveSpeed); rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxWhipMoveSpeed); } else if (whipAttack.IsWhippingOtherPlayer) { - movement = Vector2.zero; + playerInput.movement = Vector2.zero; } else { - rb.AddForce(movement * moveSpeed); + rb.AddForce(playerInput.movement * moveSpeed); } } private void RumbleWalk() { - if (vibrate && playerController != null) + if (vibrate && playerInput.controller != null) { if (right) { - rumble.GetComponent().RumblePulse(0.0f, 0.004f, stepVibrationTime, player); + rumble.GetComponent().RumblePulse(0.0f, 0.004f, stepVibrationTime, playerInput.PlayerNum); right = false; } else if (!right) { - rumble.GetComponent().RumblePulse(0.004f, 0.0f, stepVibrationTime, player); + rumble.GetComponent().RumblePulse(0.004f, 0.0f, stepVibrationTime, playerInput.PlayerNum); right = true; } vibrate = false; diff --git a/Assets/Scripts/Player/RopeWhipAttack.cs b/Assets/Scripts/Player/RopeWhipAttack.cs index 1c89a23..a3e1ef1 100644 --- a/Assets/Scripts/Player/RopeWhipAttack.cs +++ b/Assets/Scripts/Player/RopeWhipAttack.cs @@ -6,11 +6,11 @@ using System.Linq; public class RopeWhipAttack : MonoBehaviour { + [SerializeField] PlayerInput playerInput; + public RopeWhipAttack otherPlayerAttack; public PlayerMovement playerMovement; public RopeJoint joint; - - Gamepad controller; public bool IsWhippingOtherPlayer => joint.locked; public bool IsBeingWhipped => otherPlayerAttack.joint.locked; @@ -19,16 +19,14 @@ public class RopeWhipAttack : MonoBehaviour private void Awake() { initialDrag = joint.body.drag; - controller = Gamepad.all.ElementAtOrDefault(playerMovement.player); } private void Update() { // Other player is whip attacking if (otherPlayerAttack.joint.locked) return; - if (controller == null) - return; - if (controller.rightShoulder.IsPressed()) + + if (playerInput.whipAttack) { joint.locked = true; otherPlayerAttack.joint.body.drag = 0f; diff --git a/Assets/Scripts/Rope/RopeJoint.cs b/Assets/Scripts/Rope/RopeJoint.cs index 47020da..89be21e 100644 --- a/Assets/Scripts/Rope/RopeJoint.cs +++ b/Assets/Scripts/Rope/RopeJoint.cs @@ -7,6 +7,7 @@ public class RopeJoint : MonoBehaviour public Transform anchor; public Rigidbody2D body; public bool locked = false; + public PlayerInput playerInput; public Vector3 position => anchor.position; } diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index 4da987b..8c6a8f4 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -24,7 +24,7 @@ public class RopeSimulator : MonoBehaviour RopeJoint start, end; [SerializeField] - int subDivision = 50; + float subDivision = 50f; [SerializeField] float collisionCheckDist = 0.5f; @@ -50,6 +50,12 @@ public class RopeSimulator : MonoBehaviour [SerializeField] float xyGravityDampScalor = 1f; + [SerializeField, Range(0f, 5f)] + public float ropeExtendSpeed, ropeShrinkSpeed; + + [SerializeField] + public float ropeMaxLength; + [Header("Rendering")] [SerializeField] LineRenderer lineRenderer; @@ -72,11 +78,47 @@ public class RopeSimulator : MonoBehaviour // .ConnectPoints(2, 3) // .ConnectPoints(3, 4) // .Build(); + Rebuild(); + + start.playerInput.ropeLengthShrinken += ShrinkenRope; + end.playerInput.ropeLengthShrinken += ShrinkenRope; + + start.playerInput.ropeLengthExtend += ExtendRope; + end.playerInput.ropeLengthExtend += ExtendRope; + } + + void ShrinkenRope() + { + subDivision += ropeShrinkSpeed * Time.deltaTime; + Rebuild(); + } + + void ExtendRope() + { + subDivision -= ropeExtendSpeed * Time.deltaTime; + Rebuild(); + } + + private void OnDestroy() + { + start.playerInput.ropeLengthShrinken -= ShrinkenRope; + end.playerInput.ropeLengthShrinken -= ShrinkenRope; + + start.playerInput.ropeLengthExtend -= ExtendRope; + end.playerInput.ropeLengthExtend -= ExtendRope; + } + + private void Rebuild() + { + Debug.Log("rebuild"); + ropeCollidersParent.DestroyChildren(); + RopeBuilder builder = new RopeBuilder(); builder.AddPoint(new Point(start.position, locked: true)); - for (int i = 1; i < subDivision; i++) + + for (int i = 1; i < (int) subDivision; i++) { - Vector3 pointPos = Vector3.Lerp(start.position, end.position, (float)i / (float)subDivision); + Vector3 pointPos = Vector3.Lerp(start.position, end.position, (float)i / (float)(int)subDivision); //Debug.Log($"pos: {pointPos}, t={i / subDivision}"); Debug.DrawRay(pointPos, (end.position - start.position).normalized); builder.AddPoint(new Point(pointPos)); @@ -84,7 +126,7 @@ public class RopeSimulator : MonoBehaviour builder.AddPoint(new Point(end.position, locked: true)); - for (int i = 0; i < subDivision; i++) + for (int i = 0; i < (int) subDivision; i++) { builder.ConnectPointsWithDesiredLength(i, i + 1, desiredLength: distBetweenRopePoints); }