public void lateStart() { availableTypeMissile = new List <WeaponStat.WeaponType>(); availableTypeTurret = new List <WeaponStat.WeaponType>(); foreach (var item in listWeaponState) { if ((item.type == WeaponStat.WeaponType.Missile || item.type == WeaponStat.WeaponType.Torpedo) && !availableTypeMissile.Contains(item.type)) { availableTypeMissile.Add(item.type); } if ((item.type == WeaponStat.WeaponType.MachineGun || item.type == WeaponStat.WeaponType.Turret) && !availableTypeTurret.Contains(item.type)) { availableTypeTurret.Add(item.type); } } indexListMissile = indexListTurret = 0; if (availableTypeMissile.Count > 0) { selectedTypeMissile = availableTypeMissile[indexListMissile]; } else { selectedTypeMissile = WeaponStat.WeaponType.Undefined; } if (availableTypeTurret.Count > 0) { selectedTypeTurret = availableTypeTurret[indexListTurret]; } else { selectedTypeTurret = WeaponStat.WeaponType.Undefined; } }
public void ProceedFire(WeaponStat.WeaponType type, Transform target = null) { foreach (WeaponState ws in listWeaponState) { if (ws.type == type) { if (type == WeaponStat.WeaponType.MachineGun) { if (target == null) { ws.Fire(shipREF.Velocity.magnitude, target); } else { float targetDistance = Vector3.Distance(target.position, ws.spawn.position); Vector3 toSphere = target.position + target.GetComponent <Unit>().GetSpeed() * target.forward / ((ws.speedShoot + transform.parent.GetComponent <Ship>().Velocity.magnitude) / targetDistance) - ws.spawn.position; Vector3 temp, temp2; temp = Vector3.Cross(ws.spawn.forward, toSphere); temp2 = Vector3.Cross(ws.spawn.forward, temp); float distance = Mathf.Abs(Vector3.Dot(temp2.normalized, toSphere)); float limit; if (targetDistance > 400f) { limit = 8f; } else if (targetDistance > 200f) { limit = 4f; } else { limit = 2f; } if (distance < limit) { ws.Fire(shipREF.Velocity.magnitude, target); } else { ws.Fire(shipREF.Velocity.magnitude, null); } } } else if (type == WeaponStat.WeaponType.Torpedo) { if (ws.canFire) { ws.Fire(shipREF.Velocity.magnitude, target); return; } } else if (type == WeaponStat.WeaponType.Turret) { StartCoroutine(FireTurretSequenceRobot(target, ws)); } else { ws.Fire(shipREF.Velocity.magnitude, target); } } } }
// Update is called once per frame void Update() { if (SceneController.IDScene != SceneController.SceneID.MainGameScene) { return; } if (isIA) { return; } listWeaponState.ForEach(x => x.SetDirection(x.type == WeaponStat.WeaponType.MachineGun && selectedTypeTurret == WeaponStat.WeaponType.MachineGun ? GetFireDirection(transform.root) : transform.forward)); if (Input.GetButtonDown("ChangeWeaponMode") && selectedTypeMissile != WeaponStat.WeaponType.Undefined) { indexListMissile = (indexListMissile + 1) % availableTypeMissile.Count; selectedTypeMissile = availableTypeMissile[indexListMissile]; if (selectedTypeMissile == WeaponStat.WeaponType.Missile) { GameObject.FindGameObjectWithTag("UI2D").GetComponent <UI2D>().ChangeMissile("Missile"); } else if (selectedTypeMissile == WeaponStat.WeaponType.Torpedo) { GameObject.FindGameObjectWithTag("UI2D").GetComponent <UI2D>().ChangeMissile("Torpedo"); } } if (Input.GetButtonDown("ChangeWeaponModeTurret") && selectedTypeTurret != WeaponStat.WeaponType.Undefined) { indexListTurret = (indexListTurret + 1) % availableTypeTurret.Count; selectedTypeTurret = availableTypeTurret[indexListTurret]; } if (turret != null && selectedTypeTurret == WeaponStat.WeaponType.Turret) { Vector3 direction = GetFireDirection(transform.root); turret.transform.localRotation = defaultTurretRotation; turret.transform.Rotate(turret.transform.up, Mathf.Acos(-Vector3.Dot(direction, turret.transform.right)) * Mathf.Rad2Deg - 90f, Space.World); if (Vector3.Dot(direction, transform.up) > 0f) { canon.transform.localRotation = defaultCanonRotation; canon.transform.Rotate(canon.transform.right, Mathf.Acos(Vector3.Dot(direction, canon.transform.up)) * Mathf.Rad2Deg - 90f, Space.World); } } if (Input.GetButton("Fire1") && !Input.GetButton("PushRight")) { ProceedFire(selectedTypeTurret, shipREF.GetSelectedTarget() != null ? shipREF.GetSelectedTarget().transform : null); } else { foreach (WeaponState ws in listWeaponState) { if (ws.type == WeaponStat.WeaponType.MachineGun) { ws.GetComponent <AudioSource>().Stop(); } } } if ( !Input.GetButton("PushRight") && ((Input.GetButtonDown("Fire2") && selectedTypeMissile == WeaponStat.WeaponType.Torpedo) || (Input.GetButton("Fire2") && selectedTypeMissile == WeaponStat.WeaponType.Missile))) { ProceedFire(selectedTypeMissile, shipREF.GetSelectedTarget() == null || shipREF.getLockTimerNormalized() < 1f ? null : shipREF.GetSelectedTarget().transform); } }