diff options
Diffstat (limited to 'Assets/Scripts')
-rw-r--r-- | Assets/Scripts/Actors/Actor.cs | 30 | ||||
-rw-r--r-- | Assets/Scripts/Actors/Player.cs | 13 | ||||
-rw-r--r-- | Assets/Scripts/Core/Interfaces/IFollowable.cs | 1 | ||||
-rw-r--r-- | Assets/Scripts/Core/Rope.cs | 16 | ||||
-rw-r--r-- | Assets/Scripts/Core/RopeNoSwing.cs | 11 | ||||
-rw-r--r-- | Assets/Scripts/Core/RopeNoSwing.cs.meta (renamed from Assets/Scripts/InfiniteBackground.cs.meta) | 2 | ||||
-rw-r--r-- | Assets/Scripts/Core/Stats.cs | 5 | ||||
-rw-r--r-- | Assets/Scripts/InfiniteBackground.cs | 41 | ||||
-rw-r--r-- | Assets/Scripts/Items/Active.cs | 2 | ||||
-rw-r--r-- | Assets/Scripts/Items/Whip.cs | 53 | ||||
-rw-r--r-- | Assets/Scripts/Parallax.cs | 35 | ||||
-rw-r--r-- | Assets/Scripts/Parallax.cs.meta | 11 |
12 files changed, 155 insertions, 65 deletions
diff --git a/Assets/Scripts/Actors/Actor.cs b/Assets/Scripts/Actors/Actor.cs index 1c52214..bcee7a3 100644 --- a/Assets/Scripts/Actors/Actor.cs +++ b/Assets/Scripts/Actors/Actor.cs @@ -10,7 +10,6 @@ using UnityEngine; namespace MontanaJohns.Actors { [RequireComponent(typeof(Rigidbody2D))] - [RequireComponent(typeof(CapsuleCollider2D))] public abstract class Actor : MonoBehaviour { [SerializeField] protected float gravityScale = 1.5f; @@ -18,14 +17,15 @@ namespace MontanaJohns.Actors [SerializeField] protected LayerMask groundLayer; [SerializeField] protected Transform groundCheckPoint; - protected CapsuleCollider2D _collider; protected Rigidbody2D _rigidBody; protected SpriteRenderer _renderer; protected Animator _animator; protected Transform _transform; - protected Stats stats; + public Stats stats; protected Active activeItem; + protected bool isGrappling; + protected Vector2? grapplePoint = null; Collection<Item> items; bool isFalling; @@ -35,7 +35,6 @@ namespace MontanaJohns.Actors protected virtual void Awake() { - _collider = GetComponent<CapsuleCollider2D>(); _rigidBody = GetComponent<Rigidbody2D>(); _transform = GetComponent<Transform>(); _renderer = GetComponent<SpriteRenderer>(); @@ -74,6 +73,11 @@ namespace MontanaJohns.Actors else if (_rigidBody.velocity.x > 0.001) _renderer.flipX = false; } + public virtual void MoveY(float input) + { + var target = new Vector2(_rigidBody.velocity.x, input * stats.speedMultiplier * 10); + _rigidBody.velocity = Vector2.SmoothDamp(_rigidBody.velocity, target, ref acceleration, .05f); + } public virtual void Jump() { @@ -88,8 +92,18 @@ namespace MontanaJohns.Actors public virtual void Use() { + Vector2? grapplePoint; if (activeItem != null) - activeItem.Use(); + { + grapplePoint = activeItem.Use(); + if (grapplePoint != null) + isGrappling = true; + else + isGrappling = false; + } + else + grapplePoint = null; + this.grapplePoint = grapplePoint; } public virtual void AddItem(Item item) @@ -103,6 +117,12 @@ namespace MontanaJohns.Actors health -= damage; } + public virtual void Grapple(float xInput, float yInput, Vector2 grapplePoint) + { + Move(xInput); + MoveY(yInput); + } + IEnumerator Falling() { _animator.SetBool("airborn", true); diff --git a/Assets/Scripts/Actors/Player.cs b/Assets/Scripts/Actors/Player.cs index 97099eb..3192338 100644 --- a/Assets/Scripts/Actors/Player.cs +++ b/Assets/Scripts/Actors/Player.cs @@ -6,7 +6,6 @@ using UnityEngine.InputSystem; namespace MontanaJohns.Actors { [RequireComponent(typeof(Rigidbody2D))] - [RequireComponent(typeof(CapsuleCollider2D))] public class Player : Actor, IFollowable { public Transform ActorTransform => _transform; @@ -28,8 +27,6 @@ namespace MontanaJohns.Actors jump.started += context => Jump(); use.started += context => Use(); - move.started += context => Debug.Log("Moving!"); - move.performed += context => Debug.Log("Stopping!"); } protected override void Start() @@ -37,12 +34,20 @@ namespace MontanaJohns.Actors base.Start(); GameObject loadedItem = (GameObject)Instantiate(Resources.Load("ActiveItems/Whip")); activeItem = loadedItem.GetComponent<Whip>(); + stats = baseStats + activeItem.stats; } protected void Update() { ((IFollowable)this).Follow(); - base.Move(move.ReadValue<Vector2>().x); + if(isGrappling) + { + base.Grapple(move.ReadValue<Vector2>().x, move.ReadValue<Vector2>().y, (Vector2)grapplePoint); + } + else + { + base.Move(move.ReadValue<Vector2>().x); + } } } }
\ No newline at end of file diff --git a/Assets/Scripts/Core/Interfaces/IFollowable.cs b/Assets/Scripts/Core/Interfaces/IFollowable.cs index 92320de..4184c2e 100644 --- a/Assets/Scripts/Core/Interfaces/IFollowable.cs +++ b/Assets/Scripts/Core/Interfaces/IFollowable.cs @@ -12,6 +12,7 @@ namespace MontanaJohns.Core.Interfaces { Vector3 pos = MainCamera.transform.position; pos.x = Mathf.Lerp(ActorTransform.position.x, MainCamera.transform.position.x, 0.25f); + pos.y = Mathf.Lerp(ActorTransform.position.y, MainCamera.transform.position.y, 0.25f); MainCamera.transform.position = pos; } } diff --git a/Assets/Scripts/Core/Rope.cs b/Assets/Scripts/Core/Rope.cs index aff295a..0e12382 100644 --- a/Assets/Scripts/Core/Rope.cs +++ b/Assets/Scripts/Core/Rope.cs @@ -17,7 +17,7 @@ public class Rope : MonoBehaviour protected bool ropeCreated = false; // Start is called before the first frame update - private void Start() + protected void Start() { player = GameObject.FindGameObjectWithTag("Player"); dj = GetComponent<DistanceJoint2D>(); @@ -36,18 +36,22 @@ public class Rope : MonoBehaviour } // Update is called once per frame - private void Update() + protected void Update() { if (!ropeCreated) { CreateRope(); ropeCreated = true; } + else + { + + } RenderLine(); Simulate(); } - private void RenderLine() + protected void RenderLine() { for (int i = 0; i < ropePositions.Length; i++) { @@ -56,14 +60,14 @@ public class Rope : MonoBehaviour lr.SetPositions(ropePositions); } - private void CreateRope() + protected void CreateRope() { dj.connectedBody = player.GetComponent<Rigidbody2D>(); dj.maxDistanceOnly = true; dj.distance = Vector2.Distance(player.transform.position, transform.position); } - private void Simulate() + protected void Simulate() { Vector2 gravityForce = new Vector2(0f, -gravityMultiplier); @@ -83,7 +87,7 @@ public class Rope : MonoBehaviour } } - private void ApplyContraint() + protected void ApplyContraint() { RopeSegment endSegment1 = ropeSegments[0]; endSegment1.posNow = transform.position; diff --git a/Assets/Scripts/Core/RopeNoSwing.cs b/Assets/Scripts/Core/RopeNoSwing.cs new file mode 100644 index 0000000..84ce1e3 --- /dev/null +++ b/Assets/Scripts/Core/RopeNoSwing.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using UnityEngine; + +public class RopeNoSwing : Rope +{ + protected new void Update() + { + RenderLine(); + Simulate(); + } +} diff --git a/Assets/Scripts/InfiniteBackground.cs.meta b/Assets/Scripts/Core/RopeNoSwing.cs.meta index e80bc47..a08048a 100644 --- a/Assets/Scripts/InfiniteBackground.cs.meta +++ b/Assets/Scripts/Core/RopeNoSwing.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 78cf4261365d6b3418063f289c6881bb +guid: 6d8b5c110f12ee048a9dbe9c32e5ef65 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/Core/Stats.cs b/Assets/Scripts/Core/Stats.cs index 6916005..aac6fe7 100644 --- a/Assets/Scripts/Core/Stats.cs +++ b/Assets/Scripts/Core/Stats.cs @@ -24,6 +24,7 @@ namespace MontanaJohns.Core [SerializeField] public float speedMultiplier; [SerializeField] public int maxJumps; [SerializeField] public float jumpForce; + [SerializeField] public int damage; public static Stats operator +(Stats x, Stats y) { @@ -32,7 +33,8 @@ namespace MontanaJohns.Core maxHealth = x.maxHealth + y.maxHealth, speedMultiplier = x.speedMultiplier + y.speedMultiplier, maxJumps = x.maxJumps + y.maxJumps, - jumpForce = x.jumpForce + y.jumpForce + jumpForce = x.jumpForce + y.jumpForce, + damage = x.damage + y.damage, }; } @@ -44,6 +46,7 @@ namespace MontanaJohns.Core speedMultiplier = 1, maxJumps = 1, jumpForce = 500f, + damage = 0, }; } } diff --git a/Assets/Scripts/InfiniteBackground.cs b/Assets/Scripts/InfiniteBackground.cs deleted file mode 100644 index 5c28723..0000000 --- a/Assets/Scripts/InfiniteBackground.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Collections; -using UnityEngine; - -public class InfiniteBackground : MonoBehaviour -{ - [SerializeField] Camera mainCamera; - [SerializeField] float parallaxEffect = 0.3f; - - internal Vector2 pos; - internal float spriteExtentX; - - void Start() - { - Vector2 pos = transform.position; - spriteExtentX = GetComponent<SpriteRenderer>().bounds.size.x / 2; - } - - void FixedUpdate() - { - float camExtentX = Camera.main.orthographicSize * Screen.width / Screen.height; - float newPosX = pos.x + (mainCamera.transform.position.x * parallaxEffect); - - if (newPosX + spriteExtentX <= Camera.main.transform.position.x + camExtentX) - { - var clone = Instantiate(this.gameObject); - clone.transform.position = new Vector3(newPosX + spriteExtentX, transform.position.y, transform.position.z); - } - else if (newPosX - spriteExtentX >= Camera.main.transform.position.x - camExtentX) - { - var clone = Instantiate(gameObject); - clone.transform.position = new Vector3(newPosX - spriteExtentX, transform.position.y, transform.position.z); - } - - transform.position = new Vector3(newPosX, transform.position.y, transform.position.z); - } - - void OnBecameInvisible() - { - Destroy(gameObject); - } -}
\ No newline at end of file diff --git a/Assets/Scripts/Items/Active.cs b/Assets/Scripts/Items/Active.cs index 5cec317..055e7d7 100644 --- a/Assets/Scripts/Items/Active.cs +++ b/Assets/Scripts/Items/Active.cs @@ -4,6 +4,6 @@ namespace MontanaJohns.Items { public abstract class Active : Item { - public abstract void Use(); + public abstract Vector2? Use(); } }
\ No newline at end of file diff --git a/Assets/Scripts/Items/Whip.cs b/Assets/Scripts/Items/Whip.cs index 5f086eb..134ea53 100644 --- a/Assets/Scripts/Items/Whip.cs +++ b/Assets/Scripts/Items/Whip.cs @@ -1,3 +1,6 @@ +using MontanaJohns.Actors; +using MontanaJohns.Core; +using System.Collections; using UnityEngine; using UnityEngine.InputSystem; @@ -6,25 +9,63 @@ namespace MontanaJohns.Items public class Whip : Active { [SerializeField] protected GameObject hook; - [SerializeField] protected float maxRopeLength = 5f; - + [SerializeField] protected GameObject hookNoSwing; + [SerializeField] protected float maxRopeLength = 20f; + protected GameObject player; protected GameObject currentHook; + protected LayerMask ropeLayers; protected bool ropeExists = false; - public override void Use() + private void Awake() + { + stats.damage = 1; + } + + private void Start() + { + player = GameObject.FindGameObjectWithTag("Player"); + ropeLayers = LayerMask.GetMask("Grapple", "Enemy"); + } + + public override Vector2? Use() { if (!ropeExists) { Vector2 clickLocation = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue()); - - currentHook = Instantiate(hook, clickLocation, Quaternion.identity); - ropeExists = true; + Vector2 playerPos = player.transform.position; + Vector2 direction = clickLocation - playerPos; + RaycastHit2D hit = Physics2D.Raycast(playerPos, direction, maxRopeLength, ropeLayers); + if(hit.collider != null) + { + GameObject collisionGameObject = hit.collider.gameObject; + ropeExists = true; + if (LayerMask.LayerToName(collisionGameObject.layer) == "Grapple") + { + currentHook = Instantiate(hook, clickLocation, Quaternion.identity); + return clickLocation; + } + else + { + StartCoroutine(WhipSmack(collisionGameObject, clickLocation)); + } + } + return null; } else { Destroy(currentHook); ropeExists = false; + return null; } } + + private IEnumerator WhipSmack(GameObject collisionGameObject, Vector2 clickLocation) + { + currentHook = Instantiate(hookNoSwing, clickLocation, Quaternion.identity); + collisionGameObject.GetComponent<Actor>().TakeDamage(player.GetComponent<Stats>().damage); + yield return new WaitForSeconds(0.1f); + Destroy(currentHook); + ropeExists = false; + } } }
\ No newline at end of file diff --git a/Assets/Scripts/Parallax.cs b/Assets/Scripts/Parallax.cs new file mode 100644 index 0000000..2776a6d --- /dev/null +++ b/Assets/Scripts/Parallax.cs @@ -0,0 +1,35 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Parallax : MonoBehaviour +{ + [SerializeField] private new GameObject camera; + [SerializeField] private float parallaxScale; + [SerializeField] private float PixelsPerUnit; + + private float startPos, length; + private void Start() + { + startPos = transform.position.x; + length = GetComponent<SpriteRenderer>().bounds.size.x; + } + + private void Update() + { + float temp = camera.transform.position.x * (1 - parallaxScale); + float dist = camera.transform.position.x * parallaxScale; + + transform.position = Clamp(new Vector2(startPos + dist, transform.position.y), PixelsPerUnit); + + if (temp > startPos + length / 2) + startPos += length; + else if (temp < startPos - length / 2) + startPos -= length; + } + + private Vector2 Clamp(Vector2 location, float ppu) + { + return new Vector2(Mathf.CeilToInt(location.x * ppu), Mathf.CeilToInt(location.y * ppu)) / ppu; + } +} diff --git a/Assets/Scripts/Parallax.cs.meta b/Assets/Scripts/Parallax.cs.meta new file mode 100644 index 0000000..93f3978 --- /dev/null +++ b/Assets/Scripts/Parallax.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5720a5b51ba636c41b33a00d0d35e5b3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |