using System.Collections; using System.Collections.Generic; using UnityEngine; using static Demo_Project.SceneManager; namespace Demo_Project { public class Player : MonoBehaviour { // Start is called before the first frame update Transform transformComponent; int rightClickWaitTime = 0; public int rightClickWaitTimeLimit = 10; public float angleMax = 90; public float angleMin = -70; int chargeTime = 0; int midChargeTime = 60; int fullChargeLimit = 400; Color muzzleFlashColor; GameObject projectileObject = null; GameObject burstObject = null; GameObject loopObject; GameObject chargeObject = null; void Start() { SceneManager.listOfArms.Add(this.gameObject); transformComponent = GetComponent(); //loopObject = new GameObject(); } // Sets the rotation of the arm void SetArmRotation() { Quaternion rot = new Quaternion(); float shootAngle = GetAngle() * Mathf.Rad2Deg; rot.eulerAngles = new Vector3(0, 0, shootAngle); transform.rotation = rot; } // Finds the angle from the origin position of the arm to the mouse position. public float GetAngle() { Vector3 trueMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (Mathf.Atan2(trueMousePosition.y - transform.position.y, trueMousePosition.x - transform.position.x) * Mathf.Rad2Deg > angleMax) { return Mathf.Deg2Rad * angleMax; } if (Mathf.Atan2(trueMousePosition.y - transform.position.y, trueMousePosition.x - transform.position.x) * Mathf.Rad2Deg < angleMin) { return Mathf.Deg2Rad * angleMin; } return Mathf.Atan2(trueMousePosition.y - transform.position.y, trueMousePosition.x - transform.position.x); } // Finds the origin point of the end of the arm while utilzing rotation public Vector2 CircleAroundCenter(float centerX, float centerY, float dstX, float dstY) { Vector2 tempVector = new Vector2(); tempVector.x = (float)(Mathf.Cos(GetAngle()) * (dstX - centerX) - Mathf.Sin(GetAngle()) * (dstY - centerY) + centerX); tempVector.y = (float)(Mathf.Sin(GetAngle()) * (dstX - centerX) + Mathf.Cos(GetAngle()) * (dstY - centerY) + centerY); return tempVector; } // Checks if the player controller will shoot a bullet public void CheckShot() { // Increase right click time if (rightClickWaitTime < rightClickWaitTimeLimit) { rightClickWaitTime++; } for (int i = 0; i < SceneManager.listOfMenuObjects.Count; i++) { if (SceneManager.listOfMenuObjects[i].GetComponentInParent().selectedItem == "projectile") { projectileObject = SceneManager.listOfMenuObjects[i].GetComponent().selectedItemObject; // Left click shoot for both charge and not chargable shots if (Input.GetMouseButtonUp(0) && projectileObject.GetComponent().isChargeable || Input.GetMouseButtonDown(0) && !projectileObject.GetComponent().isChargeable) { ShootBullet(); chargeTime = 0; } // Increases charge time for chargable shots if (projectileObject.GetComponent().isChargeable && Input.GetMouseButton(0)) { chargeTime++; if (chargeTime > 40 && chargeObject == null) { chargeObject = Instantiate(projectileObject.GetComponent().chargingObject, CircleAroundCenter(transform.position.x, transform.position.y, transform.position.x + projectileObject.GetComponent().chargeOriginPoint.x, transform.position.y + projectileObject.GetComponent().chargeOriginPoint.y), this.transform.rotation); if (chargeObject.transform.childCount > 0) { ParticleSystem.MainModule particleModule = chargeObject.transform.GetChild(0).GetComponent().main; particleModule.startColor = projectileObject.GetComponent().chargeColor; } } ChargeParticleCheck(); } // Right click shot if (Input.GetMouseButton(1) && rightClickWaitTime >= rightClickWaitTimeLimit) { ShootBullet(); rightClickWaitTime = 0; } } } } public void ChargeParticleCheck() { if (chargeTime == 40) { chargeObject = Instantiate(projectileObject.GetComponent().chargingObject, CircleAroundCenter(transform.position.x, transform.position.y, transform.position.x + projectileObject.GetComponent().chargeOriginPoint.x, transform.position.y + projectileObject.GetComponent().chargeOriginPoint.y), this.transform.rotation); if (chargeObject.transform.childCount > 0) { ParticleSystem.MainModule particleSystemMain = chargeObject.transform.GetChild(0).GetComponent().main; particleSystemMain.startColor = projectileObject.GetComponent().chargeColor; } } if (chargeTime > 40) { chargeObject.transform.position = CircleAroundCenter(transform.position.x, transform.position.y, transform.position.x + projectileObject.GetComponent().chargeOriginPoint.x, transform.position.y + projectileObject.GetComponent().chargeOriginPoint.y); } } // Generates burst public void CheckBurst() { for (int i = 0; i < SceneManager.listOfMenuObjects.Count; i++) { if (SceneManager.listOfMenuObjects[i].GetComponentInParent().selectedItem == "burst") { Vector3 trueMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (!SceneManager.listOfMenuObjects[i].GetComponentInParent().isMenuOpen || SceneManager.listOfMenuObjects[i].GetComponentInParent().isMenuOpen && trueMousePosition.y < 4.3 && trueMousePosition.x < -7.2 || SceneManager.listOfMenuObjects[i].GetComponentInParent().isMenuOpen && trueMousePosition.x > -7.2) { if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) { PlayBurst(); } } } } } // Plays the burst animation public void PlayBurst() { for (int i = 0; i < SceneManager.listOfMenuObjects.Count; i++) { if (burstObject != null) { Destroy(burstObject); } burstObject = Instantiate(SceneManager.listOfMenuObjects[i].GetComponentInParent().selectedItemObject, new Vector3(0, 0, 0), Quaternion.Euler(0, 0, 0)); burstObject.gameObject.SetActive(true); } } // Burst Manual removal public void BurstManualRemoval() { if (burstObject != null) { Destroy(burstObject); } } // Generates the loop and hides it if loop is not selected public void CheckLoop() { for (int i = 0; i < SceneManager.listOfMenuObjects.Count; i++) { if (loopObject != null && loopObject.transform.childCount > 0) { if (SceneManager.listOfMenuObjects[i].GetComponentInParent().selectedItem == "loop") { loopObject.transform.GetChild(0).GetComponent().renderMode = ParticleSystemRenderMode.Billboard; loopObject.transform.position = loopObject.GetComponent().loopPosition; } else { GameObject.Destroy(loopObject); } } } } // Shoots the actual bullet public void ShootBullet() { Vector2 spawnPoint = CircleAroundCenter(transform.position.x, transform.position.y, transform.position.x + projectileObject.GetComponent().bulletOriginPoint.x, transform.position.y + projectileObject.GetComponent().bulletOriginPoint.y); float bulletAngle = 0; if (projectileObject.GetComponent().rotateSprite) { bulletAngle = GetAngle() * Mathf.Rad2Deg; } GameObject bullet = Instantiate(projectileObject, new Vector3(spawnPoint.x, spawnPoint.y, 1), Quaternion.Euler(0, 0, 0)); bullet.GetComponent().moveAngle = GetAngle() + (Mathf.Deg2Rad * Random.Range(-bullet.GetComponent().angleRandomness, bullet.GetComponent().angleRandomness)); bullet.GetComponent().spriteAngle = bullet.GetComponent().moveAngle; // Increases the size of the bullet if it met the charge requirements if (projectileObject.GetComponent().isChargeable) { // Remove the Charge effect if (chargeTime >= fullChargeLimit) { float bulletSize = 1.5f; bullet.transform.localScale = new Vector3(bulletSize, bulletSize, bulletSize); if (bullet.transform.childCount > 0) { bullet.transform.GetChild(0).transform.localScale = new Vector3(bulletSize, bulletSize, bulletSize); } } else if (chargeTime >= midChargeTime) { float bulletSize = 1.25f; bullet.transform.localScale = new Vector3(bulletSize, bulletSize, bulletSize); if (bullet.transform.childCount > 0) { bullet.transform.GetChild(0).transform.localScale = new Vector3(bulletSize, bulletSize, bulletSize); } } RemoveChargeObject(); } bullet.SetActive(true); if (projectileObject.GetComponent().muzzleFlash) { Vector2 muzzleFlashPoint = CircleAroundCenter(transform.position.x, transform.position.y, transform.position.x + +projectileObject.GetComponent().muzzleFlashOriginPoint.x, transform.position.y + projectileObject.GetComponent().muzzleFlashOriginPoint.y); GameObject muzzleFlashObjectTemp = Instantiate(bullet.GetComponent().muzzleFlashObject, new Vector3(muzzleFlashPoint.x, muzzleFlashPoint.y, transform.position.z), Quaternion.Euler(0, 0, Mathf.Rad2Deg * GetAngle())); ParticleSystem.MainModule particleSystemMain = muzzleFlashObjectTemp.transform.GetChild(0).GetComponent().main; particleSystemMain.startColor = projectileObject.GetComponent().muzzleFlashColor; SceneManager.listOfMuzzleFlashes.Add(muzzleFlashObjectTemp); } } // RemovesTheChargeObject public void RemoveChargeObject() { if (chargeObject != null) { Destroy(chargeObject); } } // Limits the head movement public void SendLimitsToHead() { for (int i = 0; i < SceneManager.listOfHeads.Count; i++) { SceneManager.listOfHeads[i].GetComponent().SetAngles(angleMin, angleMax); } } public void ReceiveLoopInformation(GameObject tempLoopObject) { for (int i = SceneManager.listOfLoops.Count - 1; i >= 0; i--) { if (SceneManager.listOfLoops[i] == loopObject) { SceneManager.listOfLoops.RemoveAt(i); DestroyImmediate(loopObject); break; } } loopObject = Instantiate(tempLoopObject); loopObject.SetActive(true); } void CheckMuzzleFlashAndImpact() { for (int i = SceneManager.listOfImpacts.Count - 1; i >= 0; i--) { if (SceneManager.listOfImpacts[i].GetComponent().time >= SceneManager.listOfImpacts[i].GetComponent().main.duration) { Destroy(SceneManager.listOfImpacts[i]); SceneManager.listOfImpacts.RemoveAt(i); } } for (int i = SceneManager.listOfMuzzleFlashes.Count - 1; i >= 0; i--) { if (SceneManager.listOfMuzzleFlashes[i].GetComponent().time >= SceneManager.listOfMuzzleFlashes[i].GetComponent().main.duration) { Destroy(SceneManager.listOfMuzzleFlashes[i]); SceneManager.listOfMuzzleFlashes.RemoveAt(i); } } } // Update is called once per frame void Update() { SendLimitsToHead(); SetArmRotation(); CheckShot(); CheckBurst(); CheckLoop(); CheckMuzzleFlashAndImpact(); } } }