quakeball/Assets/Scripts/Items/Raygun.cs

79 lines
2.7 KiB
C#

using UnityEngine;
using NeonTea.Quakeball.Players;
using NeonTea.Quakeball.Audio;
using NeonTea.Quakeball.Combat;
using NeonTea.Quakeball.Networking;
using NeonTea.Quakeball.Networking.Instances;
namespace NeonTea.Quakeball.Items {
public class Raygun : Item {
public override float Cooldown => RaygunCooldown;
public override Animator Animator => RaygunAnimator;
public float RaygunCooldown = 1;
[Header("Visuals")]
public GameObject LaserPrefab;
public Animator RaygunAnimator;
[Header("Shooting details")]
public Transform BulletSourcePoint;
public LayerMask BulletHitLayer;
public LayerMask BulletPassLayer;
[Header("Audio")]
public AudioSource GunShotAudioSource;
public AudioClip RaygunClip;
public override void Shoot(Player source) {
Vector3 GunPoint = BulletSourcePoint.position;
Vector3 ShotDelta = source.CameraRoot.forward * 1000f;
Vector3 From = source.CameraRoot.position;
Vector3 Direction = source.CameraRoot.forward;
RaycastHit[] Hits = Physics.RaycastAll(From, Direction, 1000f, BulletHitLayer | BulletPassLayer);
System.Array.Sort(Hits, (a, b) => { return a.distance.CompareTo(b.distance); });
foreach (RaycastHit Hit in Hits) {
ShotDelta = Hit.point - GunPoint;
Player Player = Hit.rigidbody != null ? Hit.rigidbody.GetComponent<Player>() : null;
if (Player == source) {
continue;
}
if (((1 << Hit.collider.gameObject.layer) & BulletPassLayer) != 0) {
ImpactSound ImpactSound = Hit.collider.GetComponent<ImpactSound>();
if (ImpactSound != null) {
ImpactSound.PlayAt(Hit.point);
}
continue;
}
if (Player != null) {
if (Net.Singleton.Instance is Server) {
((Server)Net.Singleton.Instance).SendHit(Player.NetId, Player.NetId);
Player.Hit(Player.NetId);
}
}
break;
}
RaygunAnimator.SetBool("Shot", true);
GameObject LaserEffect = Instantiate(LaserPrefab);
Laser Laser = LaserEffect.GetComponent<Laser>();
Laser.From = GunPoint;
Laser.To = GunPoint + ShotDelta;
GunShotAudioSource.PlayOneShot(RaygunClip);
}
private void LateUpdate() {
RaygunAnimator.SetBool("Shot", false);
}
public override void OnSwitched() {
}
public override void OnSwitchedOut() {
}
}
}