diff options
Diffstat (limited to 'Assets')
-rw-r--r-- | Assets/Prefabs/Hook.prefab | 4 | ||||
-rw-r--r-- | Assets/Prefabs/Player.prefab | 11 | ||||
-rw-r--r-- | Assets/Resources.meta | 8 | ||||
-rw-r--r-- | Assets/Resources/ActiveItems.meta | 8 | ||||
-rw-r--r-- | Assets/Resources/ActiveItems/Whip.prefab | 53 | ||||
-rw-r--r-- | Assets/Resources/ActiveItems/Whip.prefab.meta | 7 | ||||
-rw-r--r-- | Assets/Scenes/Jungle.unity | 4 | ||||
-rw-r--r-- | Assets/Scripts/Actors/Player.cs | 3 | ||||
-rw-r--r-- | Assets/Scripts/Core/Rope.cs | 132 | ||||
-rw-r--r-- | Assets/Scripts/Core/Rope.cs.meta | 11 | ||||
-rw-r--r-- | Assets/Scripts/Items/Whip.cs | 13 |
11 files changed, 236 insertions, 18 deletions
diff --git a/Assets/Prefabs/Hook.prefab b/Assets/Prefabs/Hook.prefab index 0728e6b..027babc 100644 --- a/Assets/Prefabs/Hook.prefab +++ b/Assets/Prefabs/Hook.prefab @@ -137,13 +137,13 @@ MonoBehaviour: m_GameObject: {fileID: 5459487145348792490} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: f828efbe27e65524c9d7e0d87b6a6f0f, type: 3} + m_Script: {fileID: 11500000, guid: fa96c9f0b931ab04cb7a7e72877a589e, type: 3} m_Name: m_EditorClassIdentifier: constraintIterations: 15 gravityMultiplier: 1 lineWidth: 0.2 - ropeSegmentLength: 0.2 + ropeSegmentLength: 0.1 segmentCount: 35 --- !u!120 &-1471091967506001749 LineRenderer: diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 5521ff2..c6d1e32 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -91,7 +91,7 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2776418409999972310} - m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 2, y: 2, z: 1} m_ConstrainProportionsScale: 0 @@ -223,7 +223,7 @@ Rigidbody2D: m_Interpolate: 0 m_SleepingMode: 1 m_CollisionDetection: 0 - m_Constraints: 0 + m_Constraints: 4 --- !u!70 &2776418409999972324 CapsuleCollider2D: m_ObjectHideFlags: 0 @@ -254,11 +254,12 @@ MonoBehaviour: m_EditorClassIdentifier: gravityScale: 1.5 baseStats: - lives: 3 - speedMultiplier: 1 + maxHealth: 3 + speedMultiplier: 2 maxJumps: 1 - jumpForce: 500 + jumpForce: 600 groundLayer: serializedVersion: 2 m_Bits: 64 groundCheckPoint: {fileID: 2776418409611166769} + health: 0 diff --git a/Assets/Resources.meta b/Assets/Resources.meta new file mode 100644 index 0000000..47c06c6 --- /dev/null +++ b/Assets/Resources.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1f6a165be82958d4990f6834fae79060 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/ActiveItems.meta b/Assets/Resources/ActiveItems.meta new file mode 100644 index 0000000..41d32ce --- /dev/null +++ b/Assets/Resources/ActiveItems.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a574293ff80cbfe44aecf1b2e67b55cc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/ActiveItems/Whip.prefab b/Assets/Resources/ActiveItems/Whip.prefab new file mode 100644 index 0000000..49e78c6 --- /dev/null +++ b/Assets/Resources/ActiveItems/Whip.prefab @@ -0,0 +1,53 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &3155771516553000471 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7701887087981762673} + - component: {fileID: 8972855078906585838} + m_Layer: 0 + m_Name: Whip + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &7701887087981762673 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3155771516553000471} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &8972855078906585838 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3155771516553000471} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 38e73d67ecfac5f44b3cb07408f64a56, type: 3} + m_Name: + m_EditorClassIdentifier: + stats: + maxHealth: 0 + speedMultiplier: 0 + maxJumps: 0 + jumpForce: 0 + hook: {fileID: 5459487145348792490, guid: c5636787c9fa65743baae65d7537d65c, type: 3} + maxRopeLength: 5 diff --git a/Assets/Resources/ActiveItems/Whip.prefab.meta b/Assets/Resources/ActiveItems/Whip.prefab.meta new file mode 100644 index 0000000..b0c2ea7 --- /dev/null +++ b/Assets/Resources/ActiveItems/Whip.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: d03334daaf2f54c49860876733765719 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Jungle.unity b/Assets/Scenes/Jungle.unity index 58bed0d..5330e0c 100644 --- a/Assets/Scenes/Jungle.unity +++ b/Assets/Scenes/Jungle.unity @@ -14269,9 +14269,5 @@ PrefabInstance: propertyPath: m_Name value: Player objectReference: {fileID: 0} - - target: {fileID: 2776418409999972325, guid: b2169aaeb9a0e4542b1fb9d601bcc4b2, type: 3} - propertyPath: m_Constraints - value: 4 - objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: b2169aaeb9a0e4542b1fb9d601bcc4b2, type: 3} diff --git a/Assets/Scripts/Actors/Player.cs b/Assets/Scripts/Actors/Player.cs index 1edbbd7..97099eb 100644 --- a/Assets/Scripts/Actors/Player.cs +++ b/Assets/Scripts/Actors/Player.cs @@ -35,7 +35,8 @@ namespace MontanaJohns.Actors protected override void Start() { base.Start(); - activeItem = gameObject.AddComponent<Whip>(); + GameObject loadedItem = (GameObject)Instantiate(Resources.Load("ActiveItems/Whip")); + activeItem = loadedItem.GetComponent<Whip>(); } protected void Update() diff --git a/Assets/Scripts/Core/Rope.cs b/Assets/Scripts/Core/Rope.cs new file mode 100644 index 0000000..aff295a --- /dev/null +++ b/Assets/Scripts/Core/Rope.cs @@ -0,0 +1,132 @@ +using System.Collections.Generic; +using UnityEngine; + +public class Rope : MonoBehaviour +{ + [SerializeField] protected int constraintIterations = 15; + [SerializeField] protected float gravityMultiplier = 1.0f; + [SerializeField] protected float lineWidth = 0.2f; + [SerializeField] protected float ropeSegmentLength = 0.1f; + [SerializeField] protected int segmentCount = 35; + + protected LineRenderer lr; + protected DistanceJoint2D dj; + protected GameObject player; + protected List<RopeSegment> ropeSegments = new List<RopeSegment>(); + protected Vector3[] ropePositions; + protected bool ropeCreated = false; + + // Start is called before the first frame update + private void Start() + { + player = GameObject.FindGameObjectWithTag("Player"); + dj = GetComponent<DistanceJoint2D>(); + lr = GetComponent<LineRenderer>(); + lr.startWidth = lineWidth; + lr.endWidth = lineWidth; + ropePositions = new Vector3[segmentCount]; + lr.positionCount = segmentCount; + Vector2 ropeLoc = transform.position; + + for (int i = 0; i < segmentCount; i++) + { + ropeSegments.Add(new RopeSegment(ropeLoc)); + ropeLoc.y -= ropeSegmentLength; + } + } + + // Update is called once per frame + private void Update() + { + if (!ropeCreated) + { + CreateRope(); + ropeCreated = true; + } + RenderLine(); + Simulate(); + } + + private void RenderLine() + { + for (int i = 0; i < ropePositions.Length; i++) + { + ropePositions[i] = ropeSegments[i].posNow; + } + lr.SetPositions(ropePositions); + } + + private void CreateRope() + { + dj.connectedBody = player.GetComponent<Rigidbody2D>(); + dj.maxDistanceOnly = true; + dj.distance = Vector2.Distance(player.transform.position, transform.position); + } + + private void Simulate() + { + Vector2 gravityForce = new Vector2(0f, -gravityMultiplier); + + for (int i = 0; i < ropeSegments.Count; i++) + { + RopeSegment segment = ropeSegments[i]; + Vector2 velocity = segment.posNow - segment.posOld; + segment.posOld = segment.posNow; + segment.posNow += velocity; + segment.posNow += gravityForce * Time.deltaTime; + ropeSegments[i] = segment; + } + + for (int i = 0; i < constraintIterations; i++) + { + ApplyContraint(); + } + } + + private void ApplyContraint() + { + RopeSegment endSegment1 = ropeSegments[0]; + endSegment1.posNow = transform.position; + ropeSegments[0] = endSegment1; + + RopeSegment endSegment2 = ropeSegments[ropeSegments.Count - 1]; + endSegment2.posNow = player.transform.position; + ropeSegments[ropeSegments.Count - 1] = endSegment2; + + for (int i = 0; i < ropeSegments.Count - 1; i++) + { + RopeSegment segment1 = ropeSegments[i]; + RopeSegment segment2 = ropeSegments[i + 1]; + + float distance = (segment1.posNow - segment2.posNow).magnitude; + float error = distance - ropeSegmentLength; + Vector2 normalChange = (segment1.posNow - segment2.posNow).normalized; + Vector2 change = normalChange * error; + + if (i != 0) + { + segment1.posNow -= change * 0.5f; + ropeSegments[i] = segment1; + segment2.posNow += change * 0.5f; + ropeSegments[i + 1] = segment2; + } + else + { + segment2.posNow += change; + ropeSegments[i + 1] = segment2; + } + } + } + + public struct RopeSegment + { + public Vector2 posNow; + public Vector2 posOld; + + public RopeSegment(Vector2 pos) + { + this.posNow = pos; + this.posOld = pos; + } + } +} diff --git a/Assets/Scripts/Core/Rope.cs.meta b/Assets/Scripts/Core/Rope.cs.meta new file mode 100644 index 0000000..380fdf2 --- /dev/null +++ b/Assets/Scripts/Core/Rope.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fa96c9f0b931ab04cb7a7e72877a589e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Items/Whip.cs b/Assets/Scripts/Items/Whip.cs index 1a675ce..5f086eb 100644 --- a/Assets/Scripts/Items/Whip.cs +++ b/Assets/Scripts/Items/Whip.cs @@ -1,22 +1,23 @@ using UnityEngine; +using UnityEngine.InputSystem; namespace MontanaJohns.Items { public class Whip : Active { - public GameObject hook; - public bool ropeExists = false; + [SerializeField] protected GameObject hook; + [SerializeField] protected float maxRopeLength = 5f; - GameObject currentHook; + protected GameObject currentHook; + protected bool ropeExists = false; - // Update is called once per frame public override void Use() { if (!ropeExists) { - // Vector2 clickLocation = Mouse.current.position; + Vector2 clickLocation = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue()); - // currentHook = (GameObject)Instantiate(hook, clickLocation, Quaternion.identity); + currentHook = Instantiate(hook, clickLocation, Quaternion.identity); ropeExists = true; } else |