Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.74 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpellSystem : MonoBehaviour
  6. {
  7.     public int CurrentSkillButton;
  8.     public GameObject[] CharacterSpells = new GameObject[4];
  9.  
  10.     List<Object> LoadedSpells;
  11.     List<Object> LodedProjectors;
  12.  
  13.     GameObject SpellDamageRange;
  14.     GameObject SpellRange;
  15.  
  16.     // TO BE REMOVED IN THE FUTURE - LENE SA FAC COOLDOWN-ul si ca JEG INCA NU A DEFINIT CE FACEM CU CHARGETIME SI CUM VOM LANSA SPELL_URILE (EU PREFER CAST TIME SI COOLDOWN)
  17.     int LeneSaFacCooldownul = 1;
  18.  
  19.     float ChargeTimer;
  20.  
  21.     bool FirstPhase = false;
  22.     bool SecondPhase = false;
  23.     bool ThirdPhase = false;
  24.  
  25.     RaycastHit hitInfo;
  26.     Vector3 clickLocation = Vector3.zero;
  27.     // ---------------------------
  28.  
  29.  
  30.     void Start()
  31.     {
  32.         LoadResources();
  33.         TurnThingsInToFalse();
  34.     }
  35.     void Update()
  36.     {
  37.  
  38.         SkillSelection();
  39.         MoveSpell();
  40.         CastSpell();
  41.  
  42.         // TO BE REMOVED IN THE FUTURE
  43.         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
  44.         {
  45.             clickLocation = hitInfo.point;
  46.             clickLocation.y = 0;
  47.         }
  48.         // ---------------------------
  49.     }
  50.     void TurnThingsInToFalse()
  51.     {
  52.         ChargeTimer = 0;
  53.  
  54.         FirstPhase = false;
  55.         SecondPhase = false;
  56.         ThirdPhase = false;
  57.     }
  58.     void SkillSelection()
  59.     {
  60.         if (SpellDamageRange != null)
  61.         {
  62.             SpellDamageRange.transform.position = clickLocation; // Move every frame the circle to mouse position
  63.  
  64.             if ( Input.GetKeyUp(KeyCode.Mouse1) || CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.E)
  65.             {
  66.                 SpellDamageRange.SetActive(false); // Dezactivate the current object if the left click is raised.
  67.             }
  68.         }
  69.  
  70.         if ( Input.GetKeyDown(KeyCode.Q) )
  71.         {
  72.             if ( CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.Q )
  73.             {
  74.                 CurrentSkillButton = (int)SpellSystemEnums.SpellButtons.Q;
  75.                 TurnThingsInToFalse();
  76.             }
  77.         }
  78.  
  79.         if ( Input.GetKeyDown(KeyCode.W) )
  80.         {
  81.             if ( CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.W )
  82.             {
  83.                 CurrentSkillButton = (int)SpellSystemEnums.SpellButtons.W;
  84.                 TurnThingsInToFalse();
  85.             }
  86.         }
  87.  
  88.         if ( Input.GetKeyDown(KeyCode.E) )
  89.         {
  90.             if ( SpellDamageRange == null ) {
  91.                 SpellDamageRange = Instantiate(LodedProjectors[0] as GameObject, clickLocation, Quaternion.Euler(90, 0, 0) );
  92.             }
  93.             else
  94.             if( SpellDamageRange.activeInHierarchy == false)
  95.             {
  96.                 SpellDamageRange.SetActive(true);
  97.             }
  98.  
  99.             if ( CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.E )
  100.             {
  101.                 CurrentSkillButton = (int)SpellSystemEnums.SpellButtons.E;
  102.                 TurnThingsInToFalse();
  103.             }
  104.         }
  105.     }
  106.     void CastSpell()
  107.     {
  108.         bool LaunchSpell = false;
  109.  
  110.         if (Input.GetKey(KeyCode.Mouse0) && !Input.GetKey(KeyCode.Mouse1) && CurrentSkillButton != 0 )
  111.         {
  112.             ChargeTimer += Time.deltaTime;
  113.  
  114.             // Charge Spell's <> AOE
  115.             if (CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.E)
  116.             {
  117.                 if (ChargeTimer >= 0.5f && !FirstPhase)
  118.                 {
  119.                     Debug.Log("1st Spell is launched");
  120.  
  121.                     FirstPhase = true;
  122.                     LaunchSpell = true;
  123.                 }
  124.                 else
  125.                 if (ChargeTimer >= 2f && !SecondPhase)
  126.                 {
  127.                     Debug.Log("2nd Spell is launched");
  128.  
  129.                     SecondPhase = true;
  130.                     LaunchSpell = true;
  131.                 }
  132.                 else
  133.                 if (ChargeTimer >= 3f && !ThirdPhase)
  134.                 {
  135.                     Debug.Log("2nd Spell size increased");
  136.  
  137.                     ThirdPhase = true;
  138.                     LaunchSpell = true;
  139.                 }
  140.             }
  141.             else // Launch AOE Spell On Click
  142.             {
  143.                 // LENE SA FAC COOLDOWNUL
  144.                 if (LeneSaFacCooldownul == 1) {
  145.                     LaunchSpell = true;
  146.  
  147.                     LeneSaFacCooldownul = 0;
  148.                 }
  149.             }
  150.         }
  151.         else
  152.         if ( Input.GetKeyUp(KeyCode.Mouse0) || Input.GetKeyDown(KeyCode.Mouse1) )
  153.         {
  154.             Debug.Log("Action Canceled");
  155.             TurnThingsInToFalse();
  156.         }
  157.         if ( LaunchSpell == true )
  158.         {
  159.             LaunchSpell = false;
  160.             GameObject CurrentSpell = Instantiate( LoadedSpells[CurrentSkillButton] as GameObject, transform.position, transform.rotation );
  161.             CurrentSpell.gameObject.tag = "SpellProjectile";
  162.  
  163.             if ( CurrentSpell != null )
  164.             {
  165.                 SpellComponent SpellC = CurrentSpell.GetComponent<SpellComponent>();
  166.                 SpellMovement  MoveC  = CurrentSpell.AddComponent<SpellMovement>();
  167.  
  168.                 if ( SpellC.iTypeOfSpell == (int)SpellSystemEnums.SpellType.AoE )
  169.                 {
  170.                     if ( (CurrentSpell.transform.position - clickLocation).sqrMagnitude > SpellC.fSpellRange * SpellC.fSpellRange )
  171.                     {
  172.                         Destroy(CurrentSpell);
  173.                     }
  174.                     else
  175.                     {
  176.                         MoveC.SpellDamageRange = SpellC.iDamageRange;
  177.                         MoveC.SpellTargetPosition = clickLocation;
  178.                         //Draw the circle damage radius of the AOE Spell on the ground;
  179.                         MoveC.DamageCircle = Instantiate(LodedProjectors[ (int)SpellSystemEnums.SpellProjectors.CircleSpellDamageRange] as GameObject, MoveC.SpellTargetPosition, Quaternion.Euler( 90, 0, 0 ));
  180.                         MoveC.DamageCircle.GetComponent<Projector>().orthographicSize = MoveC.SpellDamageRange;
  181.  
  182.                         SpellDamageRange.SetActive(false);
  183.                     }
  184.                 }
  185.  
  186.                 //MoveC.SpellTargetPosition = EnemeyPosition?; if we have mouse over the enemy ( follows the target)
  187.                 MoveC.SpellObjectRadius = CurrentSpell.transform.localScale.x / 2;
  188.                 MoveC.SpellDirection = Vector3.Normalize(new Vector3( clickLocation.x, 0, clickLocation.z ));
  189.                 MoveC.SpellStartPosition = CurrentSpell.transform.position;
  190.                 MoveC.SpellSpeed = SpellC.fSpeed;
  191.                 MoveC.SpellRange = SpellC.fSpellRange;
  192.                 MoveC.TypeOfSpell = SpellC.iTypeOfSpell;
  193.  
  194.                 // TODO: Maybe move this to MoveSpell if we will add something la deflection.
  195.                 float Angle = Mathf.Rad2Deg * Mathf.Atan2( MoveC.SpellDirection.z, MoveC.SpellDirection.x );
  196.                 CurrentSpell.transform.rotation = Quaternion.Euler(0, - Angle, 0);
  197.             }
  198.         }
  199.     }
  200.     void MoveSpell()
  201.     {
  202.         GameObject[] SpellArr = GameObject.FindGameObjectsWithTag("SpellProjectile"); // Fuck  you Jeg
  203.  
  204.         for (int i = 0; i < SpellArr.Length; i++)
  205.         {
  206.             SpellMovement MoveC = SpellArr[i].GetComponent<SpellMovement>();
  207.  
  208.             if ( SpellArr[i] != null && MoveC != null ) {
  209.                 // Move every frame the spell that's <> AOE
  210.                 if (MoveC.TypeOfSpell != (int)SpellSystemEnums.SpellType.AoE) {
  211.                     SpellArr[i].transform.Translate(Time.deltaTime * MoveC.SpellSpeed * MoveC.SpellDirection, Space.World);
  212.                     MoveC.SpellCurrentPosition = SpellArr[i].transform.position;
  213.  
  214.                     if ( (MoveC.SpellCurrentPosition - MoveC.SpellStartPosition).sqrMagnitude > MoveC.SpellRange * MoveC.SpellRange)
  215.                     {
  216.                         Destroy(SpellArr[i]);
  217.                     }
  218.                     else
  219.                     {
  220.                         CheckForHit(SpellArr[i], MoveC);
  221.                     }
  222.                 }
  223.                 // Move AOE Spell to target position
  224.                 else
  225.                 {
  226.                     MoveC.SpellCurrentPosition = SpellArr[i].transform.position;
  227.                     SpellArr[i].transform.position = Vector3.MoveTowards (MoveC.SpellCurrentPosition, MoveC.SpellTargetPosition, MoveC.SpellSpeed * Time.deltaTime );
  228.  
  229.                     if ( MoveC.SpellCurrentPosition == MoveC.SpellTargetPosition )
  230.                     {
  231.                         CheckForHit( SpellArr[i], MoveC);
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.     }
  237.     void CheckForHit( GameObject Spell, SpellMovement MoveC )
  238.     {
  239.         GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
  240.  
  241.         for (int i = 0; i < Enemies.Length; i++)
  242.         {
  243.             // To be updated
  244.             // 0.5f raza inamicului
  245.             float rSpell = MoveC.SpellObjectRadius;
  246.             float rEnemy = 0.5f;
  247.  
  248.             if ( MoveC.TypeOfSpell != (int)SpellSystemEnums.SpellType.AoE )
  249.             {
  250.                 if ( (MoveC.SpellCurrentPosition - Enemies[i].transform.position).sqrMagnitude - (rSpell + rEnemy) * (rSpell + rEnemy) < 0)
  251.                 {
  252.                     Debug.Log("Spell Hit -> " + Enemies[i]);
  253.                     // Send damage to Damage System
  254.                     Destroy(Spell);
  255.                 }
  256.             }
  257.             else
  258.             {
  259.                 if ( (MoveC.SpellCurrentPosition - Enemies[i].transform.position).sqrMagnitude - (MoveC.SpellDamageRange + rEnemy) * (MoveC.SpellDamageRange + rEnemy) < 0 )
  260.                 {
  261.                     Debug.Log("AOE Hit -> " + Enemies[i] );
  262.                     // Send damage to Damage System
  263.                 }
  264.                 Destroy(MoveC.DamageCircle);
  265.                 Destroy(Spell);
  266.  
  267.                 // TO BE REMOVED IN THE FUTURE CA IMI E LENE
  268.                 LeneSaFacCooldownul = 1;
  269.             }
  270.         }
  271.     }
  272.  
  273.     void LoadResources()
  274.     {
  275.         LoadedSpells = new List<Object>();
  276.         LodedProjectors = new List<Object>();
  277.  
  278.         for (int i = 0; i < (int)SpellSystemEnums.Spells.TotalNumberOfSpells || i < (int)SpellSystemEnums.SpellProjectors.TotalNumberOfProjectors; i++)
  279.         {
  280.             Object tempObject = Resources.Load("Prefabs/Spells/" + System.Enum.GetName(typeof(SpellSystemEnums.Spells), i));
  281.             LoadedSpells.Add(tempObject);
  282.  
  283.             if ( i < (int)SpellSystemEnums.SpellProjectors.TotalNumberOfProjectors )
  284.             {
  285.                 Object tempProjector = Resources.Load("Prefabs/" + System.Enum.GetName(typeof(SpellSystemEnums.SpellProjectors), i));
  286.                 LodedProjectors.Add(tempProjector);
  287.             }
  288.         }
  289.     }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement