Facebook
From Xodius, 1 Month ago, written in C#.
Embed
Download Paste or View Raw
Hits: 110
  1. CharacterClass.cs
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public enum CharacterState
  7. {
  8.     Idle,
  9.     Moving,
  10.     Attack,
  11.     SpecialAttack,
  12.     Dead
  13. }
  14.  
  15. public class CharacterClass : MonoBehaviour
  16. {
  17.     public int maxHealth;
  18.     public int currentHealth;
  19.     public int damage;
  20.     public int lives = 3;
  21.     public CharacterState currentState;
  22.     public Animator animator;
  23.     public CharacterMovement characterMovement;
  24.     public Collider2D attackCollider;
  25.     public GameOverTransition gameOverTransition;
  26.     public HealthBar healthBar;
  27.     public bool canAttack = true;
  28.     private bool inCooldown = false;
  29.     public float cooldownDuration = 5f;
  30.     private float cooldownTimer = 0f;
  31.     private List<Collider2D> cols = new List<Collider2D>();
  32.     private PlayerInput input;
  33.     private CollectionManager collectionManager;
  34.     private float speedMultiplier = 1f;
  35.  
  36.     private void Awake()
  37.     {
  38.         currentHealth = maxHealth;
  39.         GameObject HPBar = GameObject.FindGameObjectWithTag("UI");
  40.         healthBar = HPBar.GetComponent<HealthBar>();
  41.         healthBar.SetMaxHealth(maxHealth);
  42.         currentState = CharacterState.Idle;
  43.         input = GetComponent<PlayerInput>();
  44.         attackCollider = GetComponent<BoxCollider2D>();
  45.  
  46.         if (attackCollider == null)
  47.         {
  48.             attackCollider = GetComponentInChildren<BoxCollider2D>();
  49.             if (attackCollider == null)
  50.             {
  51.                 Debug.LogError("BoxCollider2D component not found in the GameObject hierarchy.");
  52.             }
  53.         }
  54.  
  55.         collectionManager = FindObjectOfType<CollectionManager>();
  56.         characterMovement = GetComponent<CharacterMovement>();
  57.     }
  58.  
  59.     public void RegularAttack()
  60.     {
  61.         if (!inCooldown && input.controls.AttackState && canAttack)
  62.         {
  63.             Debug.Log("Attack Input Recieved");
  64.             animator.SetBool("Attack", true);
  65.             StartCoroutine(EndAttackAnimationAfterDelay());
  66.             attackCollider.OverlapCollider(new ContactFilter2D(), cols);
  67.             if (cols.Count > 0)
  68.             {
  69.                 StartCooldown();
  70.                 foreach (var col in cols)
  71.                 {
  72.                     EnemyClass enemyComponent = col.transform.root.GetComponentInChildren<EnemyClass>();
  73.                     if (col.TryGetComponent(out SpriteRenderer sr) && enemyComponent != null)
  74.                     {
  75.                         sr.color = Color.red;
  76.                         SendDamage(damage, enemyComponent);
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.     public void SpecialAttack()
  84.     {
  85.         if (!inCooldown && input.controls.SpecialAttackState && canAttack)
  86.         {
  87.             Debug.Log("Special Attack Input Recieved");
  88.             animator.SetBool("SAttack", true);
  89.             StartCoroutine(EndSpecialAttackAnimationAfterDelay());
  90.             attackCollider.OverlapCollider(new ContactFilter2D(), cols);
  91.  
  92.             if (cols.Count > 0)
  93.             {
  94.                 StartCooldown();
  95.                 foreach (var col in cols)
  96.                 {
  97.                     EnemyClass enemy = col.transform.root.GetComponentInChildren<EnemyClass>();
  98.                     if (enemy != null)
  99.                     {
  100.                         enemy.Stun();
  101.                         SendDamage(damage, enemy);
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     public void TakeDamage(int damageAmount)
  109.     {
  110.         currentHealth -= damageAmount;
  111.         animator.SetTrigger("Hurt");
  112.         healthBar.SetCurrentHealth(currentHealth);
  113.  
  114.         if (currentHealth <= 0)
  115.         {
  116.             Die();
  117.         }
  118.     }
  119.  
  120.     private void SendDamage(int damageAmount, EnemyClass enemyClass)
  121.     {
  122.         enemyClass.TakeDamage(damageAmount);
  123.     }
  124.  
  125.     private void Die()
  126.     {
  127.         currentState = CharacterState.Dead;
  128.         characterMovement.enabled = false;
  129.         animator.SetTrigger("Dead");
  130.  
  131.         if (lives > 0)
  132.         {
  133.             Respawn();
  134.         }
  135.         else
  136.         {
  137.             gameOverTransition.GameOver();
  138.         }
  139.     }
  140.  
  141.     private void Respawn()
  142.     {
  143.         lives -= 1;
  144.         currentHealth = maxHealth;
  145.         healthBar.SetMaxHealth(maxHealth);
  146.         transform.position = FindObjectOfType<RespawnController>().respawnPoint.position;
  147.         characterMovement.enabled = true;
  148.         currentState = CharacterState.Idle;
  149.     }
  150.  
  151.     private void StartCooldown()
  152.     {
  153.         inCooldown = true;
  154.         cooldownTimer = cooldownDuration;
  155.         canAttack = false;
  156.     }
  157.  
  158.     private void EndCooldown()
  159.     {
  160.         inCooldown = false;
  161.         canAttack = true;
  162.     }
  163.  
  164.     private IEnumerator EndAttackAnimationAfterDelay()
  165.     {
  166.         yield return new WaitForSeconds(0.5f);
  167.         animator.SetBool("Attack", false);
  168.     }
  169.  
  170.     private IEnumerator EndSpecialAttackAnimationAfterDelay()
  171.     {
  172.         yield return new WaitForSeconds(0.5f);
  173.         animator.SetBool("SAttack", false);
  174.     }
  175.  
  176.     private void Update()
  177.     {
  178.         if (inCooldown)
  179.         {
  180.             cooldownTimer -= Time.deltaTime;
  181.             if (cooldownTimer <= 0)
  182.             {
  183.                 EndCooldown();
  184.             }
  185.         }
  186.  
  187.         if (Mathf.Abs(characterMovement.controls.HorizontalMove) > 0 || Mathf.Abs(characterMovement.controls.VerticalMove) > 0)
  188.         {
  189.             currentState = CharacterState.Moving;
  190.         }
  191.         else
  192.         {
  193.             currentState = CharacterState.Idle;
  194.         }
  195.  
  196.         characterMovement.vSpeed = characterMovement.vSpeed * speedMultiplier;
  197.         characterMovement.hSpeed = characterMovement.hSpeed * speedMultiplier;
  198.     }
  199.  
  200.     // Collectible interactions
  201.     public void Food(bool collected)
  202.     {
  203.         if (collected)
  204.         {
  205.             currentHealth += 10;
  206.             if (currentHealth > maxHealth) currentHealth = maxHealth;
  207.             healthBar.SetCurrentHealth(currentHealth);
  208.         }
  209.     }
  210.  
  211.     public void Star(bool collected)
  212.     {
  213.         if (collected)
  214.         {
  215.             cooldownDuration -= 1;
  216.             if (cooldownDuration <  ) cooldownDurati
  217.         }
  218.     }
  219.  
  220.     public void Heart(bool collected)
  221.     {
  222.         if (collected)
  223.         {
  224.             lives += 1;
  225.         }
  226.     }
  227.  
  228.     public void SpeedBuff(bool collected)
  229.     {
  230.         if (collected)
  231.         {
  232.             speedMultiplier += 0.5f;
  233.             StartCoroutine(ResetSpeedAfterDuration(5f));
  234.         }
  235.     }
  236.  
  237.     private IEnumerator ResetSpeedAfterDuration(float duration)
  238.     {
  239.         yield return new WaitForSeconds(duration);
  240.         speedMultiplier -= 0.5f;
  241.         if (speedMultiplier < 1f) speedMultiplier = 1f;
  242.     }
  243.  
  244.     public void StreetWide(bool collected)
  245.     {
  246.         if (collected)
  247.         {
  248.             // Implement the specific logic for StreetWide collectible
  249.         }
  250.     }
  251.  
  252.     public void StopMoving()
  253.     {
  254.         characterMovement.vSpeed = 0f;
  255.         characterMovement.hSpeed = 0f;
  256.     }
  257. }
  258.  
  259. Coin.cs
  260. using System.Collections;
  261. using System.Collections.Generic;
  262. using UnityEngine;
  263. using System; //lets me use Action
  264.  
  265. public class Coin : MonoBehaviour, iCollectible//calling on interface
  266. {
  267.     public static event Action OnCoinCollected; //event to change coins
  268.     public void Collect(){
  269.         OnCoinCollected?.Invoke();
  270.         Debug.Log("Coin collected!!");
  271.         Destroy(gameObject);
  272.     }
  273. }
  274.  
  275. Collector.cs
  276. using System.Collections;
  277. using System.Collections.Generic;
  278. using UnityEngine;
  279.  
  280. public class Collector : MonoBehaviour
  281. {
  282.     private void OnTriggerEnter2D(Collider2D collision){//when touched
  283.     Debug.Log("Coin Touched");
  284.     iCollectible collectible = collision.GetComponent<iCollectible>();
  285.     if(collectible != null){//if the icollectiable is found, for any collectible
  286.         collectible.Collect();//collect it
  287.     }
  288.  
  289.     }
  290. }
  291.  
  292. CollectionManager.cs
  293. using System.Collections;
  294. using System.Collections.Generic;
  295. using UnityEngine;
  296. using TMPro; // TextMeshPro
  297.  
  298. public class CollectionManager : MonoBehaviour
  299. {
  300.     private CharacterClass characterClass;
  301.     public TextMeshProUGUI CoinText; // text
  302.     public int numCoins = 0; // number of coins collected
  303.  
  304.     // add TMPRO and int for each collectible and make a class for it
  305.  
  306.     private void Awake()
  307.     {
  308.         characterClass = FindObjectOfType<CharacterClass>();
  309.     }
  310.  
  311.     private void OnEnable()
  312.     {
  313.         Coin.OnCoinCollected += CoinCollected;
  314.         Food.OnFoodCollected += FoodCollected;
  315.         Star.OnStarCollected += StarCollected;
  316.         Heart.OnHeartCollected += HeartCollected;
  317.         Late4Shift.OnSpeedIncrease += SpeedIncrease;
  318.         // Add event subscriptions for more collectibles
  319.     }
  320.  
  321.     private void OnDisable()
  322.     {
  323.         Coin.OnCoinCollected -= CoinCollected;
  324.         Food.OnFoodCollected -= FoodCollected;
  325.         Star.OnStarCollected -= StarCollected;
  326.         Heart.OnHeartCollected -= HeartCollected;
  327.         Late4Shift.OnSpeedIncrease -= SpeedIncrease;
  328.     }
  329.  
  330.     private void CoinCollected()
  331.     {
  332.         numCoins++;
  333.         CoinText.text = numCoins.ToString(); // converts int to string
  334.     }
  335.  
  336.     private void FoodCollected()
  337.     {
  338.         characterClass.Food(true);
  339.         Debug.Log("Health Sent to Player");
  340.     }
  341.  
  342.     private void StarCollected()
  343.     {
  344.         characterClass.Star(true);
  345.         Debug.Log("Cooldown Reduction Sent to Player");
  346.     }
  347.  
  348.     private void HeartCollected()
  349.     {
  350.         characterClass.Heart(true);
  351.         Debug.Log("Health Sent to Player");
  352.     }
  353.  
  354.     private void SpeedIncrease()
  355.     {
  356.         characterClass.SpeedBuff(true);
  357.         Debug.Log("Speed Boost Sent to Player");
  358.     }
  359.  
  360.     private void StreetWide()
  361.     {
  362.         characterClass.StreetWide(true);
  363.         Debug.Log("StreetWideWemb sent to player");
  364.     }
  365.  
  366.     // make new method for when new collectible is added
  367. }
  368.