Advertisement
Guest User

Untitled

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