diff options
Diffstat (limited to 'Assets/Scripts/Actors')
-rw-r--r-- | Assets/Scripts/Actors/Actor.cs | 121 | ||||
-rw-r--r-- | Assets/Scripts/Actors/Actor.cs.meta | 11 | ||||
-rw-r--r-- | Assets/Scripts/Actors/Player.cs | 47 | ||||
-rw-r--r-- | Assets/Scripts/Actors/Player.cs.meta | 11 |
4 files changed, 190 insertions, 0 deletions
diff --git a/Assets/Scripts/Actors/Actor.cs b/Assets/Scripts/Actors/Actor.cs new file mode 100644 index 0000000..52a67d3 --- /dev/null +++ b/Assets/Scripts/Actors/Actor.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using MontanaJohns.Core; +using MontanaJohns.Items; +using UnityEngine; + +namespace MontanaJohns.Actors +{ + [RequireComponent(typeof(Rigidbody2D))] + [RequireComponent(typeof(CapsuleCollider2D))] + public abstract class Actor : MonoBehaviour + { + [SerializeField] protected float gravityScale = 1.5f; + [SerializeField] protected Stats baseStats = Stats.DefaultBaseStats(); + [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; + protected Active activeItem; + Collection<Item> items; + bool isFalling; + + protected int jumpCount; + protected Vector2 acceleration; + + protected virtual void Awake() + { + _collider = GetComponent<CapsuleCollider2D>(); + _rigidBody = GetComponent<Rigidbody2D>(); + _transform = GetComponent<Transform>(); + _renderer = GetComponent<SpriteRenderer>(); + _animator = GetComponent<Animator>(); + } + + protected virtual void Start() + { + _rigidBody.freezeRotation = true; + _rigidBody.collisionDetectionMode = CollisionDetectionMode2D.Continuous; + _rigidBody.gravityScale = gravityScale; + + items = new(); + stats = baseStats; + jumpCount = stats.maxJumps; + } + + protected virtual void FixedUpdate() + { + if (!_animator.GetBool("airborn") && !Physics2D.OverlapCircle(groundCheckPoint.position, 0.2f, groundLayer)) + { + _animator.SetTrigger("fall"); + StartCoroutine(Falling()); + } + } + + public virtual void Move(float input) + { + var target = new Vector2(input * stats.speedMultiplier * 10, _rigidBody.velocity.y); + _rigidBody.velocity = Vector2.SmoothDamp(_rigidBody.velocity, target, ref acceleration, .05f); + _animator.SetBool("moving", Mathf.Abs(_rigidBody.velocity.x) > 0.001); + + if (_rigidBody.velocity.x < -0.001) + _renderer.flipX = true; + else if (_rigidBody.velocity.x > 0.001) + _renderer.flipX = false; + } + + public virtual void Jump() + { + if (jumpCount++ <= stats.maxJumps) + { + Debug.Log($"Jumping! Force: {stats.jumpForce}"); + _rigidBody.AddForce(Vector2.up * stats.jumpForce); + _animator.SetTrigger("jump"); + _animator.SetBool("airborn", true); + StartCoroutine(Falling()); + } + } + + public virtual void Use() + { + Debug.Log("Using!"); + if (activeItem != null) + activeItem.Use(); + } + + public virtual void AddItem(Item item) + { + Debug.Log($"Adding item: {item}!"); + items.Add(item); + stats = baseStats + items.Select(i => i.stats).Sum(); + } + + IEnumerator Falling() + { + Debug.Log("Falling!"); + + _animator.SetBool("airborn", true); + + while (_rigidBody.velocity.y > 0 || !Physics2D.OverlapCircle(groundCheckPoint.position, 0.2f, groundLayer)) + { + yield return new WaitForFixedUpdate(); + } + + jumpCount = 0; + _animator.SetBool("airborn", false); + + Debug.Log("Fell!"); + + yield break; + } + } +}
\ No newline at end of file diff --git a/Assets/Scripts/Actors/Actor.cs.meta b/Assets/Scripts/Actors/Actor.cs.meta new file mode 100644 index 0000000..e88cc1a --- /dev/null +++ b/Assets/Scripts/Actors/Actor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8e4e952a669f0f94786fa19bfff7095e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Actors/Player.cs b/Assets/Scripts/Actors/Player.cs new file mode 100644 index 0000000..1edbbd7 --- /dev/null +++ b/Assets/Scripts/Actors/Player.cs @@ -0,0 +1,47 @@ +using MontanaJohns.Core.Interfaces; +using MontanaJohns.Items; +using UnityEngine; +using UnityEngine.InputSystem; + +namespace MontanaJohns.Actors +{ + [RequireComponent(typeof(Rigidbody2D))] + [RequireComponent(typeof(CapsuleCollider2D))] + public class Player : Actor, IFollowable + { + public Transform ActorTransform => _transform; + public Camera MainCamera => _camera; + + Camera _camera; + PlayerInput playerInput; + InputAction use, move, jump; + + protected override void Awake() + { + base.Awake(); + _camera = FindObjectOfType<Camera>(); + playerInput = GetComponent<PlayerInput>(); + move = playerInput.currentActionMap.FindAction("Move"); + jump = playerInput.currentActionMap.FindAction("Jump"); + use = playerInput.currentActionMap.FindAction("Use"); + + jump.started += context => Jump(); + + use.started += context => Use(); + move.started += context => Debug.Log("Moving!"); + move.performed += context => Debug.Log("Stopping!"); + } + + protected override void Start() + { + base.Start(); + activeItem = gameObject.AddComponent<Whip>(); + } + + protected void Update() + { + ((IFollowable)this).Follow(); + base.Move(move.ReadValue<Vector2>().x); + } + } +}
\ No newline at end of file diff --git a/Assets/Scripts/Actors/Player.cs.meta b/Assets/Scripts/Actors/Player.cs.meta new file mode 100644 index 0000000..2b84a97 --- /dev/null +++ b/Assets/Scripts/Actors/Player.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8b7bd99a5fb2fc04eaa10dd4dca56706 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |