// Shoot in the specified direction public void Shoot(Vector3 dir) { System.TimeSpan ts = System.DateTime.Now - lastShootTime; if (turretState != TankTurretState.Firing1 && ts.TotalSeconds > tank.prop.shootCoolDown) { lastShootTime = System.DateTime.Now; // Get the delta angle to rotate the turret float turretAngle = Vector3.Angle(turretTransform.forward, dir); accumTimeTurret = 0.0f; turretState = TankTurretState.Firing1; missile = missileManager.GetItem(); missile.transform.position = spawnPointTransform.position; missile.transform.forward = cannonTransform.forward; missile.SetActive(true); missile.GetComponent <Missile>().SetOnMissileHit(OnMissileHit); tank.AI.OnShoot(); } else { Debug.LogFormat("TankTurret@Shoot. Cannot shoot. Last shoot is still too recent. State: {0}", turretState); } }
// Rotate the turret clockwise using a relative angle public void RotateRel(float angleOfs) { accumTimeTurret = 0.0f; turretState = TankTurretState.Rotating; float startAngle = Angle.Normalize(turretTransform.localRotation.eulerAngles.y); endAngleQ = Quaternion.Euler(0.0f, Angle.Normalize(startAngle + angleOfs), 0.0f); rotDirection = (angleOfs > 0.0f ? 1.0f : -1.0f); rotationTotalTime = (Mathf.Abs(angleOfs) / 360.0f); turretSndId = SoundManager.Instance.PlaySound(SndId.SND_TANK_TURRET_ROTATE); }
// Rotate the tower clockwise using an absolute angle public void RotateAbs(float targetAngle) { accumTimeTurret = 0.0f; turretState = TankTurretState.Rotating; float startAngle = turretTransform.eulerAngles.y; float dif = Angle.GetClosestAngleDif(startAngle, targetAngle); endAngleQ = Quaternion.Euler(0.0f, Angle.Normalize(targetAngle), 0.0f); rotDirection = (dif > 0.0f ? 1.0f : -1.0f); rotationTotalTime = (Mathf.Abs(dif) / 360.0f); turretSndId = SoundManager.Instance.PlaySound(SndId.SND_TANK_TURRET_ROTATE); }
// Custom Update Method public void Update() { switch (turretState) { case TankTurretState.Rotating: accumTimeTurret += Time.deltaTime * tank.prop.hullRotationSpeed; float angle = turretTransform.localRotation.eulerAngles.y + 360.0f * Time.deltaTime * tank.prop.hullRotationSpeed * rotDirection; turretTransform.localRotation = Quaternion.Euler(new Vector3(0.0f, angle, 0.0f)); if (accumTimeTurret > rotationTotalTime) { SoundManager.Instance.StopSound(turretSndId); turretTransform.localRotation = endAngleQ; turretState = TankTurretState.Idle; } break; case TankTurretState.Firing1: accumTimeTurret += Time.deltaTime * 20.0f; cannonTransform.localPosition = new Vector3(cannonTransform.localPosition.x, cannonTransform.localPosition.y, Mathf.Lerp(0.0f, -0.3f, accumTimeTurret)); if (accumTimeTurret >= 1.0f) { SoundManager.Instance.PlaySound(SndId.SND_TANK_SHOOT); accumTimeTurret = 0.0f; turretState = TankTurretState.Firing2; } break; case TankTurretState.Firing2: accumTimeTurret += Time.deltaTime * 5.0f; cannonTransform.localPosition = new Vector3(cannonTransform.localPosition.x, cannonTransform.localPosition.y, Mathf.Lerp(-0.3f, 0.0f, accumTimeTurret)); if (accumTimeTurret >= 1.0f) { accumTimeTurret = 0.0f; turretState = TankTurretState.Idle; } break; } }
// Custom Start Method public void Start() { turretState = TankTurretState.Idle; missileManager = PoolManager.Instance.GetItemPoolManager("MissileManager"); }