aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Actors
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/Actors')
-rw-r--r--Assets/Scripts/Actors/Actor.cs208
-rw-r--r--Assets/Scripts/Actors/Actor.cs.meta11
-rw-r--r--Assets/Scripts/Actors/Enemy.cs72
-rw-r--r--Assets/Scripts/Actors/Enemy.cs.meta11
-rw-r--r--Assets/Scripts/Actors/Player.cs86
-rw-r--r--Assets/Scripts/Actors/Player.cs.meta11
6 files changed, 399 insertions, 0 deletions
diff --git a/Assets/Scripts/Actors/Actor.cs b/Assets/Scripts/Actors/Actor.cs
new file mode 100644
index 0000000..eb8185e
--- /dev/null
+++ b/Assets/Scripts/Actors/Actor.cs
@@ -0,0 +1,208 @@
+using System.Collections;
+using System.Collections.ObjectModel;
+using System.Linq;
+using MontanaJohns.Core;
+using MontanaJohns.Items;
+using UnityEngine;
+
+namespace MontanaJohns.Actors
+{
+ [RequireComponent(typeof(Rigidbody2D))]
+ public abstract class Actor : MonoBehaviour
+ {
+ [SerializeField] protected float gravityScale = 1.5f;
+ [SerializeField] protected Stats baseStats = Stats.DefaultBaseStats();
+ [SerializeField] protected Transform groundCheckPoint1;
+ [SerializeField] protected Transform groundCheckPoint2;
+ [SerializeField] protected float invulnTime = 0f;
+ [SerializeField] protected float hangTime;
+
+ protected Rigidbody2D _rigidBody;
+ protected SpriteRenderer _renderer;
+ protected Animator _animator;
+ protected Transform _transform;
+ protected AudioManager _audio;
+
+ protected Active activeItem;
+ protected bool isGrappling;
+ protected bool isMoving;
+ protected Vector2? grapplePoint = null;
+ protected LayerMask groundLayers;
+ protected Collection<Item> items;
+ protected int jumpCount;
+ protected Vector2 acceleration;
+ protected bool invuln;
+ protected float hangCount;
+
+ public Stats stats;
+ public int health;
+
+
+ protected virtual void Awake()
+ {
+ _rigidBody = GetComponent<Rigidbody2D>();
+ _transform = GetComponent<Transform>();
+ _renderer = GetComponent<SpriteRenderer>();
+ _animator = GetComponent<Animator>();
+ _audio = FindObjectOfType<AudioManager>();
+ groundLayers = LayerMask.GetMask("Grapple", "Ground");
+
+ _rigidBody.freezeRotation = true;
+ _rigidBody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
+ _rigidBody.gravityScale = gravityScale;
+
+ items = new();
+ stats = baseStats;
+ health = stats.maxHealth;
+ jumpCount = stats.maxJumps;
+ }
+
+ protected virtual void Start()
+ {
+
+ }
+
+ protected virtual void FixedUpdate()
+ {
+ if (!_animator.GetBool("airborn") && !(Physics2D.OverlapCircle(groundCheckPoint1.position, 0.2f, groundLayers) || Physics2D.OverlapCircle(groundCheckPoint2.position, 0.2f, groundLayers)))
+ {
+ _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) > 1);
+ isMoving = Mathf.Abs(_rigidBody.velocity.x) > 1;
+
+ if (_rigidBody.velocity.x < -0.1) {
+ _renderer.flipX = true;
+ for (int i = 0; i < transform.childCount; i++) {
+ var rot = transform.GetChild(i).rotation;
+ rot.y = 180f;
+ transform.GetChild(i).rotation = rot;
+ }
+ }
+ else if (_rigidBody.velocity.x > 0.1) {
+ _renderer.flipX = false;
+ for (int i = 0; i < transform.childCount; i++) {
+ var rot = transform.GetChild(i).rotation;
+ rot.y = 0f;
+ transform.GetChild(i).rotation = rot;
+ }
+ }
+ }
+ 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()
+ {
+ if (isGrappling)
+ Use();
+ if(Physics2D.OverlapCircle(groundCheckPoint1.position, 0.2f, groundLayers) || Physics2D.OverlapCircle(groundCheckPoint2.position, 0.2f, groundLayers))
+ {
+ jumpCount = stats.maxJumps;
+ hangCount = hangTime;
+ }
+ else
+ {
+ hangCount -= Time.deltaTime;
+ }
+ if (jumpCount > 0 && hangCount > 0f)
+ {
+ jumpCount--;
+ _rigidBody.AddForce(Vector2.up * stats.jumpForce);
+ _animator.SetTrigger("jump");
+ _animator.SetBool("airborn", true);
+ StartCoroutine(Falling());
+ }
+ }
+
+ public virtual void Use()
+ {
+ Vector2? grapplePoint;
+ if (activeItem != null)
+ {
+ grapplePoint = activeItem.Use();
+ if (grapplePoint != null)
+ isGrappling = true;
+ else
+ isGrappling = false;
+ }
+ else
+ grapplePoint = null;
+ this.grapplePoint = grapplePoint;
+ }
+
+ public virtual void Cancel()
+ {
+ if (PauseMenu.isPaused)
+ {
+ PauseMenu.Resume();
+ }
+ else
+ {
+ PauseMenu.Pause();
+ }
+ }
+
+ public virtual void AddItem(Item item)
+ {
+ items.Add(item);
+ stats = baseStats + items.Select(i => i.stats).Sum();
+ }
+
+ public virtual void Grapple(float xInput, float yInput, Vector2 grapplePoint)
+ {
+ Move(xInput);
+ MoveY(yInput);
+ }
+
+ public virtual void TakeDamage(int damage)
+ {
+ StartCoroutine(Damage(damage));
+ }
+
+ private IEnumerator Damage(int damage)
+ {
+ if(!invuln)
+ {
+ invuln = true;
+ health -= damage;
+ StartCoroutine(DamageAnimation());
+ yield return new WaitForSeconds(invulnTime);
+ invuln = false;
+ }
+ }
+
+ private IEnumerator DamageAnimation()
+ {
+ _renderer.color = Color.red;
+ yield return new WaitForSeconds(0.5f);
+ _renderer.color = Color.white;
+ }
+
+ private IEnumerator Falling()
+ {
+ _animator.SetBool("airborn", true);
+
+ bool falling = true;
+ while (falling)
+ {
+ if (Physics2D.OverlapCircle(groundCheckPoint1.position, 0.2f, groundLayers) || Physics2D.OverlapCircle(groundCheckPoint2.position, 0.2f, groundLayers))
+ falling = false;
+ yield return new WaitForEndOfFrame();
+ }
+
+ _animator.SetBool("airborn", false);
+
+ 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/Enemy.cs b/Assets/Scripts/Actors/Enemy.cs
new file mode 100644
index 0000000..d7d699e
--- /dev/null
+++ b/Assets/Scripts/Actors/Enemy.cs
@@ -0,0 +1,72 @@
+using UnityEngine;
+using System.Collections;
+
+namespace MontanaJohns.Actors
+{
+ [RequireComponent(typeof(Rigidbody2D))]
+ public class Enemy : Actor
+ {
+ protected GameObject player;
+ protected float attackRate = 0.5f;
+ protected float nextAttackTime = 0f;
+
+ bool playerSeen;
+
+ protected override void Awake()
+ {
+ base.Awake();
+ player = GameObject.FindGameObjectWithTag("Player");
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
+ if(!playerSeen)
+ {
+ if (Mathf.Abs(player.transform.position.x - transform.position.x) <= 50 && Mathf.Abs(player.transform.position.y - transform.position.y) <= 15)
+ playerSeen = true;
+ }
+ else
+ {
+ MoveTowardsPlayer();
+ Attack();
+ }
+ CheckHealth();
+ }
+
+ protected override void FixedUpdate()
+ {
+ // Temp override while missing falling logic/animations
+ }
+
+ void MoveTowardsPlayer()
+ {
+ if (player.transform.position.x < transform.position.x) Move(-stats.speedMultiplier * 0.5f);
+ else Move(stats.speedMultiplier * 0.5f);
+ }
+
+ void CheckHealth()
+ {
+ if (health <= 0) Destroy(gameObject);
+ }
+
+ void Attack()
+ {
+ if (Mathf.Abs(player.transform.position.x - transform.position.x) <= 5 && Mathf.Abs(player.transform.position.y - transform.position.y) <= 2 && Time.time >= nextAttackTime) {
+ _animator.SetTrigger("attack");
+ player.GetComponent<Actor>().TakeDamage(1);
+ nextAttackTime = Time.time + 1f / attackRate;
+ }
+ }
+
+ private void OnCollisionEnter2D(Collision2D other)
+ {
+ if (other.gameObject.tag == "Projectile")
+ {
+ // TODO update once projectile is made an Active Item
+ TakeDamage(1);
+ Destroy(other.gameObject);
+ }
+ }
+ }
+}
diff --git a/Assets/Scripts/Actors/Enemy.cs.meta b/Assets/Scripts/Actors/Enemy.cs.meta
new file mode 100644
index 0000000..26a1d77
--- /dev/null
+++ b/Assets/Scripts/Actors/Enemy.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 6751c31468656894092c67dc838ac9a6
+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..8535a41
--- /dev/null
+++ b/Assets/Scripts/Actors/Player.cs
@@ -0,0 +1,86 @@
+using MontanaJohns.Core.Interfaces;
+using MontanaJohns.Items;
+using UnityEngine;
+using UnityEngine.InputSystem;
+
+namespace MontanaJohns.Actors
+{
+ [RequireComponent(typeof(Rigidbody2D))]
+ public class Player : Actor, IFollowable
+ {
+ public Transform ActorTransform => _transform;
+ public Camera MainCamera => _camera;
+ public GameObject projectilePrefab;
+ public Transform firePoint;
+ public Vector3 spawnPoint;
+
+ private Camera _camera;
+ private PlayerInput playerInput;
+ private InputAction use, move, jump, attack, cancel;
+
+ protected override void Awake()
+ {
+ base.Awake();
+ _camera = FindObjectOfType<Camera>();
+ playerInput = GetComponent<PlayerInput>();
+ use = move = jump = attack = cancel = null;
+ move = playerInput.currentActionMap.FindAction("Move");
+ jump = playerInput.currentActionMap.FindAction("Jump");
+ attack = playerInput.currentActionMap.FindAction("Attack");
+ use = playerInput.currentActionMap.FindAction("Use");
+ cancel = playerInput.currentActionMap.FindAction("Cancel");
+
+ jump.started += context => Jump();
+ use.started += context => Use();
+ attack.started += context => Fire();
+ cancel.started += context => Cancel();
+ }
+
+ protected override void Start()
+ {
+ base.Start();
+ GameObject loadedItem = (GameObject)Instantiate(Resources.Load("ActiveItems/Whip"));
+ activeItem = loadedItem.GetComponent<Whip>();
+ ResetStats();
+ spawnPoint = transform.position;
+ }
+
+ protected void Update()
+ {
+ ((IFollowable)this).Follow();
+ if(isGrappling)
+ {
+ base.Grapple(move.ReadValue<Vector2>().x, move.ReadValue<Vector2>().y, (Vector2)grapplePoint);
+ }
+ else
+ {
+ base.Move(move.ReadValue<Vector2>().x);
+ if (isMoving && !_animator.GetBool("airborn") && !_audio.isPlaying("RunningOnGrass")) _audio.Play("RunningOnGrass");
+ else if (!isMoving || _animator.GetBool("airborn")) _audio.Stop("RunningOnGrass");
+ }
+ DeathCheck();
+ }
+
+ protected void DeathCheck()
+ {
+ if(health <= 0)
+ {
+ MainCamera.GetComponent<LevelController>().ResetLevel();
+ ResetStats();
+ health = stats.maxHealth;
+ transform.position = spawnPoint;
+ }
+ }
+
+ protected void ResetStats()
+ {
+ stats = baseStats + activeItem.stats;
+ }
+
+ protected void Fire()
+ {
+ Instantiate(projectilePrefab, firePoint.position, firePoint.rotation);
+ _audio.Play("Gunshot");
+ }
+ }
+} \ 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: