Advertisement
Guest User

Untitled

a guest
Nov 5th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  1.  
  2. using System;
  3. using UnityEngine;
  4. using MyCustomNamespace;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7.  
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11.  
  12. namespace AC
  13. {
  14. [System.Serializable]
  15. public class ActionCharMoveRandom : Action
  16. {
  17. public Marker centerMarker;
  18. public int centerMarkerConstantID = 0;
  19. public int centerMarkerParameterID = -1;
  20. public float minRadius = 0f;
  21. public int minRadiusParameterID = -1;
  22. public float maxRadius = 10f;
  23. public int maxRadiusParameterID = -1;
  24. public Char charToMove;
  25. public int charToMoveID = 0;
  26. private bool isTimeoutStarted = false; // Add this at the top with other private members
  27. public int charToMoveParameterID = -1;
  28. public bool isPlayer;
  29. public PathSpeed speed;
  30. public bool pathFind = true;
  31. public bool doFloat = false;
  32. private TimeoutManager timeoutManager;
  33. private bool isScriptActive = true; // Added declaration
  34. private bool isPaused = false; // Added declaration
  35. private DateTime startTime; // Added declaration
  36. private Char initialCharToMove; // Added declaration
  37. private Char originalCharToMove; // Added declaration
  38. // Uncomment below line if hasTimedOut is actually defined somewhere in your project
  39. private bool hasTimedOut = false;
  40.  
  41. public ActionCharMoveRandom()
  42. {
  43. this.isScriptActive = true;
  44. this.isDisplayed = true;
  45. category = ActionCategory.Character;
  46. title = "Move randomly";
  47. description = "Moves a random point within a circle, given a centre-point and a maximum radius.";
  48. }
  49.  
  50.  
  51. public string SetLabel()
  52. {
  53. return "CharMoveRandom";
  54. }
  55.  
  56. public static string GetActionTypeLabel()
  57. {
  58. return "Custom";
  59. }
  60.  
  61. public override void AssignValues(List<ActionParameter> parameters)
  62. {
  63. centerMarker = AssignFile<Marker>(parameters, centerMarkerParameterID, centerMarkerConstantID, centerMarker);
  64. minRadius = AssignFloat(parameters, minRadiusParameterID, minRadius);
  65. maxRadius = AssignFloat(parameters, maxRadiusParameterID, maxRadius);
  66. minRadius = Mathf.Clamp(minRadius, 0f, maxRadius);
  67.  
  68. charToMove = AssignFile<Char>(parameters, charToMoveParameterID, charToMoveID, charToMove);
  69. if (isPlayer)
  70. {
  71. charToMove = KickStarter.player;
  72. if (originalCharToMove == null)
  73. {
  74. originalCharToMove = charToMove;
  75. }
  76. }
  77. }
  78.  
  79. public override float Run()
  80. {
  81.  
  82. startTime = DateTime.Now;
  83.  
  84. if (!isRunning)
  85. {
  86. if (initialCharToMove == null)
  87. {
  88. initialCharToMove = charToMove; // Store the initial character when the action first starts.
  89. }
  90. else if (initialCharToMove != charToMove)
  91. {
  92. Debug.LogWarning("Character to move has changed since the action started. Pausing movement.");
  93. isPaused = true;
  94. return 0f;
  95. }
  96.  
  97. if (isPaused)
  98. {
  99. return 0f;
  100. }
  101.  
  102. startTime = DateTime.Now;
  103. if (charToMove)
  104. {
  105. Paths path = charToMove.GetComponent<Paths>();
  106. if (path == null)
  107. {
  108. ACDebug.LogWarning("Cannot move a character with no Paths component");
  109. }
  110. else
  111. {
  112. if (charToMove is NPC)
  113. {
  114. NPC npcToMove = (NPC)charToMove;
  115. npcToMove.StopFollowing();
  116. }
  117.  
  118. path.pathType = AC_PathType.ForwardOnly;
  119. path.pathSpeed = speed;
  120. path.affectY = true;
  121.  
  122. Vector3[] pointArray;
  123. Vector3 targetPosition = GetRandomPoint();
  124.  
  125. if (SceneSettings.ActInScreenSpace())
  126. {
  127. targetPosition = AdvGame.GetScreenNavMesh(targetPosition);
  128. }
  129.  
  130. float distance = Vector3.Distance(targetPosition, charToMove.transform.position);
  131. if (distance <= KickStarter.settingsManager.destinationAccuracy)
  132. {
  133. isRunning = false;
  134. return 0f;
  135. }
  136.  
  137. if (pathFind && KickStarter.navigationManager)
  138. {
  139. pointArray = KickStarter.navigationManager.navigationEngine.GetPointsArray(charToMove.transform.position, targetPosition, charToMove);
  140. }
  141. else
  142. {
  143. List<Vector3> pointList = new List<Vector3>();
  144. pointList.Add(targetPosition);
  145. pointArray = pointList.ToArray();
  146. }
  147.  
  148. if (speed == PathSpeed.Walk)
  149. {
  150. charToMove.MoveAlongPoints(pointArray, false, pathFind);
  151. }
  152. else
  153. {
  154. charToMove.MoveAlongPoints(pointArray, true, pathFind);
  155. }
  156.  
  157. if (charToMove.GetPath())
  158. {
  159. if (!pathFind && doFloat)
  160. {
  161. charToMove.GetPath().affectY = true;
  162. }
  163. else
  164. {
  165. charToMove.GetPath().affectY = false;
  166. }
  167. }
  168.  
  169. if (willWait)
  170. {
  171. isRunning = true;
  172. return defaultPauseTime;
  173. }
  174. }
  175. }
  176. return 0f;
  177. }
  178. else
  179. {
  180. if (isPaused)
  181. {
  182. return 0f;
  183. }
  184.  
  185. if (charToMove.GetPath() == null)
  186. {
  187. isRunning = false;
  188. return 0f;
  189. }
  190. else
  191. {
  192. return defaultPauseTime;
  193. }
  194. }
  195. }
  196.  
  197.  
  198. override public void Skip ()
  199.  
  200. {
  201. if (charToMove)
  202.  
  203. {
  204.  
  205. charToMove.EndPath ();
  206.  
  207.  
  208.  
  209. if (charToMove is NPC)
  210.  
  211. {
  212.  
  213. NPC npcToMove = (NPC) charToMove;
  214.  
  215. npcToMove.StopFollowing ();
  216.  
  217. }
  218.  
  219.  
  220.  
  221.  
  222. Vector3[] pointArray;
  223.  
  224. Vector3 targetPosition = GetRandomPoint ();
  225.  
  226.  
  227.  
  228.  
  229. if (SceneSettings.ActInScreenSpace ())
  230.  
  231. {
  232.  
  233. targetPosition = AdvGame.GetScreenNavMesh (targetPosition);
  234.  
  235. }
  236.  
  237.  
  238.  
  239.  
  240. if (pathFind && KickStarter.navigationManager)
  241.  
  242. {
  243.  
  244. pointArray = KickStarter.navigationManager.navigationEngine.GetPointsArray (charToMove.transform.position, targetPosition);
  245.  
  246. KickStarter.navigationManager.navigationEngine.ResetHoles (KickStarter.sceneSettings.navMesh);
  247.  
  248. }
  249.  
  250. else
  251.  
  252. {
  253.  
  254. List<Vector3> pointList = new List<Vector3>();
  255.  
  256. pointList.Add (targetPosition);
  257.  
  258. pointArray = pointList.ToArray ();
  259.  
  260. }
  261.  
  262.  
  263.  
  264.  
  265. int i = pointArray.Length-1;
  266.  
  267.  
  268.  
  269. if (i>0)
  270.  
  271. {
  272.  
  273. charToMove.SetLookDirection (pointArray[i] - pointArray[i-1], true);
  274.  
  275. }
  276.  
  277. else
  278.  
  279. {
  280.  
  281. charToMove.SetLookDirection (pointArray[i] - charToMove.transform.position, true);
  282.  
  283. }
  284.  
  285.  
  286.  
  287. charToMove.Teleport (pointArray [i]);
  288.  
  289. }
  290.  
  291. }
  292.  
  293.  
  294.  
  295.  
  296.  
  297. public Vector3 GetRandomPoint ()
  298.  
  299. {
  300.  
  301. float radius = UnityEngine.Random.Range (minRadius, maxRadius);
  302.  
  303. Vector2 randomInCircle2D = (Vector3) UnityEngine.Random.insideUnitCircle * radius;
  304.  
  305.  
  306.  
  307. if (SceneSettings.IsUnity2D ())
  308.  
  309. {
  310.  
  311. return centerMarker.transform.position + new Vector3 (randomInCircle2D.x, randomInCircle2D.y, 0f);
  312.  
  313. }
  314.  
  315.  
  316.  
  317. return centerMarker.transform.position + new Vector3 (randomInCircle2D.x, 0f, randomInCircle2D.y);
  318.  
  319. }
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326. #if UNITY_EDITOR
  327.  
  328.  
  329.  
  330. override public void ShowGUI (List<ActionParameter> parameters)
  331.  
  332. {
  333.  
  334. isPlayer = EditorGUILayout.Toggle ("Is Player?", isPlayer);
  335.  
  336.  
  337.  
  338. if (!isPlayer)
  339.  
  340. {
  341.  
  342. charToMoveParameterID = Action.ChooseParameterGUI ("Character to move:", parameters, charToMoveParameterID, ParameterType.GameObject);
  343.  
  344. if (charToMoveParameterID >= 0)
  345.  
  346. {
  347.  
  348. charToMoveID = 0;
  349.  
  350. charToMove = null;
  351.  
  352. }
  353.  
  354. else
  355.  
  356. {
  357.  
  358. charToMove = (Char) EditorGUILayout.ObjectField ("Character to move:", charToMove, typeof (Char), true);
  359.  
  360.  
  361.  
  362.  
  363. charToMoveID = FieldToID <Char> (charToMove, charToMoveID);
  364.  
  365. charToMove = IDToField <Char> (charToMove, charToMoveID, false);
  366.  
  367. }
  368.  
  369. }
  370.  
  371.  
  372.  
  373. centerMarkerParameterID = Action.ChooseParameterGUI ("Marker to reach:", parameters, centerMarkerParameterID, ParameterType.GameObject);
  374.  
  375. if (centerMarkerParameterID >= 0)
  376.  
  377. {
  378.  
  379. centerMarkerConstantID = 0;
  380.  
  381. centerMarker = null;
  382.  
  383. }
  384.  
  385. else
  386.  
  387. {
  388.  
  389. centerMarker = (Marker) EditorGUILayout.ObjectField ("Centre-point:", centerMarker, typeof (Marker), true);
  390.  
  391.  
  392.  
  393.  
  394. centerMarkerConstantID = FieldToID <Marker> (centerMarker, centerMarkerConstantID);
  395.  
  396. centerMarker = IDToField <Marker> (centerMarker, centerMarkerConstantID, false);
  397.  
  398. }
  399.  
  400.  
  401.  
  402. minRadiusParameterID = Action.ChooseParameterGUI ("Minimum radius:", parameters, minRadiusParameterID, ParameterType.Float);
  403.  
  404. if (minRadiusParameterID < 0)
  405.  
  406. {
  407.  
  408. minRadius = EditorGUILayout.FloatField ("Minimum radius:", minRadius);
  409.  
  410. }
  411.  
  412.  
  413.  
  414. maxRadiusParameterID = Action.ChooseParameterGUI ("Maximum radius:", parameters, maxRadiusParameterID, ParameterType.Float);
  415.  
  416. if (maxRadiusParameterID < 0)
  417.  
  418. {
  419.  
  420. maxRadius = EditorGUILayout.FloatField ("Maximum radius:", maxRadius);
  421.  
  422. }
  423.  
  424.  
  425.  
  426. speed = (PathSpeed) EditorGUILayout.EnumPopup ("Move speed:" , speed);
  427.  
  428. pathFind = EditorGUILayout.Toggle ("Pathfind?", pathFind);
  429.  
  430. if (!pathFind)
  431.  
  432. {
  433.  
  434. doFloat = EditorGUILayout.Toggle ("Ignore gravity?", doFloat);
  435.  
  436. }
  437.  
  438. willWait = EditorGUILayout.Toggle ("Wait until finish?", willWait);
  439.  
  440.  
  441.  
  442. AfterRunningOption ();
  443.  
  444. }
  445.  
  446.  
  447.  
  448.  
  449.  
  450. override public void AssignConstantIDs (bool saveScriptsToo, bool fromAssetFile)
  451.  
  452. {
  453.  
  454. if (saveScriptsToo)
  455.  
  456. {
  457.  
  458. if (!isPlayer && charToMove != null && charToMove.GetComponent <NPC>())
  459.  
  460. {
  461.  
  462. AddSaveScript <RememberNPC> (charToMove);
  463.  
  464. }
  465.  
  466. }
  467.  
  468.  
  469.  
  470. if (!isPlayer)
  471.  
  472. {
  473.  
  474. AssignConstantID <Char> (charToMove, charToMoveID, charToMoveParameterID);
  475.  
  476. }
  477.  
  478. }
  479.  
  480.  
  481.  
  482. #endif
  483.  
  484.  
  485.  
  486.  
  487. }
  488.  
  489.  
  490.  
  491. }
  492.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement