CharacterShootTaskAction.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using NodeCanvas.Framework;
  5. using ParadoxNotion.Design;
  6. using KairoEngine.Core;
  7. using KairoEngine.Inventory;
  8. namespace KairoEngine.CharacterSystem
  9. {
  10. [Category("KairoEngine")]
  11. public class CharacterShootTaskAction : ActionTask
  12. {
  13. [BlackboardOnly, RequiredField] public BBParameter<CharacterController> character;
  14. [BlackboardOnly, RequiredField] public BBParameter<Transform> target;
  15. private float waitTime = 0f;
  16. private float counter = 0f;
  17. private ItemBaseFirearm firearm;
  18. private int burstCount = 0;
  19. protected override string info
  20. {
  21. get { return "Shoot at Target"; }
  22. }
  23. protected override void OnExecute()
  24. {
  25. if(character.value == null) EndAction(false);
  26. if(target.value == null) EndAction(true);
  27. if(character.value.GetEquipedItem() == null) EndAction(false);
  28. firearm = (ItemBaseFirearm)character.value.GetEquipedItem().item;
  29. waitTime = firearm.firingSpeed;
  30. counter = 0f;
  31. burstCount = 0;
  32. ICommand shootCommand = new ShootCommand(character.value);
  33. CommandInvoker.AddCommand(shootCommand);
  34. }
  35. protected override void OnUpdate()
  36. {
  37. if(character.value == null || firearm == null) EndAction(false);
  38. if(target.value != null)
  39. {
  40. ICommand mousePosCommand = new TurnCommand(character.value, target.value.position);
  41. CommandInvoker.AddCommand(mousePosCommand);
  42. }
  43. counter += Time.deltaTime;
  44. if(counter > waitTime)
  45. {
  46. if(firearm.isAutomatic)
  47. {
  48. burstCount += 1;
  49. if(burstCount <= firearm.ammoCapacity/4)
  50. {
  51. counter = 0f;
  52. ICommand shootCommand = new ShootCommand(character.value);
  53. CommandInvoker.AddCommand(shootCommand);
  54. }
  55. else EndAction(true);
  56. }
  57. else EndAction(true);
  58. }
  59. }
  60. }
  61. }