您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

ActionHolder.cs 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace coa4u
  5. {
  6. /// <summary>
  7. /// Creates the and starts the given action instance at Start. If you're using calcers for parameters, they will be also calculated at start.
  8. /// </summary>
  9. class ActionHolder<T> : ActionInstant
  10. where T : ActionInstant
  11. {
  12. protected T action;
  13. protected object[] args;
  14. public ActionHolder(params object[] args) : base()
  15. {
  16. this.args = args;
  17. }
  18. public override void Start()
  19. {
  20. if (target == null)
  21. throw new Exception("Can start the action only after it's atached to the actor");
  22. object[] nargs = new object[args.Length];
  23. for (int i = 0; i < args.Length; i++)
  24. {
  25. if (args[i] is CalcerFloat)
  26. {
  27. nargs[i] = ((CalcerFloat)args[i]).value;
  28. }
  29. else if (args[i] is CalcerVector)
  30. {
  31. nargs[i] = ((CalcerVector)args[i]).value;
  32. }
  33. else
  34. {
  35. nargs[i] = args[i];
  36. }
  37. }
  38. action = (T)Activator.CreateInstance(typeof(T), nargs);
  39. action.SetActor(target);
  40. action.locks = locks;
  41. action.Start();
  42. duration = action.duration;
  43. if (action.running)
  44. running = true;
  45. }
  46. public override void StepTimer(float dt)
  47. {
  48. if (target == null)
  49. throw new Exception("Can update the action only after it's atached to the actor");
  50. action.StepTimer(dt);
  51. if (!action.running)
  52. {
  53. dtr = action.dtr;
  54. Stop();
  55. }
  56. }
  57. public override void Stop()
  58. {
  59. if (target == null)
  60. throw new Exception("Can stop the action only after it's atached to the actor");
  61. if (action.running)
  62. action.Stop();
  63. running = false;
  64. }
  65. public override ActionInstant Clone()
  66. {
  67. return new ActionHolder<T>(args);
  68. }
  69. }
  70. }