weaponAttachmentsMgr.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. void Start()
  18. {
  19. player.EquippedItem.AddChangeListener((SaveableItem item) => { changeAttachments((item == null) ? "" : item.Name); });
  20. pp.profile.TryGetSettings(out depthOfField);
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if(aimValue && aimed < 1){
  26. aimed+=0.05f;depthOfField.focusDistance.value = 0.1f + (0.1f * (1-aimed));
  27. }else if(!aimValue && aimed > 0){
  28. aimed-=0.05f;depthOfField.focusDistance.value =0.1f + (0.1f * (1-aimed));
  29. }
  30. }
  31. void changeAttachments(string item){
  32. StartCoroutine(waitAndChangeAttachments(item));
  33. }
  34. IEnumerator waitAndChangeAttachments(string item){
  35. yield return new WaitForSeconds(0.6f);
  36. foreach(AttachmentConfig config in attachmentConfigs){
  37. bool isSelected = config.weaponName == item;
  38. foreach(GameObject attachment in config.attachments){
  39. attachment.SetActive(isSelected);
  40. }
  41. }
  42. }
  43. public void focus(bool value){
  44. aimValue = value;
  45. }
  46. // IEnumerator _focus(bool value){
  47. // aimed = (value) ? 0: 1;
  48. // if(value){
  49. // while(aimed < 1){
  50. // aimed +=0.1f;
  51. // yield return new WaitForEndOfFrame();
  52. // }
  53. // }else{
  54. // while(aimed > 0){
  55. // aimed -=0.1f;
  56. // depthOfField.focusDistance.value = 1-aimed;
  57. // yield return new WaitForEndOfFrame();
  58. // }
  59. // }
  60. // }
  61. }
  62. [Serializable]
  63. public class AttachmentConfig{
  64. public string weaponName;
  65. public List<GameObject> attachments;
  66. public AttachmentConfig(string _weaponName){
  67. weaponName = _weaponName;
  68. attachments = new List<GameObject>();
  69. }
  70. }