weaponAttachmentsMgr.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using HQFPSWeapons;
  6. using UnityEngine.Rendering.PostProcessing;
  7. public class weaponAttachmentsMgr : MonoBehaviour
  8. {
  9. public Player player;
  10. [SerializeField]
  11. public AttachmentConfig[] attachmentConfigs;
  12. public PostProcessVolume pp;
  13. DepthOfField depthOfField;
  14. bool aimValue;
  15. [Range(0.00f,1.00f)]
  16. public float aimed;
  17. bool gotScope= false;
  18. void Start()
  19. {
  20. player.EquippedItem.AddChangeListener((SaveableItem item) => { changeAttachments((item == null) ? "" : item.Name); });
  21. pp.profile.TryGetSettings(out depthOfField);
  22. }
  23. // Update is called once per frame
  24. void Update()
  25. {
  26. if(aimValue && aimed < 1){
  27. aimed+=0.05f;if(gotScope){depthOfField.focusDistance.value = 0.1f + (0.1f * (1-aimed));}
  28. }else if(!aimValue && aimed > 0){
  29. aimed-=0.05f;depthOfField.focusDistance.value =0.1f + (0.1f * (1-aimed));
  30. }
  31. }
  32. void changeAttachments(string item){
  33. StartCoroutine(waitAndChangeAttachments(item));
  34. }
  35. IEnumerator waitAndChangeAttachments(string item){
  36. yield return new WaitForSeconds(0.6f);
  37. foreach(AttachmentConfig config in attachmentConfigs){
  38. bool isSelected = config.weaponName == item;
  39. foreach(GameObject attachment in config.attachments){
  40. attachment.SetActive(isSelected);
  41. if(isSelected)gotScope = attachment.GetComponent<scopeBehaviour>()!=null;
  42. }
  43. }
  44. }
  45. public void focus(bool value){
  46. aimValue = value;
  47. }
  48. // IEnumerator _focus(bool value){
  49. // aimed = (value) ? 0: 1;
  50. // if(value){
  51. // while(aimed < 1){
  52. // aimed +=0.1f;
  53. // yield return new WaitForEndOfFrame();
  54. // }
  55. // }else{
  56. // while(aimed > 0){
  57. // aimed -=0.1f;
  58. // depthOfField.focusDistance.value = 1-aimed;
  59. // yield return new WaitForEndOfFrame();
  60. // }
  61. // }
  62. // }
  63. }
  64. [Serializable]
  65. public class AttachmentConfig{
  66. public string weaponName;
  67. public List<GameObject> attachments;
  68. public AttachmentConfig(string _weaponName){
  69. weaponName = _weaponName;
  70. attachments = new List<GameObject>();
  71. }
  72. }