Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SpellSystem : MonoBehaviour
- {
- public int CurrentSkillButton;
- public GameObject[] CharacterSpells = new GameObject[4];
- List<Object> LoadedSpells;
- List<Object> LodedProjectors;
- GameObject SpellDamageRange;
- GameObject SpellRange;
- // 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)
- int LeneSaFacCooldownul = 1;
- float ChargeTimer;
- bool FirstPhase = false;
- bool SecondPhase = false;
- bool ThirdPhase = false;
- RaycastHit hitInfo;
- Vector3 clickLocation = Vector3.zero;
- // ---------------------------
- void Start()
- {
- LoadResources();
- TurnThingsInToFalse();
- }
- void Update()
- {
- SkillSelection();
- MoveSpell();
- CastSpell();
- // TO BE REMOVED IN THE FUTURE
- if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
- {
- clickLocation = hitInfo.point;
- clickLocation.y = 0;
- }
- // ---------------------------
- }
- void TurnThingsInToFalse()
- {
- ChargeTimer = 0;
- FirstPhase = false;
- SecondPhase = false;
- ThirdPhase = false;
- }
- void SkillSelection()
- {
- if (SpellDamageRange != null)
- {
- SpellDamageRange.transform.position = clickLocation; // Move every frame the circle to mouse position
- if ( Input.GetKeyUp(KeyCode.Mouse1) || CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.E)
- {
- SpellDamageRange.SetActive(false); // Dezactivate the current object if the left click is raised.
- }
- }
- if ( Input.GetKeyDown(KeyCode.Q) )
- {
- if ( CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.Q )
- {
- CurrentSkillButton = (int)SpellSystemEnums.SpellButtons.Q;
- TurnThingsInToFalse();
- }
- }
- if ( Input.GetKeyDown(KeyCode.W) )
- {
- if ( CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.W )
- {
- CurrentSkillButton = (int)SpellSystemEnums.SpellButtons.W;
- TurnThingsInToFalse();
- }
- }
- if ( Input.GetKeyDown(KeyCode.E) )
- {
- if ( SpellDamageRange == null ) {
- SpellDamageRange = Instantiate(LodedProjectors[0] as GameObject, clickLocation, Quaternion.Euler(90, 0, 0) );
- }
- else
- if( SpellDamageRange.activeInHierarchy == false)
- {
- SpellDamageRange.SetActive(true);
- }
- if ( CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.E )
- {
- CurrentSkillButton = (int)SpellSystemEnums.SpellButtons.E;
- TurnThingsInToFalse();
- }
- }
- }
- void CastSpell()
- {
- bool LaunchSpell = false;
- if (Input.GetKey(KeyCode.Mouse0) && !Input.GetKey(KeyCode.Mouse1) && CurrentSkillButton != 0 )
- {
- ChargeTimer += Time.deltaTime;
- // Charge Spell's <> AOE
- if (CurrentSkillButton != (int)SpellSystemEnums.SpellButtons.E)
- {
- if (ChargeTimer >= 0.5f && !FirstPhase)
- {
- Debug.Log("1st Spell is launched");
- FirstPhase = true;
- LaunchSpell = true;
- }
- else
- if (ChargeTimer >= 2f && !SecondPhase)
- {
- Debug.Log("2nd Spell is launched");
- SecondPhase = true;
- LaunchSpell = true;
- }
- else
- if (ChargeTimer >= 3f && !ThirdPhase)
- {
- Debug.Log("2nd Spell size increased");
- ThirdPhase = true;
- LaunchSpell = true;
- }
- }
- else // Launch AOE Spell On Click
- {
- // LENE SA FAC COOLDOWNUL
- if (LeneSaFacCooldownul == 1) {
- LaunchSpell = true;
- LeneSaFacCooldownul = 0;
- }
- }
- }
- else
- if ( Input.GetKeyUp(KeyCode.Mouse0) || Input.GetKeyDown(KeyCode.Mouse1) )
- {
- Debug.Log("Action Canceled");
- TurnThingsInToFalse();
- }
- if ( LaunchSpell == true )
- {
- LaunchSpell = false;
- GameObject CurrentSpell = Instantiate( LoadedSpells[CurrentSkillButton] as GameObject, transform.position, transform.rotation );
- CurrentSpell.gameObject.tag = "SpellProjectile";
- if ( CurrentSpell != null )
- {
- SpellComponent SpellC = CurrentSpell.GetComponent<SpellComponent>();
- SpellMovement MoveC = CurrentSpell.AddComponent<SpellMovement>();
- if ( SpellC.iTypeOfSpell == (int)SpellSystemEnums.SpellType.AoE )
- {
- if ( (CurrentSpell.transform.position - clickLocation).sqrMagnitude > SpellC.fSpellRange * SpellC.fSpellRange )
- {
- Destroy(CurrentSpell);
- }
- else
- {
- MoveC.SpellDamageRange = SpellC.iDamageRange;
- MoveC.SpellTargetPosition = clickLocation;
- //Draw the circle damage radius of the AOE Spell on the ground;
- MoveC.DamageCircle = Instantiate(LodedProjectors[ (int)SpellSystemEnums.SpellProjectors.CircleSpellDamageRange] as GameObject, MoveC.SpellTargetPosition, Quaternion.Euler( 90, 0, 0 ));
- MoveC.DamageCircle.GetComponent<Projector>().orthographicSize = MoveC.SpellDamageRange;
- SpellDamageRange.SetActive(false);
- }
- }
- //MoveC.SpellTargetPosition = EnemeyPosition?; if we have mouse over the enemy ( follows the target)
- MoveC.SpellObjectRadius = CurrentSpell.transform.localScale.x / 2;
- MoveC.SpellDirection = Vector3.Normalize(new Vector3( clickLocation.x, 0, clickLocation.z ));
- MoveC.SpellStartPosition = CurrentSpell.transform.position;
- MoveC.SpellSpeed = SpellC.fSpeed;
- MoveC.SpellRange = SpellC.fSpellRange;
- MoveC.TypeOfSpell = SpellC.iTypeOfSpell;
- // TODO: Maybe move this to MoveSpell if we will add something la deflection.
- float Angle = Mathf.Rad2Deg * Mathf.Atan2( MoveC.SpellDirection.z, MoveC.SpellDirection.x );
- CurrentSpell.transform.rotation = Quaternion.Euler(0, - Angle, 0);
- }
- }
- }
- void MoveSpell()
- {
- GameObject[] SpellArr = GameObject.FindGameObjectsWithTag("SpellProjectile"); // Fuck you Jeg
- for (int i = 0; i < SpellArr.Length; i++)
- {
- SpellMovement MoveC = SpellArr[i].GetComponent<SpellMovement>();
- if ( SpellArr[i] != null && MoveC != null ) {
- // Move every frame the spell that's <> AOE
- if (MoveC.TypeOfSpell != (int)SpellSystemEnums.SpellType.AoE) {
- SpellArr[i].transform.Translate(Time.deltaTime * MoveC.SpellSpeed * MoveC.SpellDirection, Space.World);
- MoveC.SpellCurrentPosition = SpellArr[i].transform.position;
- if ( (MoveC.SpellCurrentPosition - MoveC.SpellStartPosition).sqrMagnitude > MoveC.SpellRange * MoveC.SpellRange)
- {
- Destroy(SpellArr[i]);
- }
- else
- {
- CheckForHit(SpellArr[i], MoveC);
- }
- }
- // Move AOE Spell to target position
- else
- {
- MoveC.SpellCurrentPosition = SpellArr[i].transform.position;
- SpellArr[i].transform.position = Vector3.MoveTowards (MoveC.SpellCurrentPosition, MoveC.SpellTargetPosition, MoveC.SpellSpeed * Time.deltaTime );
- if ( MoveC.SpellCurrentPosition == MoveC.SpellTargetPosition )
- {
- CheckForHit( SpellArr[i], MoveC);
- }
- }
- }
- }
- }
- void CheckForHit( GameObject Spell, SpellMovement MoveC )
- {
- GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
- for (int i = 0; i < Enemies.Length; i++)
- {
- // To be updated
- // 0.5f raza inamicului
- float rSpell = MoveC.SpellObjectRadius;
- float rEnemy = 0.5f;
- if ( MoveC.TypeOfSpell != (int)SpellSystemEnums.SpellType.AoE )
- {
- if ( (MoveC.SpellCurrentPosition - Enemies[i].transform.position).sqrMagnitude - (rSpell + rEnemy) * (rSpell + rEnemy) < 0)
- {
- Debug.Log("Spell Hit -> " + Enemies[i]);
- // Send damage to Damage System
- Destroy(Spell);
- }
- }
- else
- {
- if ( (MoveC.SpellCurrentPosition - Enemies[i].transform.position).sqrMagnitude - (MoveC.SpellDamageRange + rEnemy) * (MoveC.SpellDamageRange + rEnemy) < 0 )
- {
- Debug.Log("AOE Hit -> " + Enemies[i] );
- // Send damage to Damage System
- }
- Destroy(MoveC.DamageCircle);
- Destroy(Spell);
- // TO BE REMOVED IN THE FUTURE CA IMI E LENE
- LeneSaFacCooldownul = 1;
- }
- }
- }
- void LoadResources()
- {
- LoadedSpells = new List<Object>();
- LodedProjectors = new List<Object>();
- for (int i = 0; i < (int)SpellSystemEnums.Spells.TotalNumberOfSpells || i < (int)SpellSystemEnums.SpellProjectors.TotalNumberOfProjectors; i++)
- {
- Object tempObject = Resources.Load("Prefabs/Spells/" + System.Enum.GetName(typeof(SpellSystemEnums.Spells), i));
- LoadedSpells.Add(tempObject);
- if ( i < (int)SpellSystemEnums.SpellProjectors.TotalNumberOfProjectors )
- {
- Object tempProjector = Resources.Load("Prefabs/" + System.Enum.GetName(typeof(SpellSystemEnums.SpellProjectors), i));
- LodedProjectors.Add(tempProjector);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement