quakeball/Assets/Scripts/Items/Raygun.cs

87 lines
3.0 KiB
C#
Raw Permalink Normal View History

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 override Transform RightHandAnchor => Handle;
public float RaygunCooldown = 1;
public Transform Handle;
[Header("Visuals")]
public GameObject LaserPrefab;
public Animator RaygunAnimator;
2020-08-14 19:48:27 +02:00
public Transform ScopeCamera;
[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);
}
2020-08-14 19:48:27 +02:00
public override void Switched() {
}
public override void SwitchedOut() {
GameObject.Destroy(this.gameObject);
}
2020-08-14 19:48:27 +02:00
private void Start() {
ScopeCamera.transform.localPosition = -Holder.ItemRoot.transform.localPosition;
}
2020-08-14 19:48:27 +02:00
private void LateUpdate() {
RaygunAnimator.SetBool("Shot", false);
}
}
}