This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Item/Pistol/PistolComponent.cs

207 lines
7.5 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
using UnityEngine.VFX;
2023-06-15 17:34:45 +02:00
using FishNet.Object;
using FishNet;
2023-06-02 06:30:58 +02:00
namespace Item
{
2023-06-15 17:34:45 +02:00
public class PistolComponent : NetworkBehaviour
2023-06-01 17:03:48 +02:00
{
2023-06-02 06:30:58 +02:00
public enum AimMode
{
GUN,
MODIFIED,
CAMERA
}
2023-06-02 06:30:58 +02:00
public AimMode aimMode = AimMode.CAMERA;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private Light targetingLight;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private GameObject targetObjectPrefab;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] public GameObject projectilePrefab;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] public string projectileName;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private Transform bulletSpawnPoint;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private float firePower = 20f;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private float maxProjectileDuration = 5f;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private float maxTargetObjDistance = 15f;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private VisualEffect shootEffect;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private Light shootLight;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private float shootLightDuration = 0.1f;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private LayerMask ignoreLayers;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
private bool hasCloseTarget;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
private bool IsEnabled;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
private GameObject targetObject;
private float timeSinceLightDuration;
public bool IsLightOn => targetingLight.gameObject.activeSelf;
2023-06-15 18:49:37 +02:00
2023-04-10 04:30:54 +02:00
2023-06-02 06:30:58 +02:00
//private Dictionary<int,float> projectiles = new Dictionary<int, float>();
2023-06-15 18:49:37 +02:00
2023-06-02 06:30:58 +02:00
// Start is called before the first frame update
private void Start()
{
2023-06-15 18:49:37 +02:00
2023-06-02 06:30:58 +02:00
}
2023-06-02 06:30:58 +02:00
// Update is called once per frame
private void Update()
{
timeSinceLightDuration += Time.deltaTime;
}
2023-04-10 04:30:54 +02:00
2023-06-02 06:30:58 +02:00
private void FixedUpdate()
{
2023-06-02 06:30:58 +02:00
if (shootLight.gameObject.activeSelf && timeSinceLightDuration > shootLightDuration)
shootLight.gameObject.SetActive(false);
if (aimMode == AimMode.CAMERA) targetObject.gameObject.transform.position = Player.PlayerAim.active.targetPosition;
if (IsEnabled && aimMode != AimMode.CAMERA)
{
2023-06-02 06:30:58 +02:00
var ray = new Ray(transform.position, transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 50, ignoreLayers))
{
2023-06-02 06:30:58 +02:00
var hitDist = Vector3.Distance(hit.point, transform.position);
if (hitDist < maxTargetObjDistance)
{
targetObject.gameObject.transform.position = hit.point;
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0]
.SetColor("_EmissiveColor", new Color(255, 0, 0));
//Track if we have a close target
hasCloseTarget = true;
}
else
{
targetObject.gameObject.transform.position =
transform.position + ray.direction * maxTargetObjDistance;
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0]
.SetColor("_EmissiveColor", new Color(255, 255, 255));
//Track if we have a close target
hasCloseTarget = false;
}
//float drop = CalculateDrop(this.bulletSpawnPoint.position, hit.point, this.transform.up * this.firePower);
//print(drop);
}
else
{
2023-06-02 06:30:58 +02:00
targetObject.gameObject.transform.position = transform.position + ray.direction * maxTargetObjDistance;
2023-06-01 17:03:48 +02:00
targetObject.gameObject.GetComponent<MeshRenderer>().materials[0]
.SetColor("_EmissiveColor", new Color(255, 255, 255));
hasCloseTarget = false;
}
}
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
private float CalculateDrop(Vector3 origin, Vector3 destination, Vector3 force)
{
// Calculate the initial velocity required to reach the destination.
var displacement = destination - origin;
var time = Mathf.Sqrt(2f * displacement.magnitude / Physics.gravity.magnitude);
var velocity = (displacement - 0.5f * Physics.gravity * time * time) / time + force;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
// Calculate the height the object will reach during its flight.
var maxHeight = origin.y + velocity.y * time - 0.5f * Physics.gravity.y * time * time;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
// Calculate the distance the object will drop during its flight.
var dropDistance = maxHeight - destination.y;
2023-04-04 01:23:20 +02:00
2023-06-02 06:30:58 +02:00
return dropDistance;
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
public void Fire()
{
Fire(!hasCloseTarget);
}
2023-06-15 18:55:42 +02:00
2023-06-15 18:40:11 +02:00
[ServerRpc]
2023-06-02 06:30:58 +02:00
public void Fire(bool offsetWithTargetBall)
{
2023-07-15 01:39:52 +02:00
2023-06-02 06:30:58 +02:00
shootLightDuration = 0;
shootLight.gameObject.SetActive(true);
2023-06-15 18:49:37 +02:00
2023-06-15 18:55:42 +02:00
// var projectile = Instantiate(projectilePrefab, bulletSpawnPoint);
2023-07-15 01:35:15 +02:00
var projectile = Instantiate(projectilePrefab);
projectile.transform.position = bulletSpawnPoint.transform.position;
projectile.transform.rotation = bulletSpawnPoint.transform.rotation;
2023-07-15 01:43:38 +02:00
//Comment this line if you want to remove the mega scale.
2023-07-15 01:35:15 +02:00
projectile.transform.localScale =bulletSpawnPoint.transform.localScale;
2023-06-15 18:55:42 +02:00
InstanceFinder.ServerManager.Spawn(projectile); //var projectile = Instantiate(projectilePrefab, bulletSpawnPoint);
2023-07-15 01:43:38 +02:00
2023-06-15 18:40:11 +02:00
//InstanceFinder.ServerManager.Spawn(projectile);
2023-06-15 17:58:17 +02:00
2023-06-02 06:30:58 +02:00
var pRigid = projectile.GetComponent<Rigidbody>();
/*Modified targeting system
1. Since aim direction is vector from camera to ball (where player thinks its gonna go), raycast forward there, till hit. If no hit,
then set target distance to ~50.
2. Modify launch vector apply modified force
*/
var launchVector = pRigid.transform.up * firePower;
if (offsetWithTargetBall || aimMode == AimMode.MODIFIED)
{
var ballCamVector = targetObject.transform.position -
GetComponentInParent<Player.PlayerMovementController>().cam.transform.position;
var r = new Ray();
r.origin = targetObject.transform.position;
r.direction = ballCamVector.normalized;
RaycastHit hit;
if (Physics.Raycast(r, out hit, ignoreLayers))
{
launchVector = (hit.point - pRigid.transform.position).normalized;
launchVector *= firePower;
}
}
else if (aimMode == AimMode.CAMERA)
{
2023-06-02 06:30:58 +02:00
var target = Player.PlayerAim.active.targetPosition;
var lv = target - pRigid.transform.position;
launchVector = lv.normalized;
2023-06-01 17:03:48 +02:00
launchVector *= firePower;
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
pRigid.AddForce(launchVector, ForceMode.Impulse);
projectile.transform.parent = null;
2023-07-15 01:35:15 +02:00
2023-06-02 06:30:58 +02:00
shootEffect.Play();
}
2023-06-02 06:30:58 +02:00
public void Enable()
{
IsEnabled = true;
if (targetObject == null) targetObject = Instantiate(targetObjectPrefab);
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
public void Disable()
{
IsEnabled = false;
if (targetObject != null) Destroy(targetObject);
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
public void LightToggle()
{
targetingLight.gameObject.SetActive(!targetingLight.gameObject.activeSelf);
}
}
2023-06-01 17:03:48 +02:00
}